]> git.tdb.fi Git - libs/core.git/blob - source/core/variant.h
Deprecate various meta-programming constructs in favor of std ones
[libs/core.git] / source / core / variant.h
1 #ifndef MSP_CORE_VARIANT_H_
2 #define MSP_CORE_VARIANT_H_
3
4 #include <stdexcept>
5 #include <type_traits>
6 #include <typeinfo>
7 #include "meta.h"
8
9 namespace Msp {
10
11 class type_mismatch: public std::runtime_error
12 {
13 public:
14         type_mismatch(const std::type_info &, const std::type_info &);
15         ~type_mismatch() throw() { }
16 };
17
18
19 class Variant
20 {
21 private:
22         struct StoreBase
23         {
24                 virtual ~StoreBase() { }
25
26                 virtual const std::type_info &type_id() const = 0;
27                 virtual StoreBase *clone() const = 0;
28                 virtual bool type_equals(const StoreBase &) const = 0;
29                 virtual bool value_equals(const StoreBase &) const = 0;
30         };
31
32         template<typename T>
33         struct Store: public StoreBase
34         {
35                 T data;
36
37                 Store(const T &d): data(d) { }
38
39                 virtual const std::type_info &type_id() const { return typeid(T); }
40                 virtual StoreBase *clone() const { return new Store<T>(data); }
41                 virtual bool type_equals(const StoreBase &s) const { return dynamic_cast<const Store<T> *>(&s); }
42                 virtual bool value_equals(const StoreBase &s) const { return value_equals_<T>(s); }
43
44                 template<typename U>
45                 typename std::enable_if<IsEqualityComparable<U>::value, bool>::type value_equals_(const StoreBase &s) const
46                 { const Store<T> *t = dynamic_cast<const Store<T> *>(&s); return (t && t->data==data); }
47
48                 template<typename U>
49                 typename std::enable_if<!IsEqualityComparable<U>::value, bool>::type value_equals_(const StoreBase &) const
50                 { return false; }
51         };
52
53         StoreBase *store;
54
55 public:
56         Variant(): store(0) { }
57         template<typename T>
58         Variant(const T &v): store(new Store<typename std::remove_cv<T>::type>(v)) { }
59         Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
60         ~Variant() { delete store; }
61
62         template<typename T>
63         Variant &operator=(const T &v)
64         {
65                 delete store;
66                 store = new Store<typename std::remove_cv<T>::type>(v);
67                 return *this;
68         }
69
70         Variant &operator=(const Variant &v)
71         {
72                 delete store;
73                 store = (v.store ? v.store->clone() : 0);
74                 return *this;
75         }
76
77 private:
78         template<typename T>
79         Store<typename std::remove_cv<T>::type> *get_typed_store() const
80         {
81                 typedef typename std::remove_cv<T>::type NCT;
82                 Store<NCT> *s = dynamic_cast<Store<NCT> *>(store);
83                 if(!s)
84                         throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void)));
85                 return s;
86         }
87
88 public:
89         template<typename T>
90         T &value()
91         {
92                 return get_typed_store<T>()->data;
93         }
94
95         template<typename T>
96         const T &value() const
97         {
98                 return get_typed_store<T>()->data;
99         }
100
101         template<typename T>
102         bool check_type() const
103         {
104                 return dynamic_cast<Store<typename std::remove_cv<T>::type> *>(store)!=0;
105         }
106
107         bool check_same_type(const Variant &v) const
108         { return store && v.store && store->type_equals(*v.store); }
109
110         bool operator==(const Variant &v) const
111         { return store && v.store && store->value_equals(*v.store); }
112
113         bool operator!=(const Variant &v) const
114         { return !(operator==(v)); }
115
116         template<typename T>
117         operator T() const
118         { return value<T>(); }
119 };
120
121 } // namespace Msp
122
123 #endif