]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/variant.h
Add move semantics to Variant
[libs/core.git] / source / core / variant.h
index e00885c94210ef720c3d48c42769d99a7d3182f8..5cb1b931ca403eab55a0ef38e9be86b3afa3d0c5 100644 (file)
 #define MSP_CORE_VARIANT_H_
 
 #include <stdexcept>
+#include <type_traits>
 #include <typeinfo>
 #include "meta.h"
+#include "mspcore_api.h"
 
 namespace Msp {
 
-class type_mismatch: public std::runtime_error
+class MSPCORE_API type_mismatch: public std::runtime_error
 {
 public:
        type_mismatch(const std::type_info &, const std::type_info &);
-       ~type_mismatch() throw() { }
 };
 
 
-class Variant
+class MSPCORE_API 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;
+       struct Functions
+       {
+               const std::type_info &(*get_type)();
+               bool (*compare)(const char *, const char *);
+               void (*clone)(char *, const char *);
+               void (*move)(char *, char *);
+               void (*destroy)(char *);
        };
 
+private:
        template<typename T>
-       struct Store: public StoreBase
-       {
-               T data;
+       using EnableNotVariant = typename std::enable_if<!std::is_same<typename std::remove_cv<typename std::remove_reference<T>::type>::type, Variant>::value>::type;
 
-               Store(T d): data(d) { }
+       const Functions *funcs = nullptr;
+       alignas(void *) char storage[INTERNAL_SIZE];
 
-               virtual const std::type_info &type_id() const { return typeid(T); }
-               virtual StoreBase *clone() const { return new Store<T>(data); }
-       };
+public:
+       Variant() = default;
+       template<typename T, typename = EnableNotVariant<T>>
+       Variant(T &&v) { assign(std::forward<T>(v)); }
+       Variant(const Variant &v) { copy_from(v); }
+       Variant(Variant &&v) { move_from(std::move(v)); }
+       ~Variant() { clear(); }
+
+       template<typename T, typename = EnableNotVariant<T>>
+       Variant &operator=(T &&v) { assign(std::forward<T>(v)); return *this; }
 
-       StoreBase *store;
+       Variant &operator=(const Variant &v) { if(&v!=this) copy_from(v); return *this; }
+       Variant &operator=(Variant &&v) { if(&v!=this) move_from(std::move(v)); return *this; }
+
+       void clear();
+
+private:
+       template<typename T>
+       void assign(T &&);
+
+       void copy_from(const Variant &);
+       void move_from(Variant &&);
+
+       template<typename T>
+       T &get();
 
 public:
-       Variant(): store(0) { }
        template<typename T>
-       Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
-       Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
-       ~Variant() { delete store; }
+       T &value() { return get<T>(); }
 
        template<typename T>
-       Variant &operator=(const T &v)
-       {
-               delete store;
-               store = new Store<typename RemoveConst<T>::Type>(v);
-               return *this;
-       }
+       const T &value() const { return const_cast<Variant *>(this)->get<T>(); }
 
-       Variant &operator=(const Variant &v)
-       {
-               delete store;
-               store = (v.store ? v.store->clone() : 0);
-               return *this;
-       }
+       template<typename T>
+       bool has_type() const { return type_equals(funcs, get_functions<typename std::remove_cv<T>::type>()); }
+
+       bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); }
 
        template<typename T>
-       T &value() const
-       {
-               typedef typename RemoveConst<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->data;
-       }
+       DEPRECATED bool check_type() const { return has_type<T>(); }
+
+       DEPRECATED bool check_same_type(const Variant &v) const { return has_same_type(v); }
+
+       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>
-       bool check_type() const
-       {
-               return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
-       }
+       operator T() const { return value<T>(); }
+
+private:
+       static bool type_equals(const Functions *, const Functions *);
+
+       template<typename T>
+       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;
+
+       template<typename T>
+       static const Functions *get_functions();
+
+       template<typename T>
+       static const std::type_info &get_type() { return typeid(T); }
+
+       template<typename T>
+       static EnableSmall<T, void> create(char *s, T &&v)
+       { new(s) typename std::remove_reference<T>::type(std::forward<T>(v)); }
+
+       template<typename T>
+       static EnableLarge<T, void> create(char *s, T &&v)
+       { using V = typename std::remove_reference<T>::type; *reinterpret_cast<V **>(s) = new V(std::forward<T>(v)); }
+
+       template<typename T>
+       static typename std::enable_if<!IsEqualityComparable<T>::value, bool>::type compare(const char *, const char *)
+       { return false; }
+
+       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>
+       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>
-       operator T() const
-       { return value<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> move(char *s, char *v)
+       { new(s) T(std::move(*reinterpret_cast<T *>(v))); }
+
+       template<typename T>
+       static EnableLarge<T, void> move(char *s, char *v)
+       { T *&p = *reinterpret_cast<T **>(v); *reinterpret_cast<T **>(s) = p; p = nullptr; }
+
+       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); }
 };
 
+
+inline void Variant::clear()
+{
+       if(funcs)
+               funcs->destroy(storage);
+       funcs = nullptr;
+}
+
+template<typename T>
+inline void Variant::assign(T &&v)
+{
+       clear();
+       funcs = get_functions<typename std::remove_cv<typename std::remove_reference<T>::type>::type>();
+       create(storage, std::forward<T>(v));
+}
+
+inline void Variant::copy_from(const Variant &v)
+{
+       clear();
+       if((funcs = v.funcs))
+               funcs->clone(storage, v.storage);
+}
+
+inline void Variant::move_from(Variant &&v)
+{
+       clear();
+       if((funcs = v.funcs))
+               funcs->move(storage, v.storage);
+       v.clear();
+}
+
+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);
+}
+
+inline bool Variant::type_equals(const Functions *funcs1, const Functions *funcs2)
+{
+       if(!funcs1 || !funcs2)
+               return false;
+       else if(funcs1==funcs2)
+               return true;
+       else
+               return funcs1->get_type()==funcs2->get_type();
+}
+
+template<typename T>
+inline const Variant::Functions *Variant::get_functions()
+{
+       static Functions funcs =
+       {
+               &get_type<T>,
+               &compare<T>,
+               &clone<T>,
+               &move<T>,
+               &destroy<T>
+       };
+       return &funcs;
+}
+
 } // namespace Msp
 
 #endif