X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=af204f2233f3c7759665a0f6b729bd52459d4326;hp=6632831e9efa4557f777f36e03f30d416a853d0b;hb=HEAD;hpb=7db1e6d50594b47a32ecca5a349a4e8540f890c0 diff --git a/source/core/variant.h b/source/core/variant.h index 6632831..5cb1b93 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,81 +1,221 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_CORE_VARIANT_H_ #define MSP_CORE_VARIANT_H_ -#include "except.h" +#include +#include +#include #include "meta.h" +#include "mspcore_api.h" namespace Msp { -class Variant +class MSPCORE_API type_mismatch: public std::runtime_error { -private: - struct StoreBase +public: + type_mismatch(const std::type_info &, const std::type_info &); +}; + + +class MSPCORE_API Variant +{ +public: + static constexpr unsigned INTERNAL_SIZE = 2*sizeof(void *); + + struct Functions { - virtual ~StoreBase() { } - virtual StoreBase *clone() const =0; + 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(T d): data(d) { } - virtual StoreBase *clone() const { return new Store(data); } - }; + const Functions *funcs = nullptr; + alignas(void *) char storage[INTERNAL_SIZE]; + +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(); } - StoreBase *store; + template> + Variant &operator=(T &&v) { assign(std::forward(v)); return *this; } + + 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 + 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 - T &value() const - { - typedef typename RemoveConst::Type NCT; - Store *s=dynamic_cast *>(store); - if(!s) - throw InvalidState("Type mismatch"); - return s->data; - } + 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 - bool check_type() const - { - return dynamic_cast::Type> *>(store)!=0; - } + operator T() const { return value(); } + +private: + static bool type_equals(const Functions *, const Functions *); + + template + 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; + + template + static const Functions *get_functions(); template - operator T() const - { return value(); } + static const std::type_info &get_type() { return typeid(T); } + + template + static EnableSmall create(char *s, T &&v) + { new(s) typename std::remove_reference::type(std::forward(v)); } + + 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))); } + + template + static EnableLarge move(char *s, char *v) + { T *&p = *reinterpret_cast(v); *reinterpret_cast(s) = p; p = nullptr; } + + template + static EnableSmall destroy(char *s) + { reinterpret_cast(s)->~T(); } + + template + 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