]> git.tdb.fi Git - libs/core.git/blob - source/core/variant.h
Add decorations for things which are considered part of the API
[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 #include "mspcore_api.h"
9
10 namespace Msp {
11
12 class MSPCORE_API type_mismatch: public std::runtime_error
13 {
14 public:
15         type_mismatch(const std::type_info &, const std::type_info &);
16 };
17
18
19 class MSPCORE_API Variant
20 {
21 public:
22         static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *);
23
24         struct Functions
25         {
26                 const std::type_info &(*get_type)();
27                 bool (*compare)(const char *, const char *);
28                 void (*clone)(char *, const char *);
29                 void (*destroy)(char *);
30         };
31
32 private:
33         const Functions *funcs = nullptr;
34         alignas(void *) char storage[INTERNAL_SIZE];
35
36 public:
37         Variant() = default;
38         template<typename T>
39         Variant(const T &v) { assign(v); }
40         Variant(const Variant &v) { copy_from(v); }
41         ~Variant() { if(funcs) funcs->destroy(storage); }
42
43         template<typename T>
44         Variant &operator=(const T &v) { assign(v); return *this; }
45
46         Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
47
48 private:
49         template<typename T>
50         void assign(const T &);
51
52         void copy_from(const Variant &);
53
54         template<typename T>
55         T &get();
56
57 public:
58         template<typename T>
59         T &value() { return get<T>(); }
60
61         template<typename T>
62         const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
63
64         template<typename T>
65         bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
66
67         bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
68
69         template<typename T>
70         DEPRECATED bool check_type() const { return has_type<T>(); }
71
72         DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
73
74         bool operator==(const Variant &v) const { return (has_same_type(v) && funcs->compare(storage, v.storage)); }
75         bool operator!=(const Variant &v) const { return !(operator==(v)); }
76
77         template<typename T>
78         operator T() const { return value<T>(); }
79
80 private:
81         static bool type_equals(const Functions *, const Functions *);
82
83         template<typename T>
84         static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
85
86         template<typename T, typename U>
87         using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
88
89         template<typename T, typename U>
90         using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
91
92         template<typename T>
93         static const Functions *get_functions();
94
95         template<typename T>
96         static const std::type_info &get_type() { return typeid(T); }
97
98         template<typename T>
99         static EnableSmall<T, void> create(char *s, const T &v)
100         { new(s) T(v); }
101
102         template<typename T>
103         static EnableLarge<T, void> create(char *s, const T &v)
104         { *reinterpret_cast<T **>(s) = new T(v); }
105
106         template<typename T>
107         static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
108         { return false; }
109
110         template<typename T>
111         static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
112         { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
113
114         template<typename T>
115         static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
116         { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
117
118         template<typename T>
119         static EnableSmall<T, void> clone(char *s, const char *v)
120         { new(s) T(*reinterpret_cast<const T *>(v)); }
121
122         template<typename T>
123         static EnableLarge<T, void> clone(char *s, const char *v)
124         { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
125
126         template<typename T>
127         static EnableSmall<T, void> destroy(char *s)
128         { reinterpret_cast<T *>(s)->~T(); }
129
130         template<typename T>
131         static EnableLarge<T, void> destroy(char *s)
132         { delete *reinterpret_cast<T **>(s); }
133 };
134
135
136 template<typename T>
137 inline void Variant::assign(const T &v)
138 {
139         if(funcs)
140                 funcs->destroy(storage);
141
142         funcs = get_functions<typename std::remove_cv<T>::type>();
143         create(storage, v);
144 }
145
146 inline void Variant::copy_from(const Variant &v)
147 {
148         if(funcs)
149                 funcs->destroy(storage);
150
151         funcs = v.funcs;
152         if(funcs)
153                 funcs->clone(storage, v.storage);
154 }
155
156 template<typename T>
157 inline T &Variant::get()
158 {
159         if(!has_type<T>())
160                 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
161
162         if(sizeof(T)<=INTERNAL_SIZE)
163                 return *reinterpret_cast<T *>(storage);
164         else
165                 return **reinterpret_cast<T **>(storage);
166 }
167
168 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
169 {
170         if(!funcs1 || !funcs2)
171                 return false;
172         else if(funcs1==funcs2)
173                 return true;
174         else
175                 return funcs1->get_type()==funcs2->get_type();
176 }
177
178 template<typename T>
179 inline const Variant::Functions *Variant::get_functions()
180 {
181         static Functions funcs =
182         {
183                 &get_type<T>,
184                 &compare<T>,
185                 &clone<T>,
186                 &destroy<T>
187         };
188         return &funcs;
189 }
190
191 } // namespace Msp
192
193 #endif