class Variant
{
-private:
- struct StoreBase
- {
- virtual ~StoreBase() { }
+public:
+ static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *);
- virtual const std::type_info &type_id() const = 0;
- virtual StoreBase *clone() const = 0;
- virtual bool type_equals(const StoreBase &) const = 0;
- virtual bool value_equals(const StoreBase &) const = 0;
+ struct Functions
+ {
+ const std::type_info &(*get_type)();
+ bool (*compare)(const char *, const char *);
+ void (*clone)(char *, const char *);
+ void (*destroy)(char *);
};
+private:
+ const Functions *funcs = nullptr;
+ alignas(void *) char storage[INTERNAL_SIZE];
+
+public:
+ Variant() = default;
template<typename T>
- struct Store: public StoreBase
- {
- T data;
+ Variant(const T &v) { assign(v); }
+ Variant(const Variant &v) { copy_from(v); }
+ ~Variant() { if(funcs) funcs->destroy(storage); }
- Store(const T &d): data(d) { }
+ template<typename T>
+ Variant &operator=(const T &v) { assign(v); return *this; }
- const std::type_info &type_id() const override { return typeid(T); }
- StoreBase *clone() const override { return new Store<T>(data); }
- bool type_equals(const StoreBase &s) const override { return dynamic_cast<const Store<T> *>(&s); }
- bool value_equals(const StoreBase &s) const override { return _value_equals<T>(s); }
+ Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
- template<typename U>
- typename std::enable_if<IsEqualityComparable<U>::value, bool>::type _value_equals(const StoreBase &s) const
- { const Store<T> *t = dynamic_cast<const Store<T> *>(&s); return (t && t->data==data); }
+private:
+ template<typename T>
+ void assign(const T &);
- template<typename U>
- typename std::enable_if<!IsEqualityComparable<U>::value, bool>::type _value_equals(const StoreBase &) const
- { return false; }
- };
+ void copy_from(const Variant &);
- StoreBase *store = nullptr;
+ template<typename T>
+ T &get();
public:
- Variant() = default;
template<typename T>
- Variant(const T &v): store(new Store<typename std::remove_cv<T>::type>(v)) { }
- Variant(const Variant &v): store(v.store ? v.store->clone() : nullptr) { }
- ~Variant() { delete store; }
+ T &value() { return get<T>(); }
template<typename T>
- Variant &operator=(const T &v)
- {
- delete store;
- store = new Store<typename std::remove_cv<T>::type>(v);
- return *this;
- }
+ const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
- Variant &operator=(const Variant &v)
- {
- if(&v==this)
- return *this;
+ template<typename T>
+ bool check_type() const { return funcs==get_functions<typename std::remove_cv<T>::type>(); }
- delete store;
- store = (v.store ? v.store->clone() : nullptr);
- return *this;
- }
+ bool check_same_type(const Variant &v) const { return (funcs && funcs==v.funcs); }
+
+ bool operator==(const Variant &v) const { return (has_same_type(v) && funcs->compare(storage, v.storage)); }
+ bool operator!=(const Variant &v) const { return !(operator==(v)); }
+
+ template<typename T>
+ operator T() const { return value<T>(); }
private:
template<typename T>
- Store<typename std::remove_cv<T>::type> *get_typed_store() const
- {
- typedef typename std::remove_cv<T>::type NCT;
- Store<NCT> *s = dynamic_cast<Store<NCT> *>(store);
- if(!s)
- throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void)));
- return s;
- }
+ static constexpr bool is_small() { return (sizeof(T)<=INTERNAL_SIZE && alignof(T)<=alignof(void *)); }
+
+ template<typename T, typename U>
+ using EnableSmall = typename std::enable_if<is_small<T>(), U>::type;
+
+ template<typename T, typename U>
+ using EnableLarge = typename std::enable_if<!is_small<T>(), U>::type;
-public:
template<typename T>
- T &value()
- {
- return get_typed_store<T>()->data;
- }
+ static const Functions *get_functions();
template<typename T>
- const T &value() const
- {
- return get_typed_store<T>()->data;
- }
+ static const std::type_info &get_type() { return typeid(T); }
template<typename T>
- bool check_type() const
- {
- return dynamic_cast<Store<typename std::remove_cv<T>::type> *>(store);
- }
+ static EnableSmall<T, void> create(char *s, const T &v)
+ { new(s) T(v); }
- bool check_same_type(const Variant &v) const
- { return store && v.store && store->type_equals(*v.store); }
+ template<typename T>
+ static EnableLarge<T, void> create(char *s, const T &v)
+ { *reinterpret_cast<T **>(s) = new T(v); }
- bool operator==(const Variant &v) const
- { return store && v.store && store->value_equals(*v.store); }
+ template<typename T>
+ static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
+ { return false; }
- bool operator!=(const Variant &v) const
- { return !(operator==(v)); }
+ template<typename T>
+ static typename std::enable_if<IsEqualityComparable<T>::value, EnableSmall<T, bool>>::type compare(const char *s1, const char *s2)
+ { return *reinterpret_cast<const T *>(s1)==*reinterpret_cast<const T *>(s2); }
template<typename T>
- operator T() const
- { return value<T>(); }
+ static typename std::enable_if<IsEqualityComparable<T>::value, EnableLarge<T, bool>>::type compare(const char *s1, const char *s2)
+ { return **reinterpret_cast<const T *const *>(s1)==**reinterpret_cast<const T *const *>(s2); }
+
+ template<typename T>
+ static EnableSmall<T, void> clone(char *s, const char *v)
+ { new(s) T(*reinterpret_cast<const T *>(v)); }
+
+ template<typename T>
+ static EnableLarge<T, void> clone(char *s, const char *v)
+ { *reinterpret_cast<T **>(s) = new T(**reinterpret_cast<const T *const *>(v)); }
+
+ template<typename T>
+ static EnableSmall<T, void> destroy(char *s)
+ { reinterpret_cast<T *>(s)->~T(); }
+
+ template<typename T>
+ static EnableLarge<T, void> destroy(char *s)
+ { delete *reinterpret_cast<T **>(s); }
};
+
+template<typename T>
+inline void Variant::assign(const T &value)
+{
+ if(funcs)
+ funcs->destroy(storage);
+
+ funcs = get_functions<typename std::remove_cv<T>::type>();
+ create(storage, value);
+}
+
+inline void Variant::copy_from(const Variant &v)
+{
+ if(funcs)
+ funcs->destroy(storage);
+
+ funcs = v.funcs;
+ if(funcs)
+ funcs->clone(storage, v.storage);
+}
+
+template<typename T>
+inline T &Variant::get()
+{
+ if(!has_type<T>())
+ throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void)));
+
+ if(sizeof(T)<=INTERNAL_SIZE)
+ return *reinterpret_cast<T *>(storage);
+ else
+ return **reinterpret_cast<T **>(storage);
+}
+
+template<typename T>
+inline const Variant::Functions *Variant::get_functions()
+{
+ static Functions funcs =
+ {
+ &get_type<T>,
+ &compare<T>,
+ &clone<T>,
+ &destroy<T>
+ };
+ return &funcs;
+}
+
} // namespace Msp
#endif