summaryrefslogtreecommitdiff
path: root/include/CommonAPI/Variant.hpp
blob: 4500aae12acf8ec3e22dbbb13a22fa50d919fd42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <cassert>
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <type_traits>

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.h> can be included directly, this file may disappear or change contents."
#endif

#include <CommonAPI/Deployable.hpp>
#include <CommonAPI/Deployment.hpp>

#ifndef COMMONAPI_VARIANT_HPP_
#define COMMONAPI_VARIANT_HPP_

namespace CommonAPI {

template<class Derived_>
class InputStream;

template<class Derived_>
class OutputStream;

template<typename... Types_>
struct MaxSize;

template<>
struct MaxSize<> {
    static const unsigned int value = 0;
};

template<typename Type_, typename... Types_>
struct MaxSize<Type_, Types_...> {
    static const unsigned int current_type_size = sizeof(Type_);
    static const unsigned int next_type_size = MaxSize<Types_...>::value;
    static const unsigned int value =
                    current_type_size > next_type_size ?
                                    current_type_size : next_type_size;
};

template<typename SearchType_, typename... RestTypes_>
struct VariantTypeSelector;

template<typename SearchType_, typename... RestTypes_>
struct VariantTypeSelector<SearchType_, SearchType_, RestTypes_...> {
    typedef SearchType_ type;
};

/**
 * \brief A templated generic variant class which provides type safe access and operators
 *
 * A templated generic variant class which provides type safe access and operators
 */
template<typename... Types_>
class Variant {
private:
    typedef std::tuple_size<std::tuple<Types_...>> TypesTupleSize;

public:

    static const unsigned int maxSize = MaxSize<Types_...>::value;

    /**
     * \brief Construct an empty variant
     *
     * Construct an empty variant
     */
    Variant();


    /**
     * \brief Copy constructor. Must have identical templates.
     *
     * Copy constructor. Must have identical templates.
     *
     * @param _source Variant to copy
     */
    Variant(const Variant &_source);

    /**
     * \brief Copy constructor. Must have identical templates.
     *
     * Copy constructor. Must have identical templates.
     *
     * @param _source Variant to copy
     */
    Variant(Variant &&_source);

    ~Variant();

    /**
      * \brief Assignment of another variant. Must have identical templates.
      *
      * Assignment of another variant. Must have identical templates.
      *
      * @param _source Variant to assign
      */
    Variant &operator=(const Variant &_source);
    /**
     * \brief Assignment of another variant. Must have identical templates.
     *
     * Assignment of another variant. Must have identical templates.
     *
     * @param _source Variant to assign
     */
    Variant &operator=(Variant &&_source);

    /**
     * \brief Assignment of a contained type. Must be one of the valid templated types.
     *
     * Assignment of a contained type. Must be one of the valid templated types.
     *
     * @param _value Value to assign
     */
    template<typename Type_>
    typename std::enable_if<!std::is_same<Type_, Variant<Types_...>>::value, Variant<Types_...>&>::type
    operator=(const Type_ &_value);

    /**
     * \brief Equality of another variant. Must have identical template list and content.
     *
     * Equality of another variant. Must have identical template list and content.
     *
     * @param _other Variant to compare
     */
    bool operator==(const Variant<Types_...> &_other) const;

    /**
      * \brief Not-Equality of another variant. Must have identical template list and content.
      *
      * Not-Equality of another variant. Must have identical template list and content.
      *
      * @param _other Variant to compare
      */
    bool operator!=(const Variant<Types_...> &_other) const;

    /**
      * \brief Testif the contained type is the same as the template on this method.
      *
      * Testif the contained type is the same as the template on this method.
      *
      * @return Is same type
      */
    template<typename Type_>
    bool isType() const;

    /**
     * \brief Construct variant with content type set to value.
     *
     * Construct variant with content type set to value.
     *
     * @param _value Value to place
     */
    template<typename Type_>
    Variant(const Type_ &_value,
            typename std::enable_if<!std::is_const<Type_>::value>::type* = 0,
            typename std::enable_if<!std::is_reference<Type_>::value>::type* = 0,
            typename std::enable_if<!std::is_same<Type_, Variant>::value>::type* = 0);

    /**
     * \brief Construct variant with content type set to value.
     *
     * Construct variant with content type set to value.
     *
     * @param _value Value to place
     */
    template<typename Type_>
    Variant(Type_ &&_value,
            typename std::enable_if<!std::is_const<Type_>::value>::type* = 0,
            typename std::enable_if<!std::is_reference<Type_>::value>::type* = 0,
            typename std::enable_if<!std::is_same<Type_, Variant>::value>::type* = 0);

