src/zserio/OptionalHolder.h
Line | Count | Source |
1 | | #ifndef ZSERIO_OPTIONAL_HOLDER_H_INC |
2 | | #define ZSERIO_OPTIONAL_HOLDER_H_INC |
3 | | |
4 | | #include <type_traits> |
5 | | |
6 | | #include "zserio/CppRuntimeException.h" |
7 | | #include "zserio/NoInit.h" |
8 | | #include "zserio/Types.h" |
9 | | #include "zserio/UniquePtr.h" |
10 | | |
11 | | namespace zserio |
12 | | { |
13 | | |
14 | | /** |
15 | | * Helper type for specification of an unset optional holder. |
16 | | */ |
17 | | struct NullOptType |
18 | | { |
19 | | /** |
20 | | * Explicit constructor from int. |
21 | | * |
22 | | * \see https://en.cppreference.com/w/cpp/utility/optional/nullopt_t |
23 | | */ |
24 | | constexpr explicit NullOptType(int) |
25 | 2 | {} |
26 | | }; |
27 | | |
28 | | /** |
29 | | * Constant used to convenient specification of an unset optional holder. |
30 | | */ |
31 | | constexpr NullOptType NullOpt{int()}; |
32 | | |
33 | | /** |
34 | | * Helper type for specification of in-place construction. |
35 | | */ |
36 | | struct InPlaceT |
37 | | { |
38 | | constexpr explicit InPlaceT() = default; |
39 | | }; |
40 | | |
41 | | /** |
42 | | * Constant used as a marker for in-place construction. |
43 | | */ |
44 | | constexpr InPlaceT InPlace{}; |
45 | | |
46 | | namespace detail |
47 | | { |
48 | | |
49 | | /** |
50 | | * Base class for optional holders. Provides common interface for various types of optional holders. |
51 | | */ |
52 | | template <typename T, typename Derived> |
53 | | class optional_holder_base |
54 | | { |
55 | | public: |
56 | | /** |
57 | | * Operator equality. |
58 | | * |
59 | | * \param other Other holder to compare. |
60 | | * |
61 | | * \return True when the other holder has same value as this. False otherwise. |
62 | | */ |
63 | | bool operator==(const optional_holder_base& other) const |
64 | 43 | { |
65 | 43 | if (this == &other) |
66 | 5 | { |
67 | 5 | return true; |
68 | 5 | } |
69 | | |
70 | 38 | if (getDerived()->hasValue() != other.getDerived()->hasValue()) |
71 | 5 | { |
72 | 5 | return false; |
73 | 5 | } |
74 | | |
75 | 33 | if (getDerived()->hasValue()) |
76 | 26 | { |
77 | 26 | return get() == other.get(); |
78 | 26 | } |
79 | | |
80 | 7 | return true; |
81 | 33 | } |
82 | | |
83 | | /** |
84 | | * Operator less than. |
85 | | * |
86 | | * \param other Other holder to compare. |
87 | | * |
88 | | * \return True when the other holder has less value than this. False otherwise. |
89 | | */ |
90 | | bool operator<(const optional_holder_base& other) const |
91 | 10 | { |
92 | 10 | if (getDerived()->hasValue() && other.getDerived()->hasValue()6 ) |
93 | 4 | { |
94 | 4 | return get() < other.get(); |
95 | 4 | } |
96 | | |
97 | 6 | return !getDerived()->hasValue() && other.getDerived()->hasValue()4 ; |
98 | 10 | } |
99 | | |
100 | | /** |
101 | | * Bool operator. |
102 | | * |
103 | | * Evaluates to true when this holder has assigned any value. |
104 | | */ |
105 | | explicit operator bool() const noexcept |
106 | 539 | { |
107 | 539 | return getDerived()->hasValue(); |
108 | 539 | } |
109 | | |
110 | | /** |
111 | | * Dereference operator ->. |
112 | | * |
113 | | * \return Const reference to the assigned value. |
114 | | * |
115 | | * \throw CppRuntimeException when the holder is unset. |
116 | | */ |
117 | | const T* operator->() const |
118 | 4 | { |
119 | 4 | return std::addressof(get()); |
120 | 4 | } |
121 | | |
122 | | /** |
123 | | * Dereference operator ->. |
124 | | * |
125 | | * \return Reference to the assigned value. |
126 | | * |
127 | | * \throw CppRuntimeException when the holder is unset. |
128 | | */ |
129 | | T* operator->() |
130 | 480 | { |
131 | 480 | return std::addressof(get()); |
132 | 480 | } |
133 | | |
134 | | /** |
135 | | * Gets held value. |
136 | | * |
137 | | * \return Const reference to the assigned value. |
138 | | * |
139 | | * \throw CppRuntimeException when the holder is unset. |
140 | | */ |
141 | | const T& value() const |
142 | 48.7k | { |
143 | 48.7k | return get(); |
144 | 48.7k | } |
145 | | |
146 | | /** |
147 | | * Gets held value. |
148 | | * |
149 | | * \return Reference to the assigned value. |
150 | | * |
151 | | * \throw CppRuntimeException when the holder is unset. |
152 | | */ |
153 | | T& value() |
154 | 859 | { |
155 | 859 | return get(); |
156 | 859 | } |
157 | | |
158 | | protected: |
159 | | void checkHasValue() const |
160 | 50.2k | { |
161 | 50.2k | if (!getDerived()->hasValue()) |
162 | 14 | { |
163 | 14 | throwNonPresentException(); |
164 | 14 | } |
165 | 50.2k | } |
166 | | |
167 | | private: |
168 | | T& get() |
169 | 1.33k | { |
170 | 1.33k | return getDerived()->operator*(); |
171 | 1.33k | } |
172 | | |
173 | | const T& get() const |
174 | 48.8k | { |
175 | 48.8k | return getDerived()->operator*(); |
176 | 48.8k | } |
177 | | |
178 | | Derived* getDerived() |
179 | 1.33k | { |
180 | 1.33k | return static_cast<Derived*>(this); |
181 | 1.33k | } |
182 | | |
183 | | const Derived* getDerived() const |
184 | 99.8k | { |
185 | 99.8k | return static_cast<const Derived*>(this); |
186 | 99.8k | } |
187 | | |
188 | | /** Optimization which increases chances to inline checkHasValue(). */ |
189 | | void throwNonPresentException() const |
190 | 14 | { |
191 | 14 | throw CppRuntimeException("Trying to access value of non-present optional field!"); |
192 | 14 | } |
193 | | }; |
194 | | |
195 | | /** |
196 | | * Optional holder implementation that stores the object on heap. |
197 | | */ |
198 | | template <typename T, typename ALLOC> |
199 | | class heap_optional_holder : public optional_holder_base<T, heap_optional_holder<T, ALLOC>> |
200 | | { |
201 | | public: |
202 | | using allocator_type = ALLOC; |
203 | | using allocator_traits = std::allocator_traits<allocator_type>; |
204 | | using value_type = T; |
205 | | using storage_type = zserio::unique_ptr<T, allocator_type>; |
206 | | |
207 | | /** |
208 | | * Getter for the allocator. |
209 | | * |
210 | | * \return Allocator that is used for the dynamic memory allocation. |
211 | | */ |
212 | | allocator_type get_allocator() const |
213 | 98 | { |
214 | 98 | return m_storage.get_deleter().get_allocator(); |
215 | 98 | } |
216 | | |
217 | | /** |
218 | | * Empty constructor which creates an unset holder. |
219 | | * |
220 | | * \param allocator Allocator to be used to perform dynamic memory allocations. |
221 | | */ |
222 | | explicit constexpr heap_optional_holder(const allocator_type& allocator = allocator_type()) noexcept : |
223 | | m_storage(nullptr, allocator) |
224 | 45 | {} |
225 | | |
226 | | /** |
227 | | * Constructor from zserio::NullOpt constant to create an unset holder. |
228 | | * |
229 | | * \param allocator Allocator to be used to perform dynamic memory allocations. |
230 | | */ |
231 | | constexpr heap_optional_holder(NullOptType, const allocator_type& allocator = allocator_type()) noexcept : |
232 | | m_storage(nullptr, allocator) |
233 | 2 | {} |
234 | | |
235 | | /** |
236 | | * Constructor from a given value. |
237 | | * |
238 | | * \param val Value to store in the holder. |
239 | | * \param allocator Allocator to be used to perform dynamic memory allocations. |
240 | | */ |
241 | | heap_optional_holder(const T& val, const allocator_type& allocator = allocator_type()) : |
242 | | m_storage(zserio::allocate_unique<T, allocator_type>(allocator, val)) |
243 | 2 | {} |
244 | | |
245 | | /** |
246 | | * Constructor from a given value passed by rvalue reference. |
247 | | * |
248 | | * \param val Value to store in the holder. |
249 | | * \param allocator Allocator to be used to perform dynamic memory allocations. |
250 | | */ |
251 | | heap_optional_holder(T&& val, const allocator_type& allocator = allocator_type()) : |
252 | | m_storage(zserio::allocate_unique<T, allocator_type>(allocator, std::move(val))) |
253 | 27 | {} |
254 | | |
255 | | // called from allocatorPropagatingCopy |
256 | | /** |
257 | | * Constructor from a given value passed by rvalue reference which prevents initialization. |
258 | | * |
259 | | * \param val Value to store in the holder. |
260 | | * \param allocator Allocator to be used to perform dynamic memory allocations. |
261 | | */ |
262 | | template <typename U = T, |
263 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
264 | | heap_optional_holder(NoInitT, T&& val, const allocator_type& allocator = allocator_type()) : |
265 | | m_storage(zserio::allocate_unique<T, allocator_type>(allocator, NoInit, std::move(val))) |
266 | 2 | {} |
267 | | |
268 | | /** |
269 | | * Constructor that initializes the value inplace by forwarding the arguments to the object's constructor. |
270 | | * |
271 | | * \param allocator Allocator to be used for dynamic memory allocations. |
272 | | * \param parameters Parameters for object's constructor. |
273 | | */ |
274 | | template <typename... U> |
275 | | explicit heap_optional_holder(const allocator_type& allocator, U&&... parameters) : |
276 | | m_storage(zserio::allocate_unique<T, allocator_type>(allocator, std::forward<U>(parameters)...)) |
277 | 2 | {} |
278 | | |
279 | | /** |
280 | | * Destructor. |
281 | | */ |
282 | 98 | ~heap_optional_holder() = default; |
283 | | |
284 | | /** |
285 | | * Copy constructor. |
286 | | * |
287 | | * \param other Other holder to copy. |
288 | | */ |
289 | | heap_optional_holder(const heap_optional_holder& other) : |
290 | | m_storage(copy_initialize( |
291 | | other, allocator_traits::select_on_container_copy_construction(other.get_allocator()))) |
292 | 6 | {} |
293 | | |
294 | | /** |
295 | | * Copy constructor which prevents initialization. |
296 | | * |
297 | | * \param other Other holder to copy. |
298 | | */ |
299 | | template <typename U = T, |
300 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
301 | | heap_optional_holder(NoInitT, const heap_optional_holder& other) : |
302 | | m_storage(copy_initialize(NoInit, other, |
303 | | allocator_traits::select_on_container_copy_construction(other.get_allocator()))) |
304 | 2 | {} |
305 | | |
306 | | /** |
307 | | * Allocator-extended copy constructor. |
308 | | * |
309 | | * \param other Other holder to copy. |
310 | | * \param allocator Allocator to be used for dynamic memory allocations. |
311 | | */ |
312 | | heap_optional_holder(const heap_optional_holder& other, const allocator_type& allocator) : |
313 | | m_storage(copy_initialize(other, allocator)) |
314 | 2 | {} |
315 | | |
316 | | /** |
317 | | * Move constructor. |
318 | | * |
319 | | * \param other Other holder to move. |
320 | | */ |
321 | 1 | heap_optional_holder(heap_optional_holder&& other) noexcept = default; |
322 | | |
323 | | /** |
324 | | * Move constructor which prevents initialization. |
325 | | * |
326 | | * \param other Other holder to move. |
327 | | */ |
328 | | template <typename U = T, |
329 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
330 | | heap_optional_holder(NoInitT, heap_optional_holder&& other) : |
331 | | m_storage(move_initialize(NoInit, std::move(other), other.get_allocator())) |
332 | 4 | {} |
333 | | |
334 | | /** |
335 | | * Allocator-extended move constructor. |
336 | | * |
337 | | * \param other Other holder to move. |
338 | | * \param allocator Allocator to be used for dynamic memory allocations. |
339 | | */ |
340 | | heap_optional_holder(heap_optional_holder&& other, const allocator_type& allocator) : |
341 | | m_storage(move_initialize(std::move(other), allocator)) |
342 | 3 | {} |
343 | | |
344 | | /** |
345 | | * Assignment operator. |
346 | | * |
347 | | * \param other Other holder to copy-assign. |
348 | | * |
349 | | * \return Reference to the current holder. |
350 | | */ |
351 | | heap_optional_holder& operator=(const heap_optional_holder& other) |
352 | 5 | { |
353 | 5 | if (this == &other) |
354 | 2 | { |
355 | 2 | return *this; |
356 | 2 | } |
357 | | |
358 | 3 | m_storage = copy_initialize(other, |
359 | 3 | select_allocator(other.get_allocator(), |
360 | 3 | typename allocator_traits::propagate_on_container_copy_assignment())); |
361 | | |
362 | 3 | return *this; |
363 | 5 | } |
364 | | |
365 | | /** |
366 | | * Assignment operator which prevents initialization. |
367 | | * |
368 | | * \param other Other holder to copy-assign. |
369 | | * |
370 | | * \return Reference to the current holder. |
371 | | */ |
372 | | template <typename U = T, |
373 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
374 | | heap_optional_holder& assign(NoInitT, const heap_optional_holder& other) |
375 | 3 | { |
376 | 3 | if (this == &other) |
377 | 1 | { |
378 | 1 | return *this; |
379 | 1 | } |
380 | | |
381 | 2 | m_storage = copy_initialize(NoInit, other, |
382 | 2 | select_allocator(other.get_allocator(), |
383 | 2 | typename allocator_traits::propagate_on_container_copy_assignment())); |
384 | | |
385 | 2 | return *this; |
386 | 3 | } |
387 | | |
388 | | /** |
389 | | * Move assignment operator. |
390 | | * |
391 | | * \param other Other holder to move-assign. |
392 | | * |
393 | | * \return Reference to the current holder. |
394 | | */ |
395 | | heap_optional_holder& operator=(heap_optional_holder&& other) |
396 | 4 | { |
397 | 4 | if (this == &other) |
398 | 2 | { |
399 | 2 | return *this; |
400 | 2 | } |
401 | | |
402 | 2 | m_storage = move_initialize(std::move(other), |
403 | 2 | select_allocator(other.get_allocator(), |
404 | 2 | typename allocator_traits::propagate_on_container_move_assignment())); |
405 | | |
406 | 2 | return *this; |
407 | 4 | } |
408 | | |
409 | | /** |
410 | | * Move assignment operator which prevents initialization. |
411 | | * |
412 | | * \param other Other holder to move-assign. |
413 | | * |
414 | | * \return Reference to the current holder. |
415 | | */ |
416 | | template <typename U = T, |
417 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
418 | | heap_optional_holder& assign(NoInitT, heap_optional_holder&& other) |
419 | 5 | { |
420 | 5 | if (this == &other) |
421 | 2 | { |
422 | 2 | return *this; |
423 | 2 | } |
424 | | |
425 | 3 | m_storage = move_initialize(NoInit, std::move(other), |
426 | 3 | select_allocator(other.get_allocator(), |
427 | 3 | typename allocator_traits::propagate_on_container_move_assignment())); |
428 | | |
429 | 3 | return *this; |
430 | 5 | } |
431 | | |
432 | | /** |
433 | | * Assignment operator from value. |
434 | | * |
435 | | * \param val Value to assign. |
436 | | * |
437 | | * \return Reference to the current holder. |
438 | | */ |
439 | | heap_optional_holder& operator=(const T& val) |
440 | 8 | { |
441 | 8 | set(val); |
442 | | |
443 | 8 | return *this; |
444 | 8 | } |
445 | | |
446 | | /** |
447 | | * Assignment operator from rvalue reference to value. |
448 | | * |
449 | | * \param val Value to move-assign. |
450 | | * |
451 | | * \return Reference to the current holder. |
452 | | */ |
453 | | heap_optional_holder& operator=(T&& val) |
454 | 7 | { |
455 | 7 | set(std::move(val)); |
456 | | |
457 | 7 | return *this; |
458 | 7 | } |
459 | | |
460 | | /** |
461 | | * Resets the current holder (switch to the unset state). |
462 | | */ |
463 | | void reset() noexcept |
464 | 17 | { |
465 | 17 | m_storage.reset(); |
466 | 17 | } |
467 | | |
468 | | /** |
469 | | * Gets whether the holder has any value. |
470 | | * |
471 | | * \return True when this holder has assigned any value. False otherwise. |
472 | | */ |
473 | | bool hasValue() const noexcept |
474 | 208 | { |
475 | 208 | return static_cast<bool>(m_storage); |
476 | 208 | } |
477 | | |
478 | | /** |
479 | | * Dereference operator *. |
480 | | * |
481 | | * \return Const reference to the assigned value. |
482 | | * |
483 | | * \throw CppRuntimeException when the holder is unset. |
484 | | */ |
485 | | const T& operator*() const |
486 | 34 | { |
487 | 34 | this->checkHasValue(); |
488 | 34 | return *m_storage.get(); |
489 | 34 | } |
490 | | |
491 | | /** |
492 | | * Dereference operator *. |
493 | | * |
494 | | * \return Reference to the assigned value. |
495 | | * |
496 | | * \throw CppRuntimeException when the holder is unset. |
497 | | */ |
498 | | T& operator*() |
499 | 76 | { |
500 | 76 | this->checkHasValue(); |
501 | 76 | return *m_storage.get(); |
502 | 76 | } |
503 | | |
504 | | private: |
505 | | allocator_type select_allocator(allocator_type other_allocator, std::true_type) |
506 | 7 | { |
507 | 7 | return other_allocator; |
508 | 7 | } |
509 | | |
510 | | allocator_type select_allocator(allocator_type, std::false_type) |
511 | 3 | { |
512 | 3 | return get_allocator(); // current allocator |
513 | 3 | } |
514 | | |
515 | | template <typename U = T> |
516 | | void set(U&& val) |
517 | 15 | { |
518 | 15 | reset(); |
519 | 15 | m_storage = zserio::allocate_unique<T, allocator_type>(get_allocator(), std::forward<U>(val)); |
520 | 15 | } |
521 | | |
522 | | static storage_type copy_initialize(const heap_optional_holder& other, const allocator_type& allocator) |
523 | 11 | { |
524 | 11 | if (other.hasValue()) |
525 | 9 | { |
526 | 9 | return zserio::allocate_unique<T, allocator_type>(allocator, *other); |
527 | 9 | } |
528 | 2 | else |
529 | 2 | { |
530 | 2 | return storage_type(nullptr, allocator); |
531 | 2 | } |
532 | 11 | } |
533 | | |
534 | | static storage_type copy_initialize( |
535 | | NoInitT, const heap_optional_holder& other, const allocator_type& allocator) |
536 | 4 | { |
537 | 4 | if (other.hasValue()) |
538 | 2 | { |
539 | 2 | return zserio::allocate_unique<T, allocator_type>(allocator, NoInit, *other); |
540 | 2 | } |
541 | 2 | else |
542 | 2 | { |
543 | 2 | return storage_type(nullptr, allocator); |
544 | 2 | } |
545 | 4 | } |
546 | | |
547 | | static storage_type move_initialize(heap_optional_holder&& other, const allocator_type& allocator) |
548 | 5 | { |
549 | 5 | if (other.hasValue()) |
550 | 4 | { |
551 | 4 | if (allocator == other.get_allocator()) |
552 | 2 | { |
553 | 2 | return std::move(other.m_storage); |
554 | 2 | } |
555 | | |
556 | 2 | return zserio::allocate_unique<T, allocator_type>(allocator, std::move(*other)); |
557 | 4 | } |
558 | 1 | else |
559 | 1 | { |
560 | 1 | return storage_type(nullptr, allocator); |
561 | 1 | } |
562 | 5 | } |
563 | | |
564 | | static storage_type move_initialize(NoInitT, heap_optional_holder&& other, const allocator_type& allocator) |
565 | 7 | { |
566 | 7 | if (other.hasValue()) |
567 | 4 | { |
568 | 4 | if (allocator == other.get_allocator()) |
569 | 3 | { |
570 | 3 | return std::move(other.m_storage); |
571 | 3 | } |
572 | | |
573 | 1 | return zserio::allocate_unique<T, allocator_type>(allocator, NoInit, std::move(*other)); |
574 | 4 | } |
575 | 3 | else |
576 | 3 | { |
577 | 3 | return storage_type(nullptr, allocator); |
578 | 3 | } |
579 | 7 | } |
580 | | |
581 | | storage_type m_storage; |
582 | | }; |
583 | | |
584 | | /** |
585 | | * In place storage for optional holder. |
586 | | */ |
587 | | template <typename T> |
588 | | class in_place_storage |
589 | | { |
590 | | public: |
591 | | /** |
592 | | * Constructor. |
593 | | */ |
594 | | in_place_storage() = default; |
595 | | |
596 | | /** |
597 | | * Destructor. |
598 | | */ |
599 | | ~in_place_storage() = default; |
600 | | |
601 | | /** Copying is disabled. */ |
602 | | /** \{ */ |
603 | | in_place_storage(const in_place_storage&) = delete; |
604 | | in_place_storage& operator=(const in_place_storage&) = delete; |
605 | | /** \} */ |
606 | | |
607 | | /** Move constructor is disabled. */ |
608 | | in_place_storage(in_place_storage&& other) = delete; |
609 | | |
610 | | /** |
611 | | * Move assignment operator. |
612 | | * |
613 | | * \param other Other storage to move. |
614 | | * |
615 | | * \return Reference to the current storage. |
616 | | */ |
617 | | in_place_storage& operator=(in_place_storage&& other) noexcept(std::is_nothrow_move_constructible<T>::value) |
618 | 16.3k | { |
619 | 16.3k | new (&m_inPlace) T(std::move(*other.getObject())); |
620 | 16.3k | other.getObject()->~T(); // ensure that destructor of object in original storage is called |
621 | | |
622 | 16.3k | return *this; |
623 | 16.3k | } |
624 | | |
625 | | /** |
626 | | * Move assignment operator which prevents initialization. |
627 | | * |
628 | | * \param other Other storage to move. |
629 | | * |
630 | | * \return Reference to the current storage. |
631 | | */ |
632 | | in_place_storage& assign(NoInitT, in_place_storage&& other) noexcept( |
633 | | std::is_nothrow_move_constructible<T>::value) |
634 | 10 | { |
635 | 10 | new (&m_inPlace) T(NoInit, std::move(*other.getObject())); |
636 | 10 | other.getObject()->~T(); // ensure that destructor of object in original storage is called |
637 | | |
638 | 10 | return *this; |
639 | 10 | } |
640 | | |
641 | | /** |
642 | | * Gets pointer to the underlying in-place memory. |
643 | | * |
644 | | * \return Pointer to the storage. |
645 | | */ |
646 | | void* getStorage() |
647 | 50.5k | { |
648 | 50.5k | return &m_inPlace; |
649 | 50.5k | } |
650 | | |
651 | | /** |
652 | | * Gets pointer to the stored object. |
653 | | * |
654 | | * \return Pointer to the object. |
655 | | */ |
656 | | T* getObject() |
657 | 84.5k | { |
658 | 84.5k | return reinterpret_cast<T*>(&m_inPlace); |
659 | 84.5k | } |
660 | | |
661 | | /** |
662 | | * Gets pointer to the stored object. |
663 | | * |
664 | | * \return Const pointer to the object. |
665 | | */ |
666 | | const T* getObject() const |
667 | 48.9k | { |
668 | 48.9k | return reinterpret_cast<const T*>(&m_inPlace); |
669 | 48.9k | } |
670 | | |
671 | | private: |
672 | | // Caution! |
673 | | // We use static constants as a WORKAROUND for a GCC 8/9 bug observed when cross-compiling with -m32 flag. |
674 | | // The compilers somehow spoils the aligned storage when alignof(T) is used directly as the template |
675 | | // argument which leads to "*** stack smashing detected ***" error (as observed in some language tests). |
676 | | static constexpr size_t SIZEOF_T = sizeof(T); |
677 | | static constexpr size_t ALIGNOF_T = alignof(T); |
678 | | using AlignedStorage = typename std::aligned_storage<SIZEOF_T, ALIGNOF_T>::type; |
679 | | AlignedStorage m_inPlace; |
680 | | }; |
681 | | |
682 | | /** |
683 | | * Optional holder implementation for Zserio which allows usage of in place storage. |
684 | | */ |
685 | | template <typename T> |
686 | | class inplace_optional_holder : public optional_holder_base<T, inplace_optional_holder<T>> |
687 | | { |
688 | | public: |
689 | | using value_type = T; |
690 | | |
691 | | /** |
692 | | * Empty constructor which creates an unset holder. |
693 | | */ |
694 | 66.5k | constexpr inplace_optional_holder() noexcept = default; |
695 | | |
696 | | /** |
697 | | * Constructor from zserio::NullOpt constant to create an unset holder. |
698 | | */ |
699 | | constexpr inplace_optional_holder(NullOptType) noexcept |
700 | 301 | {} |
701 | | |
702 | | /** |
703 | | * Constructor from a given value. |
704 | | * |
705 | | * \param val Value to store in the holder. |
706 | | */ |
707 | | inplace_optional_holder(const T& val) |
708 | 42 | { |
709 | 42 | new (m_storage.getStorage()) T(val); |
710 | 42 | m_hasValue = true; |
711 | 42 | } |
712 | | |
713 | | /** |
714 | | * Constructor from a given value passed by rvalue reference. |
715 | | * |
716 | | * \param val Value to store in the holder. |
717 | | */ |
718 | | inplace_optional_holder(T&& val) noexcept(std::is_nothrow_constructible<T>::value) |
719 | 48 | { |
720 | 48 | new (m_storage.getStorage()) T(std::move(val)); |
721 | 48 | m_hasValue = true; |
722 | 48 | } |
723 | | |
724 | | // called from allocatorPropagatingCopy |
725 | | /** |
726 | | * Constructor from a given value passed by rvalue reference which prevents initialization. |
727 | | * |
728 | | * \param val Value to store in the holder. |
729 | | */ |
730 | | template <typename U = T, |
731 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
732 | | inplace_optional_holder(NoInitT, T&& val) noexcept(std::is_nothrow_move_constructible<T>::value) |
733 | 6 | { |
734 | 6 | new (m_storage.getStorage()) T(NoInit, std::move(val)); |
735 | 6 | m_hasValue = true; |
736 | 6 | } |
737 | | |
738 | | /** |
739 | | * Copy constructor. |
740 | | * |
741 | | * \param other Other holder to copy. |
742 | | */ |
743 | | inplace_optional_holder(const inplace_optional_holder& other) |
744 | 72 | { |
745 | 72 | if (other.hasValue()) |
746 | 44 | { |
747 | 44 | new (m_storage.getStorage()) T(*other.m_storage.getObject()); |
748 | 44 | m_hasValue = true; |
749 | 44 | } |
750 | 72 | } |
751 | | |
752 | | /** |
753 | | * Copy constructor which prevents initialization. |
754 | | * |
755 | | * \param other Other holder to copy. |
756 | | */ |
757 | | template <typename U = T, |
758 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
759 | | inplace_optional_holder(NoInitT, const inplace_optional_holder& other) |
760 | 2 | { |
761 | 2 | if (other.hasValue()) |
762 | 1 | { |
763 | 1 | new (m_storage.getStorage()) T(NoInit, *other.m_storage.getObject()); |
764 | 1 | m_hasValue = true; |
765 | 1 | } |
766 | 2 | } |
767 | | |
768 | | /** |
769 | | * Move constructor. |
770 | | * |
771 | | * \param other Other holder to move. |
772 | | */ |
773 | | inplace_optional_holder(inplace_optional_holder&& other) noexcept( |
774 | | std::is_nothrow_move_assignable<in_place_storage<T>>::value) |
775 | 4 | { |
776 | 4 | if (other.hasValue()) |
777 | 2 | { |
778 | 2 | m_storage = std::move(other.m_storage); |
779 | 2 | other.m_hasValue = false; |
780 | 2 | m_hasValue = true; |
781 | 2 | } |
782 | 4 | } |
783 | | |
784 | | /** |
785 | | * Move constructor which prevents initialization. |
786 | | * |
787 | | * \param other Other holder to move. |
788 | | */ |
789 | | template <typename U = T, |
790 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
791 | | inplace_optional_holder(NoInitT, inplace_optional_holder&& other) noexcept( |
792 | | std::is_nothrow_move_assignable<in_place_storage<T>>::value) |
793 | 4 | { |
794 | 4 | if (other.hasValue()) |
795 | 2 | { |
796 | 2 | m_storage.assign(NoInit, std::move(other.m_storage)); |
797 | 2 | other.m_hasValue = false; |
798 | 2 | m_hasValue = true; |
799 | 2 | } |
800 | 4 | } |
801 | | |
802 | | /** |
803 | | * Constructor that initializes the value inplace by forwarding the arguments to the object's constructor. |
804 | | * |
805 | | * \param parameters Parameters for object's constructor. |
806 | | */ |
807 | | template <typename... U> |
808 | | explicit inplace_optional_holder(InPlaceT, U&&... parameters) |
809 | 3 | { |
810 | 3 | new (m_storage.getStorage()) T(std::forward<U>(parameters)...); |
811 | 3 | m_hasValue = true; |
812 | 3 | } |
813 | | |
814 | | /** |
815 | | * Destructor. |
816 | | */ |
817 | | ~inplace_optional_holder() |
818 | 67.0k | { |
819 | 67.0k | reset(); |
820 | 67.0k | } |
821 | | |
822 | | /** |
823 | | * Assignment operator. |
824 | | * |
825 | | * \param other Other holder to copy-assign. |
826 | | * |
827 | | * \return Reference to the current holder. |
828 | | */ |
829 | | inplace_optional_holder& operator=(const inplace_optional_holder& other) |
830 | 61 | { |
831 | 61 | if (this != &other) |
832 | 60 | { |
833 | 60 | reset(); |
834 | 60 | if (other.hasValue()) |
835 | 59 | { |
836 | 59 | new (m_storage.getStorage()) T(*other.m_storage.getObject()); |
837 | 59 | m_hasValue = true; |
838 | 59 | } |
839 | 60 | } |
840 | | |
841 | 61 | return *this; |
842 | 61 | } |
843 | | |
844 | | /** |
845 | | * Assignment operator which prevents initialization. |
846 | | * |
847 | | * \param other Other holder to copy-assign. |
848 | | * |
849 | | * \return Reference to the current holder. |
850 | | */ |
851 | | template <typename U = T, |
852 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
853 | | inplace_optional_holder& assign(NoInitT, const inplace_optional_holder& other) |
854 | 9 | { |
855 | 9 | if (this != &other) |
856 | 8 | { |
857 | 8 | reset(); |
858 | 8 | if (other.hasValue()) |
859 | 7 | { |
860 | 7 | new (m_storage.getStorage()) T(NoInit, *other.m_storage.getObject()); |
861 | 7 | m_hasValue = true; |
862 | 7 | } |
863 | 8 | } |
864 | | |
865 | 9 | return *this; |
866 | 9 | } |
867 | | |
868 | | /** |
869 | | * Move assignment operator. |
870 | | * |
871 | | * \param other Other holder to move-assign. |
872 | | * |
873 | | * \return Reference to the current holder. |
874 | | */ |
875 | | inplace_optional_holder& operator=(inplace_optional_holder&& other) noexcept( |
876 | | std::is_nothrow_move_assignable<in_place_storage<T>>::value) |
877 | 16.3k | { |
878 | 16.3k | if (this != &other) |
879 | 16.3k | { |
880 | 16.3k | reset(); |
881 | 16.3k | if (other.hasValue()) |
882 | 16.3k | { |
883 | 16.3k | m_storage = std::move(other.m_storage); |
884 | 16.3k | other.m_hasValue = false; |
885 | 16.3k | m_hasValue = true; |
886 | 16.3k | } |
887 | 16.3k | } |
888 | | |
889 | 16.3k | return *this; |
890 | 16.3k | } |
891 | | |
892 | | /** |
893 | | * Move assignment operator which prevents initialization. |
894 | | * |
895 | | * \param other Other holder to move-assign. |
896 | | * |
897 | | * \return Reference to the current holder. |
898 | | */ |
899 | | template <typename U = T, |
900 | | typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0> |
901 | | inplace_optional_holder& assign(NoInitT, inplace_optional_holder&& other) noexcept( |
902 | | std::is_nothrow_move_assignable<in_place_storage<T>>::value) |
903 | 10 | { |
904 | 10 | if (this != &other) |
905 | 9 | { |
906 | 9 | reset(); |
907 | 9 | if (other.hasValue()) |
908 | 8 | { |
909 | 8 | m_storage.assign(NoInit, std::move(other.m_storage)); |
910 | 8 | other.m_hasValue = false; |
911 | 8 | m_hasValue = true; |
912 | 8 | } |
913 | 9 | } |
914 | | |
915 | 10 | return *this; |
916 | 10 | } |
917 | | |
918 | | /** |
919 | | * Assignment operator from value. |
920 | | * |
921 | | * \param val Value to assign. |
922 | | * |
923 | | * \return Reference to the current holder. |
924 | | */ |
925 | | inplace_optional_holder& operator=(const T& val) |
926 | 33.4k | { |
927 | 33.4k | set(val); |
928 | | |
929 | 33.4k | return *this; |
930 | 33.4k | } |
931 | | |
932 | | /** |
933 | | * Assignment operator from rvalue reference to value. |
934 | | * |
935 | | * \param val Value to move-assign. |
936 | | * |
937 | | * \return Reference to the current holder. |
938 | | */ |
939 | | inplace_optional_holder& operator=(T&& val) |
940 | 16.8k | { |
941 | 16.8k | set(std::move(val)); |
942 | | |
943 | 16.8k | return *this; |
944 | 16.8k | } |
945 | | |
946 | | /** |
947 | | * Resets the current holder (switch to the unset state). |
948 | | */ |
949 | | void reset() noexcept |
950 | 133k | { |
951 | 133k | if (hasValue()) |
952 | 50.5k | { |
953 | 50.5k | m_storage.getObject()->~T(); |
954 | 50.5k | m_hasValue = false; |
955 | 50.5k | } |
956 | 133k | } |
957 | | |
958 | | /** |
959 | | * Gets whether the holder has any value. |
960 | | * |
961 | | * \return True when this holder has assigned any value. False otherwise. |
962 | | */ |
963 | | bool hasValue() const noexcept |
964 | 201k | { |
965 | 201k | return m_hasValue; |
966 | 201k | } |
967 | | |
968 | | /** |
969 | | * Dereference operator *. |
970 | | * |
971 | | * \return Const reference to the assigned value. |
972 | | * |
973 | | * \throw CppRuntimeException when the holder is unset. |
974 | | */ |
975 | | const T& operator*() const |
976 | 48.8k | { |
977 | 48.8k | this->checkHasValue(); |
978 | 48.8k | return *m_storage.getObject(); |
979 | 48.8k | } |
980 | | |
981 | | /** |
982 | | * Dereference operator *. |
983 | | * |
984 | | * \return Reference to the assigned value. |
985 | | * |
986 | | * \throw CppRuntimeException when the holder is unset. |
987 | | */ |
988 | | T& operator*() |
989 | 1.35k | { |
990 | 1.35k | this->checkHasValue(); |
991 | 1.35k | return *m_storage.getObject(); |
992 | 1.35k | } |
993 | | |
994 | | private: |
995 | | template <typename U = T> |
996 | | void set(U&& val) |
997 | 50.3k | { |
998 | 50.3k | reset(); |
999 | 50.3k | new (m_storage.getStorage()) T(std::forward<U>(val)); |
1000 | 50.3k | m_hasValue = true; |
1001 | 50.3k | } |
1002 | | |
1003 | | in_place_storage<T> m_storage; |
1004 | | bool m_hasValue = false; |
1005 | | }; |
1006 | | |
1007 | | } // namespace detail |
1008 | | |
1009 | | // Be aware that if Inplace/HeapOptionalHolder is defined by typename, C++ compiler will have problem with |
1010 | | // template function overload, see HashCodeUtil.h (overloads for objects and for Inplace/HeapOptionalHolder). |
1011 | | /** |
1012 | | * Optional holder which uses in place storage. |
1013 | | */ |
1014 | | template <typename T> |
1015 | | using InplaceOptionalHolder = detail::inplace_optional_holder<T>; |
1016 | | |
1017 | | /** |
1018 | | * Optional holder which uses heap storage. |
1019 | | */ |
1020 | | template <typename T, typename ALLOC = std::allocator<T>> |
1021 | | using HeapOptionalHolder = detail::heap_optional_holder<T, ALLOC>; |
1022 | | |
1023 | | } // namespace zserio |
1024 | | |
1025 | | #endif // ifndef ZSERIO_OPTIONAL_HOLDER_H_INC |