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 //***************************************************************************
487 template <size_t Index, typename T>
488 explicit variant(etl::in_place_index_t<Index>, T const& value)
489 : type_id(Index)
490 {
491 ETL_STATIC_ASSERT(Type_Id_Lookup<T>::type_id == Index, "Missmatched type");
492 ::new (static_cast<T*>(data)) T(value);
493 type_id = Index;
494 }
495
496 //***************************************************************************
499 //***************************************************************************
502 {
503 switch (other.type_id)
504 {
505 case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
506 case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
507 case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
508 case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
509 case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
510 case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
511 case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
512 case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
513 default: break;
514 }
515
516 type_id = other.type_id;
517 }
518#include "diagnostic_pop.h"
519
520#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_VARIANT_FORCE_CPP03_IMPLEMENTATION)
521 //*************************************************************************
523 //*************************************************************************
524 template <typename T, typename... Args>
525 T& emplace(Args&&... args)
526 {
527 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
528
529 destruct_current();
530 ::new (static_cast<T*>(data)) T(etl::forward<Args>(args)...);
531 type_id = Type_Id_Lookup<T>::type_id;
532
533 return *static_cast<T*>(data);
534 }
535#else
536 //***************************************************************************
538 //***************************************************************************
539 template <typename T>
541 {
542 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
543
544 destruct_current();
545 ::new (static_cast<T*>(data)) T();
546 type_id = Type_Id_Lookup<T>::type_id;
547
548 return *static_cast<T*>(data);
549 }
550
551 //***************************************************************************
553 //***************************************************************************
554 template <typename T, typename TP1>
555 T& emplace(const TP1& value1)
556 {
557 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
558
559 destruct_current();
560 ::new (static_cast<T*>(data)) T(value1);
561 type_id = Type_Id_Lookup<T>::type_id;
562
563 return *static_cast<T*>(data);
564 }
565
566 //***************************************************************************
568 //***************************************************************************
569 template <typename T, typename TP1, typename TP2>
570 T& emplace(const TP1& value1, const TP2& value2)
571 {
572 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
573
574 destruct_current();
575 ::new (static_cast<T*>(data)) T(value1, value2);
576 type_id = Type_Id_Lookup<T>::type_id;
577
578 return *static_cast<T*>(data);
579 }
580
581 //***************************************************************************
583 //***************************************************************************
584 template <typename T, typename TP1, typename TP2, typename TP3>
585 T& emplace(const TP1& value1, const TP2& value2, const TP3& value3)
586 {
587 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
588
589 destruct_current();
590 ::new (static_cast<T*>(data)) T(value1, value2, value3);
591 type_id = Type_Id_Lookup<T>::type_id;
592
593 return *static_cast<T*>(data);
594 }
595
596 //***************************************************************************
598 //***************************************************************************
599 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
600 T& emplace(const TP1& value1, const TP2& value2, const TP3& value3, const TP4& value4)
601 {
602 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
603
604 destruct_current();
605 ::new (static_cast<T*>(data)) T(value1, value2, value3, value4);
606 type_id = Type_Id_Lookup<T>::type_id;
607
608 return *static_cast<T*>(data);
609 }
610#endif
611
612 //***************************************************************************
615 //***************************************************************************
616 template <typename T>
617 variant& operator =(const T& value)
618 {
619 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
620
621 destruct_current();
622 ::new (static_cast<T*>(data)) T(value);
623 type_id = Type_Id_Lookup<T>::type_id;
624
625 return *this;
626 }
627
628 //***************************************************************************
631 //***************************************************************************
633 {
634 if (this != &other)
635 {
636 destruct_current();
637
638 switch (other.type_id)
639 {
640 case 0: ::new (static_cast<T1*>(data)) T1(other.get<T1>()); break;
641 case 1: ::new (static_cast<T2*>(data)) T2(other.get<T2>()); break;
642 case 2: ::new (static_cast<T3*>(data)) T3(other.get<T3>()); break;
643 case 3: ::new (static_cast<T4*>(data)) T4(other.get<T4>()); break;
644 case 4: ::new (static_cast<T5*>(data)) T5(other.get<T5>()); break;
645 case 5: ::new (static_cast<T6*>(data)) T6(other.get<T6>()); break;
646 case 6: ::new (static_cast<T7*>(data)) T7(other.get<T7>()); break;
647 case 7: ::new (static_cast<T8*>(data)) T8(other.get<T8>()); break;
648 default: break;
649 }
650
651 type_id = other.type_id;
652 }
653
654 return *this;
655 }
656
657 //***************************************************************************
661 //***************************************************************************
662 bool is_same_type(const variant& other) const
663 {
664 return type_id == other.type_id;
665 }
666
667 //***************************************************************************
671 //***************************************************************************
672 template <typename U1, typename U2, typename U3, typename U4, typename U5, typename U6, typename U7, typename U8>
674 {
675 bool is_same = false;
676
677 switch (other.type_id)
678 {
679 case 0: is_same = (type_id == Type_Id_Lookup<U1>::type_id); break;
680 case 1: is_same = (type_id == Type_Id_Lookup<U2>::type_id); break;
681 case 2: is_same = (type_id == Type_Id_Lookup<U3>::type_id); break;
682 case 3: is_same = (type_id == Type_Id_Lookup<U4>::type_id); break;
683 case 4: is_same = (type_id == Type_Id_Lookup<U5>::type_id); break;
684 case 5: is_same = (type_id == Type_Id_Lookup<U6>::type_id); break;
685 case 6: is_same = (type_id == Type_Id_Lookup<U7>::type_id); break;
686 case 7: is_same = (type_id == Type_Id_Lookup<U8>::type_id); break;
687 default: break;
688 }
689
690 return is_same;
691 }
692
693 //***************************************************************************
696 //***************************************************************************
697 void call(reader& r)
698 {
699 switch (type_id)
700 {
701 case 0: r.read(static_cast<T1&>(data)); break;
702 case 1: r.read(static_cast<T2&>(data)); break;
703 case 2: r.read(static_cast<T3&>(data)); break;
704 case 3: r.read(static_cast<T4&>(data)); break;
705 case 4: r.read(static_cast<T5&>(data)); break;
706 case 5: r.read(static_cast<T6&>(data)); break;
707 case 6: r.read(static_cast<T7&>(data)); break;
708 case 7: r.read(static_cast<T8&>(data)); break;
709 default: break;
710 }
711 }
712
713 //***************************************************************************
716 //***************************************************************************
717 bool is_valid() const
718 {
719 return type_id != UNSUPPORTED_TYPE_ID;
720 }
721
722 //***************************************************************************
725 //***************************************************************************
726 template <typename T>
727 bool is_type() const
728 {
729 return type_id == Type_Id_Lookup<T>::type_id;
730 }
731
732 //***************************************************************************
734 //***************************************************************************
735 size_t index() const
736 {
737 return type_id;
738 }
739
740 //***************************************************************************
742 //***************************************************************************
743 void clear()
744 {
745 destruct_current();
746 }
747
748 //***************************************************************************
752 //***************************************************************************
753 template <typename T>
755 {
756 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
758
759 return static_cast<T&>(data);
760 }
761
762 //***************************************************************************
766 //***************************************************************************
767 template <typename T>
768 const T& get() const
769 {
770 ETL_STATIC_ASSERT(Type_Is_Supported<T>::value, "Unsupported type");
772
773 return static_cast<const T&>(data);
774 }
775
776 //***************************************************************************
779 //***************************************************************************
780 template <typename TBase>
782 {
783 if (is_base_of<TBase>())
784 {
785 return reinterpret_cast<TBase*>(static_cast<uint_least8_t*>(data));
786 }
787 else
788 {
789 return ETL_NULLPTR;
790 }
791 }
792
793 //***************************************************************************
796 //***************************************************************************
797 template <typename TBase>
799 {
800 TBase* ptr = upcast_ptr<TBase>();
801
802 ETL_ASSERT(ptr != ETL_NULLPTR, ETL_ERROR(variant_not_a_base_exception));
803
804 return *ptr;
805 }
806
807 //***************************************************************************
810 //***************************************************************************
811 template <typename TBase>
812 const TBase* upcast_ptr() const
813 {
814 if (is_base_of<TBase>())
815 {
816 return reinterpret_cast<const TBase*>(static_cast<const uint_least8_t*>(data));
817 }
818 else
819 {
820 return ETL_NULLPTR;
821 }
822 }
823
824 //***************************************************************************
827 //***************************************************************************
828 template <typename TBase>
829 const TBase& upcast() const
830 {
831 const TBase* ptr = upcast_ptr<TBase>();
832
833 ETL_ASSERT(ptr != ETL_NULLPTR, ETL_ERROR(variant_not_a_base_exception));
834
835 return *ptr;
836 }
837
838 //***************************************************************************
840 //***************************************************************************
841 template <typename TBase>
842 bool is_base_of() const
843 {
844 bool is_base;
845
846 switch (type_id)
847 {
856 default: is_base = false; break;
857 }
858
859 return is_base;
860 }
861
862 //***************************************************************************
864 //***************************************************************************
865 operator T1& () { return get<T1>(); }
866 operator T2& () { return get<T2>(); }
867 operator T3& () { return get<T3>(); }
868 operator T4& () { return get<T4>(); }
869 operator T5& () { return get<T5>(); }
870 operator T6& () { return get<T6>(); }
871 operator T7& () { return get<T7>(); }
872 operator T8& () { return get<T8>(); }
873
874 //***************************************************************************
877 //***************************************************************************
878 template <typename T>
879 static bool is_supported_type()
880 {
881 return Type_Is_Supported<T>::value;
882 }
883
884 private:
886 //***************************************************************************
888 //***************************************************************************
889 void destruct_current()
890 {
891 switch (type_id)
892 {
893 case 0: { static_cast<T1*>(data)->~T1(); break; }
894 case 1: { static_cast<T2*>(data)->~T2(); break; }
895 case 2: { static_cast<T3*>(data)->~T3(); break; }
896 case 3: { static_cast<T4*>(data)->~T4(); break; }
897 case 4: { static_cast<T5*>(data)->~T5(); break; }
898 case 5: { static_cast<T6*>(data)->~T6(); break; }
899 case 6: { static_cast<T7*>(data)->~T7(); break; }
900 case 7: { static_cast<T8*>(data)->~T8(); break; }
901 default: { break; }
902 }
903
904 type_id = UNSUPPORTED_TYPE_ID;
905 }
906#include "diagnostic_pop.h"
907
908 //***************************************************************************
911 //***************************************************************************
913
914 //***************************************************************************
916 //***************************************************************************
917 type_id_t type_id;
918 };
919
920 namespace private_variant
921 {
922 template <size_t, typename>
923 struct variant_alternative_helper;
924#define ETL_VARIANT_HELPER(INDEX, TYPE) \
925 template <typename T1, \
926 typename T2, \
927 typename T3, \
928 typename T4, \
929 typename T5, \
930 typename T6, \
931 typename T7, \
932 typename T8> \
933 struct variant_alternative_helper<INDEX, variant<T1, T2, T3, T4, T5, T6, T7, T8> > \
934 { \
935 typedef TYPE type; \
936 };
937 ETL_VARIANT_HELPER(0, T1)
938 ETL_VARIANT_HELPER(1, T2)
939 ETL_VARIANT_HELPER(2, T3)
940 ETL_VARIANT_HELPER(3, T4)
941 ETL_VARIANT_HELPER(4, T5)
942 ETL_VARIANT_HELPER(5, T6)
943 ETL_VARIANT_HELPER(6, T7)
944 ETL_VARIANT_HELPER(7, T8)
945#undef ETL_VARIANT_HELPER
946 } // namespace private_variant
947
948 template <size_t tIndex, typename TVariant>
950 {
951 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type type;
952 };
953
954 template <size_t tIndex, typename TVariant>
956 {
957 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type const type;
958 };
959
960 template <size_t tIndex, typename TVariant>
962 {
963 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type volatile type;
964 };
965
966 template <size_t tIndex, typename TVariant>
968 {
969 typedef typename private_variant::variant_alternative_helper<tIndex, TVariant>::type const volatile type;
970 };
971
972 template <typename T, typename TVariant>
973 inline T& get(TVariant& variant)
974 {
975 return variant.template get<T>();
976 }
977
978 template <typename T, typename TVariant>
979 inline T const& get(TVariant const& variant)
980 {
981 return variant.template get<T>();
982 }
983
984 template <size_t tIndex, typename TVariant>
985 inline typename variant_alternative<tIndex, TVariant>::type& get(TVariant& variant)
986 {
988 }
989
990 template <size_t tIndex, typename TVariant>
991 inline typename variant_alternative<tIndex, TVariant const>::type& get(TVariant const& variant)
992 {
994 }
995
996#define ETL_GEN_LEGACY_VISIT(VISITQUAL, VARIANTQUAL) \
997 template <typename TReturn, typename TVisitor, typename TVariant> \
998 static TReturn visit(TVisitor VISITQUAL visitor, TVariant VARIANTQUAL variant) \
999 { \
1000 switch (variant.index()) \
1001 { \
1002 case 0: return static_cast<TReturn>(visitor(get<0>(variant))); \
1003 case 1: return static_cast<TReturn>(visitor(get<1>(variant))); \
1004 case 2: return static_cast<TReturn>(visitor(get<2>(variant))); \
1005 case 3: return static_cast<TReturn>(visitor(get<3>(variant))); \
1006 case 4: return static_cast<TReturn>(visitor(get<4>(variant))); \
1007 case 5: return static_cast<TReturn>(visitor(get<5>(variant))); \
1008 case 6: return static_cast<TReturn>(visitor(get<6>(variant))); \
1009 case 7: return static_cast<TReturn>(visitor(get<7>(variant))); \
1010 default: ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(bad_variant_access), TReturn()); \
1011 } \
1012 }
1013
1014 ETL_GEN_LEGACY_VISIT(&, &)
1015 ETL_GEN_LEGACY_VISIT(const&, &)
1016 ETL_GEN_LEGACY_VISIT(&, const&)
1017 ETL_GEN_LEGACY_VISIT(const&, const&)
1018
1019#undef ETL_GEN_LEGACY_VISIT
1020
1021#if ETL_NOT_USING_LEGACY_VARIANT
1022 }
1023#endif
1024}
Definition variant_legacy.h:246
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
Definition exception.h:47
Definition integral_limits.h:516
Definition largest.h:227
integral_constant
Definition type_traits_generator.h:827
is_base_of
Definition type_traits_generator.h:1247
is_same
Definition type_traits_generator.h:1036
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:673
static bool is_supported_type()
Definition variant_legacy.h:879
const T & get() const
Definition variant_legacy.h:768
~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:600
variant()
Definition variant_legacy.h:464
bool is_same_type(const variant &other) const
Definition variant_legacy.h:662
T & emplace(const TP1 &value1, const TP2 &value2, const TP3 &value3)
Emplace with three constructor parameters.
Definition variant_legacy.h:585
void call(reader &r)
Definition variant_legacy.h:697
T & get()
Definition variant_legacy.h:754
bool is_base_of() const
Check that TBase is a base class of the current variant value.
Definition variant_legacy.h:842
variant(etl::in_place_index_t< Index >, T const &value)
Definition variant_legacy.h:488
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:555
const TBase * upcast_ptr() const
Definition variant_legacy.h:812
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:727
void clear()
Clears the value to 'no valid stored value'.
Definition variant_legacy.h:743
T & emplace(const TP1 &value1, const TP2 &value2)
Emplace with two constructor parameters.
Definition variant_legacy.h:570
TBase * upcast_ptr()
Definition variant_legacy.h:781
T & emplace()
Emplace with one constructor parameter.
Definition variant_legacy.h:540
size_t index() const
Gets the index of the type currently stored or UNSUPPORTED_TYPE_ID.
Definition variant_legacy.h:735
TBase & upcast()
Definition variant_legacy.h:798
variant(const variant &other)
Definition variant_legacy.h:501
const TBase & upcast() const
Definition variant_legacy.h:829
bool is_valid() const
Definition variant_legacy.h:717
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:950