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() { clear(); }
44 Variant &operator=(const T &v) { assign(v); return *this; }
46 Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
52 void assign(const T &);
54 void copy_from(const Variant &);
61 T &value() { return get<T>(); }
64 const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
67 bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
69 bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
72 DEPRECATED bool check_type() const { return has_type<T>(); }
74 DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
76 bool operator==(const Variant &v) const { return (has_same_type(v) && funcs->compare(storage, v.storage)); }
77 bool operator!=(const Variant &v) const { return !(operator==(v)); }
80 operator T() const { return value<T>(); }
83 static bool type_equals(const Functions *, const Functions *);
86 static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
88 template<typename T, typename U>
89 using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
91 template<typename T, typename U>
92 using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
95 static const Functions *get_functions();
98 static const std::type_info &get_type() { return typeid(T); }
101 static EnableSmall<T, void> create(char *s, const T &v)
105 static EnableLarge<T, void> create(char *s, const T &v)
106 { *reinterpret_cast<T **>(s) = new T(v); }
109 static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
113 static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
114 { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
117 static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
118 { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
121 static EnableSmall<T, void> clone(char *s, const char *v)
122 { new(s) T(*reinterpret_cast<const T *>(v)); }
125 static EnableLarge<T, void> clone(char *s, const char *v)
126 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
129 static EnableSmall<T, void> destroy(char *s)
130 { reinterpret_cast<T *>(s)->~T(); }
133 static EnableLarge<T, void> destroy(char *s)
134 { delete *reinterpret_cast<T **>(s); }
138 inline void Variant::clear()
141 funcs->destroy(storage);
146 inline void Variant::assign(const T &v)
149 funcs = get_functions<typename std::remove_cv<T>::type>();
153 inline void Variant::copy_from(const Variant &v)
156 if((funcs = v.funcs))
157 funcs->clone(storage, v.storage);
161 inline T &Variant::get()
164 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
166 if(sizeof(T)<=INTERNAL_SIZE)
167 return *reinterpret_cast<T *>(storage);
169 return **reinterpret_cast<T **>(storage);
172 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
174 if(!funcs1 || !funcs2)
176 else if(funcs1==funcs2)
179 return funcs1->get_type()==funcs2->get_type();
183 inline const Variant::Functions *Variant::get_functions()
185 static Functions funcs =