X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=af204f2233f3c7759665a0f6b729bd52459d4326;hp=2dd9732fc60dd4faf1a692eef1be63d9c83328cd;hb=HEAD;hpb=90fc7638aee23270fe005f86dd1b492d67015c13 diff --git a/source/core/variant.h b/source/core/variant.h index 2dd9732..5cb1b93 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -2,121 +2,220 @@ #define MSP_CORE_VARIANT_H_ #include +#include #include #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; - 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 (*move)(char *, char *); + void (*destroy)(char *); }; +private: template - struct Store: public StoreBase - { - T data; + using EnableNotVariant = typename std::enable_if::type>::type, Variant>::value>::type; - Store(const 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(data); } - virtual bool type_equals(const StoreBase &s) const { return dynamic_cast *>(&s); } - virtual bool value_equals(const StoreBase &s) const { return value_equals_(s); } +public: + Variant() = default; + template> + Variant(T &&v) { assign(std::forward(v)); } + Variant(const Variant &v) { copy_from(v); } + Variant(Variant &&v) { move_from(std::move(v)); } + ~Variant() { clear(); } - template - typename EnableIf::value, bool>::Yes value_equals_(const StoreBase &s) const - { const Store *t = dynamic_cast *>(&s); return (t && t->data==data); } + template> + Variant &operator=(T &&v) { assign(std::forward(v)); return *this; } - template - typename EnableIf::value, bool>::No value_equals_(const StoreBase &) const - { return false; } - }; + 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; } - StoreBase *store; + void clear(); + +private: + template + void assign(T &&); + + void copy_from(const Variant &); + void move_from(Variant &&); + + template + T &get(); public: - Variant(): store(0) { } template - Variant(const T &v): store(new Store::Type>(v)) { } - Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { } - ~Variant() { delete store; } + T &value() { return get(); } template - Variant &operator=(const T &v) - { - delete store; - store = new Store::Type>(v); - return *this; - } + const T &value() const { return const_cast(this)->get(); } - Variant &operator=(const Variant &v) - { - delete store; - store = (v.store ? v.store->clone() : 0); - return *this; - } + template + bool has_type() const { return type_equals(funcs, get_functions::type>()); } + + bool has_same_type(const Variant &v) const { return type_equals(funcs, v.funcs); } + + template + DEPRECATED bool check_type() const { return has_type(); } + + 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 + operator T() const { return value(); } private: + static bool type_equals(const Functions *, const Functions *); + template - Store::Type> *get_typed_store() const - { - typedef typename RemoveConst::Type NCT; - Store *s = dynamic_cast *>(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 + using EnableSmall = typename std::enable_if(), U>::type; + + template + using EnableLarge = typename std::enable_if(), U>::type; -public: template - T &value() - { - return get_typed_store()->data; - } + static const Functions *get_functions(); template - const T &value() const - { - return get_typed_store()->data; - } + static const std::type_info &get_type() { return typeid(T); } template - bool check_type() const - { - return dynamic_cast::Type> *>(store)!=0; - } + static EnableSmall create(char *s, T &&v) + { new(s) typename std::remove_reference::type(std::forward(v)); } - bool check_same_type(const Variant &v) const - { return store && v.store && store->type_equals(*v.store); } + template + static EnableLarge create(char *s, T &&v) + { using V = typename std::remove_reference::type; *reinterpret_cast(s) = new V(std::forward(v)); } + + template + static typename std::enable_if::value, bool>::type compare(const char *, const char *) + { return false; } + + template + static typename std::enable_if::value, EnableSmall>::type compare(const char *s1, const char *s2) + { return *reinterpret_cast(s1)==*reinterpret_cast(s2); } + + template + static typename std::enable_if::value, EnableLarge>::type compare(const char *s1, const char *s2) + { return **reinterpret_cast(s1)==**reinterpret_cast(s2); } + + template + static EnableSmall clone(char *s, const char *v) + { new(s) T(*reinterpret_cast(v)); } + + template + static EnableLarge clone(char *s, const char *v) + { *reinterpret_cast(s) = new T(**reinterpret_cast(v)); } + + template + static EnableSmall move(char *s, char *v) + { new(s) T(std::move(*reinterpret_cast(v))); } - bool operator==(const Variant &v) const - { return store && v.store && store->value_equals(*v.store); } + template + static EnableLarge move(char *s, char *v) + { T *&p = *reinterpret_cast(v); *reinterpret_cast(s) = p; p = nullptr; } - bool operator!=(const Variant &v) const - { return !(operator==(v)); } + template + static EnableSmall destroy(char *s) + { reinterpret_cast(s)->~T(); } template - operator T() const - { return value(); } + static EnableLarge destroy(char *s) + { delete *reinterpret_cast(s); } }; + +inline void Variant::clear() +{ + if(funcs) + funcs->destroy(storage); + funcs = nullptr; +} + +template +inline void Variant::assign(T &&v) +{ + clear(); + funcs = get_functions::type>::type>(); + create(storage, std::forward(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 +inline T &Variant::get() +{ + if(!has_type()) + throw type_mismatch(typeid(T), (funcs ? funcs->get_type() : typeid(void))); + + if(sizeof(T)<=INTERNAL_SIZE) + return *reinterpret_cast(storage); + else + return **reinterpret_cast(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 +inline const Variant::Functions *Variant::get_functions() +{ + static Functions funcs = + { + &get_type, + &compare, + &clone, + &move, + &destroy + }; + return &funcs; +} + } // namespace Msp #endif