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 type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
66 bool has_same_type(const Variant &v) const { return type_equals(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>(); }
80 static bool type_equals(const Functions *, const Functions *);
83 static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
85 template<typename T, typename U>
86 using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
88 template<typename T, typename U>
89 using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
92 static const Functions *get_functions();
95 static const std::type_info &get_type() { return typeid(T); }
98 static EnableSmall<T, void> create(char *s, const T &v)
102 static EnableLarge<T, void> create(char *s, const T &v)
103 { *reinterpret_cast<T **>(s) = new T(v); }
106 static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
110 static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
111 { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
114 static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
115 { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
118 static EnableSmall<T, void> clone(char *s, const char *v)
119 { new(s) T(*reinterpret_cast<const T *>(v)); }
122 static EnableLarge<T, void> clone(char *s, const char *v)
123 { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
126 static EnableSmall<T, void> destroy(char *s)
127 { reinterpret_cast<T *>(s)->~T(); }
130 static EnableLarge<T, void> destroy(char *s)
131 { delete *reinterpret_cast<T **>(s); }
136 inline void Variant::assign(const T &v)
139 funcs->destroy(storage);
141 funcs = get_functions<typename std::remove_cv<T>::type>();
145 inline void Variant::copy_from(const Variant &v)
148 funcs->destroy(storage);
152 funcs->clone(storage, v.storage);
156 inline T &Variant::get()
159 throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
161 if(sizeof(T)<=INTERNAL_SIZE)
162 return *reinterpret_cast<T *>(storage);
164 return **reinterpret_cast<T **>(storage);
167 inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
169 if(!funcs1 || !funcs2)
171 else if(funcs1==funcs2)
174 return funcs1->get_type()==funcs2->get_type();
178 inline const Variant::Functions *Variant::get_functions()
180 static Functions funcs =