22#define KLIB_MULTISPAN_HPP
33
44#include < cstdint>
5+ #include < type_traits>
56#include < span>
67#include < array>
78
89namespace klib {
910 /* *
10- * @brief Non owning wrapper to map two std::span in a single array
11+ * @brief Base class to a multispan, implementation dependend
1112 *
1213 * @tparam T
1314 */
1415 template <typename T>
1516 class multispan {
1617 protected:
17- // amount of items in the buffer
18- constexpr static uint32_t Amount = 2 ;
19-
20- // the two internal spans
21- std::array<std::span<T>, Amount> storage ;
18+ /* *
19+ * @brief Default constructor
20+ *
21+ */
22+ constexpr multispan () {} ;
2223
2324 public:
2425 // member types (no iterators as it is not a continuous
@@ -33,33 +34,70 @@ namespace klib {
3334 using const_reference = const element_type&;
3435
3536 /* *
36- * @brief Construct a new multispan object using a initializer list, raw
37- * array(not a pointer) or std::array
37+ * @brief Operator to get data from the two spans
3838 *
39- * for raw pointers {ptr, size} should be used as a parameter
39+ * @param index
40+ * @return constexpr T&
41+ */
42+ virtual reference operator [](const uint32_t index) const = 0 ;
43+
44+ /* *
45+ * @brief Returns the total size of both spans
4046 *
41- * @param first
42- * @param second
47+ * @return uint32_t
4348 */
44- template <typename ... E>
45- constexpr multispan (const std::span<T>& first, const std::span<T>& second):
46- storage{first, second}
47- {}
49+ virtual uint32_t size () const = 0;
4850
4951 /* *
50- * @brief Copy constructor for the multispan
52+ * @brief Returns if the span is empty
5153 *
54+ * @return uint32_t
5255 */
53- constexpr multispan (const multispan&) noexcept = default;
56+ uint32_t empty () const {
57+ return size () == 0 ;
58+ }
5459
5560 /* *
56- * @brief Construct a multispan from a different multispan
61+ * @brief Returns the amount of bytes the combined spans use
5762 *
58- * @tparam G
63+ * @return constexpr uint32_t
64+ */
65+ virtual uint32_t size_bytes () const = 0;
66+ };
67+
68+ /* *
69+ * @brief Non owning wrapper for multiple arrays
70+ *
71+ * @tparam T
72+ */
73+ template <typename T, uint32_t Amount>
74+ class span_array : public klib ::multispan<T> {
75+ protected:
76+ std::array<std::span<T>, Amount> storage = {};
77+
78+ public:
79+ /* *
80+ * @brief Span_array constructor from multple std::spans
81+ *
82+ */
83+ constexpr span_array (const std::initializer_list<std::span<T>>& list) {
84+ uint32_t index = 0 ;
85+
86+ // copy the spans into the storage
87+ for (const auto & l: list) {
88+ storage[index] = l;
89+ index++;
90+ }
91+ }
92+
93+ /* *
94+ * @brief Construct a multispan using variadic templates
95+ *
96+ * @tparam Args
5997 */
60- template <typename G >
61- constexpr multispan ( const multispan<G>& other) noexcept :
62- storage(other.store() )
98+ template <typename ... Args >
99+ constexpr span_array (Args... args):
100+ span_array({args...} )
63101 {}
64102
65103 /* *
@@ -68,7 +106,7 @@ namespace klib {
68106 * @param index
69107 * @return constexpr T&
70108 */
71- constexpr reference operator [](const uint32_t index) const {
109+ constexpr klib::multispan<T>:: reference operator [](const uint32_t index) const {
72110 uint32_t offset = 0 ;
73111
74112 // check where we need to point to
@@ -91,7 +129,7 @@ namespace klib {
91129 *
92130 * @return uint32_t
93131 */
94- constexpr uint32_t size () const {
132+ constexpr uint32_t size () const override {
95133 uint32_t count = 0 ;
96134
97135 for (auto & s: storage) {
@@ -101,21 +139,12 @@ namespace klib {
101139 return count;
102140 }
103141
104- /* *
105- * @brief Returns if the span is empty
106- *
107- * @return uint32_t
108- */
109- constexpr uint32_t empty () const {
110- return size () == 0 ;
111- }
112-
113142 /* *
114143 * @brief Returns the amount of bytes the combined spans use
115144 *
116145 * @return constexpr uint32_t
117146 */
118- constexpr uint32_t size_bytes () const {
147+ constexpr uint32_t size_bytes () const override {
119148 uint32_t count = 0 ;
120149
121150 for (auto & s: storage) {
@@ -137,6 +166,23 @@ namespace klib {
137166 return storage;
138167 }
139168 };
169+
170+ /* *
171+ * @brief Helper to create a span_array from multiple std::span. Uses the first
172+ * argument as the base type for all the elements
173+ *
174+ * @tparam Args
175+ * @param args
176+ * @return auto
177+ */
178+ template <typename ... Args>
179+ auto make_span_array (Args... args) {
180+ // return a span_array with the correct size and initalize it
181+ return klib::span_array<
182+ typename std::tuple_element_t <0 , std::tuple<Args...>>::element_type,
183+ sizeof ...(Args)
184+ >(args...);
185+ }
140186}
141187
142188#endif
0 commit comments