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 (*destroy)(char *);
33 const Functions *funcs = nullptr;
34 alignas(void *) char storage[INTERNAL_SIZE];
39 Variant(const T &v) { assign(v); }
40 Variant(const Variant &v) { copy_from(v); }
41 ~Variant() { if(funcs) funcs->destroy(storage); }
44 Variant &operator=(const T &v) { assign(v); return *this; }
46 Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
50 void assign(const T &);
52 void copy_from(const Variant &);
59 T &value() { return get<T>(); }
62 const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
65 bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
67 bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
70 DEPRECATED bool check_type() const { return has_type<T>(); }
72 DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
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)); }
78 operator T() const { return value<T>(); }
81 static bool type_equals(const Functions *, const Functions *);
84 static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
86 template<typename T, typename U>
87 using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
89 template<typename T, typename U>
90 using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
93 static const Functions *get_functions();
96 static const std::type_info &get_type() { return typeid(T); }
99 static EnableSmall<T, void> create(char *s, const T &v)
103 static EnableLarge<T, void> create(char *s, const T &v)
104 { *reinterpret_cast<T **>(s) = new T(v); }
107 static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
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); }
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); }
119 static EnableSmall<T, void> clone(char *s, const char *v)
120 { new(s) T(*reinterpret_cast<const T *>(v)); }
123 static EnableLarge<T, void> clone(char *s, const char *v)
124 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
127 static EnableSmall<T, void> destroy(char *s)
128 { reinterpret_cast<T *>(s)->~T(); }
131 static EnableLarge<T, void> destroy(char *s)
132 { delete *reinterpret_cast<T **>(s); }
137 inline void Variant::assign(const T &v)
140 funcs->destroy(storage);
142 funcs = get_functions<typename std::remove_cv<T>::type>();
146 inline void Variant::copy_from(const Variant &v)
149 funcs->destroy(storage);
153 funcs->clone(storage, v.storage);
157 inline T &Variant::get()
160 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
162 if(sizeof(T)<=INTERNAL_SIZE)
163 return *reinterpret_cast<T *>(storage);
165 return **reinterpret_cast<T **>(storage);
168 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
170 if(!funcs1 || !funcs2)
172 else if(funcs1==funcs2)
175 return funcs1->get_type()==funcs2->get_type();
179 inline const Variant::Functions *Variant::get_functions()
181 static Functions funcs =