From 09ab21b85ad83243878dab898eb0a6d0e85e6780 Mon Sep 17 00:00:00 2001 From: Jacopo Mondi Date: Thu, 2 Jan 2020 11:17:39 +0100 Subject: libcamera: Add a C++20-compliant std::span<> implementation C++20 will contain a std::span<> class that provides view over a contiguous sequence of objects, the storage of which is owned by some other object. Add a compatible implementation to the utils namespace. This will be used to implement array controls. Signed-off-by: Jacopo Mondi Signed-off-by: Laurent Pinchart Acked-by: Kieran Bingham --- include/libcamera/meson.build | 1 + include/libcamera/span.h | 417 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 418 insertions(+) create mode 100644 include/libcamera/span.h (limited to 'include') diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index f58c02d2..f47c583c 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -14,6 +14,7 @@ libcamera_api = files([ 'pixelformats.h', 'request.h', 'signal.h', + 'span.h', 'stream.h', 'timer.h', ]) diff --git a/include/libcamera/span.h b/include/libcamera/span.h new file mode 100644 index 00000000..513ddb43 --- /dev/null +++ b/include/libcamera/span.h @@ -0,0 +1,417 @@ +/* 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) + { + } + + constexpr Span(pointer ptr, size_type count) + : data_(ptr) + { + } + + constexpr Span(pointer first, 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 + 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 + 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 + 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 + { + data_ = other.data_; + 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(data() + size() - 1); } + constexpr const_reverse_iterator crbegin() const { return rbegin(); } + constexpr reverse_iterator rend() const { return reverse_iterator(data() - 1); } + 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 { data(), Count }; + } + + constexpr Span first(std::size_t Count) const + { + return { data(), Count }; + } + + template + constexpr Span last() const + { + static_assert(Count <= Extent, "Count larger than size"); + return { data() + size() - Count, Count }; + } + + constexpr Span last(std::size_t Count) const + { + return { 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 { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + } + + constexpr Span + subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const + { + return { 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(data() + size() - 1); } + constexpr const_reverse_iterator crbegin() const { return rbegin(); } + constexpr reverse_iterator rend() const { return reverse_iterator(data() - 1); } + 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 { data(), Count }; + } + + constexpr Span first(std::size_t Count) const + { + return { data(), Count }; + } + + template + constexpr Span last() const + { + return { data() + size() - Count, Count }; + } + + constexpr Span last(std::size_t Count) const + { + return { data() + size() - Count, Count }; + } + + template + constexpr Span subspan() const + { + return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + } + + constexpr Span + subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const + { + return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + } + +private: + pointer data_; + size_type size_; +}; + +}; /* namespace libcamera */ + +#endif /* __LIBCAMERA_SPAN_H__ */ -- cgit v1.2.1