From b71e8c2f39d823891aab4c681cccab9af5a49406 Mon Sep 17 00:00:00 2001 From: Kieran Bingham Date: Fri, 25 Jun 2021 02:15:54 +0100 Subject: libcamera/base: Move span to base library Move span, and adjust the Doxygen exclusion as well. Reviewed-by: Paul Elder Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- include/libcamera/base/file.h | 3 +- include/libcamera/base/meson.build | 1 + include/libcamera/base/span.h | 424 ++++++++++++++++++++++++ include/libcamera/controls.h | 2 +- include/libcamera/internal/buffer.h | 2 +- include/libcamera/internal/byte_stream_buffer.h | 3 +- include/libcamera/internal/pub_key.h | 2 +- include/libcamera/internal/v4l2_device.h | 2 +- include/libcamera/meson.build | 1 - include/libcamera/span.h | 424 ------------------------ 10 files changed, 431 insertions(+), 433 deletions(-) create mode 100644 include/libcamera/base/span.h delete mode 100644 include/libcamera/span.h (limited to 'include') diff --git a/include/libcamera/base/file.h b/include/libcamera/base/file.h index 5cd98579..465f3ef8 100644 --- a/include/libcamera/base/file.h +++ b/include/libcamera/base/file.h @@ -13,8 +13,7 @@ #include #include - -#include +#include namespace libcamera { diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build index 83c664af..6b90f9f7 100644 --- a/include/libcamera/base/meson.build +++ b/include/libcamera/base/meson.build @@ -14,6 +14,7 @@ libcamera_base_headers = files([ 'object.h', 'semaphore.h', 'signal.h', + 'span.h', 'thread.h', 'timer.h', 'utils.h', diff --git a/include/libcamera/base/span.h b/include/libcamera/base/span.h new file mode 100644 index 00000000..21099d56 --- /dev/null +++ b/include/libcamera/base/span.h @@ -0,0 +1,424 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * span.h - C++20 std::span<> implementation for C++11 + */ + +#ifndef __LIBCAMERA_BASE_SPAN_H__ +#define __LIBCAMERA_BASE_SPAN_H__ + +#include +#include +#include +#include +#include + +namespace libcamera { + +static constexpr std::size_t dynamic_extent = std::numeric_limits::max(); + +template +class Span; + +namespace details { + +template +struct is_array : public std::false_type { +}; + +template +struct is_array> : public std::true_type { +}; + +template +struct is_span : public std::false_type { +}; + +template +struct is_span> : public std::true_type { +}; + +} /* namespace details */ + +namespace utils { + +template +constexpr auto size(const C &c) -> decltype(c.size()) +{ + return c.size(); +} + +template +constexpr auto data(const C &c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr auto data(C &c) -> decltype(c.data()) +{ + return c.data(); +} + +template +constexpr T *data(T (&array)[N]) noexcept +{ + return array; +} + +template +struct tuple_element; + +template +struct tuple_element> { + using type = T; +}; + +template +struct tuple_size; + +template +struct tuple_size> : public std::integral_constant { +}; + +template +struct tuple_size>; + +} /* namespace utils */ + +template +class Span +{ +public: + using element_type = T; + using value_type = typename std::remove_cv_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = T *; + using const_pointer = const T *; + using reference = T &; + using const_reference = const T &; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + static constexpr std::size_t extent = Extent; + + template> + constexpr Span() noexcept + : data_(nullptr) + { + } + + explicit constexpr Span(pointer ptr, [[maybe_unused]] size_type count) + : data_(ptr) + { + } + + explicit constexpr Span(pointer first, [[maybe_unused]] pointer last) + : data_(first) + { + } + + template + constexpr Span(element_type (&arr)[N], + std::enable_if_t (*)[], + element_type (*)[]>::value && + N == Extent, + std::nullptr_t> = nullptr) noexcept + : data_(arr) + { + } + + template + constexpr Span(std::array &arr, + std::enable_if_t (*)[], + element_type (*)[]>::value && + N == Extent, + std::nullptr_t> = nullptr) noexcept + : data_(arr.data()) + { + } + + template + constexpr Span(const std::array &arr, + std::enable_if_t (*)[], + element_type (*)[]>::value && + N == Extent, + std::nullptr_t> = nullptr) noexcept + : data_(arr.data()) + { + } + + template + explicit constexpr Span(Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) + : data_(utils::data(cont)) + { + } + + template + explicit constexpr Span(const Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) + : data_(utils::data(cont)) + { + static_assert(utils::size(cont) == Extent, "Size mismatch"); + } + + template + explicit constexpr Span(const Span &s, + std::enable_if_t::value && + N == Extent, + std::nullptr_t> = nullptr) noexcept + : data_(s.data()) + { + } + + constexpr Span(const Span &other) noexcept = default; + constexpr Span &operator=(const Span &other) noexcept = default; + + constexpr iterator begin() const { return data(); } + constexpr const_iterator cbegin() const { return begin(); } + constexpr iterator end() const { return data() + size(); } + constexpr const_iterator cend() const { return end(); } + constexpr reverse_iterator rbegin() const { return reverse_iterator(end()); } + constexpr const_reverse_iterator crbegin() const { return rbegin(); } + constexpr reverse_iterator rend() const { return reverse_iterator(begin()); } + constexpr const_reverse_iterator crend() const { return rend(); } + + constexpr reference front() const { return *data(); } + constexpr reference back() const { return *(data() + size() - 1); } + constexpr reference operator[](size_type idx) const { return data()[idx]; } + constexpr pointer data() const noexcept { return data_; } + + constexpr size_type size() const noexcept { return Extent; } + constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); } + constexpr bool empty() const noexcept { return size() == 0; } + + template + constexpr Span first() const + { + static_assert(Count <= Extent, "Count larger than size"); + return Span{ data(), Count }; + } + + constexpr Span first(std::size_t Count) const + { + return Span{ data(), Count }; + } + + template + constexpr Span last() const + { + static_assert(Count <= Extent, "Count larger than size"); + return Span{ data() + size() - Count, Count }; + } + + constexpr Span last(std::size_t Count) const + { + return Span{ data() + size() - Count, Count }; + } + + template + constexpr Span subspan() const + { + static_assert(Offset <= Extent, "Offset larger than size"); + static_assert(Count == dynamic_extent || Count + Offset <= Extent, + "Offset + Count larger than size"); + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; + } + + constexpr Span + subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const + { + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; + } + +private: + pointer data_; +}; + +template +class Span +{ +public: + using element_type = T; + using value_type = typename std::remove_cv_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = T *; + using const_pointer = const T *; + using reference = T &; + using const_reference = const T &; + using iterator = T *; + using const_iterator = const T *; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + static constexpr std::size_t extent = dynamic_extent; + + constexpr Span() noexcept + : data_(nullptr), size_(0) + { + } + + constexpr Span(pointer ptr, size_type count) + : data_(ptr), size_(count) + { + } + + constexpr Span(pointer first, pointer last) + : data_(first), size_(last - first) + { + } + + template + constexpr Span(element_type (&arr)[N], + std::enable_if_t (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) noexcept + : data_(arr), size_(N) + { + } + + template + constexpr Span(std::array &arr, + std::enable_if_t (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) noexcept + : data_(utils::data(arr)), size_(N) + { + } + + template + constexpr Span(const std::array &arr) noexcept + : data_(utils::data(arr)), size_(N) + { + } + + template + constexpr Span(Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) + : data_(utils::data(cont)), size_(utils::size(cont)) + { + } + + template + constexpr Span(const Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) + : data_(utils::data(cont)), size_(utils::size(cont)) + { + } + + template + constexpr Span(const Span &s, + std::enable_if_t::value, + std::nullptr_t> = nullptr) noexcept + : data_(s.data()), size_(s.size()) + { + } + + constexpr Span(const Span &other) noexcept = default; + + constexpr Span &operator=(const Span &other) noexcept + { + data_ = other.data_; + size_ = other.size_; + return *this; + } + + constexpr iterator begin() const { return data(); } + constexpr const_iterator cbegin() const { return begin(); } + constexpr iterator end() const { return data() + size(); } + constexpr const_iterator cend() const { return end(); } + constexpr reverse_iterator rbegin() const { return reverse_iterator(end()); } + constexpr const_reverse_iterator crbegin() const { return rbegin(); } + constexpr reverse_iterator rend() const { return reverse_iterator(begin()); } + constexpr const_reverse_iterator crend() const { return rend(); } + + constexpr reference front() const { return *data(); } + constexpr reference back() const { return *(data() + size() - 1); } + constexpr reference operator[](size_type idx) const { return data()[idx]; } + constexpr pointer data() const noexcept { return data_; } + + constexpr size_type size() const noexcept { return size_; } + constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); } + constexpr bool empty() const noexcept { return size() == 0; } + + template + constexpr Span first() const + { + return Span{ data(), Count }; + } + + constexpr Span first(std::size_t Count) const + { + return { data(), Count }; + } + + template + constexpr Span last() const + { + return Span{ data() + size() - Count, Count }; + } + + constexpr Span last(std::size_t Count) const + { + return Span{ data() + size() - Count, Count }; + } + + template + constexpr Span subspan() const + { + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; + } + + constexpr Span + subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const + { + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; + } + +private: + pointer data_; + size_type size_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_BASE_SPAN_H__ */ diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index f62b6cf0..1bc958a4 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -15,9 +15,9 @@ #include #include +#include #include -#include namespace libcamera { diff --git a/include/libcamera/internal/buffer.h b/include/libcamera/internal/buffer.h index 91dd2496..beae0cb9 100644 --- a/include/libcamera/internal/buffer.h +++ b/include/libcamera/internal/buffer.h @@ -11,9 +11,9 @@ #include #include +#include #include -#include namespace libcamera { diff --git a/include/libcamera/internal/byte_stream_buffer.h b/include/libcamera/internal/byte_stream_buffer.h index 7eefb1a7..d0f0df5e 100644 --- a/include/libcamera/internal/byte_stream_buffer.h +++ b/include/libcamera/internal/byte_stream_buffer.h @@ -12,8 +12,7 @@ #include #include - -#include +#include namespace libcamera { diff --git a/include/libcamera/internal/pub_key.h b/include/libcamera/internal/pub_key.h index f5f988b5..9261c9c2 100644 --- a/include/libcamera/internal/pub_key.h +++ b/include/libcamera/internal/pub_key.h @@ -9,7 +9,7 @@ #include -#include +#include #if HAVE_GNUTLS struct gnutls_pubkey_st; diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index 1edd664a..77b835b3 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -15,9 +15,9 @@ #include #include +#include #include -#include namespace libcamera { diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index c69d63d2..e4d5c655 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -12,7 +12,6 @@ libcamera_public_headers = files([ 'logging.h', 'pixel_format.h', 'request.h', - 'span.h', 'stream.h', 'transform.h', ]) diff --git a/include/libcamera/span.h b/include/libcamera/span.h deleted file mode 100644 index 7a35806b..00000000 --- a/include/libcamera/span.h +++ /dev/null @@ -1,424 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -/* - * Copyright (C) 2020, Google Inc. - * - * span.h - C++20 std::span<> implementation for C++11 - */ - -#ifndef __LIBCAMERA_SPAN_H__ -#define __LIBCAMERA_SPAN_H__ - -#include -#include -#include -#include -#include - -namespace libcamera { - -static constexpr std::size_t dynamic_extent = std::numeric_limits::max(); - -template -class Span; - -namespace details { - -template -struct is_array : public std::false_type { -}; - -template -struct is_array> : public std::true_type { -}; - -template -struct is_span : public std::false_type { -}; - -template -struct is_span> : public std::true_type { -}; - -} /* namespace details */ - -namespace utils { - -template -constexpr auto size(const C &c) -> decltype(c.size()) -{ - return c.size(); -} - -template -constexpr auto data(const C &c) -> decltype(c.data()) -{ - return c.data(); -} - -template -constexpr auto data(C &c) -> decltype(c.data()) -{ - return c.data(); -} - -template -constexpr T *data(T (&array)[N]) noexcept -{ - return array; -} - -template -struct tuple_element; - -template -struct tuple_element> { - using type = T; -}; - -template -struct tuple_size; - -template -struct tuple_size> : public std::integral_constant { -}; - -template -struct tuple_size>; - -} /* namespace utils */ - -template -class Span -{ -public: - using element_type = T; - using value_type = typename std::remove_cv_t; - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - using pointer = T *; - using const_pointer = const T *; - using reference = T &; - using const_reference = const T &; - using iterator = pointer; - using const_iterator = const_pointer; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - - static constexpr std::size_t extent = Extent; - - template> - constexpr Span() noexcept - : data_(nullptr) - { - } - - explicit constexpr Span(pointer ptr, [[maybe_unused]] size_type count) - : data_(ptr) - { - } - - explicit constexpr Span(pointer first, [[maybe_unused]] pointer last) - : data_(first) - { - } - - template - constexpr Span(element_type (&arr)[N], - std::enable_if_t (*)[], - element_type (*)[]>::value && - N == Extent, - std::nullptr_t> = nullptr) noexcept - : data_(arr) - { - } - - template - constexpr Span(std::array &arr, - std::enable_if_t (*)[], - element_type (*)[]>::value && - N == Extent, - std::nullptr_t> = nullptr) noexcept - : data_(arr.data()) - { - } - - template - constexpr Span(const std::array &arr, - std::enable_if_t (*)[], - element_type (*)[]>::value && - N == Extent, - std::nullptr_t> = nullptr) noexcept - : data_(arr.data()) - { - } - - template - explicit constexpr Span(Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) - : data_(utils::data(cont)) - { - } - - template - explicit constexpr Span(const Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) - : data_(utils::data(cont)) - { - static_assert(utils::size(cont) == Extent, "Size mismatch"); - } - - template - explicit constexpr Span(const Span &s, - std::enable_if_t::value && - N == Extent, - std::nullptr_t> = nullptr) noexcept - : data_(s.data()) - { - } - - constexpr Span(const Span &other) noexcept = default; - constexpr Span &operator=(const Span &other) noexcept = default; - - constexpr iterator begin() const { return data(); } - constexpr const_iterator cbegin() const { return begin(); } - constexpr iterator end() const { return data() + size(); } - constexpr const_iterator cend() const { return end(); } - constexpr reverse_iterator rbegin() const { return reverse_iterator(end()); } - constexpr const_reverse_iterator crbegin() const { return rbegin(); } - constexpr reverse_iterator rend() const { return reverse_iterator(begin()); } - constexpr const_reverse_iterator crend() const { return rend(); } - - constexpr reference front() const { return *data(); } - constexpr reference back() const { return *(data() + size() - 1); } - constexpr reference operator[](size_type idx) const { return data()[idx]; } - constexpr pointer data() const noexcept { return data_; } - - constexpr size_type size() const noexcept { return Extent; } - constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); } - constexpr bool empty() const noexcept { return size() == 0; } - - template - constexpr Span first() const - { - static_assert(Count <= Extent, "Count larger than size"); - return Span{ data(), Count }; - } - - constexpr Span first(std::size_t Count) const - { - return Span{ data(), Count }; - } - - template - constexpr Span last() const - { - static_assert(Count <= Extent, "Count larger than size"); - return Span{ data() + size() - Count, Count }; - } - - constexpr Span last(std::size_t Count) const - { - return Span{ data() + size() - Count, Count }; - } - - template - constexpr Span subspan() const - { - static_assert(Offset <= Extent, "Offset larger than size"); - static_assert(Count == dynamic_extent || Count + Offset <= Extent, - "Offset + Count larger than size"); - return Span{ - data() + Offset, - Count == dynamic_extent ? size() - Offset : Count - }; - } - - constexpr Span - subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const - { - return Span{ - data() + Offset, - Count == dynamic_extent ? size() - Offset : Count - }; - } - -private: - pointer data_; -}; - -template -class Span -{ -public: - using element_type = T; - using value_type = typename std::remove_cv_t; - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - using pointer = T *; - using const_pointer = const T *; - using reference = T &; - using const_reference = const T &; - using iterator = T *; - using const_iterator = const T *; - using reverse_iterator = std::reverse_iterator; - using const_reverse_iterator = std::reverse_iterator; - - static constexpr std::size_t extent = dynamic_extent; - - constexpr Span() noexcept - : data_(nullptr), size_(0) - { - } - - constexpr Span(pointer ptr, size_type count) - : data_(ptr), size_(count) - { - } - - constexpr Span(pointer first, pointer last) - : data_(first), size_(last - first) - { - } - - template - constexpr Span(element_type (&arr)[N], - std::enable_if_t (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) noexcept - : data_(arr), size_(N) - { - } - - template - constexpr Span(std::array &arr, - std::enable_if_t (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) noexcept - : data_(utils::data(arr)), size_(N) - { - } - - template - constexpr Span(const std::array &arr) noexcept - : data_(utils::data(arr)), size_(N) - { - } - - template - constexpr Span(Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) - : data_(utils::data(cont)), size_(utils::size(cont)) - { - } - - template - constexpr Span(const Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) - : data_(utils::data(cont)), size_(utils::size(cont)) - { - } - - template - constexpr Span(const Span &s, - std::enable_if_t::value, - std::nullptr_t> = nullptr) noexcept - : data_(s.data()), size_(s.size()) - { - } - - constexpr Span(const Span &other) noexcept = default; - - constexpr Span &operator=(const Span &other) noexcept - { - data_ = other.data_; - size_ = other.size_; - return *this; - } - - constexpr iterator begin() const { return data(); } - constexpr const_iterator cbegin() const { return begin(); } - constexpr iterator end() const { return data() + size(); } - constexpr const_iterator cend() const { return end(); } - constexpr reverse_iterator rbegin() const { return reverse_iterator(end()); } - constexpr const_reverse_iterator crbegin() const { return rbegin(); } - constexpr reverse_iterator rend() const { return reverse_iterator(begin()); } - constexpr const_reverse_iterator crend() const { return rend(); } - - constexpr reference front() const { return *data(); } - constexpr reference back() const { return *(data() + size() - 1); } - constexpr reference operator[](size_type idx) const { return data()[idx]; } - constexpr pointer data() const noexcept { return data_; } - - constexpr size_type size() const noexcept { return size_; } - constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); } - constexpr bool empty() const noexcept { return size() == 0; } - - template - constexpr Span first() const - { - return Span{ data(), Count }; - } - - constexpr Span first(std::size_t Count) const - { - return { data(), Count }; - } - - template - constexpr Span last() const - { - return Span{ data() + size() - Count, Count }; - } - - constexpr Span last(std::size_t Count) const - { - return Span{ data() + size() - Count, Count }; - } - - template - constexpr Span subspan() const - { - return Span{ - data() + Offset, - Count == dynamic_extent ? size() - Offset : Count - }; - } - - constexpr Span - subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const - { - return Span{ - data() + Offset, - Count == dynamic_extent ? size() - Offset : Count - }; - } - -private: - pointer data_; - size_type size_; -}; - -} /* namespace libcamera */ - -#endif /* __LIBCAMERA_SPAN_H__ */ -- cgit v1.2.1