1 #ifndef MSP_CORE_VARIANT_H_
2 #define MSP_CORE_VARIANT_H_
11 class type_mismatch: public std::runtime_error
14 type_mismatch(const std::type_info &, const std::type_info &);
21 static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *);
25 const std::type_info &(*get_type)();
26 bool (*compare)(const char *, const char *);
27 void (*clone)(char *, const char *);
28 void (*destroy)(char *);
32 const Functions *funcs = nullptr;
33 alignas(void *) char storage[INTERNAL_SIZE];
38 Variant(const T &v) { assign(v); }
39 Variant(const Variant &v) { copy_from(v); }
40 ~Variant() { if(funcs) funcs->destroy(storage); }
43 Variant &operator=(const T &v) { assign(v); return *this; }
45 Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
49 void assign(const T &);
51 void copy_from(const Variant &);
58 T &value() { return get<T>(); }
61 const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
64 bool has_type() const { return funcs==get_functions<typename std::remove_cv<T>::type>(); }
66 bool has_same_type(const Variant &v) const { return (funcs && funcs==v.funcs); }
69 DEPRECATED bool check_type() const { return has_type<T>(); }
71 DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
73 bool operator==(const Variant &v) const { return (has_same_type(v) && funcs->compare(storage, v.storage)); }
74 bool operator!=(const Variant &v) const { return !(operator==(v)); }
77 operator T() const { return value<T>(); }
81 static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
83 template<typename T, typename U>
84 using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
86 template<typename T, typename U>
87 using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
90 static const Functions *get_functions();
93 static const std::type_info &get_type() { return typeid(T); }
96 static EnableSmall<T, void> create(char *s, const T &v)
100 static EnableLarge<T, void> create(char *s, const T &v)
101 { *reinterpret_cast<T **>(s) = new T(v); }
104 static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
108 static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
109 { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
112 static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
113 { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
116 static EnableSmall<T, void> clone(char *s, const char *v)
117 { new(s) T(*reinterpret_cast<const T *>(v)); }
120 static EnableLarge<T, void> clone(char *s, const char *v)
121 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
124 static EnableSmall<T, void> destroy(char *s)
125 { reinterpret_cast<T *>(s)->~T(); }
128 static EnableLarge<T, void> destroy(char *s)
129 { delete *reinterpret_cast<T **>(s); }
134 inline void Variant::assign(const T &value)
137 funcs->destroy(storage);
139 funcs = get_functions<typename std::remove_cv<T>::type>();
140 create(storage, value);
143 inline void Variant::copy_from(const Variant &v)
146 funcs->destroy(storage);
150 funcs->clone(storage, v.storage);
154 inline T &Variant::get()
157 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
159 if(sizeof(T)<=INTERNAL_SIZE)
160 return *reinterpret_cast<T *>(storage);
162 return **reinterpret_cast<T **>(storage);
166 inline const Variant::Functions *Variant::get_functions()
168 static Functions funcs =