{"id":2628,"url":"https://patchwork.libcamera.org/api/1.1/patches/2628/?format=json","web_url":"https://patchwork.libcamera.org/patch/2628/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200113164245.52535-14-jacopo@jmondi.org>","date":"2020-01-13T16:42:35","name":"[libcamera-devel,13/23] libcamera: span: Add support for STL containers","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"244a33c57987aa813ecb694c2766e70b064e6c4a","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":{"id":15,"url":"https://patchwork.libcamera.org/api/1.1/users/15/?format=json","username":"jmondi","first_name":"Jacopo","last_name":"Mondi","email":"jacopo@jmondi.org"},"mbox":"https://patchwork.libcamera.org/patch/2628/mbox/","series":[{"id":618,"url":"https://patchwork.libcamera.org/api/1.1/series/618/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=618","date":"2020-01-13T16:42:22","name":"Properties and compound controls","version":1,"mbox":"https://patchwork.libcamera.org/series/618/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2628/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2628/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay11.mail.gandi.net (relay11.mail.gandi.net\n\t[217.70.178.231])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BC5D3606EA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 13 Jan 2020 17:40:34 +0100 (CET)","from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay11.mail.gandi.net (Postfix) with ESMTPSA id 43B0D100012;\n\tMon, 13 Jan 2020 16:40:34 +0000 (UTC)"],"From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 13 Jan 2020 17:42:35 +0100","Message-Id":"<20200113164245.52535-14-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.24.0","In-Reply-To":"<20200113164245.52535-1-jacopo@jmondi.org>","References":"<20200113164245.52535-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 13/23] libcamera: span: Add support for\n\tSTL containers","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Mon, 13 Jan 2020 16:40:34 -0000"},"content":"From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nAdd support for STL containers to the Span class implementation and\nmake it fully C++20-compliant.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/span.h | 187 +++++++++++++++++++++++++++++++--------\n src/libcamera/span.cpp   | 115 ------------------------\n 2 files changed, 152 insertions(+), 150 deletions(-)","diff":"diff --git a/include/libcamera/span.h b/include/libcamera/span.h\nindex 3e63603f60ed..0fffad3a99db 100644\n--- a/include/libcamera/span.h\n+++ b/include/libcamera/span.h\n@@ -9,79 +9,196 @@\n #define __LIBCAMERA_SPAN_H__\n \n #include <array>\n+#include <iterator>\n+#include <limits>\n #include <stddef.h>\n+#include <type_traits>\n \n namespace libcamera {\n \n+static constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();\n+\n+template<typename T, size_t Extent = dynamic_extent>\n+class Span;\n+\n template<typename T>\n-class Span\n+class Span<T, dynamic_extent>\n {\n private:\n+\ttemplate<typename U>\n+\tstruct is_array : public std::false_type {\n+\t};\n+\n+\ttemplate<typename U, std::size_t N>\n+\tstruct is_array<std::array<U, N>> : public std::true_type {\n+\t};\n+\n+\ttemplate<typename U>\n+\tstruct is_span : public std::false_type {\n+\t};\n+\n+\ttemplate<typename U, std::size_t Extent>\n+\tstruct is_span<Span<U, Extent>> : public std::true_type {\n+\t};\n+\n+\ttemplate<bool B, class U = void>\n+\tusing enable_if_t = typename std::enable_if<B, U>::type;\n+\n+\ttemplate<typename U>\n+\tusing remove_pointer_t = typename std::remove_pointer<U>::type;\n+\n+public:\n+\tusing element_type = T;\n+\tusing value_type = typename std::remove_cv<T>::type;\n+\tusing size_type = std::size_t;\n+\tusing different_type = std::ptrdiff_t;\n+\tusing pointer = T *;\n+\tusing const_pointer = const T *;\n+\tusing reference = T &;\n+\tusing const_reference = const T &;\n \tusing iterator = T *;\n \tusing const_iterator = const T *;\n+\tusing reverse_iterator = std::reverse_iterator<iterator>;\n+\tusing const_reverse_iterator = std::reverse_iterator<const iterator>;\n \n-\tclass Storage\n+\tstatic constexpr std::size_t extent = dynamic_extent;\n+\n+\tconstexpr Span() noexcept\n+\t\t: data_(nullptr), size_(0)\n \t{\n-\tpublic:\n-\t\tStorage(T *ptr, size_t size)\n-\t\t\t: ptr_(ptr), size_(size)\n-\t\t{\n-\t\t}\n+\t}\n \n-\t\tT *ptr() const { return ptr_; }\n-\t\tsize_t size() const { return size_; }\n+\tconstexpr Span(pointer ptr, size_type count)\n+\t\t: data_(ptr), size_(count)\n+\t{\n+\t}\n \n-\tprivate:\n-\t\tT *ptr_;\n-\t\tsize_t size_;\n-\t};\n+\tconstexpr Span(pointer first, pointer last)\n+\t\t: data_(first), size_(last - first)\n+\t{\n+\t}\n \n-public:\n-\tSpan(T &v)\n-\t\t: storage_(&v, 1)\n+\ttemplate<std::size_t N>\n+\tconstexpr Span(element_type (&arr)[N],\n+\t\t       enable_if_t<std::is_convertible<remove_pointer_t<decltype(arr)> (*)[],\n+\t\t\t\t\t\t       element_type (*)[]>::value,\n+\t\t\t\t   std::nullptr_t> = nullptr) noexcept\n+\t\t: data_(arr), size_(N)\n \t{\n \t}\n \n-\tSpan(const T &v)\n-\t\t: storage_(const_cast<T *>(&v), 1)\n+\ttemplate<std::size_t N>\n+\tconstexpr Span(std::array<value_type, N> &arr,\n+\t\t       enable_if_t<std::is_convertible<remove_pointer_t<decltype(arr.data())> (*)[],\n+\t\t\t\t\t\t       element_type (*)[]>::value,\n+\t\t\t\t   std::nullptr_t> = nullptr) noexcept\n+\t\t: data_(arr.data()), size_(N)\n \t{\n \t}\n \n-\tSpan(T *v, size_t s)\n-\t\t: storage_(v, s)\n+\ttemplate<std::size_t N>\n+\tconstexpr Span(const std::array<value_type, N> &arr) noexcept\n+\t\t: data_(arr.data()), size_(N)\n \t{\n \t}\n \n-\tSpan(const T *v, size_t s)\n-\t\t: storage_(const_cast<T *>(v), s)\n+\ttemplate<class Container>\n+\tconstexpr Span(Container &cont,\n+\t\t       enable_if_t<!is_span<Container>::value &&\n+\t\t\t\t   !is_array<Container>::value &&\n+\t\t\t\t   !std::is_array<Container>::value &&\n+\t\t\t\t   std::is_convertible<remove_pointer_t<decltype(cont.data())> (*)[],\n+\t\t\t\t\t\t       element_type (*)[]>::value,\n+\t\t\t\t   std::nullptr_t> = nullptr)\n+\t\t: data_(cont.data()), size_(cont.size())\n \t{\n \t}\n \n-\tSpan(std::initializer_list<T> list)\n-\t\t: storage_(const_cast<T *>(list.begin()), list.size())\n+\ttemplate<class Container>\n+\tconstexpr Span(const Container &cont,\n+\t\t       enable_if_t<!is_span<Container>::value &&\n+\t\t\t\t   !is_array<Container>::value &&\n+\t\t\t\t   !std::is_array<Container>::value &&\n+\t\t\t\t   std::is_convertible<remove_pointer_t<decltype(cont.data())> (*)[],\n+\t\t\t\t\t\t       element_type (*)[]>::value,\n+\t\t\t\t   std::nullptr_t> = nullptr)\n+\t\t: data_(cont.data()), size_(cont.size())\n \t{\n \t}\n \n-\tSpan(const Span &other) = default;\n-\tSpan &operator=(const Span &other) = default;\n+\ttemplate<class U, std::size_t N>\n+\tconstexpr Span(const Span<U, N> &s,\n+\t\t       enable_if_t<std::is_convertible<U (*)[], element_type (*)[]>::value,\n+\t\t\t\t   std::nullptr_t> = nullptr) noexcept\n+\t\t: data_(s.data()), size_(s.size())\n+\t{\n+\t}\n \n-\tT *data() const { return storage_.ptr(); }\n-\tsize_t size() const { return storage_.size(); }\n+\tconstexpr Span(const Span &other) noexcept = default;\n \n-\tT &operator[](unsigned int index) const\n+#pragma GCC diagnostic push\n+#pragma GCC diagnostic ignored \"-Wconstexpr-not-const\"\n+\tconstexpr Span &operator=(const Span &other) noexcept\n \t{\n-\t\tif (index >= size())\n-\t\t\treturn *(end() - 1);\n-\t\treturn *(data() + index);\n+\t\treturn *this, data_ = other.data_, size_ = other.size_;\n \t}\n+#pragma GCC diagnostic pop\n \n \tconstexpr iterator begin() const { return data(); }\n-\tconstexpr iterator cbegin() const { return begin(); }\n+\tconstexpr const_iterator cbegin() const { return begin(); }\n \tconstexpr iterator end() const { return data() + size(); }\n-\tconstexpr iterator cend() const { return end(); }\n+\tconstexpr const_iterator cend() const { return end(); }\n+\tconstexpr reverse_iterator rbegin() const { return data() + size() - 1; }\n+\tconstexpr const_reverse_iterator crbegin() const { return rbegin(); }\n+\tconstexpr reverse_iterator rend() const { return data() - 1; }\n+\tconstexpr const_reverse_iterator crend() const { return rend(); }\n+\n+\tconstexpr reference front() const { return *data(); }\n+\tconstexpr reference back() const { return *(data() + size() - 1); }\n+\tconstexpr reference operator[](size_type idx) const { return data()[idx]; }\n+\tconstexpr pointer data() const noexcept { return data_; }\n+\n+\tconstexpr size_type size() const noexcept { return size_; }\n+\tconstexpr size_type size_bytes() const noexcept { return size_ * sizeof(element_type); }\n+\tconstexpr bool empty() const noexcept { return size_ == 0; }\n+\n+\ttemplate<std::size_t Count>\n+\tconstexpr Span<element_type, Count> first() const\n+\t{\n+\t\treturn { data(), Count };\n+\t}\n+\n+\tconstexpr Span<element_type, dynamic_extent> first(std::size_t Count) const\n+\t{\n+\t\treturn { data(), Count };\n+\t}\n+\n+\ttemplate<std::size_t Count>\n+\tconstexpr Span<element_type, Count> last() const\n+\t{\n+\t\treturn { data() + size() - Count, Count };\n+\t}\n+\n+\tconstexpr Span<element_type, dynamic_extent> last(std::size_t Count) const\n+\t{\n+\t\treturn { data() + size() - Count, Count };\n+\t}\n+\n+\ttemplate<std::size_t Offset, std::size_t Count = dynamic_extent>\n+\tconstexpr Span<element_type, dynamic_extent> subspan() const\n+\t{\n+\t\treturn { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };\n+\t}\n+\n+\tconstexpr Span<element_type, dynamic_extent>\n+\tsubspan(std::size_t Offset, std::size_t Count = dynamic_extent) const\n+\t{\n+\t\treturn { data() + Offset, Count == dynamic_extent ? size() - Offset : Count };\n+\t}\n \n private:\n-\tStorage storage_;\n+\tpointer data_;\n+\tsize_type size_;\n };\n \n }; /* namespace libcamera */\ndiff --git a/src/libcamera/span.cpp b/src/libcamera/span.cpp\nindex 72ffdf7481c4..43391b29ec85 100644\n--- a/src/libcamera/span.cpp\n+++ b/src/libcamera/span.cpp\n@@ -7,122 +7,7 @@\n \n #include <libcamera/span.h>\n \n-/**\n- * \\file span.h\n- * \\brief libcamera implementation of C++20 std::span<>\n- */\n-\n namespace libcamera {\n \n-/**\n- * \\class Span\n- * \\brief Representation of a sequence of contiguous objects of type T\n- *\n- * This class represents a sequence of fixed size of contiguous objects of\n- * template type T with the first object residing in position 0.\n- *\n- * A Span internally consists of a pointer to raw memory and an associated\n- * number of elements there located. It does not enforce any ownership on the\n- * memory it represents, but it only provides a convenient, lightweight and\n- * easily transportable view on such memory area.\n- *\n- * A Span can be constructed from a single element as well as from raw memory\n- * by providing an associated size. It can be accessed by index and iterated\n- * as a regular standard library container. As Span does not enforce any memory\n- * ownership, destroying a Span instance does not invalidate the memory it\n- * represents.\n- *\n- * The libcamera implementation of Span it's a simplified implementation of\n- * C++20 the std::span<> class and it is no 1-to-1 compatible with it. Care\n- * should be taken in not mixing usage of the two classes.\n- */\n-\n-/**\n- * \\fn Span::Span(T &v)\n- * \\brief Contruct a Span of size 1 representing element \\a v\n- * \\param[in] v The element represented by the Span\n- */\n-\n-/**\n- * \\fn Span::Span(const T &v)\n- * \\brief Contruct a Span of size 1 representing the constant element \\a v\n- * \\param[in] v The constant element represented by the Span\n- */\n-\n-/**\n- * \\fn Span::Span(T *v, size_t s)\n- * \\brief Contruct a Span of size \\a s representing elements in memory \\a v\n- * \\param[in] v The memory area represeted by the Span\n- * \\param[in] s The number of elements in memory area \\a v\n- */\n-\n-/**\n- * \\fn Span::Span(const T *v, size_t s)\n- * \\brief Contruct a Span of size \\a s representing elements in constant memory \\a v\n- * \\param[in] v The constant memory area represeted by the Span\n- * \\param[in] s The number of elements in memory area \\a v\n- */\n-\n-/**\n- * \\fn Span::Span(std::initializer_list<T> list)\n- * \\brief Contruct a Span with an initialier list of elements\n- * \\param[in] list The initializer list\n- */\n-\n-/**\n- * \\fn Span::Span(const Span &other)\n- * \\brief Contruct a Span with the content of \\a other\n- * \\param[in] other The other Span\n- */\n-\n-/**\n- * \\fn Span::operator=(const Span &other)\n- * \\brief Replace the content of the Span with the one from \\a other\n- * \\param[in] other The other Span\n- */\n-\n-/**\n- * \\fn Span::data()\n- * \\brief Retrieve a pointer to the beginning of the memory area represented by\n- * the Span\n- * \\return A pointer to the raw memory area\n- */\n-\n-/**\n- * \\fn Span::size()\n- * \\brief Retrieve the number of elements in the Span\n- * \\return The number of elements in the Span\n- */\n-\n-/**\n- * \\fn Span::operator[](unsigned int index)\n- * \\brief Retrieve element in position \\a index\n- * \\param[in] index\n- *\n- * If \\a index is larger than the number of elements in the Span, the last\n- * element is returned.\n- *\n- * \\return The element at position \\a index\n- */\n-\n-/**\n- * \\fn iterator Span::begin()\n- * \\brief Retrieve an iterator to the first element in the Span\n- */\n-\n-/**\n- * \\fn const_iterator Span::cbegin()\n- * \\brief Retrieve a constant iterator to the first element in the Span\n- */\n-\n-/**\n- * \\fn iterator Span::end()\n- * \\brief Retrieve an iterator pointing to the past-the-end element in the Span\n- */\n-\n-/**\n- * \\fn const_iterator Span::cend()\n- * \\brief Retrieve a constant iterator pointing to the past-the-end element in the Span\n- */\n \n } /* namespace libcamera */\n","prefixes":["libcamera-devel","13/23"]}