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 *);
34 template<typename T, bool small = (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *))>
38 using EnableNotVariant = typename std::enable_if<!std::is_same<typename std::remove_cv<typename std::remove_reference<T>::type>::type, Variant>::value>::type;
40 const Functions *funcs = nullptr;
41 alignas(void *) char storage[INTERNAL_SIZE];
45 template<typename T, typename = EnableNotVariant<T>>
46 Variant(T &&v) { assign(std::forward<T>(v)); }
47 Variant(const Variant &v) { copy_from(v); }
48 Variant(Variant &&v) { move_from(std::move(v)); }
49 ~Variant() { clear(); }
51 template<typename T, typename = EnableNotVariant<T>>
52 Variant &operator=(T &&v) { assign(std::forward<T>(v)); return *this; }
54 Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
55 Variant &operator=(Variant &&v) { if(&v!=this) move_from(std::move(v)); return *this; }
58 bool has_value() const { return funcs; }
64 void copy_from(const Variant &);
65 void move_from(Variant &&);
72 T &value() { return get<T>(); }
75 const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
78 bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
80 bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
83 DEPRECATED bool check_type() const { return has_type<T>(); }
85 DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
87 bool operator==(const Variant &v) const;
88 bool operator!=(const Variant &v) const { return !(operator==(v)); }
91 operator T() const { return value<T>(); }
94 static bool type_equals(const Functions *, const Functions *);
97 static const Functions *get_functions();
100 static const std::type_info &get_type() { return typeid(T); }
102 /* The extra function parameter is needed to avoid duplicate definition error
103 from MSVC 19.29 (VS 2019) */
104 template<typename T, typename = typename std::enable_if<!IsEqualityComparable<T>::value>::type>
105 static bool compare(const T &, const T &, int = 0)
108 template<typename T, typename = typename std::enable_if<IsEqualityComparable<T>::value>::type>
109 static bool compare(const T &v1, const T &v2)
115 struct Variant::FunctionsImpl<T, true>
117 static void create(char *s, T &&v)
118 { new(s) typename std::remove_reference<T>::type(std::forward<T>(v)); }
120 static bool compare(const char *s1, const char *s2)
121 { return Variant::compare<T>(*reinterpret_cast<const T *>(s1), *reinterpret_cast<const T *>(s2)); }
123 static void clone(char *s, const char *v)
124 { new(s) T(*reinterpret_cast<const T *>(v)); }
126 static void move(char *s, char *v)
127 { new(s) T(std::move(*reinterpret_cast<T *>(v))); }
129 static void destroy(char *s)
130 { reinterpret_cast<T *>(s)->~T(); }
134 struct Variant::FunctionsImpl<T, false>
136 static void create(char *s, T &&v)
137 { using V = typename std::remove_reference<T>::type; *reinterpret_cast<V **>(s) = new V(std::forward<T>(v)); }
139 static bool compare(const char *s1, const char *s2)
140 { return Variant::compare<T>(**reinterpret_cast<const T *const *>(s1), **reinterpret_cast<const T *const *>(s2)); }
142 static void clone(char *s, const char *v)
143 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
145 static void move(char *s, char *v)
146 { T *&p = *reinterpret_cast<T **>(v); *reinterpret_cast<T **>(s) = p; p = nullptr; }
148 static 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 FunctionsImpl<T>::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::operator==(const Variant &other) const
197 if(!funcs && !other.funcs)
199 else if(has_same_type(other))
200 return funcs->compare(storage, other.storage);
205 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
207 if(!funcs1 || !funcs2)
209 else if(funcs1==funcs2)
212 return funcs1->get_type()==funcs2->get_type();
216 inline const Variant::Functions *Variant::get_functions()
218 using Impl = FunctionsImpl<T>;
219 static Functions funcs =