CVB++ 14.1
block_helper_ref_value.hpp
1#pragma once
2
3#include "../../shims/stdtype_traits.hpp"
4
5#include "detail_block.hpp"
6
7namespace Cvb
8{
9 CVB_BEGIN_INLINE_NS
10
11 namespace // NOLINT(cert-dcl59-cpp)
12 {
16 template <template <class, size_t> class CRefValue>
18 {
23 template <size_t I, class Ty, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
24 static CVB_FORCE_INLINE void set(CRefValue<Ty, K> &refVal, const Ty *val) noexcept
25 {
26 refVal.pointers_[I] = const_cast<Ty *>(val); // NOLINT(cppcoreguidelines-pro-type-const-cast)
27 // we have to const_cast here, as the stored pointers must be
28 // mutable as we set them here and for RefValue.
29 }
30 };
31
35 template <template <class, size_t> class RValue>
37 {
42 template <size_t I, class Ty, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
43 static CVB_FORCE_INLINE void set(RValue<Ty, K> &refVal, Ty *val) noexcept
44 {
45 refVal.pointers_[I] = val;
46 }
47 };
48 } // namespace
49
55 template <class T, size_t K>
56 class ConstRefValue final
57 {
58 static_assert(K > 0, "Cvb: ConstRefValue must hold at least one element.");
59
60 template <class Ty, class ACCESSTRAIT>
61 friend class Block;
62 template <template <class, size_t> class CRefValue>
63 friend struct ConstRefValueSetter;
64
65 std::array<T *, K> pointers_;
66
67 /*
68 * \brief Default Ctor creating invalid object.
69 */
71 {
72 for (auto &ptr : pointers_)
73 {
74 ptr = nullptr;
75 }
76 }
77
78 public:
79 using ComponentType = T;
80
87 template <class... TT>
88 explicit ConstRefValue(const TT &... refs)
89 : pointers_({const_cast<T *>(&refs)...}) // NOLINT(cppcoreguidelines-pro-type-const-cast)
90 // we have to const_cast here, as the stored pointers must be mutable
91 // through the setters and for RefValue.
92 {
93 static_assert(sizeof...(TT) == K, "Cvb: Number of parameters must equal the number of elements.");
94 }
95
96
97 ConstRefValue(const ConstRefValue & other) noexcept = default;
98 ConstRefValue &operator=(const ConstRefValue & other) noexcept = default;
99 ConstRefValue(ConstRefValue&& other) noexcept = default;
100 ConstRefValue& operator=(ConstRefValue&& other) noexcept = default;
101 ~ConstRefValue() = default;
102
103 private:
104 template <class Ty, size_t... I>
105 Ty Cast(std::index_sequence<I...>) const noexcept
106 {
107 using expander = int[]; // NOLINT(cppcoreguidelines-avoid-c-arrays) fold expression support for pre C++17 standards..
108 // for explanation see Block::FillPixelValueElements
109
110 Ty value{};
111 expander{0, ((void)set<I>(value, *pointers_[I]), 0)...};
112 return value;
113 }
114
115 public:
122 template <class Ty>
123 Ty Cast() const noexcept
124 {
125 return Cast<Ty>(std::make_index_sequence<K>());
126 }
127
133 T X() const noexcept
134 {
135 return *pointers_[0];
136 }
137
143 template <size_t N = K, std::enable_if_t<less<1, N>::value, int> = 0>
144 T Y() const noexcept
145 {
146 return *pointers_[1];
147 }
148
154 template <size_t N = K, std::enable_if_t<less<2, N>::value, int> = 0>
155 T Z() const noexcept
156 {
157 return *pointers_[2];
158 }
159
165 template <size_t N = K, std::enable_if_t<less<3, N>::value, int> = 0>
166 T W() const noexcept
167 {
168 return *pointers_[3];
169 }
170
177 T operator[](size_t i) const noexcept
178 {
179 assert(i < K);
180
181 return *pointers_[i];
182 }
183
189 static constexpr size_t NumValues() noexcept
190 {
191 return K;
192 }
193 };
194
200 template <class T, size_t K>
201 class RefValue final
202 {
203 /*
204 * This class could theoretically inherit from ConstRefValue,
205 * but as that would require adding a virtual destructor to ConstRefValue
206 * to ensure correct handling of deletion of pointers to these classes,
207 * both classes are implemented as concrete classes.
208 *
209 * The caveat of adding a virtual destructor is, that it requires constructing
210 * a vtable which seems to disable inlining and therefore hurts the performance.
211 */
212
213 static_assert(K > 0, "Cvb: RefValue must hold at least one element.");
214
215 template <class Ty, class ACCESSTRAIT>
216 friend class Block;
217 template <template <class, size_t> class RValue>
218 friend struct RefValueSetter;
219
220 std::array<T *, K> pointers_;
221
222 /*
223 * \brief Default Ctor creating invalid object.
224 */
225 RefValue()
226 {
227 for (auto &ptr : pointers_)
228 {
229 ptr = nullptr;
230 }
231 }
232
233 public:
234 using ComponentType = T;
235
241 template <class... TT>
242 explicit RefValue(TT &... refs)
243 : pointers_({const_cast<T *>(&refs)...}) // NOLINT(cppcoreguidelines-pro-type-const-cast)
244 // we have to const_cast here, as the stored pointers must be
245 // mutable through the setters and for RefValue.
246 {
247 }
248
249
250 RefValue(const RefValue & other) noexcept = default;
251 RefValue &operator=(const RefValue & other) noexcept = default;
252 RefValue(RefValue&& other) noexcept = default;
253 RefValue& operator=(RefValue&& other) noexcept = default;
254 ~RefValue() = default;
255
256 private:
257 template <class Ty, size_t... I>
258 RefValue<T, K> &Assign(const Ty &other, std::index_sequence<I...>) noexcept
259 {
260 using expander = int[]; // NOLINT(cppcoreguidelines-avoid-c-arrays) fold expression support for pre C++17 standards..
261 // for explanation see Block::FillPixelValueElements
262
263 expander{0, ((void)(*pointers_[I] = get<I>(other)), 0)...};
264 return *this;
265 }
266
267 public:
277 template <class Ty>
278 RefValue<T, K> &operator=(const Ty &other) noexcept
279 {
280 return Assign(other, std::make_index_sequence<K>()); // NOLINT(cppcoreguidelines-c-copy-assignment-signature)
281 }
282
291 template <size_t N>
292 RefValue<T, K> &operator=(const T (&list)[N]) noexcept // NOLINT(cppcoreguidelines-avoid-c-arrays)
293 {
294 static_assert(N == K, "Cvb: The initializer list must contain K elements.");
295
296 for (int i = 0; i < N; ++i)
297 {
298 *pointers_[i] = list[i];
299 }
300 return *this;
301 }
302
303 private:
304 template <class Ty, size_t... I>
305 Ty Cast(std::index_sequence<I...>) const noexcept
306 {
307 using expander = int[]; // NOLINT(cppcoreguidelines-avoid-c-arrays) fold expression support for pre C++17 standards..
308 // for explanation see Block::FillPixelValueElements
309
310 Ty value{};
311 expander{0, ((void)set<I>(value, *pointers_[I]), 0)...};
312 return value;
313 }
314
315 public:
322 template <class Ty>
323 Ty Cast() const noexcept
324 {
325 return Cast<Ty>(std::make_index_sequence<K>());
326 }
327
333 T X() const noexcept
334 {
335 return *pointers_[0];
336 }
337
343 T &X() noexcept
344 {
345 return *pointers_[0];
346 }
347
353 template <size_t N = K, std::enable_if_t<less<1, N>::value, int> = 0>
354 T Y() const noexcept
355 {
356 return *pointers_[1];
357 }
358
364 template <size_t N = K, std::enable_if_t<less<1, N>::value, int> = 0>
365 T &Y() noexcept
366 {
367 return *pointers_[1];
368 }
369
375 template <size_t N = K, std::enable_if_t<less<2, N>::value, int> = 0>
376 T Z() const noexcept
377 {
378 return *pointers_[2];
379 }
380
386 template <size_t N = K, std::enable_if_t<less<2, N>::value, int> = 0>
387 T &Z() noexcept
388 {
389 return *pointers_[2];
390 }
391
397 template <size_t N = K, std::enable_if_t<less<3, N>::value, int> = 0>
398 T W() const noexcept
399 {
400 return *pointers_[3];
401 }
402
408 template <size_t N = K, std::enable_if_t<less<3, N>::value, int> = 0>
409 T &W() noexcept
410 {
411 return *pointers_[3];
412 }
413
420 T operator[](size_t i) const noexcept
421 {
422 assert(i < K);
423
424 return *pointers_[i];
425 }
426
433 T &operator[](size_t i) noexcept
434 {
435 assert(i < K);
436
437 return *pointers_[i];
438 }
439
445 static constexpr const size_t NumValues() noexcept
446 {
447 return K;
448 }
449 };
450
463 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
464 auto get(const RefValue<T, K> &refVal) noexcept -> decltype(refVal[I])
465 {
466 return refVal[I];
467 }
468
481 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
482 auto get(const ConstRefValue<T, K> &refVal) noexcept -> decltype(refVal[I])
483 {
484 return refVal[I];
485 }
486
499 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
500 void set(RefValue<T, K> &refVal, const T &val) noexcept
501 {
502 refVal[I] = val;
503 }
504
522 template <size_t I, class Ty, size_t K>
523 CVB_FORCE_INLINE void internal_set(RefValue<Ty, K> &refVal, Ty *val) noexcept
524 {
525 assert(val != nullptr);
526
527 RefValueSetter<RefValue>::template set<I>(refVal, val);
528 }
546 template <size_t I, class Ty, size_t K>
547 CVB_FORCE_INLINE void internal_set(ConstRefValue<Ty, K> &refVal, const Ty *val) noexcept
548 {
549 assert(val != nullptr);
550
551 ConstRefValueSetter<ConstRefValue>::template set<I>(refVal, val);
552 }
553
554 CVB_END_INLINE_NS
555} // namespace Cvb
Non-owning view on a 2d-plane of data.
Definition: decl_block.hpp:23
This class holds references to K values of type T.
Definition: block_helper_ref_value.hpp:57
ConstRefValue(const TT &... refs)
Constructor from reference values.
Definition: block_helper_ref_value.hpp:88
T X() const noexcept
Get const value of the first (x) value.
Definition: block_helper_ref_value.hpp:133
T W() const noexcept
Get const value of the fourth (w) value.
Definition: block_helper_ref_value.hpp:166
Ty Cast() const noexcept
Cast to any type, for which a set<size_t>(Ty) function exists.
Definition: block_helper_ref_value.hpp:123
T Z() const noexcept
Get const value of the third (z) value.
Definition: block_helper_ref_value.hpp:155
static constexpr size_t NumValues() noexcept
Get number of values stored in this ConstRefValue.
Definition: block_helper_ref_value.hpp:189
T operator[](size_t i) const noexcept
Element access.
Definition: block_helper_ref_value.hpp:177
auto get(const ConstRefValue< T, K > &refVal) noexcept -> decltype(refVal[I])
Get's the I-th element of the ConstRefValue<T,K>.
Definition: block_helper_ref_value.hpp:482
T Y() const noexcept
Get const value of the second (y) value.
Definition: block_helper_ref_value.hpp:144
CVB_FORCE_INLINE void internal_set(ConstRefValue< Ty, K > &refVal, const Ty *val) noexcept
Definition: block_helper_ref_value.hpp:547
This class holds mutable references to K values of type T.
Definition: block_helper_ref_value.hpp:202
void set(RefValue< T, K > &refVal, const T &val) noexcept
Set's the value of the I-th element in the RefValue<T,K> refVal.
Definition: block_helper_ref_value.hpp:500
T X() const noexcept
Get const value of the first (x) value.
Definition: block_helper_ref_value.hpp:333
T & Y() noexcept
Get reference to the second (y) value.
Definition: block_helper_ref_value.hpp:365
T W() const noexcept
Get const value of the fourth (w) value.
Definition: block_helper_ref_value.hpp:398
Ty Cast() const noexcept
Cast to any type, for which a set<size_t>(Ty) function exists.
Definition: block_helper_ref_value.hpp:323
CVB_FORCE_INLINE void internal_set(RefValue< Ty, K > &refVal, Ty *val) noexcept
Definition: block_helper_ref_value.hpp:523
T & X() noexcept
Get reference to the first (x) value.
Definition: block_helper_ref_value.hpp:343
T Z() const noexcept
Get const value of the third (z) value.
Definition: block_helper_ref_value.hpp:376
RefValue< T, K > & operator=(const T(&list)[N]) noexcept
Assignment from a N-element initializer list or array. The values pointed to by this class are overwr...
Definition: block_helper_ref_value.hpp:292
RefValue(TT &... refs)
Constructor from reference values.
Definition: block_helper_ref_value.hpp:242
T & W() noexcept
Get reference to the fourth (w) value.
Definition: block_helper_ref_value.hpp:409
T operator[](size_t i) const noexcept
Element access.
Definition: block_helper_ref_value.hpp:420
T & Z() noexcept
Get reference to the third (z) value.
Definition: block_helper_ref_value.hpp:387
T & operator[](size_t i) noexcept
Mutable element access.
Definition: block_helper_ref_value.hpp:433
T Y() const noexcept
Get const value of the second (y) value.
Definition: block_helper_ref_value.hpp:354
static constexpr const size_t NumValues() noexcept
Get number of values stored in this RefValue.
Definition: block_helper_ref_value.hpp:445
RefValue< T, K > & operator=(const Ty &other) noexcept
Assignment from any type, for which get<size_t>(Ty) function exists. The values pointed to by this cl...
Definition: block_helper_ref_value.hpp:278
auto get(const RefValue< T, K > &refVal) noexcept -> decltype(refVal[I])
Get's the I-th element of the RefValue<T,K>.
Definition: block_helper_ref_value.hpp:464
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
Helper class for setting the pointers, as friending template functions is not really stable.
Definition: block_helper_ref_value.hpp:18
static CVB_FORCE_INLINE void set(CRefValue< Ty, K > &refVal, const Ty *val) noexcept
Definition: block_helper_ref_value.hpp:24
Helper class for setting the pointers, as friending template functions is not really stable.
Definition: block_helper_ref_value.hpp:37
static CVB_FORCE_INLINE void set(RValue< Ty, K > &refVal, Ty *val) noexcept
Definition: block_helper_ref_value.hpp:43