    /**
     * \brief Get value of variant, template to content type. Throws exception if type is not contained.
     *
     * Get value of variant, template to content type. Throws exception if type is not contained.
     */
    template<typename Type_>
    const Type_ &get() const;

    /**
     * \brief Get index in template list of type actually contained, starting at 1 at the end of the template list
     *
     * Get index in template list of type actually contained, starting at 1 at the end of the template list
     *
     * @return Index of contained type
     */
    uint8_t getValueType() const {
        return valueType_;
    }

    uint8_t getMaxValueType() const {
        return uint8_t(TypesTupleSize::value);
    }

private:
    template<typename Type_>
    void set(const Type_ &_value, const bool clear);

    template<typename Type_>
    void set(Type_ &&_value, const bool clear);

    template<typename FriendType_>
    friend struct TypeWriter;
    template<typename ... FriendTypes_>
    friend struct AssignmentVisitor;
    template<typename FriendType_>
    friend struct TypeEqualsVisitor;
    template<typename ... FriendTypes_>
    friend struct PartialEqualsVisitor;
    template<class Derived_, typename ... FriendTypes_>
    friend struct InputStreamReadVisitor;
    template<class Variant_, typename ... FTypes_>
    friend struct ApplyVoidIndexVisitor;

public:
    inline bool hasValue() const {
        return (valueType_ <= TypesTupleSize::value);
    }
    typename std::aligned_storage<maxSize>::type valueStorage_;

    uint8_t valueType_;
};

template<class Variant_, typename... Types_>
struct ApplyVoidIndexVisitor;

template<class Variant_>
struct ApplyVoidIndexVisitor<Variant_> {
    static const uint8_t index = 0;

    static
    void visit(Variant_ &, uint8_t &) {
        assert(false);
    }
};

template<class Variant_, typename Type_, typename... Types_>
struct ApplyVoidIndexVisitor<Variant_, Type_, Types_...> {
    static const uint8_t index
        = ApplyVoidIndexVisitor<Variant_, Types_...>::index + 1;

    static void visit(Variant_ &_variant, uint8_t &_index) {
        if (index == _index) {
            new (&_variant.valueStorage_) Type_();
            _variant.valueType_ = index;
        } else {
            ApplyVoidIndexVisitor<
                Variant_, Types_...
            >::visit(_variant, _index);
        }
    }
};

template<class Visitor_, class Variant_, typename... Types_>
struct ApplyVoidVisitor;

template<class Visitor_, class Variant_>
struct ApplyVoidVisitor<Visitor_, Variant_> {
    static const uint8_t index = 0;

    static
    void visit(Visitor_ &, Variant_ &) {
        assert(false);
    }

    static
    void visit(Visitor_ &, const Variant_ &) {
        assert(false);
    }
};

template<class Visitor_, class Variant_, typename Type_, typename ... Types_>
struct ApplyVoidVisitor<Visitor_, Variant_, Type_, Types_...> {
    static const uint8_t index
        = ApplyVoidVisitor<Visitor_, Variant_, Types_...>::index + 1;

    static void visit(Visitor_ &_visitor, Variant_ &_variant) {
        if (_variant.getValueType() == index) {
            _visitor(_variant.template get<Type_>());
        } else {
            ApplyVoidVisitor<
                Visitor_, Variant_, Types_...
            >::visit(_visitor, _variant);
        }
    }

    static void visit(Visitor_ &_visitor, const Variant_ &_variant) {
        if (_variant.getValueType() == index) {
            _visitor(_variant.template get<Type_>());
        } else {
            ApplyVoidVisitor<
                Visitor_, Variant_, Types_...
            >::visit(_visitor, _variant);
        }
    }
};

template<class Visitor_, class Variant_, typename ... Types_>
struct ApplyBoolVisitor;

template<class Visitor_, class Variant_>
struct ApplyBoolVisitor<Visitor_, Variant_> {
    static const uint8_t index = 0;

    static bool visit(Visitor_ &, Variant_ &) {
        assert(false);
        return false;
    }
};

template<class Visitor_, class Variant_, typename Type_, typename ... Types_>
struct ApplyBoolVisitor<Visitor_, Variant_, Type_, Types_...> {
    static const uint8_t index
        = ApplyBoolVisitor<Visitor_, Variant_, Types_...>::index + 1;

