Line | Count | Source |
1 | | #ifndef ZSERIO_ARRAY_H_INC |
2 | | #define ZSERIO_ARRAY_H_INC |
3 | | |
4 | | #include <cstdlib> |
5 | | #include <type_traits> |
6 | | |
7 | | #include "zserio/AllocatorPropagatingCopy.h" |
8 | | #include "zserio/ArrayTraits.h" |
9 | | #include "zserio/BitSizeOfCalculator.h" |
10 | | #include "zserio/BitStreamReader.h" |
11 | | #include "zserio/BitStreamWriter.h" |
12 | | #include "zserio/DeltaContext.h" |
13 | | #include "zserio/SizeConvertUtil.h" |
14 | | #include "zserio/Traits.h" |
15 | | #include "zserio/Types.h" |
16 | | #include "zserio/UniquePtr.h" |
17 | | |
18 | | namespace zserio |
19 | | { |
20 | | |
21 | | namespace detail |
22 | | { |
23 | | |
24 | | // array expressions for arrays which do not need expressions |
25 | | struct DummyArrayExpressions |
26 | | {}; |
27 | | |
28 | | // array owner for arrays which do not need the owner |
29 | | struct DummyArrayOwner |
30 | | {}; |
31 | | |
32 | | // helper trait to choose the owner type for an array from combination of ARRAY_TRAITS and ARRAY_EXPRESSIONS |
33 | | template <typename ARRAY_TRAITS, typename ARRAY_EXPRESSIONS, typename = void> |
34 | | struct array_owner_type |
35 | | { |
36 | | using type = DummyArrayOwner; |
37 | | }; |
38 | | |
39 | | template <typename ARRAY_TRAITS, typename ARRAY_EXPRESSIONS> |
40 | | struct array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS, |
41 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value>::type> |
42 | | { |
43 | | using type = typename ARRAY_TRAITS::OwnerType; |
44 | | }; |
45 | | |
46 | | template <typename ARRAY_TRAITS, typename ARRAY_EXPRESSIONS> |
47 | | struct array_owner_type<ARRAY_TRAITS, ARRAY_EXPRESSIONS, |
48 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value && |
49 | | has_owner_type<ARRAY_EXPRESSIONS>::value>::type> |
50 | | { |
51 | | using type = typename ARRAY_EXPRESSIONS::OwnerType; |
52 | | }; |
53 | | |
54 | | // helper trait to choose packing context type for an array from an element type T |
55 | | template <typename T, typename = void> |
56 | | struct packing_context_type |
57 | | { |
58 | | using type = DeltaContext; |
59 | | }; |
60 | | |
61 | | template <typename T> |
62 | | struct packing_context_type<T, typename std::enable_if<has_zserio_packing_context<T>::value>::type> |
63 | | { |
64 | | using type = typename T::ZserioPackingContext; |
65 | | }; |
66 | | |
67 | | // calls the initializeOffset static method on ARRAY_EXPRESSIONS if available |
68 | | template <typename ARRAY_EXPRESSIONS, typename OWNER_TYPE, |
69 | | typename std::enable_if<has_initialize_offset<ARRAY_EXPRESSIONS>::value, int>::type = 0> |
70 | | void initializeOffset(OWNER_TYPE& owner, size_t index, size_t bitPosition) |
71 | 192 | { |
72 | 192 | ARRAY_EXPRESSIONS::initializeOffset(owner, index, bitPosition); |
73 | 192 | } |
74 | | |
75 | | template <typename ARRAY_EXPRESSIONS, typename OWNER_TYPE, |
76 | | typename std::enable_if<!has_initialize_offset<ARRAY_EXPRESSIONS>::value, int>::type = 0> |
77 | | void initializeOffset(OWNER_TYPE&, size_t, size_t) |
78 | 4.51k | {} |
79 | | |
80 | | // calls the checkOffset static method on ARRAY_EXPRESSIONS if available |
81 | | template <typename ARRAY_EXPRESSIONS, typename OWNER_TYPE, |
82 | | typename std::enable_if<has_check_offset<ARRAY_EXPRESSIONS>::value, int>::type = 0> |
83 | | void checkOffset(const OWNER_TYPE& owner, size_t index, size_t bitPosition) |
84 | 575 | { |
85 | 575 | ARRAY_EXPRESSIONS::checkOffset(owner, index, bitPosition); |
86 | 575 | } |
87 | | |
88 | | template <typename ARRAY_EXPRESSIONS, typename OWNER_TYPE, |
89 | | typename std::enable_if<!has_check_offset<ARRAY_EXPRESSIONS>::value, int>::type = 0> |
90 | | void checkOffset(const OWNER_TYPE&, size_t, size_t) |
91 | 15.2k | {} |
92 | | |
93 | | // call the initContext method properly on packed array traits |
94 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
95 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value && |
96 | | !std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
97 | | int>::type = 0> |
98 | | void packedArrayTraitsInitContext(const OWNER_TYPE& owner, PACKING_CONTEXT& context, |
99 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
100 | 576 | { |
101 | 576 | PACKED_ARRAY_TRAITS::initContext(owner, context, element); |
102 | 576 | } |
103 | | |
104 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
105 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value && |
106 | | std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
107 | | int>::type = 0> |
108 | | void packedArrayTraitsInitContext( |
109 | | const OWNER_TYPE& owner, PACKING_CONTEXT& context, typename PACKED_ARRAY_TRAITS::ElementType element) |
110 | 576 | { |
111 | 576 | PACKED_ARRAY_TRAITS::initContext(owner, context, element); |
112 | 576 | } |
113 | | |
114 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
115 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value && |
116 | | !std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
117 | | int>::type = 0> |
118 | | void packedArrayTraitsInitContext( |
119 | | const OWNER_TYPE&, PACKING_CONTEXT& context, const typename PACKED_ARRAY_TRAITS::ElementType& element) |
120 | 288 | { |
121 | 288 | PACKED_ARRAY_TRAITS::initContext(context, element); |
122 | 288 | } |
123 | | |
124 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
125 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value && |
126 | | std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
127 | | int>::type = 0> |
128 | | void packedArrayTraitsInitContext( |
129 | | const OWNER_TYPE&, PACKING_CONTEXT& context, typename PACKED_ARRAY_TRAITS::ElementType element) |
130 | 9.98k | { |
131 | 9.98k | PACKED_ARRAY_TRAITS::initContext(context, element); |
132 | 9.98k | } |
133 | | |
134 | | // calls the bitSizeOf method properly on array traits which have constant bit size |
135 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
136 | | typename std::enable_if<ARRAY_TRAITS::IS_BITSIZEOF_CONSTANT && has_owner_type<ARRAY_TRAITS>::value, |
137 | | int>::type = 0> |
138 | | size_t arrayTraitsConstBitSizeOf(const OWNER_TYPE& owner) |
139 | 462 | { |
140 | 462 | return ARRAY_TRAITS::bitSizeOf(owner); |
141 | 462 | } |
142 | | |
143 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
144 | | typename std::enable_if<ARRAY_TRAITS::IS_BITSIZEOF_CONSTANT && !has_owner_type<ARRAY_TRAITS>::value, |
145 | | int>::type = 0> |
146 | | size_t arrayTraitsConstBitSizeOf(const OWNER_TYPE&) |
147 | 2.37k | { |
148 | 2.37k | return ARRAY_TRAITS::bitSizeOf(); |
149 | 2.37k | } |
150 | | |
151 | | // calls the bitSizeOf method properly on array traits which haven't constant bit size |
152 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
153 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value, int>::type = 0> |
154 | | size_t arrayTraitsBitSizeOf( |
155 | | const OWNER_TYPE& owner, size_t bitPosition, const typename ARRAY_TRAITS::ElementType& element) |
156 | 192 | { |
157 | 192 | return ARRAY_TRAITS::bitSizeOf(owner, bitPosition, element); |
158 | 192 | } |
159 | | |
160 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
161 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value, int>::type = 0> |
162 | | size_t arrayTraitsBitSizeOf( |
163 | | const OWNER_TYPE&, size_t bitPosition, const typename ARRAY_TRAITS::ElementType& element) |
164 | 2.56k | { |
165 | 2.56k | return ARRAY_TRAITS::bitSizeOf(bitPosition, element); |
166 | 2.56k | } |
167 | | |
168 | | // calls the bitSizeOf method properly on packed array traits which haven't constant bit size |
169 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
170 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0> |
171 | | size_t packedArrayTraitsBitSizeOf(const OWNER_TYPE& owner, PACKING_CONTEXT& context, size_t bitPosition, |
172 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
173 | 384 | { |
174 | 384 | return PACKED_ARRAY_TRAITS::bitSizeOf(owner, context, bitPosition, element); |
175 | 384 | } |
176 | | |
177 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
178 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0> |
179 | | size_t packedArrayTraitsBitSizeOf(const OWNER_TYPE&, PACKING_CONTEXT& context, size_t bitPosition, |
180 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
181 | 3.42k | { |
182 | 3.42k | return PACKED_ARRAY_TRAITS::bitSizeOf(context, bitPosition, element); |
183 | 3.42k | } |
184 | | |
185 | | // calls the initializeOffsets method properly on array traits |
186 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
187 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value, int>::type = 0> |
188 | | size_t arrayTraitsInitializeOffsets( |
189 | | OWNER_TYPE& owner, size_t bitPosition, typename ARRAY_TRAITS::ElementType& element) |
190 | 816 | { |
191 | 816 | return ARRAY_TRAITS::initializeOffsets(owner, bitPosition, element); |
192 | 816 | } |
193 | | |
194 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
195 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value && |
196 | | !std::is_scalar<typename ARRAY_TRAITS::ElementType>::value, |
197 | | int>::type = 0> |
198 | | size_t arrayTraitsInitializeOffsets( |
199 | | OWNER_TYPE&, size_t bitPosition, const typename ARRAY_TRAITS::ElementType& element) |
200 | 352 | { |
201 | 352 | return ARRAY_TRAITS::initializeOffsets(bitPosition, element); |
202 | 352 | } |
203 | | |
204 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
205 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value && |
206 | | std::is_scalar<typename ARRAY_TRAITS::ElementType>::value, |
207 | | int>::type = 0> |
208 | | size_t arrayTraitsInitializeOffsets(OWNER_TYPE&, size_t bitPosition, typename ARRAY_TRAITS::ElementType element) |
209 | 4.74k | { |
210 | 4.74k | return ARRAY_TRAITS::initializeOffsets(bitPosition, element); |
211 | 4.74k | } |
212 | | |
213 | | // calls the initializeOffsets method properly on packed array traits |
214 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
215 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0> |
216 | | size_t packedArrayTraitsInitializeOffsets(OWNER_TYPE& owner, PACKING_CONTEXT& context, size_t bitPosition, |
217 | | typename PACKED_ARRAY_TRAITS::ElementType& element) |
218 | 384 | { |
219 | 384 | return PACKED_ARRAY_TRAITS::initializeOffsets(owner, context, bitPosition, element); |
220 | 384 | } |
221 | | |
222 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
223 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value && |
224 | | !std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
225 | | int>::type = 0> |
226 | | size_t packedArrayTraitsInitializeOffsets(OWNER_TYPE&, PACKING_CONTEXT& context, size_t bitPosition, |
227 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
228 | 96 | { |
229 | 96 | return PACKED_ARRAY_TRAITS::initializeOffsets(context, bitPosition, element); |
230 | 96 | } |
231 | | |
232 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
233 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value && |
234 | | std::is_scalar<typename PACKED_ARRAY_TRAITS::ElementType>::value, |
235 | | int>::type = 0> |
236 | | size_t packedArrayTraitsInitializeOffsets(OWNER_TYPE&, PACKING_CONTEXT& context, size_t bitPosition, |
237 | | typename PACKED_ARRAY_TRAITS::ElementType element) |
238 | 3.32k | { |
239 | 3.32k | return PACKED_ARRAY_TRAITS::initializeOffsets(context, bitPosition, element); |
240 | 3.32k | } |
241 | | |
242 | | // calls the read method properly on array traits |
243 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, |
244 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value && !has_allocator<ARRAY_TRAITS>::value, |
245 | | int>::type = 0> |
246 | | void arrayTraitsRead(const OWNER_TYPE& owner, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index) |
247 | 624 | { |
248 | 624 | rawArray.push_back(ARRAY_TRAITS::read(owner, in, index)); |
249 | 624 | } |
250 | | |
251 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, |
252 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value && has_allocator<ARRAY_TRAITS>::value, |
253 | | int>::type = 0> |
254 | | void arrayTraitsRead(OWNER_TYPE& owner, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index) |
255 | 448 | { |
256 | 448 | ARRAY_TRAITS::read(owner, rawArray, in, index); |
257 | 448 | } |
258 | | |
259 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, |
260 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value && !has_allocator<ARRAY_TRAITS>::value, |
261 | | int>::type = 0> |
262 | | void arrayTraitsRead(const OWNER_TYPE&, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index) |
263 | 13.0k | { |
264 | 13.0k | rawArray.push_back(ARRAY_TRAITS::read(in, index)); |
265 | 13.0k | } |
266 | | |
267 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, |
268 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value && has_allocator<ARRAY_TRAITS>::value, |
269 | | int>::type = 0> |
270 | | void arrayTraitsRead(const OWNER_TYPE&, RAW_ARRAY& rawArray, BitStreamReader& in, size_t index) |
271 | 1.43k | { |
272 | 1.43k | ARRAY_TRAITS::read(rawArray, in, index); |
273 | 1.43k | } |
274 | | |
275 | | // calls the read method properly on packed array traits |
276 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, typename PACKING_CONTEXT, |
277 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value && |
278 | | has_allocator<PACKED_ARRAY_TRAITS>::value, |
279 | | int>::type = 0> |
280 | | void packedArrayTraitsRead( |
281 | | OWNER_TYPE& owner, RAW_ARRAY& rawArray, PACKING_CONTEXT& context, BitStreamReader& in, size_t index) |
282 | 318 | { |
283 | 318 | PACKED_ARRAY_TRAITS::read(owner, rawArray, context, in, index); |
284 | 318 | } |
285 | | |
286 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, typename PACKING_CONTEXT, |
287 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value && |
288 | | !has_allocator<PACKED_ARRAY_TRAITS>::value, |
289 | | int>::type = 0> |
290 | | void packedArrayTraitsRead(const OWNER_TYPE& owner, RAW_ARRAY& rawArray, PACKING_CONTEXT& context, |
291 | | BitStreamReader& in, size_t index) |
292 | 412 | { |
293 | 412 | rawArray.push_back(PACKED_ARRAY_TRAITS::read(owner, context, in, index)); |
294 | 412 | } |
295 | | |
296 | | // note: types which doesn't have owner and have allocator are never packed (e.g. string, bytes ...) |
297 | | // and thus such specialization is not needed |
298 | | |
299 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename RAW_ARRAY, typename PACKING_CONTEXT, |
300 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value && |
301 | | !has_allocator<PACKED_ARRAY_TRAITS>::value, |
302 | | int>::type = 0> |
303 | | void packedArrayTraitsRead( |
304 | | const OWNER_TYPE&, RAW_ARRAY& rawArray, PACKING_CONTEXT& context, BitStreamReader& in, size_t index) |
305 | 6.22k | { |
306 | 6.22k | rawArray.push_back(PACKED_ARRAY_TRAITS::read(context, in, index)); |
307 | 6.22k | } |
308 | | |
309 | | // call the write method properly on array traits |
310 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
311 | | typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value, int>::type = 0> |
312 | | void arrayTraitsWrite( |
313 | | const OWNER_TYPE& owner, BitStreamWriter& out, const typename ARRAY_TRAITS::ElementType& element) |
314 | 816 | { |
315 | 816 | ARRAY_TRAITS::write(owner, out, element); |
316 | 816 | } |
317 | | |
318 | | template <typename ARRAY_TRAITS, typename OWNER_TYPE, |
319 | | typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value, int>::type = 0> |
320 | | void arrayTraitsWrite( |
321 | | const OWNER_TYPE&, BitStreamWriter& out, const typename ARRAY_TRAITS::ElementType& element) |
322 | 5.09k | { |
323 | 5.09k | ARRAY_TRAITS::write(out, element); |
324 | 5.09k | } |
325 | | |
326 | | // call the write method properly on packed array traits |
327 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
328 | | typename std::enable_if<has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0> |
329 | | void packedArrayTraitsWrite(const OWNER_TYPE& owner, PACKING_CONTEXT& context, BitStreamWriter& out, |
330 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
331 | 384 | { |
332 | 384 | PACKED_ARRAY_TRAITS::write(owner, context, out, element); |
333 | 384 | } |
334 | | |
335 | | template <typename PACKED_ARRAY_TRAITS, typename OWNER_TYPE, typename PACKING_CONTEXT, |
336 | | typename std::enable_if<!has_owner_type<PACKED_ARRAY_TRAITS>::value, int>::type = 0> |
337 | | void packedArrayTraitsWrite(const OWNER_TYPE&, PACKING_CONTEXT& context, BitStreamWriter& out, |
338 | | const typename PACKED_ARRAY_TRAITS::ElementType& element) |
339 | 3.42k | { |
340 | 3.42k | PACKED_ARRAY_TRAITS::write(context, out, element); |
341 | 3.42k | } |
342 | | |
343 | | } // namespace detail |
344 | | |
345 | | /** |
346 | | * Array type enum which defined type of the underlying array. |
347 | | */ |
348 | | enum ArrayType |
349 | | { |
350 | | NORMAL, /**< Normal zserio array which has size defined by the Zserio schema. */ |
351 | | IMPLICIT, /**< Implicit zserio array which size is defined by number of remaining bits in the bit stream. */ |
352 | | ALIGNED, /**< Aligned zserio array which is normal zserio array with indexed offsets. */ |
353 | | AUTO, /**< Auto zserio array which has size stored in a hidden field before the array. */ |
354 | | ALIGNED_AUTO /**< Aligned auto zserio array which is auto zserio array with indexed offsets. */ |
355 | | }; |
356 | | |
357 | | /** |
358 | | * Array wrapper for zserio arrays which are not explicitly packed but the element type is packable |
359 | | * and thus it can be packed if requested from a parent. |
360 | | */ |
361 | | /** |
362 | | * Array wrapper for zserio arrays which are never packed. |
363 | | */ |
364 | | template <typename RAW_ARRAY, typename ARRAY_TRAITS, ArrayType ARRAY_TYPE, |
365 | | typename ARRAY_EXPRESSIONS = detail::DummyArrayExpressions> |
366 | | class Array |
367 | | { |
368 | | public: |
369 | | /** Typedef for raw array type. */ |
370 | | using RawArray = RAW_ARRAY; |
371 | | |
372 | | /** Typedef for array traits. */ |
373 | | using ArrayTraits = ARRAY_TRAITS; |
374 | | |
375 | | /** Typedef for array expressions. */ |
376 | | using ArrayExpressions = ARRAY_EXPRESSIONS; |
377 | | |
378 | | /** |
379 | | * Typedef for the array's owner type. |
380 | | * |
381 | | * Owner type is needed for proper expressions evaluation. If neither traits nor array need the owner |
382 | | * for expressions evaluation, detail::DummyArrayOwner is used and such array do not need the owner at all. |
383 | | */ |
384 | | using OwnerType = typename detail::array_owner_type<ArrayTraits, ArrayExpressions>::type; |
385 | | |
386 | | /** Typedef for allocator type. */ |
387 | | using allocator_type = typename RawArray::allocator_type; |
388 | | |
389 | | /** |
390 | | * Empty constructor. |
391 | | * |
392 | | * \param allocator Allocator to use for the raw array. |
393 | | */ |
394 | | explicit Array(const allocator_type& allocator = allocator_type()) : |
395 | | m_rawArray(allocator) |
396 | 10.6k | {} |
397 | | |
398 | | /** |
399 | | * Constructor from l-value raw array. |
400 | | * |
401 | | * \param rawArray Raw array. |
402 | | */ |
403 | | explicit Array(const RawArray& rawArray) : |
404 | | m_rawArray(rawArray) |
405 | 3.31k | {} |
406 | | |
407 | | /** |
408 | | * Constructor from r-value raw array. |
409 | | * |
410 | | * \param rawArray Raw array. |
411 | | */ |
412 | | explicit Array(RawArray&& rawArray) : |
413 | | m_rawArray(std::move(rawArray)) |
414 | 44 | {} |
415 | | |
416 | | /** |
417 | | * Method generated by default. |
418 | | * |
419 | | * \{ |
420 | | */ |
421 | 24.0k | ~Array() = default; |
422 | 3.31k | Array(const Array& other) = default; |
423 | 3.31k | Array& operator=(const Array& other) = default; |
424 | 3.39k | Array(Array&& other) = default; |
425 | 3.39k | Array& operator=(Array&& other) = default; |
426 | | /** |
427 | | * \} |
428 | | */ |
429 | | |
430 | | /** |
431 | | * Copy constructor which prevents initialization of parameterized elements. |
432 | | * |
433 | | * Note that elements will be initialized later by a parent compound. |
434 | | * |
435 | | * \param other Instance to construct from. |
436 | | */ |
437 | | template <typename T = typename RAW_ARRAY::value_type, |
438 | | typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0> |
439 | | Array(NoInitT, const Array& other) : |
440 | | Array(std::allocator_traits<allocator_type>::select_on_container_copy_construction( |
441 | | other.m_rawArray.get_allocator())) |
442 | 64 | { |
443 | 64 | m_rawArray.reserve(other.m_rawArray.size()); |
444 | 64 | for (const auto& value : other.m_rawArray) |
445 | 192 | { |
446 | 192 | m_rawArray.emplace_back(NoInit, value); |
447 | 192 | } |
448 | 64 | } |
449 | | |
450 | | /** |
451 | | * Assignment which prevents initialization of parameterized elements. |
452 | | * |
453 | | * Note that elements will be initialized later by a parent compound. |
454 | | * |
455 | | * \param other Instance to move-assign from. |
456 | | */ |
457 | | template <typename T = typename RAW_ARRAY::value_type, |
458 | | typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0> |
459 | | Array& assign(NoInitT, const Array& other) |
460 | 64 | { |
461 | 64 | const RawArray rawArray(other.m_rawArray.get_allocator()); |
462 | 64 | m_rawArray = rawArray; // copy assign to get correct allocator propagation behaviour |
463 | 64 | m_rawArray.reserve(other.m_rawArray.size()); |
464 | 64 | for (const auto& value : other.m_rawArray) |
465 | 192 | { |
466 | 192 | m_rawArray.emplace_back(NoInit, value); |
467 | 192 | } |
468 | | |
469 | 64 | return *this; |
470 | 64 | } |
471 | | |
472 | | /** |
473 | | * Move constructors which prevents initialization of parameterized elements. |
474 | | * |
475 | | * Provided for convenience since vector move constructor doesn't call move constructors of its elements. |
476 | | * |
477 | | * \param other Instance to move-construct from. |
478 | | */ |
479 | | template <typename T = typename RAW_ARRAY::value_type, |
480 | | typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0> |
481 | | Array(NoInitT, Array&& other) : |
482 | | Array(std::move(other)) |
483 | 64 | {} |
484 | | |
485 | | /** |
486 | | * Assignment which prevents initialization of parameterized elements. |
487 | | * |
488 | | * Provided for convenience since vector move assignment operator doesn't call move assignment operators |
489 | | * of its elements. |
490 | | * |
491 | | * \param other Instance to move-assign from. |
492 | | */ |
493 | | template <typename T = typename RAW_ARRAY::value_type, |
494 | | typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0> |
495 | | Array& assign(NoInitT, Array&& other) |
496 | 64 | { |
497 | 64 | return operator=(std::move(other)); |
498 | 64 | } |
499 | | |
500 | | /** |
501 | | * Copy constructor which forces allocator propagating while copying the raw array. |
502 | | * |
503 | | * \param other Source array to copy. |
504 | | * \param allocator Allocator to propagate during copying. |
505 | | */ |
506 | | Array(PropagateAllocatorT, const Array& other, const allocator_type& allocator) : |
507 | | m_rawArray(allocatorPropagatingCopy(other.m_rawArray, allocator)) |
508 | 3.31k | {} |
509 | | |
510 | | /** |
511 | | * Copy constructor which prevents initialization and forces allocator propagating while copying |
512 | | * the raw array. |
513 | | * |
514 | | * \param other Source array to copy. |
515 | | * \param allocator Allocator to propagate during copying. |
516 | | */ |
517 | | template <typename T = typename RAW_ARRAY::value_type, |
518 | | typename std::enable_if<std::is_constructible<T, NoInitT, T>::value, int>::type = 0> |
519 | | Array(PropagateAllocatorT, NoInitT, const Array& other, const allocator_type& allocator) : |
520 | | m_rawArray(allocatorPropagatingCopy(NoInit, other.m_rawArray, allocator)) |
521 | 64 | {} |
522 | | |
523 | | /** |
524 | | * Operator equality. |
525 | | * |
526 | | * \param other Array to compare. |
527 | | * |
528 | | * \return True when the underlying raw arrays have same contents, false otherwise. |
529 | | */ |
530 | | bool operator==(const Array& other) const |
531 | 19.8k | { |
532 | 19.8k | return m_rawArray == other.m_rawArray; |
533 | 19.8k | } |
534 | | |
535 | | /** |
536 | | * Operator less than. |
537 | | * |
538 | | * \param other Array to compare. |
539 | | * |
540 | | * \return True when the underlying raw array is less than the other underlying raw array. |
541 | | */ |
542 | | bool operator<(const Array& other) const |
543 | 6.62k | { |
544 | 6.62k | return m_rawArray < other.m_rawArray; |
545 | 6.62k | } |
546 | | |
547 | | /** |
548 | | * Hash code. |
549 | | * |
550 | | * \return Hash code calculated on the underlying raw array. |
551 | | */ |
552 | | uint32_t hashCode() const |
553 | 1 | { |
554 | 1 | return calcHashCode(HASH_SEED, m_rawArray); |
555 | 1 | } |
556 | | |
557 | | /** |
558 | | * Gets raw array. |
559 | | * |
560 | | * \return Constant reference to the raw array. |
561 | | */ |
562 | | const RawArray& getRawArray() const |
563 | 20.3k | { |
564 | 20.3k | return m_rawArray; |
565 | 20.3k | } |
566 | | |
567 | | /** |
568 | | * Gets raw array. |
569 | | * |
570 | | * \return Reference to the raw array. |
571 | | */ |
572 | | RawArray& getRawArray() |
573 | 13.9k | { |
574 | 13.9k | return m_rawArray; |
575 | 13.9k | } |
576 | | |
577 | | /** |
578 | | * Initializes array elements. |
579 | | * |
580 | | * \param owner Array owner. |
581 | | */ |
582 | | template <typename ARRAY_EXPRESSIONS_ = ArrayExpressions, |
583 | | typename std::enable_if<has_initialize_element<ARRAY_EXPRESSIONS_>::value, int>::type = 0> |
584 | | void initializeElements(OwnerType& owner) |
585 | 70 | { |
586 | 70 | size_t index = 0; |
587 | 70 | for (auto&& element : m_rawArray) |
588 | 195 | { |
589 | 195 | ArrayExpressions::initializeElement(owner, element, index); |
590 | 195 | index++; |
591 | 195 | } |
592 | 70 | } |
593 | | |
594 | | /** |
595 | | * Calculates bit size of this array. |
596 | | * |
597 | | * Available for arrays which do not need the owner. |
598 | | * |
599 | | * \param bitPosition Current bit position. |
600 | | * |
601 | | * \return Bit size of the array. |
602 | | */ |
603 | | template <typename OWNER_TYPE_ = OwnerType, |
604 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
605 | | size_t bitSizeOf(size_t bitPosition) const |
606 | 2.11k | { |
607 | 2.11k | return bitSizeOfImpl(detail::DummyArrayOwner(), bitPosition); |
608 | 2.11k | } |
609 | | |
610 | | /** |
611 | | * Calculates bit size of this array. |
612 | | * |
613 | | * Available for arrays which need the owner. |
614 | | * |
615 | | * \param owner Array owner. |
616 | | * \param bitPosition Current bit position. |
617 | | * |
618 | | * \return Bit size of the array. |
619 | | */ |
620 | | template <typename OWNER_TYPE_ = OwnerType, |
621 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
622 | | size_t bitSizeOf(const OwnerType& owner, size_t bitPosition) const |
623 | 304 | { |
624 | 304 | return bitSizeOfImpl(owner, bitPosition); |
625 | 304 | } |
626 | | |
627 | | /** |
628 | | * Initializes indexed offsets. |
629 | | * |
630 | | * Available for arrays which do not need the owner. |
631 | | * |
632 | | * \param bitPosition Current bit position. |
633 | | * |
634 | | * \return Updated bit position which points to the first bit after the array. |
635 | | */ |
636 | | template <typename OWNER_TYPE_ = OwnerType, |
637 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
638 | | size_t initializeOffsets(size_t bitPosition) |
639 | 2.11k | { |
640 | 2.11k | detail::DummyArrayOwner owner; |
641 | 2.11k | return initializeOffsetsImpl(owner, bitPosition); |
642 | 2.11k | } |
643 | | |
644 | | /** |
645 | | * Initializes indexed offsets. |
646 | | * |
647 | | * Available for arrays which need the owner. |
648 | | * |
649 | | * \param owner Array owner. |
650 | | * \param bitPosition Current bit position. |
651 | | * |
652 | | * \return Updated bit position which points to the first bit after the array. |
653 | | */ |
654 | | template <typename OWNER_TYPE_ = OwnerType, |
655 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
656 | | size_t initializeOffsets(OwnerType& owner, size_t bitPosition) |
657 | 304 | { |
658 | 304 | return initializeOffsetsImpl(owner, bitPosition); |
659 | 304 | } |
660 | | |
661 | | /** |
662 | | * Reads the array from the bit stream. |
663 | | * |
664 | | * Available for arrays which do not need the owner. |
665 | | * |
666 | | * \param in Bit stream reader to use for reading. |
667 | | * \param arrayLength Array length. Not needed for auto / implicit arrays. |
668 | | */ |
669 | | template <typename OWNER_TYPE_ = OwnerType, |
670 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
671 | | void read(BitStreamReader& in, size_t arrayLength = 0) |
672 | 2.36k | { |
673 | 2.36k | detail::DummyArrayOwner owner; |
674 | 2.36k | readImpl(owner, in, arrayLength); |
675 | 2.36k | } |
676 | | |
677 | | /** |
678 | | * Reads the array from the bit stream. |
679 | | * |
680 | | * Available for arrays which need the owner. |
681 | | * |
682 | | * \param owner Array owner. |
683 | | * \param in Bit stream reader to use for reading. |
684 | | * \param arrayLength Array length. Not needed for auto / implicit arrays. |
685 | | */ |
686 | | template <typename OWNER_TYPE_ = OwnerType, |
687 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
688 | | void read(OwnerType& owner, BitStreamReader& in, size_t arrayLength = 0) |
689 | 340 | { |
690 | 340 | readImpl(owner, in, arrayLength); |
691 | 340 | } |
692 | | |
693 | | /** |
694 | | * Writes the array to the bit stream. |
695 | | * |
696 | | * Available for arrays which do not need the owner. |
697 | | * |
698 | | * \param out Bit stream write to use for writing. |
699 | | */ |
700 | | template <typename OWNER_TYPE_ = OwnerType, |
701 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
702 | | void write(BitStreamWriter& out) const |
703 | 2.11k | { |
704 | 2.11k | writeImpl(detail::DummyArrayOwner(), out); |
705 | 2.11k | } |
706 | | |
707 | | /** |
708 | | * Writes the array to the bit stream. |
709 | | * |
710 | | * Available for arrays which need the owner. |
711 | | * |
712 | | * \param owner Array owner. |
713 | | * \param out Bit stream write to use for writing. |
714 | | */ |
715 | | template <typename OWNER_TYPE_ = OwnerType, |
716 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
717 | | void write(const OwnerType& owner, BitStreamWriter& out) const |
718 | 304 | { |
719 | 304 | writeImpl(owner, out); |
720 | 304 | } |
721 | | |
722 | | /** |
723 | | * Returns length of the packed array stored in the bit stream in bits. |
724 | | * |
725 | | * Available for arrays which do not need the owner. |
726 | | * |
727 | | * \param bitPosition Current bit stream position. |
728 | | * |
729 | | * \return Length of the array stored in the bit stream in bits. |
730 | | */ |
731 | | template <typename OWNER_TYPE_ = OwnerType, |
732 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
733 | | size_t bitSizeOfPacked(size_t bitPosition) const |
734 | 800 | { |
735 | 800 | return bitSizeOfPackedImpl(detail::DummyArrayOwner(), bitPosition); |
736 | 800 | } |
737 | | |
738 | | /** |
739 | | * Returns length of the packed array stored in the bit stream in bits. |
740 | | * |
741 | | * Available for arrays which need the owner. |
742 | | * |
743 | | * \param owner Array owner. |
744 | | * \param bitPosition Current bit stream position. |
745 | | * |
746 | | * \return Length of the array stored in the bit stream in bits. |
747 | | */ |
748 | | template <typename OWNER_TYPE_ = OwnerType, |
749 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
750 | | size_t bitSizeOfPacked(const OwnerType& ownerType, size_t bitPosition) const |
751 | 96 | { |
752 | 96 | return bitSizeOfPackedImpl(ownerType, bitPosition); |
753 | 96 | } |
754 | | |
755 | | /** |
756 | | * Initializes indexed offsets for the packed array. |
757 | | * |
758 | | * Available for arrays which do not need the owner. |
759 | | * |
760 | | * \param bitPosition Current bit stream position. |
761 | | * |
762 | | * \return Updated bit stream position which points to the first bit after the array. |
763 | | */ |
764 | | template <typename OWNER_TYPE_ = OwnerType, |
765 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
766 | | size_t initializeOffsetsPacked(size_t bitPosition) |
767 | 800 | { |
768 | 800 | detail::DummyArrayOwner owner; |
769 | 800 | return initializeOffsetsPackedImpl(owner, bitPosition); |
770 | 800 | } |
771 | | |
772 | | /** |
773 | | * Initializes indexed offsets for the packed array. |
774 | | * |
775 | | * Available for arrays which need the owner. |
776 | | * |
777 | | * \param owner Array owner. |
778 | | * \param bitPosition Current bit stream position. |
779 | | * |
780 | | * \return Updated bit stream position which points to the first bit after the array. |
781 | | */ |
782 | | template <typename OWNER_TYPE_ = OwnerType, |
783 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
784 | | size_t initializeOffsetsPacked(OwnerType& owner, size_t bitPosition) |
785 | 96 | { |
786 | 96 | return initializeOffsetsPackedImpl(owner, bitPosition); |
787 | 96 | } |
788 | | |
789 | | /** |
790 | | * Reads packed array from the bit stream. |
791 | | * |
792 | | * Available for arrays which do not need the owner. |
793 | | * |
794 | | * \param in Bit stream from which to read. |
795 | | * \param arrayLength Number of elements to read or 0 in case of auto arrays. |
796 | | */ |
797 | | template <typename OWNER_TYPE_ = OwnerType, |
798 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
799 | | void readPacked(BitStreamReader& in, size_t arrayLength = 0) |
800 | 900 | { |
801 | 900 | detail::DummyArrayOwner owner; |
802 | 900 | readPackedImpl(owner, in, arrayLength); |
803 | 900 | } |
804 | | |
805 | | /** |
806 | | * Reads packed array from the bit stream. |
807 | | * |
808 | | * Available for arrays which need the owner. |
809 | | * |
810 | | * \param owner Array owner. |
811 | | * \param in Bit stream from which to read. |
812 | | * \param arrayLength Number of elements to read or 0 in case of auto arrays. |
813 | | */ |
814 | | template <typename OWNER_TYPE_ = OwnerType, |
815 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
816 | | void readPacked(OwnerType& owner, BitStreamReader& in, size_t arrayLength = 0) |
817 | 108 | { |
818 | 108 | readPackedImpl(owner, in, arrayLength); |
819 | 108 | } |
820 | | |
821 | | /** |
822 | | * Writes packed array to the bit stream. |
823 | | * |
824 | | * Available for arrays which do not need the owner. |
825 | | * |
826 | | * \param out Bit stream where to write. |
827 | | */ |
828 | | template <typename OWNER_TYPE_ = OwnerType, |
829 | | typename std::enable_if<std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
830 | | void writePacked(BitStreamWriter& out) const |
831 | 800 | { |
832 | 800 | writePackedImpl(detail::DummyArrayOwner(), out); |
833 | 800 | } |
834 | | |
835 | | /** |
836 | | * Writes packed array to the bit stream. |
837 | | * |
838 | | * Available for arrays which need the owner. |
839 | | * |
840 | | * \param owner Array owner. |
841 | | * \param out Bit stream where to write. |
842 | | */ |
843 | | template <typename OWNER_TYPE_ = OwnerType, |
844 | | typename std::enable_if<!std::is_same<OWNER_TYPE_, detail::DummyArrayOwner>::value, int>::type = 0> |
845 | | void writePacked(const OwnerType& owner, BitStreamWriter& out) const |
846 | 96 | { |
847 | 96 | writePackedImpl(owner, out); |
848 | 96 | } |
849 | | |
850 | | private: |
851 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
852 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::AUTO && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
853 | | int>::type = 0> |
854 | | static void addBitSizeOfArrayLength(size_t&, size_t) |
855 | 3.45k | {} |
856 | | |
857 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
858 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::AUTO || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
859 | | int>::type = 0> |
860 | | static void addBitSizeOfArrayLength(size_t& bitPosition, size_t arrayLength) |
861 | 3.16k | { |
862 | 3.16k | bitPosition += bitSizeOfVarSize(convertSizeToUInt32(arrayLength)); |
863 | 3.16k | } |
864 | | |
865 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
866 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::ALIGNED && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
867 | | int>::type = 0> |
868 | | static void alignBitPosition(size_t&) |
869 | 3.28k | {} |
870 | | |
871 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
872 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::ALIGNED || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
873 | | int>::type = 0> |
874 | | static void alignBitPosition(size_t& bitPosition) |
875 | 3.28k | { |
876 | 3.28k | bitPosition = alignTo(8, bitPosition); |
877 | 3.28k | } |
878 | | |
879 | | template <typename IO, typename OWNER_TYPE, ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
880 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::ALIGNED && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
881 | | int>::type = 0> |
882 | | static void alignAndCheckOffset(IO&, OWNER_TYPE&, size_t) |
883 | 16.4k | {} |
884 | | |
885 | | template <typename IO, typename OWNER_TYPE, ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
886 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::ALIGNED || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
887 | | int>::type = 0> |
888 | | static void alignAndCheckOffset(IO& io, OWNER_TYPE& owner, size_t index) |
889 | 15.8k | { |
890 | 15.8k | io.alignTo(8); |
891 | 15.8k | detail::checkOffset<ArrayExpressions>(owner, index, io.getBitPosition() / 8); |
892 | 15.8k | } |
893 | | |
894 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
895 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::ALIGNED && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
896 | | int>::type = 0> |
897 | | static void initializeOffset(OwnerType&, size_t, size_t&) |
898 | 5.01k | {} |
899 | | |
900 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
901 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::ALIGNED || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
902 | | int>::type = 0> |
903 | | static void initializeOffset(OwnerType& owner, size_t index, size_t& bitPosition) |
904 | 4.70k | { |
905 | 4.70k | bitPosition = alignTo(8, bitPosition); |
906 | 4.70k | detail::initializeOffset<ArrayExpressions>(owner, index, bitPosition / 8); |
907 | 4.70k | } |
908 | | |
909 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
910 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::AUTO && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO && |
911 | | ARRAY_TYPE_ != ArrayType::IMPLICIT, |
912 | | int>::type = 0> |
913 | | static size_t readArrayLength(OwnerType&, BitStreamReader&, size_t arrayLength) |
914 | 1.78k | { |
915 | 1.78k | return arrayLength; |
916 | 1.78k | } |
917 | | |
918 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
919 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::AUTO || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
920 | | int>::type = 0> |
921 | | static size_t readArrayLength(OwnerType&, BitStreamReader& in, size_t) |
922 | 1.78k | { |
923 | 1.78k | return in.readVarSize(); |
924 | 1.78k | } |
925 | | |
926 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
927 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::IMPLICIT, int>::type = 0> |
928 | | static size_t readArrayLength(OwnerType& owner, BitStreamReader& in, size_t) |
929 | 144 | { |
930 | 144 | static_assert(ARRAY_TYPE != ArrayType::IMPLICIT || ArrayTraits::IS_BITSIZEOF_CONSTANT, |
931 | 144 | "Implicit array elements must have constant bit size!"); |
932 | | |
933 | 144 | const size_t remainingBits = in.getBufferBitSize() - in.getBitPosition(); |
934 | 144 | return remainingBits / detail::arrayTraitsConstBitSizeOf<ArrayTraits>(owner); |
935 | 144 | } |
936 | | |
937 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
938 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::AUTO && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
939 | | int>::type = 0> |
940 | | static void writeArrayLength(BitStreamWriter&, size_t) |
941 | 1.72k | {} |
942 | | |
943 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
944 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::AUTO || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
945 | | int>::type = 0> |
946 | | static void writeArrayLength(BitStreamWriter& out, size_t arrayLength) |
947 | 1.58k | { |
948 | 1.58k | out.writeVarSize(convertSizeToUInt32(arrayLength)); |
949 | 1.58k | } |
950 | | |
951 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
952 | | typename std::enable_if<ARRAY_TYPE_ != ArrayType::ALIGNED && ARRAY_TYPE_ != ArrayType::ALIGNED_AUTO, |
953 | | int>::type = 0> |
954 | | static size_t constBitSizeOfElements(size_t, size_t arrayLength, size_t elementBitSize) |
955 | 584 | { |
956 | 584 | return arrayLength * elementBitSize; |
957 | 584 | } |
958 | | |
959 | | template <ArrayType ARRAY_TYPE_ = ARRAY_TYPE, |
960 | | typename std::enable_if<ARRAY_TYPE_ == ArrayType::ALIGNED || ARRAY_TYPE_ == ArrayType::ALIGNED_AUTO, |
961 | | int>::type = 0> |
962 | | static size_t constBitSizeOfElements(size_t bitPosition, size_t arrayLength, size_t elementBitSize) |
963 | 480 | { |
964 | 480 | size_t endBitPosition = alignTo(8, bitPosition); |
965 | 480 | endBitPosition += elementBitSize + (arrayLength - 1) * alignTo(8, elementBitSize); |
966 | | |
967 | 480 | return endBitPosition - bitPosition; |
968 | 480 | } |
969 | | |
970 | | template <typename ARRAY_TRAITS_ = ArrayTraits, |
971 | | typename std::enable_if<ARRAY_TRAITS_::IS_BITSIZEOF_CONSTANT, int>::type = 0> |
972 | | size_t bitSizeOfImpl(const OwnerType& owner, size_t bitPosition) const |
973 | 1.42k | { |
974 | 1.42k | size_t endBitPosition = bitPosition; |
975 | | |
976 | 1.42k | const size_t arrayLength = m_rawArray.size(); |
977 | 1.42k | addBitSizeOfArrayLength(endBitPosition, arrayLength); |
978 | | |
979 | 1.42k | if (arrayLength > 0) |
980 | 1.06k | { |
981 | 1.06k | const size_t elementBitSize = detail::arrayTraitsConstBitSizeOf<ArrayTraits>(owner); |
982 | 1.06k | endBitPosition += constBitSizeOfElements(endBitPosition, arrayLength, elementBitSize); |
983 | 1.06k | } |
984 | | |
985 | 1.42k | return endBitPosition - bitPosition; |
986 | 1.42k | } |
987 | | |
988 | | template <typename ARRAY_TRAITS_ = ArrayTraits, |
989 | | typename std::enable_if<!ARRAY_TRAITS_::IS_BITSIZEOF_CONSTANT, int>::type = 0> |
990 | | size_t bitSizeOfImpl(const OwnerType& owner, size_t bitPosition) const |
991 | 993 | { |
992 | 993 | size_t endBitPosition = bitPosition; |
993 | | |
994 | 993 | const size_t arrayLength = m_rawArray.size(); |
995 | 993 | addBitSizeOfArrayLength(endBitPosition, arrayLength); |
996 | | |
997 | 3.74k | for (size_t index = 0; index < arrayLength; ++index2.75k ) |
998 | 2.75k | { |
999 | 2.75k | alignBitPosition(endBitPosition); |
1000 | 2.75k | endBitPosition += |
1001 | 2.75k | detail::arrayTraitsBitSizeOf<ArrayTraits>(owner, endBitPosition, m_rawArray[index]); |
1002 | 2.75k | } |
1003 | | |
1004 | 993 | return endBitPosition - bitPosition; |
1005 | 993 | } |
1006 | | |
1007 | | size_t initializeOffsetsImpl(OwnerType& owner, size_t bitPosition) |
1008 | 2.41k | { |
1009 | 2.41k | size_t endBitPosition = bitPosition; |
1010 | | |
1011 | 2.41k | const size_t arrayLength = m_rawArray.size(); |
1012 | 2.41k | addBitSizeOfArrayLength(endBitPosition, arrayLength); |
1013 | | |
1014 | 8.33k | for (size_t index = 0; index < arrayLength; ++index5.91k ) |
1015 | 5.91k | { |
1016 | 5.91k | initializeOffset(owner, index, endBitPosition); |
1017 | 5.91k | endBitPosition = |
1018 | 5.91k | detail::arrayTraitsInitializeOffsets<ArrayTraits>(owner, endBitPosition, m_rawArray[index]); |
1019 | 5.91k | } |
1020 | | |
1021 | 2.41k | return endBitPosition; |
1022 | 2.41k | } |
1023 | | |
1024 | | template <typename ARRAY_TRAITS_ = ArrayTraits, |
1025 | | typename std::enable_if<ARRAY_TRAITS_::IS_BITSIZEOF_CONSTANT, int>::type = 0> |
1026 | | static void checkReadArraySize(OwnerType& owner, BitStreamReader& in, size_t readLength) |
1027 | 1.58k | { |
1028 | 1.58k | const size_t elementSize = detail::arrayTraitsConstBitSizeOf<ArrayTraits>(owner); |
1029 | 1.58k | if (in.getBitPosition() + readLength * static_cast<uint64_t>(elementSize) > in.getBufferBitSize()) |
1030 | 160 | { |
1031 | 160 | throw CppRuntimeException("Array: Array size exceeds available buffer!"); |
1032 | 160 | } |
1033 | 1.58k | } |
1034 | | |
1035 | | template <typename ARRAY_TRAITS_ = ArrayTraits, |
1036 | | typename std::enable_if<!ARRAY_TRAITS_::IS_BITSIZEOF_CONSTANT, int>::type = 0> |
1037 | | static void checkReadArraySize(OwnerType&, BitStreamReader&, size_t) |
1038 | 1.11k | {} |
1039 | | |
1040 | | void readImpl(OwnerType& owner, BitStreamReader& in, size_t arrayLength) |
1041 | 2.70k | { |
1042 | 2.70k | const size_t readLength = readArrayLength(owner, in, arrayLength); |
1043 | 2.70k | checkReadArraySize(owner, in, readLength); |
1044 | | |
1045 | 2.70k | size_t reserve = readLength; |
1046 | 2.70k | if (reserve * static_cast<uint64_t>(sizeof(typename RawArray::value_type)) > |
1047 | 2.70k | in.getMaxArrayPreallocation()) |
1048 | 125 | { |
1049 | 125 | reserve = in.getMaxArrayPreallocation() / sizeof(typename RawArray::value_type); |
1050 | 125 | } |
1051 | 2.70k | m_rawArray.clear(); |
1052 | 2.70k | m_rawArray.reserve(reserve); |
1053 | 18.2k | for (size_t index = 0; index < readLength; ++index15.5k ) |
1054 | 15.5k | { |
1055 | 15.5k | alignAndCheckOffset(in, owner, index); |
1056 | 15.5k | detail::arrayTraitsRead<ArrayTraits>(owner, m_rawArray, in, index); |
1057 | 15.5k | } |
1058 | 2.70k | } |
1059 | | |
1060 | | void writeImpl(const OwnerType& owner, BitStreamWriter& out) const |
1061 | 2.41k | { |
1062 | 2.41k | const size_t arrayLength = m_rawArray.size(); |
1063 | 2.41k | writeArrayLength(out, arrayLength); |
1064 | | |
1065 | 8.33k | for (size_t index = 0; index < arrayLength; ++index5.91k ) |
1066 | 5.91k | { |
1067 | 5.91k | alignAndCheckOffset(out, owner, index); |
1068 | 5.91k | detail::arrayTraitsWrite<ArrayTraits>(owner, out, m_rawArray[index]); |
1069 | 5.91k | } |
1070 | 2.41k | } |
1071 | | |
1072 | | using PackingContext = typename detail::packing_context_type<typename RawArray::value_type>::type; |
1073 | | |
1074 | | size_t bitSizeOfPackedImpl(const OwnerType& owner, size_t bitPosition) const |
1075 | 896 | { |
1076 | 896 | static_assert(ARRAY_TYPE != ArrayType::IMPLICIT, "Implicit array cannot be packed!"); |
1077 | | |
1078 | 896 | size_t endBitPosition = bitPosition; |
1079 | | |
1080 | 896 | const size_t arrayLength = m_rawArray.size(); |
1081 | 896 | addBitSizeOfArrayLength(endBitPosition, arrayLength); |
1082 | | |
1083 | 896 | if (arrayLength > 0) |
1084 | 832 | { |
1085 | 832 | PackingContext context; |
1086 | | |
1087 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1088 | 3.80k | { |
1089 | 3.80k | detail::packedArrayTraitsInitContext<PackedArrayTraits<ArrayTraits>>( |
1090 | 3.80k | owner, context, m_rawArray[index]); |
1091 | 3.80k | } |
1092 | | |
1093 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1094 | 3.80k | { |
1095 | 3.80k | alignBitPosition(endBitPosition); |
1096 | 3.80k | endBitPosition += detail::packedArrayTraitsBitSizeOf<PackedArrayTraits<ArrayTraits>>( |
1097 | 3.80k | owner, context, endBitPosition, m_rawArray[index]); |
1098 | 3.80k | } |
1099 | 832 | } |
1100 | | |
1101 | 896 | return endBitPosition - bitPosition; |
1102 | 896 | } |
1103 | | |
1104 | | size_t initializeOffsetsPackedImpl(OwnerType& owner, size_t bitPosition) |
1105 | 896 | { |
1106 | 896 | static_assert(ARRAY_TYPE != ArrayType::IMPLICIT, "Implicit array cannot be packed!"); |
1107 | | |
1108 | 896 | size_t endBitPosition = bitPosition; |
1109 | | |
1110 | 896 | const size_t arrayLength = m_rawArray.size(); |
1111 | 896 | addBitSizeOfArrayLength(endBitPosition, arrayLength); |
1112 | | |
1113 | 896 | if (arrayLength > 0) |
1114 | 832 | { |
1115 | 832 | PackingContext context; |
1116 | | |
1117 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1118 | 3.80k | { |
1119 | 3.80k | detail::packedArrayTraitsInitContext<PackedArrayTraits<ArrayTraits>>( |
1120 | 3.80k | owner, context, m_rawArray[index]); |
1121 | 3.80k | } |
1122 | | |
1123 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1124 | 3.80k | { |
1125 | 3.80k | initializeOffset(owner, index, endBitPosition); |
1126 | 3.80k | endBitPosition = detail::packedArrayTraitsInitializeOffsets<PackedArrayTraits<ArrayTraits>>( |
1127 | 3.80k | owner, context, endBitPosition, m_rawArray[index]); |
1128 | 3.80k | } |
1129 | 832 | } |
1130 | | |
1131 | 896 | return endBitPosition; |
1132 | 896 | } |
1133 | | |
1134 | | void readPackedImpl(OwnerType& owner, BitStreamReader& in, size_t arrayLength = 0) |
1135 | 1.00k | { |
1136 | 1.00k | static_assert(ARRAY_TYPE != ArrayType::IMPLICIT, "Implicit array cannot be packed!"); |
1137 | | |
1138 | 1.00k | const size_t readLength = readArrayLength(owner, in, arrayLength); |
1139 | 1.00k | size_t reserve = readLength; |
1140 | 1.00k | if (reserve * static_cast<uint64_t>(sizeof(typename RawArray::value_type)) > |
1141 | 1.00k | in.getMaxArrayPreallocation()) |
1142 | 112 | { |
1143 | 112 | reserve = in.getMaxArrayPreallocation() / sizeof(typename RawArray::value_type); |
1144 | 112 | } |
1145 | | |
1146 | 1.00k | m_rawArray.clear(); |
1147 | | |
1148 | 1.00k | if (readLength > 0) |
1149 | 944 | { |
1150 | 944 | m_rawArray.reserve(reserve); |
1151 | | |
1152 | 944 | PackingContext context; |
1153 | | |
1154 | 7.89k | for (size_t index = 0; index < readLength; ++index6.95k ) |
1155 | 6.95k | { |
1156 | 6.95k | alignAndCheckOffset(in, owner, index); |
1157 | 6.95k | detail::packedArrayTraitsRead<PackedArrayTraits<ArrayTraits>>( |
1158 | 6.95k | owner, m_rawArray, context, in, index); |
1159 | 6.95k | } |
1160 | 944 | } |
1161 | 1.00k | } |
1162 | | |
1163 | | void writePackedImpl(const OwnerType& owner, BitStreamWriter& out) const |
1164 | 896 | { |
1165 | 896 | static_assert(ARRAY_TYPE != ArrayType::IMPLICIT, "Implicit array cannot be packed!"); |
1166 | | |
1167 | 896 | const size_t arrayLength = m_rawArray.size(); |
1168 | 896 | writeArrayLength(out, arrayLength); |
1169 | | |
1170 | 896 | if (arrayLength > 0) |
1171 | 832 | { |
1172 | 832 | PackingContext context; |
1173 | | |
1174 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1175 | 3.80k | { |
1176 | 3.80k | detail::packedArrayTraitsInitContext<PackedArrayTraits<ArrayTraits>>( |
1177 | 3.80k | owner, context, m_rawArray[index]); |
1178 | 3.80k | } |
1179 | | |
1180 | 4.64k | for (size_t index = 0; index < arrayLength; ++index3.80k ) |
1181 | 3.80k | { |
1182 | 3.80k | alignAndCheckOffset(out, owner, index); |
1183 | 3.80k | detail::packedArrayTraitsWrite<PackedArrayTraits<ArrayTraits>>( |
1184 | 3.80k | owner, context, out, m_rawArray[index]); |
1185 | 3.80k | } |
1186 | 832 | } |
1187 | 896 | } |
1188 | | |
1189 | | RawArray m_rawArray; |
1190 | | }; |
1191 | | |
1192 | | /** |
1193 | | * Helper for creating an optional array within templated field constructor, where the raw array can be |
1194 | | * actually the NullOpt. |
1195 | | */ |
1196 | | template <typename ARRAY, typename RAW_ARRAY> |
1197 | | ARRAY createOptionalArray(RAW_ARRAY&& rawArray) |
1198 | 1 | { |
1199 | 1 | return ARRAY(std::forward<RAW_ARRAY>(rawArray)); |
1200 | 1 | } |
1201 | | |
1202 | | /** |
1203 | | * Overload for NullOpt. |
1204 | | */ |
1205 | | template <typename ARRAY> |
1206 | | NullOptType createOptionalArray(NullOptType) |
1207 | 14 | { |
1208 | 14 | return NullOpt; |
1209 | 14 | } |
1210 | | |
1211 | | } // namespace zserio |
1212 | | |
1213 | | #endif // ZSERIO_ARRAY_H_INC |