1 #ifndef MSP_CORE_VARIANT_H_
2 #define MSP_CORE_VARIANT_H_
8 #include "mspcore_api.h"
12 class MSPCORE_API type_mismatch: public std::runtime_error
15 type_mismatch(const std::type_info &, const std::type_info &);
19 class MSPCORE_API Variant
22 static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *);
26 const std::type_info &(*get_type)();
27 bool (*compare)(const char *, const char *);
28 void (*clone)(char *, const char *);
29 void (*move)(char *, char *);
30 void (*destroy)(char *);
35 using EnableNotVariant = typename std::enable_if<!std::is_same<typename std::remove_cv<typename std::remove_reference<T>::type>::type, Variant>::value>::type;
37 const Functions *funcs = nullptr;
38 alignas(void *) char storage[INTERNAL_SIZE];
42 template<typename T, typename = EnableNotVariant<T>>
43 Variant(T &&v) { assign(std::forward<T>(v)); }
44 Variant(const Variant &v) { copy_from(v); }
45 Variant(Variant &&v) { move_from(std::move(v)); }
46 ~Variant() { clear(); }
48 template<typename T, typename = EnableNotVariant<T>>
49 Variant &operator=(T &&v) { assign(std::forward<T>(v)); return *this; }
51 Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
52 Variant &operator=(Variant &&v) { if(&v!=this) move_from(std::move(v)); return *this; }
60 void copy_from(const Variant &);
61 void move_from(Variant &&);
68 T &value() { return get<T>(); }
71 const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
74 bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
76 bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
79 DEPRECATED bool check_type() const { return has_type<T>(); }
81 DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
83 bool operator==(const Variant &v) const { return (has_same_type(v) && funcs->compare(storage, v.storage)); }
84 bool operator!=(const Variant &v) const { return !(operator==(v)); }
87 operator T() const { return value<T>(); }
90 static bool type_equals(const Functions *, const Functions *);
93 static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
95 template<typename T, typename U>
96 using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
98 template<typename T, typename U>
99 using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
102 static const Functions *get_functions();
105 static const std::type_info &get_type() { return typeid(T); }
108 static EnableSmall<T, void> create(char *s, T &&v)
109 { new(s) typename std::remove_reference<T>::type(std::forward<T>(v)); }
112 static EnableLarge<T, void> create(char *s, T &&v)
113 { using V = typename std::remove_reference<T>::type; *reinterpret_cast<V **>(s) = new V(std::forward<T>(v)); }
116 static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
120 static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
121 { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
124 static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
125 { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
128 static EnableSmall<T, void> clone(char *s, const char *v)
129 { new(s) T(*reinterpret_cast<const T *>(v)); }
132 static EnableLarge<T, void> clone(char *s, const char *v)
133 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
136 static EnableSmall<T, void> move(char *s, char *v)
137 { new(s) T(std::move(*reinterpret_cast<T *>(v))); }
140 static EnableLarge<T, void> move(char *s, char *v)
141 { T *&p = *reinterpret_cast<T **>(v); *reinterpret_cast<T **>(s) = p; p = nullptr; }
144 static EnableSmall<T, void> destroy(char *s)
145 { reinterpret_cast<T *>(s)->~T(); }
148 static EnableLarge<T, void> destroy(char *s)
149 { delete *reinterpret_cast<T **>(s); }
153 inline void Variant::clear()
156 funcs->destroy(storage);
161 inline void Variant::assign(T &&v)
164 funcs = get_functions<typename std::remove_cv<typename std::remove_reference<T>::type>::type>();
165 create(storage, std::forward<T>(v));
168 inline void Variant::copy_from(const Variant &v)
171 if((funcs = v.funcs))
172 funcs->clone(storage, v.storage);
175 inline void Variant::move_from(Variant &&v)
178 if((funcs = v.funcs))
179 funcs->move(storage, v.storage);
184 inline T &Variant::get()
187 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
189 if(sizeof(T)<=INTERNAL_SIZE)
190 return *reinterpret_cast<T *>(storage);
192 return **reinterpret_cast<T **>(storage);
195 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
197 if(!funcs1 || !funcs2)
199 else if(funcs1==funcs2)
202 return funcs1->get_type()==funcs2->get_type();
206 inline const Variant::Functions *Variant::get_functions()
208 static Functions funcs =