Embedded Template Library 1.0
Loading...
Searching...
No Matches
variant_legacy.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 jwellbelove, Robin S�derholm
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#include "../platform.h"
32#include "../utility.h"
33#include "../array.h"
34#include "../largest.h"
35#include "../exception.h"
36#include "../type_traits.h"
37#include "../integral_limits.h"
38#include "../static_assert.h"
39#include "../alignment.h"
40#include "../error_handler.h"
41#include "../null_type.h"
42#include "../placement_new.h"
43
44#include <stdint.h>
45
46#if defined(ETL_COMPILER_KEIL)
47 #pragma diag_suppress 940
48 #pragma diag_suppress 111
49#endif
50
51//*****************************************************************************
55//*****************************************************************************
56namespace etl
57{
58#if ETL_NOT_USING_LEGACY_VARIANT
59 namespace legacy
60 {
61#endif
62 namespace private_variant
63 {
64 //*************************************************************************
67 //*************************************************************************
68 template <size_t ID>
69 struct no_type
70 {
71 };
72 }
73
74 //***************************************************************************
77 //***************************************************************************
78 struct monostate
79 {
80 };
81
82 //***************************************************************************
85 //***************************************************************************
94
95 //***************************************************************************
98 //***************************************************************************
100 {
101 public:
103 : variant_exception(ETL_ERROR_TEXT("variant:unsupported type", ETL_VARIANT_FILE_ID"A"), file_name_, line_number_)
104 {
105 }
106 };
107
108 //***************************************************************************
111 //***************************************************************************
113 {
114 public:
116 : variant_exception(ETL_ERROR_TEXT("variant:bad variant access", ETL_VARIANT_FILE_ID"B"), file_name_, line_number_)
117 {}
118 };
119
120 //***************************************************************************
123 //***************************************************************************
125 {
126 public:
128 : variant_exception(ETL_ERROR_TEXT("variant:not_a base", ETL_VARIANT_FILE_ID"C"), file_name_, line_number_)
129 {
130 }
131 };
132
133 //***************************************************************************
137 //***************************************************************************
138 template <typename T1,
139 typename T2 = etl::null_type<2>,
140 typename T3 = etl::null_type<3>,
141 typename T4 = etl::null_type<4>,
142 typename T5 = etl::null_type<5>,
143 typename T6 = etl::null_type<6>,
144 typename T7 = etl::null_type<7>,
145 typename T8 = etl::null_type<8> >
147 {
148 public:
149
150 //***************************************************************************
152 //***************************************************************************
154
155 //***************************************************************************
157 //***************************************************************************
158 static const type_id_t UNSUPPORTED_TYPE_ID = etl::integral_limits<type_id_t>::max;
159
160 private:
161
162 // All types of variant are friends.
163 template <typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7, typename U8>
164 friend class variant;
165
166 //***************************************************************************
168 //***************************************************************************
169 typedef typename etl::largest_type<T1, T2, T3, T4, T5, T6, T7, T8>::type largest_t;
170
171 //***************************************************************************
173 //***************************************************************************
174 static const size_t SIZE = sizeof(largest_t);
175
176 //***************************************************************************
178 //***************************************************************************
180
181 //***************************************************************************
183 //***************************************************************************
191
192 //***************************************************************************
194 //***************************************************************************
195 template <typename T>
196 struct Type_Id_Lookup
197 {
198 static const uint_least8_t type_id = etl::is_same<T, T1>::value ? 0 :
206 UNSUPPORTED_TYPE_ID;
207 };
208
209 //***************************************************************************
211 //***************************************************************************
212 template <typename T>
213 struct Type_Is_Supported : public etl::integral_constant<bool,
214 etl::is_same<T, T1>::value ||
215 etl::is_same<T, T2>::value ||
216 etl::is_same<T, T3>::value ||
217 etl::is_same<T, T4>::value ||
218 etl::is_same<T, T5>::value ||
219 etl::is_same<T, T6>::value ||
220 etl::is_same<T, T7>::value ||
221 etl::is_same<T, T8>::value>
222 {
223 };
224
225 public:
226
227 //***************************************************************************
229 //***************************************************************************
231 {
232 destruct_current();
233 }
234
235 //*************************************************************************
236 //**** Reader types *******************************************************
237 //*************************************************************************
238
239 //*************************************************************************
243 //*************************************************************************
244 template <typename R1, typename R2 = no_type2, typename R3 = no_type3, typename R4 = no_type4, typename R5 = no_type5, typename R6 = no_type6, typename R7 = no_type7, typename R8 = no_type8>
246 {
247 public:
248
249 friend class variant;
250
251 virtual ~reader_type()
252 {
253 }
254
255 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
256 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
257 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
258 virtual void read(typename etl::parameter_type<R4>::type value) = 0;
259 virtual void read(typename etl::parameter_type<R5>::type value) = 0;
260 virtual void read(typename etl::parameter_type<R6>::type value) = 0;
261 virtual void read(typename etl::parameter_type<R7>::type value) = 0;
262 virtual void read(typename etl::parameter_type<R8>::type value) = 0;
263 };
264
265 //*************************************************************************
267 //*************************************************************************
268 template <typename R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7>
270 {
271 public:
272
273 friend class variant;
274
275 virtual ~reader_type()
276 {
277 }
278
279 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
280 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
281 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
282 virtual void read(typename etl::parameter_type<R4>::type value) = 0;
283 virtual void read(typename etl::parameter_type<R5>::type value) = 0;
284 virtual void read(typename etl::parameter_type<R6>::type value) = 0;
285 virtual void read(typename etl::parameter_type<R7>::type value) = 0;
286
287 private:
288
289 void read(no_type8&) {};
290 };
291
292 //*************************************************************************
294 //*************************************************************************
295 template <typename R1, typename R2, typename R3, typename R4, typename R5, typename R6>
297 {
298 public:
299
300 friend class variant;
301
302 virtual ~reader_type()
303 {
304 }
305
306 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
307 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
308 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
309 virtual void read(typename etl::parameter_type<R4>::type value) = 0;
310 virtual void read(typename etl::parameter_type<R5>::type value) = 0;
311 virtual void read(typename etl::parameter_type<R6>::type value) = 0;
312
313 private:
314
315 void read(no_type7&) {};
316 void read(no_type8&) {};
317 };
318
319 //*************************************************************************
321 //*************************************************************************
322 template <typename R1, typename R2, typename R3, typename R4, typename R5>
324 {
325 public:
326
327 friend class variant;
328
329 virtual ~reader_type()
330 {
331 }
332
333 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
334 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
335 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
336 virtual void read(typename etl::parameter_type<R4>::type value) = 0;
337 virtual void read(typename etl::parameter_type<R5>::type value) = 0;
338
339 private:
340
341 void read(no_type6&) {};
342 void read(no_type7&) {};
343 void read(no_type8&) {};
344 };
345
346 //*************************************************************************
348 //*************************************************************************
349 template <typename R1, typename R2, typename R3, typename R4>
351 {
352 public:
353
354 friend class variant;
355
356 virtual ~reader_type()
357 {
358 }
359
360 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
361 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
362 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
363 virtual void read(typename etl::parameter_type<R4>::type value) = 0;
364
365 private:
366
367 void read(no_type5&) {};
368 void read(no_type6&) {};
369 void read(no_type7&) {};
370 void read(no_type8&) {};
371 };
372
373 //*************************************************************************
375 //*************************************************************************
376 template <typename R1, typename R2, typename R3>
378 {
379 public:
380
381 friend class variant;
382
383 virtual ~reader_type()
384 {
385 }
386
387 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
388 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
389 virtual void read(typename etl::parameter_type<R3>::type value) = 0;
390
391 private:
392
393 void read(no_type4&) {};
394 void read(no_type5&) {};
395 void read(no_type6&) {};
396 void read(no_type7&) {};
397 void read(no_type8&) {};
398 };
399
400 //*************************************************************************
402 //*************************************************************************
403 template <typename R1, typename R2>
405 {
406 public:
407
408 friend class variant;
409
410 virtual ~reader_type()
411 {
412 }
413
414 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
415 virtual void read(typename etl::parameter_type<R2>::type value) = 0;
416
417 private:
418
419 void read(no_type3&) {};
420 void read(no_type4&) {};
421 void read(no_type5&) {};
422 void read(no_type6&) {};
423 void read(no_type7&) {};
424 void read(no_type8&) {};
425 };
426
427 //*************************************************************************
429 //*************************************************************************
430 template <typename R1>
432 {
433 public:
434
435 friend class variant;
436
437 virtual ~reader_type()
438 {
439 }
440
441 virtual void read(typename etl::parameter_type<R1>::type value) = 0;
442
443 private:
444
445 void read(no_type2&) {};
446 void read(no_type3&) {};
447 void read(no_type4&) {};
448 void read(no_type5&) {};
449 void read(no_type6&) {};
450 void read(no_type7&) {};
451 void read(no_type8&) {};
452 };
453
454 //***************************************************************************
456 //***************************************************************************
458
459 //***************************************************************************
462 //***************************************************************************
465 : type_id(UNSUPPORTED_TYPE_ID)
466 {
467 }
468#include "diagnostic_pop.h"
469
470 //***************************************************************************
473 //***************************************************************************
474 template <typename T>
475 variant(const T& value)
476 {
477 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
478
479 ::new (static_cast<T*>(data)) T(value);
480 type_id = Type_Id_Lookup<T>::type_id;
481 }
482
483 //***************************************************************************
486 //***************************************************************************
489 {
490 switch (other.type_id)
491 {
492 case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
493 case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
494 case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
495 case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
496 case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
497 case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
498 case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
499 case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
500 default: break;
501 }
502
503 type_id = other.type_id;
504 }
505#include "diagnostic_pop.h"
506
507#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_VARIANT_FORCE_CPP03_IMPLEMENTATION)
508 //*************************************************************************
510 //*************************************************************************
511 template <typename T, typename... Args>
512 T& emplace(Args&&... args)
513 {
514 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
515
516 destruct_current();
517 ::new (static_cast<T*>(data)) T(etl::forward<Args>(args)...);
518 type_id = Type_Id_Lookup<T>::type_id;
519
520 return *static_cast<T*>(data);
521 }
522#else
523 //***************************************************************************
525 //***************************************************************************
526 template <typename T>
528 {
529 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
530
531 destruct_current();
532 ::new (static_cast<T*>(data)) T();
533 type_id = Type_Id_Lookup<T>::type_id;
534
535 return *static_cast<T*>(data);
536 }
537
538 //***************************************************************************
540 //***************************************************************************
541 template <typename T, typename TP1>
542 T& emplace(const TP1& value1)
543 {
544 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
545
546 destruct_current();
547 ::new (static_cast<T*>(data)) T(value1);
548 type_id = Type_Id_Lookup<T>::type_id;
549
550 return *static_cast<T*>(data);
551 }
552
553 //***************************************************************************
555 //***************************************************************************
556 template <typename T, typename TP1, typename TP2>
557 T& emplace(const TP1& value1, const TP2& value2)
558 {
559 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
560
561 destruct_current();
562 ::new (static_cast<T*>(data)) T(value1, value2);
563 type_id = Type_Id_Lookup<T>::type_id;
564
565 return *static_cast<T*>(data);
566 }
567
568 //***************************************************************************
570 //***************************************************************************
571 template <typename T, typename TP1, typename TP2, typename TP3>
572 T& emplace(const TP1& value1, const TP2& value2, const TP3& value3)
573 {
574 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
575
576 destruct_current();
577 ::new (static_cast<T*>(data)) T(value1, value2, value3);
578 type_id = Type_Id_Lookup<T>::type_id;
579
580 return *static_cast<T*>(data);
581 }
582
583 //***************************************************************************
585 //***************************************************************************
586 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
587 T& emplace(const TP1& value1, const TP2& value2, const TP3& value3, const TP4& value4)
588 {
589 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
590
591 destruct_current();
592 ::new (static_cast<T*>(data)) T(value1, value2, value3, value4);
593 type_id = Type_Id_Lookup<T>::type_id;
594
595 return *static_cast<T*>(data);
596 }
597#endif
598
599 //***************************************************************************
602 //***************************************************************************
603 template <typename T>
604 variant& operator =(const T& value)
605 {
606 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
607
608 destruct_current();
609 ::new (static_cast<T*>(data)) T(value);
610 type_id = Type_Id_Lookup<T>::type_id;
611
612 return *this;
613 }
614
615 //***************************************************************************
618 //***************************************************************************
620 {
621 if (this != &other)
622 {
623 destruct_current();
624
625 switch (other.type_id)
626 {
627 case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
628 case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
629 case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
630 case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
631 case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
632 case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
633 case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
634 case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
635 default: break;
636 }
637
638 type_id = other.type_id;
639 }
640
641 return *this;
642 }
643
644 //***************************************************************************
648 //***************************************************************************
649 bool is_same_type(const variant& other) const
650 {
651 return type_id == other.type_id;
652 }
653
654 //***************************************************************************
658 //***************************************************************************
659 template <typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7, typename U8>
661 {
662 bool is_same = false;
663
664 switch (other.type_id)
665 {
666 case 0: is_same = (type_id == Type_Id_Lookup<U1>::type_id); break;
667 case 1: is_same = (type_id == Type_Id_Lookup<U2>::type_id); break;
668 case 2: is_same = (type_id == Type_Id_Lookup<U3>::type_id); break;
669 case 3: is_same = (type_id == Type_Id_Lookup<U4>::type_id); break;
670 case 4: is_same = (type_id == Type_Id_Lookup<U5>::type_id); break;
671 case 5: is_same = (type_id == Type_Id_Lookup<U6>::type_id); break;
672 case 6: is_same = (type_id == Type_Id_Lookup<U7>::type_id); break;
673 case 7: is_same = (type_id == Type_Id_Lookup<U8>::type_id); break;
674 default: break;
675 }
676
677 return is_same;
678 }
679
680 //***************************************************************************
683 //***************************************************************************
684 void call(reader& r)
685 {
686 switch (type_id)
687 {
688 case 0: r.read(static_cast<T1&>(data)); break;
689 case 1: r.read(static_cast<T2&>(data)); break;
690 case 2: r.read(static_cast<T3&>(data)); break;
691 case 3: r.read(static_cast<T4&>(data)); break;
692 case 4: r.read(static_cast<T5&>(data)); break;
693 case 5: r.read(static_cast<T6&>(data)); break;
694 case 6: r.read(static_cast<T7&>(data)); break;
695 case 7: r.read(static_cast<T8&>(data)); break;
696 default: break;
697 }
698 }
699
700 //***************************************************************************
703 //***************************************************************************
704 bool is_valid() const
705 {
706 return type_id != UNSUPPORTED_TYPE_ID;
707 }
708
709 //***************************************************************************
712 //***************************************************************************
713 template <typename T>
714 bool is_type() const
715 {
716 return type_id == Type_Id_Lookup<T>::type_id;
717 }
718
719 //***************************************************************************
721 //***************************************************************************
722 size_t index() const
723 {
724 return type_id;
725 }
726
727 //***************************************************************************
729 //***************************************************************************
730 void clear()
731 {
732 destruct_current();
733 }
734
735 //***************************************************************************
739 //***************************************************************************
740 template <typename T>
742 {
743 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
745
746 return static_cast<T&>(data);
747 }
748
749 //***************************************************************************
753 //***************************************************************************
754 template <typename T>
755 const T& get() const
756 {
757 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
759
760 return static_cast<const T&>(data);
761 }
762
763 //***************************************************************************
766 //***************************************************************************
767 template <typename TBase>
769 {
770 if (is_base_of<TBase>())
771 {
772 return reinterpret_cast<TBase*>(static_cast<uint_least8_t*>(data));
773 }
774 else
775 {
776 return ETL_NULLPTR;
777 }
778 }
779
780 //***************************************************************************
783 //***************************************************************************
784 template <typename TBase>
786 {
787 TBase* ptr = upcast_ptr<TBase>();
788
789 ETL_ASSERT(ptr != ETL_NULLPTR, ETL_ERROR(variant_not_a_base_exception));
790
791 return *ptr;
792 }
793
794 //***************************************************************************
797 //***************************************************************************
798 template <typename TBase>
799 const TBase* upcast_ptr() const
800 {
801 if (is_base_of<TBase>())
802 {
803 return reinterpret_cast<const TBase*>(static_cast<const uint_least8_t*>(data));
804 }
805 else
806 {
807 return ETL_NULLPTR;
808 }
809 }
810
811 //***************************************************************************
814 //***************************************************************************
815 template <typename TBase>
816 const TBase& upcast() const
817 {
818 const TBase* ptr = upcast_ptr<TBase>();
819
820 ETL_ASSERT(ptr != ETL_NULLPTR, ETL_ERROR(variant_not_a_base_exception));
821
822 return *ptr;
823 }
824
825 //***************************************************************************
827 //***************************************************************************
828 template <typename TBase>
829 bool is_base_of() const
830 {
831 bool is_base;
832
833 switch (type_id)
834 {
843 default: is_base = false; break;
844 }
845
846 return is_base;
847 }
848
849 //***************************************************************************
851 //***************************************************************************
852 operator T1& () { return get<T1>(); }
853 operator T2& () { return get<T2>(); }
854 operator T3& () { return get<T3>(); }
855 operator T4& () { return get<T4>(); }
856 operator T5& () { return get<T5>(); }
857 operator T6& () { return get<T6>(); }
858 operator T7& () { return get<T7>(); }
859 operator T8& () { return get<T8>(); }
860
861 //***************************************************************************
864 //***************************************************************************
865 template <typename T>
866 static bool is_supported_type()
867 {
868 return Type_Is_Supported<T>::value;
869 }
870
871 private:
873 //***************************************************************************
875 //***************************************************************************
876 void destruct_current()
877 {
878 switch (type_id)
879 {
880 case 0: { static_cast<T1*>(data)->~T1(); break; }
881 case 1: { static_cast<T2*>(data)->~T2(); break; }
882 case 2: { static_cast<T3*>(data)->~T3(); break; }
883 case 3: { static_cast<T4*>(data)->~T4(); break; }
884 case 4: { static_cast<T5*>(data)->~T5(); break; }
885 case 5: { static_cast<T6*>(data)->~T6(); break; }
886 case 6: { static_cast<T7*>(data)->~T7(); break; }
887 case 7: { static_cast<T8*>(data)->~T8(); break; }
888 default: { break; }
889 }
890
891 type_id = UNSUPPORTED_TYPE_ID;
892 }
893#include "diagnostic_pop.h"
894
895 //***************************************************************************
898 //***************************************************************************
900
901 //***************************************************************************
903 //***************************************************************************
904 type_id_t type_id;
905 };
906
907 namespace private_variant
908 {
909 template <size_t, typename>
910 struct variant_alternative_helper;
911#define ETL_VARIANT_HELPER(INDEX, TYPE) \
912 template <typename T1, \
913 typename T2, \
914 typename T3, \
915 typename T4, \
916 typename T5, \
917 typename T6, \
918 typename T7, \
919 typename T8> \
920 struct variant_alternative_helper<INDEX, variant<T1, T2, T3, T4, T5, T6, T7, T8> > \
921 { \
922 typedef TYPE type; \
923 };
924 ETL_VARIANT_HELPER(0, T1)
925 ETL_VARIANT_HELPER(1, T2)
926 ETL_VARIANT_HELPER(2, T3)
927 ETL_VARIANT_HELPER(3, T4)
928 ETL_VARIANT_HELPER(4, T5)
929 ETL_VARIANT_HELPER(5, T6)
930 ETL_VARIANT_HELPER(6, T7)
931 ETL_VARIANT_HELPER(7, T8)
932#undef ETL_VARIANT_HELPER
933 } // namespace private_variant
934
935 template <size_t tIndex, typename TVariant>
937 {
938 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type type;
939 };
940
941 template <size_t tIndex, typename TVariant>
943 {
944 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type const type;
945 };
946
947 template <size_t tIndex, typename TVariant>
949 {
950 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type volatile type;
951 };
952
953 template <size_t tIndex, typename TVariant>
955 {
956 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type const volatile type;
957 };
958
959 template <typename T, typename TVariant>
960 inline T& get(TVariant& variant)
961 {
962 return variant.template get<T>();
963 }
964
965 template <typename T, typename TVariant>
966 inline T const& get(TVariant const& variant)
967 {
968 return variant.template get<T>();
969 }
970
971 template <size_t tIndex, typename TVariant>
972 inline typename variant_alternative<tIndex, TVariant>::type& get(TVariant& variant)
973 {
975 }
976
977 template <size_t tIndex, typename TVariant>
978 inline typename variant_alternative<tIndex, TVariant const>::type& get(TVariant const& variant)
979 {
981 }
982
983#define ETL_GEN_LEGACY_VISIT(VISITQUAL, VARIANTQUAL) \
984 template <typename TReturn, typename TVisitor, typename TVariant> \
985 static TReturn visit(TVisitor VISITQUAL visitor, TVariant VARIANTQUAL variant) \
986 { \
987 switch (variant.index()) \
988 { \
989 case 0: return static_cast<TReturn>(visitor(get<0>(variant))); \
990 case 1: return static_cast<TReturn>(visitor(get<1>(variant))); \
991 case 2: return static_cast<TReturn>(visitor(get<2>(variant))); \
992 case 3: return static_cast<TReturn>(visitor(get<3>(variant))); \
993 case 4: return static_cast<TReturn>(visitor(get<4>(variant))); \
994 case 5: return static_cast<TReturn>(visitor(get<5>(variant))); \
995 case 6: return static_cast<TReturn>(visitor(get<6>(variant))); \
996 case 7: return static_cast<TReturn>(visitor(get<7>(variant))); \
997 default: ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(bad_variant_access), TReturn()); \
998 } \
999 }
1000
1001 ETL_GEN_LEGACY_VISIT(&, &)
1002 ETL_GEN_LEGACY_VISIT(const&, &)
1003 ETL_GEN_LEGACY_VISIT(&, const&)
1004 ETL_GEN_LEGACY_VISIT(const&, const&)
1005
1006#undef ETL_GEN_LEGACY_VISIT
1007
1008#if ETL_NOT_USING_LEGACY_VARIANT
1009 }
1010#endif
1011}
Definition variant_legacy.h:246
#define ETL_ASSERT(b, e)
Definition error_handler.h:316
Definition exception.h:47
Definition integral_limits.h:516
Definition largest.h:227
integral_constant
Definition type_traits_generator.h:832
is_base_of
Definition type_traits_generator.h:1252
is_same
Definition type_traits_generator.h:1041
variant(const T &value)
Definition variant_legacy.h:475
bool is_same_type(const variant< U1, U2, U3, U4, U5, U6, U7, U8 > &other) const
Definition variant_legacy.h:660
static bool is_supported_type()
Definition variant_legacy.h:866
const T & get() const
Definition variant_legacy.h:755
~variant()
Destructor.
Definition variant_legacy.h:230
T & emplace(const TP1 &value1, const TP2 &value2, const TP3 &value3, const TP4 &value4)
Emplace with four constructor parameters.
Definition variant_legacy.h:587
variant()
Definition variant_legacy.h:464
bool is_same_type(const variant &other) const
Definition variant_legacy.h:649
T & emplace(const TP1 &value1, const TP2 &value2, const TP3 &value3)
Emplace with three constructor parameters.
Definition variant_legacy.h:572
void call(reader &r)
Definition variant_legacy.h:684
T & get()
Definition variant_legacy.h:741
bool is_base_of() const
Check that TBase is a base class of the current variant value.
Definition variant_legacy.h:829
uint_least8_t type_id_t
The type used for ids.
Definition variant_legacy.h:153
T & emplace(const TP1 &value1)
Emplace with one constructor parameter.
Definition variant_legacy.h:542
const TBase * upcast_ptr() const
Definition variant_legacy.h:799
reader_type< T1, T2, T3, T4, T5, T6, T7, T8 > reader
The base type for derived readers.
Definition variant_legacy.h:457
bool is_type() const
Definition variant_legacy.h:714
void clear()
Clears the value to 'no valid stored value'.
Definition variant_legacy.h:730
T & emplace(const TP1 &value1, const TP2 &value2)
Emplace with two constructor parameters.
Definition variant_legacy.h:557
TBase * upcast_ptr()
Definition variant_legacy.h:768
T & emplace()
Emplace with one constructor parameter.
Definition variant_legacy.h:527
size_t index() const
Gets the index of the type currently stored or UNSUPPORTED_TYPE_ID.
Definition variant_legacy.h:722
TBase & upcast()
Definition variant_legacy.h:785
variant(const variant &other)
Definition variant_legacy.h:488
const TBase & upcast() const
Definition variant_legacy.h:816
bool is_valid() const
Definition variant_legacy.h:704
Definition variant_legacy.h:113
Definition variant_legacy.h:147
Definition variant_legacy.h:87
Definition variant_legacy.h:100
Definition variant_legacy.h:125
Definition variant_legacy.h:79
bitset_ext
Definition absolute.h:38
T & get(array< T, MAXN > &a)
Definition array.h:719
etl::optional< T > read(etl::bit_stream_reader &stream)
Read a checked type from a stream.
Definition bit_stream.h:1377
Definition alignment.h:233
pair holds two objects of arbitrary type
Definition utility.h:164
ETL_CONSTEXPR pair()
Default constructor.
Definition utility.h:176
Definition variant_legacy.h:70
Definition variant_legacy.h:937