    static bool visit(Visitor_ &_visitor, Variant_ &_variant) {
        if (_variant.getValueType() == index) {
            return _visitor(_variant.template get<Type_>());
        } else {
            return ApplyBoolVisitor<
                        Visitor_, Variant_, Types_...
                   >::visit(_visitor, _variant);
        }
    }
};

template<class Visitor_, class Variant_, class Deployment_, typename ... Types_>
struct ApplyStreamVisitor;

template<class Visitor_, class Variant_, class Deployment_>
struct ApplyStreamVisitor<Visitor_, Variant_, Deployment_> {
    static const uint8_t index = 0;

    static
    void visit(Visitor_ &, Variant_ &, const Deployment_ *) {
        assert(false);
    }

    static
    void visit(Visitor_ &, const Variant_ &, const Deployment_ *) {
        assert(false);
    }
};

template<class Visitor_, class Variant_, class Deployment_, typename Type_, typename ... Types_>
struct ApplyStreamVisitor<Visitor_, Variant_, Deployment_, Type_, Types_...> {
    static const uint8_t index
        = ApplyStreamVisitor<Visitor_, Variant_, Deployment_, Types_...>::index + 1;

    static void visit(Visitor_ &_visitor, Variant_ &_variant, const Deployment_ *_depl) {
        if (_variant.getValueType() == index) {
            _visitor(_variant.template get<Type_>(),
                     (_depl ? std::get<std::tuple_size<decltype(_depl->values_)>::value-index>(_depl->values_)
                            : nullptr));
        } else {
            ApplyStreamVisitor<
                Visitor_, Variant_, Deployment_, Types_...
            >::visit(_visitor, _variant, _depl);
        }
    }

    static void visit(Visitor_ &_visitor, const Variant_ &_variant, const Deployment_ *_depl) {
        if (_variant.getValueType() == index) {
            _visitor(_variant.template get<Type_>(),
                     (_depl ? std::get<std::tuple_size<decltype(_depl->values_)>::value-index>(_depl->values_)
                            : nullptr));
        } else {
            ApplyStreamVisitor<
                Visitor_, Variant_, Deployment_, Types_...
            >::visit(_visitor, _variant, _depl);
        }
    }
};

template<uint32_t Size_>
struct DeleteVisitor {
public:
    DeleteVisitor(typename std::aligned_storage<Size_>::type &_storage)
        : storage_(_storage) {
    }

    template<typename Type_>
    void operator()(const Type_ &) const {
        (reinterpret_cast<const Type_ *>(&storage_))->~Type_();
    }

private:
    typename std::aligned_storage<Size_>::type &storage_;
};

template<class Derived_>
struct OutputStreamWriteVisitor {
public:
    OutputStreamWriteVisitor(OutputStream<Derived_> &_output)
        : output_(_output) {
    }

    template<typename Type_, typename Deployment_ = EmptyDeployment>
    void operator()(const Type_ &_value, const Deployment_ *_depl = nullptr) const {
        Deployable<Type_, Deployment_> itsValue(_value, _depl);
        output_ << itsValue;
    }

private:
    OutputStream<Derived_> &output_;
};

template<class Derived_, typename ... Types_>
struct InputStreamReadVisitor {
public:
    InputStreamReadVisitor(InputStream<Derived_> &_input, Variant<Types_...> &_target)
        : input_(_input), target_(_target) {
    }

    template<typename Type_, typename Deployment_ = EmptyDeployment>
    void operator()(const Type_ &_value, const Deployment_ *_depl = nullptr) {
        (void)_value;
        Deployable<Type_, Deployment_> itsValue(_depl);
        input_ >> itsValue;
        target_.Variant<Types_...>::template set<Type_>(std::move(itsValue.getValue()), false);
    }

private:
    InputStream<Derived_> &input_;
    Variant<Types_...> &target_;
};

template<class Derived_>
struct TypeOutputStreamWriteVisitor {
public:
    TypeOutputStreamWriteVisitor(Derived_ &_output)
        : output_(_output) {
    }

    template<typename Type_, typename Deployment_ = EmptyDeployment>
    void operator()(const Type_ &_value, const Deployment_ *_depl = nullptr) const {
        Deployable<Type_, Deployment_> _itsValue(_value, _depl);
        output_ << _itsValue;
    }

private:
    Derived_ &output_;
};

template<typename Type_>
struct TypeEqualsVisitor
{
public:
    TypeEqualsVisitor(const Type_ &_me)
        : me_(_me) {
    }

    bool operator()(const Type_ &_other) const {
        return (me_ == _other);
    }

