CVB++ 15.0
Loading...
Searching...
No Matches
block_helper_linear_value.hpp
1#pragma once
2
3#include "detail_block.hpp"
4
5namespace Cvb
6{
7 CVB_BEGIN_INLINE_NS
8
9 template <typename T, size_t K>
10 class LinearValue;
11
24 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
25 T get(const LinearValue<T, K> &linVal) noexcept;
26
39 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
40 void set(LinearValue<T, K> &linVal, const T &val) noexcept;
41
47#pragma pack(push, 1)
48 template <typename T, size_t K>
49 class LinearValue final
50 {
51 template <class TTY, class ACCESSTRAITT>
52 friend class Block;
53
54 std::array<T, K> values_;
55
56 public:
57 using ComponentType = T;
58
59 /*
60 * \brief Ctor.
61 * This shouldn't be public as it destroys the whole point of the LinearValue. Todo: revisit.
62 * could in theory be private with friending block (see RefValue), but GCC 7.3 (at least) doesn't like it.
63 */
64 LinearValue() = default;
65
66 LinearValue(const LinearValue &other) noexcept = default;
67 LinearValue &operator=(const LinearValue &other) noexcept = default;
68 LinearValue(LinearValue &&other) noexcept = default;
69 LinearValue &operator=(LinearValue &&other) noexcept = default;
70 ~LinearValue() = default;
71
72 private:
73 template <class Ty, size_t... I>
74 LinearValue<T, K> &Assign(const Ty &other, std::index_sequence<I...>) noexcept
75 {
76 using expander = int[]; // NOLINT(cppcoreguidelines-avoid-c-arrays) fold expression support for pre C++17
77 // standards.. for explanation see Block::FillPixelValueElements
78
79 expander{0, ((void)(values_[I] = get<I>(other)), 0)...};
80 return *this;
81 }
82
83 public:
93 template <class Ty>
94 LinearValue<T, K> &operator=(const Ty &other) noexcept
95 {
96 return Assign(other, std::make_index_sequence<K>()); // NOLINT(cppcoreguidelines-c-copy-assignment-signature)
97 }
98
107 template <std::size_t N>
108 LinearValue<T, K> &operator=(const T (&list)[N]) noexcept // NOLINT(cppcoreguidelines-avoid-c-arrays)
109 {
110 static_assert(N == K, "Cvb: The initializer list must contain K elements.");
111
112 for (int i = 0; i < N; ++i)
113 {
114 values_[i] = list[i];
115 }
116 return *this;
117 }
118
119 private:
120 template <class Ty, size_t... I>
121 Ty Cast(std::index_sequence<I...>) const noexcept
122 {
123 using expander = int[]; // NOLINT(cppcoreguidelines-avoid-c-arrays) fold expression support for pre C++17
124 // standards.. for explanation see Block::FillPixelValueElements
125
126 Ty value{};
127 expander{0, ((void)set<I>(value, values_[I]), 0)...};
128 return value;
129 }
130
131 public:
138 template <class Ty>
139 Ty Cast() const noexcept
140 {
141 return Cast<Ty>(std::make_index_sequence<K>());
142 }
143
149 T &X() noexcept
150 {
151 return values_[0];
152 }
153
159 const T &X() const noexcept
160 {
161 return values_[0];
162 }
163
169 template <size_t N = K, std::enable_if_t<less<1, N>::value, int> = 0>
170 T &Y() noexcept
171 {
172 return values_[1];
173 }
174
180 template <size_t N = K, std::enable_if_t<less<1, N>::value, int> = 0>
181 const T &Y() const noexcept
182 {
183 return values_[1];
184 }
185
191 template <size_t N = K, std::enable_if_t<less<2, N>::value, int> = 0>
192 T &Z() noexcept
193 {
194 return values_[2];
195 }
196
202 template <size_t N = K, std::enable_if_t<less<2, N>::value, int> = 0>
203 const T &Z() const noexcept
204 {
205 return values_[2];
206 }
207
213 template <size_t N = K, std::enable_if_t<less<3, N>::value, int> = 0>
214 T &W() noexcept
215 {
216 return values_[3];
217 }
218
224 template <size_t N = K, std::enable_if_t<less<3, N>::value, int> = 0>
225 const T &W() const noexcept
226 {
227 return values_[3];
228 }
229
236 const T &operator[](size_t i) const noexcept
237 {
238 assert(i < K);
239
240 return values_[i];
241 }
242
249 T &operator[](size_t i) noexcept
250 {
251 assert(i < K);
252
253 return values_[i];
254 }
255
261 static constexpr const size_t NumValues() noexcept
262 {
263 return K;
264 }
265 };
266#pragma pack(pop)
267
268 static_assert(sizeof(LinearValue<float, 1>) == 1 * sizeof(float), "LinearValue is not packed");
269 static_assert(sizeof(LinearValue<float, 2>) == 2 * sizeof(float), "LinearValue is not packed");
270 static_assert(sizeof(LinearValue<float, 3>) == 3 * sizeof(float), "LinearValue is not packed");
271 static_assert(sizeof(LinearValue<float, 4>) == 4 * sizeof(float), "LinearValue is not packed");
272 static_assert(sizeof(LinearValue<double, 5>) == 5 * sizeof(double), "LinearValue is not packed");
273
274 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int>>
275 T get(const LinearValue<T, K> &linVal) noexcept
276 {
277 return linVal[I];
278 }
279
280 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int>>
281 void set(LinearValue<T, K> &linVal, const T &val) noexcept
282 {
283 linVal[I] = val;
284 }
285
286 /*
287 * \relates LinearValue
288 * \sa Block, Visit
289 *
290 * \brief Set's the value of the \a I-th element in the \em LinearValue<T,K> \a linVal to the value pointed to by \a
291 * val.
292 *
293 * \pre I < K
294 *
295 * \tparam I The index of the element.
296 * \param linVal The \em LinearValue<T,K> to retrieve the \a I-th element from.
297 * \param val The pointer to the value to be written to the \a I-th element of \a linVal.
298 */
299 template <size_t I, class T, size_t K, std::enable_if_t<less<I, K>::value, int> = 0>
300 void internal_set(LinearValue<T, K> &linVal, const T *val) noexcept
301 {
302 assert(val != nullptr);
303 linVal[I] = *val;
304 }
305 CVB_END_INLINE_NS
306} // namespace Cvb
This class is memory compatible to K values of type T.
Definition block_helper_linear_value.hpp:50
LinearValue< 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_linear_value.hpp:94
T & Y() noexcept
Get reference to the second (y) value.
Definition block_helper_linear_value.hpp:170
Ty Cast() const noexcept
Cast to any type, for which a set<size_t>(Ty) function exists.
Definition block_helper_linear_value.hpp:139
T & X() noexcept
Get reference to the first (x) value.
Definition block_helper_linear_value.hpp:149
const T & X() const noexcept
Get const reference to the first (x) value.
Definition block_helper_linear_value.hpp:159
T & W() noexcept
Get reference to the fourth (w) value.
Definition block_helper_linear_value.hpp:214
const T & Z() const noexcept
Get const reference to the third (z) value.
Definition block_helper_linear_value.hpp:203
const T & W() const noexcept
Get const reference to the fourth (w) value.
Definition block_helper_linear_value.hpp:225
LinearValue< 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_linear_value.hpp:108
const T & operator[](size_t i) const noexcept
Element access.
Definition block_helper_linear_value.hpp:236
T & Z() noexcept
Get reference to the third (z) value.
Definition block_helper_linear_value.hpp:192
T get(const LinearValue< T, K > &linVal) noexcept
Get's the I-th element of the LinearValue<T,K>.
Definition block_helper_linear_value.hpp:275
void set(LinearValue< T, K > &linVal, const T &val) noexcept
Set's the value of the I-th element in the LinearValue<T,K> linVal.
Definition block_helper_linear_value.hpp:281
T & operator[](size_t i) noexcept
Mutable element access.
Definition block_helper_linear_value.hpp:249
static constexpr const size_t NumValues() noexcept
Get number of values stored in this LinearValue.
Definition block_helper_linear_value.hpp:261
const T & Y() const noexcept
Get const reference to the second (y) value.
Definition block_helper_linear_value.hpp:181
Root namespace for the Image Manager interface.
Definition version.hpp:11