    template<typename OtherType_>
    bool operator()(const OtherType_ &) const {
        return false;
    }

private:
    const Type_& me_;
};

template<typename ... Types_>
struct PartialEqualsVisitor
{
public:
    PartialEqualsVisitor(const Variant<Types_...> &_me)
        : me_(_me) {
    }

    template<typename Type_>
    bool operator()(const Type_ &_other) const {
        TypeEqualsVisitor<Type_> visitor(_other);
        return ApplyBoolVisitor<
                    TypeEqualsVisitor<Type_>, const Variant<Types_...>, Types_...
               >::visit(visitor, me_);
    }

private:
    const Variant<Types_...> &me_;
};

template<typename ... Types_>
struct AssignmentVisitor {
public:
    AssignmentVisitor(Variant<Types_...> &_me, const bool _clear = true)
        : me_(_me), clear_(_clear) {
    }

    template<typename Type_>
    void operator()(const Type_ &_value) const {
        me_.Variant<Types_...>::template set<Type_>(_value, clear_);
    }

    template<typename Type_>
    void operator()(Type_ &_value) const {
        me_.Variant<Types_...>::template set<Type_>(_value, clear_);
    }

private:
    Variant<Types_...> &me_;
    const bool clear_;
};

template<typename... Types_>
struct TypeSelector;

template<typename Type_>
struct TypeSelector<Type_> {
};

template<typename Type_, typename... Types_>
struct TypeSelector<typename Type_::Literal, Type_, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<typename Type_::Literal &, Type_, Types_...> {
    typedef Type_ type;
};


template<typename Type_, typename... Types_>
struct TypeSelector<typename Type_::Literal, const Type_&, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<const typename Type_::Literal &, Type_, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_, Type_, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_, Type_ &, Types_...> {
    typedef Type_& type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_ &, Type_, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_, const Type_ &, Types_...> {
    typedef const Type_ &type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<const Type_&, Type_, Types_...> {
    typedef Type_ type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_*, const Type_*, Types_...> {
    typedef const Type_ *type;
};

template<typename Type_, typename... Types_>
struct TypeSelector<Type_ &, const Type_ &, Types_...> {
    typedef const Type_ &type;
};

template<typename U_, typename Type_, typename... Types_>
struct TypeSelector<U_, Type_, Types_...> {
    typedef typename TypeSelector<U_, Types_...>::type type;
};

template<typename... Types_>
struct TypeIndex;

template<>
struct TypeIndex<> {
    static const uint8_t index = 0;

    template<typename U_>
    static uint8_t get() {
        return 0;
    }
};

template<typename Type_, typename ... Types_>
struct TypeIndex<Type_, Types_...> {
    static const uint8_t index = TypeIndex<Types_...>::index + 1;

    template<typename U_>
    static uint8_t get(typename std::enable_if<std::is_same<Type_, U_>::value>::type* = 0) {
        return index;
    }

    template<typename U_>
    static uint8_t get(typename std::enable_if<!std::is_same<Type_, U_>::value>::type* = 0) {
        return TypeIndex<Types_...>::template get<U_>();
    }
};

template<typename... Types_>
Variant<Types_...>::Variant()
    : valueType_(TypesTupleSize::value) {
    ApplyVoidIndexVisitor<Variant<Types_...>, Types_...>::visit(*this, valueType_);
}

template<typename... Types_>
Variant<Types_...>::Variant(const Variant &_source) {
    AssignmentVisitor<Types_...> visitor(*this, false);
    ApplyVoidVisitor<
        AssignmentVisitor<Types_...> , Variant<Types_...>, Types_...
    >::visit(visitor, _source);
}

template<typename... Types_>
Variant<Types_...>::Variant(Variant &&_source)
{
    AssignmentVisitor<Types_...> visitor(*this, false);
    ApplyVoidVisitor<
        AssignmentVisitor<Types_...> , Variant<Types_...>, Types_...
    >::visit(visitor, _source);
}

template<typename... Types_>
Variant<Types_...>::~Variant() {
    if (hasValue()) {
        DeleteVisitor<maxSize> visitor(valueStorage_);
        ApplyVoidVisitor<
            DeleteVisitor<maxSize>, Variant<Types_...>, Types_...
        >::visit(visitor, *this);
    }
}

template<typename... Types_>
Variant<Types_...>& Variant<Types_...>::operator=(const Variant<Types_...> &_source) {
    AssignmentVisitor<Types_...> visitor(*this, hasValue());
    ApplyVoidVisitor<
        AssignmentVisitor<Types_...>, Variant<Types_...>, Types_...
    >::visit(visitor, _source);
    return *this;
}

template<typename... Types_>
Variant<Types_...>& Variant<Types_...>::operator=(Variant<Types_...> &&_source) {
    AssignmentVisitor<Types_...> visitor(*this, hasValue());
    ApplyVoidVisitor<
        AssignmentVisitor<Types_...>, Variant<Types_...>, Types_...
    >::visit(visitor, _source);
    return *this;
}

template<typename ... Types_>
template<typename Type_>
typename std::enable_if<!std::is_same<Type_, Variant<Types_...>>::value, Variant<Types_...>&>::type
Variant<Types_...>::operator=(const Type_ &_value) {
    set<typename TypeSelector<Type_, Types_...>::type>(_value, hasValue());
    return *this;
}

template<typename ... Types_>
template<typename Type_>
bool Variant<Types_...>::isType() const {
    typedef typename TypeSelector<Type_, Types_...>::type selected_type_t;
    uint8_t itsType = TypeIndex<Types_...>::template get<selected_type_t>();
    if (itsType == valueType_) {
        return true;
    } else {
        return false;
    }
}

template<typename ... Types_>
template<typename Type_>
Variant<Types_...>::Variant(const Type_ &_value,
                            typename std::enable_if<!std::is_const<Type_>::value>::type*,
                            typename std::enable_if<!std::is_reference<Type_>::value>::type*,
                            typename std::enable_if<!std::is_same<Type_, Variant<Types_...>>::value>::type*) {
    set<typename TypeSelector<Type_, Types_...>::type>(_value, false);
}

template<typename ... Types_>
template<typename Type_>
Variant<Types_...>::Variant(Type_ &&_value,
typename std::enable_if<!std::is_const<Type_>::value>::type*,
typename std::enable_if<!std::is_reference<Type_>::value>::type*,
typename std::enable_if<!std::is_same<Type_, Variant<Types_...>>::value>::type*) {
    set<typename TypeSelector<Type_, Types_...>::type>(std::move(_value), false);
}


template<typename ... Types_>
template<typename Type_>
const Type_ & Variant<Types_...>::get() const {
    typedef typename TypeSelector<Type_, Types_...>::type selected_type_t;
    uint8_t itsType = TypeIndex<Types_...>::template get<selected_type_t>();
    if (itsType == valueType_) {
        return *(reinterpret_cast<const Type_ *>(&valueStorage_));
    } else {
#ifdef __EXCEPTIONS
        std::bad_cast toThrow;
        throw toThrow;
#else
        printf("SerializableVariant.hpp:%i %s: Incorrect access to variant; attempting to get type not currently contained", __LINE__, __FUNCTION__);
        abort();
#endif
    }
}


template<typename ... Types_>
template<typename U_>
void Variant<Types_...>::set(const U_ &_value, const bool _clear) {
    typedef typename TypeSelector<U_, Types_...>::type selected_type_t;

    if (_clear) {
        DeleteVisitor<maxSize> visitor(valueStorage_);
        ApplyVoidVisitor<
            DeleteVisitor<maxSize>, Variant<Types_...>, Types_...
        >::visit(visitor, *this);
    }
    new (&valueStorage_) selected_type_t(std::move(_value));
    valueType_ = TypeIndex<Types_...>::template get<selected_type_t>();
}

template<typename ... Types_>
template<typename U_>
void Variant<Types_...>::set(U_ &&_value, const bool _clear) {
    typedef typename TypeSelector<U_, Types_...>::type selected_type_t;

    selected_type_t&& any_container_value = std::move(_value);
    if(_clear)
    {
        DeleteVisitor<maxSize> visitor(valueStorage_);
        ApplyVoidVisitor<
            DeleteVisitor<maxSize>, Variant<Types_...>, Types_...
        >::visit(visitor, *this);
    } else {
        new (&valueStorage_) selected_type_t(std::move(any_container_value));
    }

    valueType_ = TypeIndex<Types_...>::template get<selected_type_t>();
}

template<typename ... Types_>
bool Variant<Types_...>::operator==(const Variant<Types_...> &_other) const
{
    PartialEqualsVisitor<Types_...> visitor(*this);
    return ApplyBoolVisitor<
                PartialEqualsVisitor<Types_...>, const Variant<Types_...>, Types_...
           >::visit(visitor, _other);
}

template<typename ... Types_>
bool Variant<Types_...>::operator!=(const Variant<Types_...> &_other) const
{
    return !(*this == _other);
}

} // namespace CommonAPI

#endif // COMMONAPI_VARIANT_HPP_