X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=443befb481620b4242e178a4b377192179af1adc;hp=e00885c94210ef720c3d48c42769d99a7d3182f8;hb=41363aed34382386f915f17c1a961750b4fdcb14;hpb=967785734be5c3fc6f75da122c2d93ebbb338271 diff --git a/source/core/variant.h b/source/core/variant.h index e00885c..443befb 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -2,6 +2,7 @@ #define MSP_CORE_VARIANT_H_ #include +#include #include #include "meta.h" @@ -11,7 +12,7 @@ class type_mismatch: public std::runtime_error { public: type_mismatch(const std::type_info &, const std::type_info &); - ~type_mismatch() throw() { } + virtual ~type_mismatch() throw() = default; }; @@ -24,6 +25,8 @@ private: 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; }; template @@ -31,52 +34,85 @@ private: { T data; - Store(T d): data(d) { } + Store(const T &d): data(d) { } 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); } + + template + typename std::enable_if::value, bool>::type _value_equals(const StoreBase &s) const + { const Store *t = dynamic_cast *>(&s); return (t && t->data==data); } + + template + typename std::enable_if::value, bool>::type _value_equals(const StoreBase &) const + { return false; } }; - StoreBase *store; + StoreBase *store = nullptr; public: - Variant(): store(0) { } + Variant() = default; template - Variant(const T &v): store(new Store::Type>(v)) { } - Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { } + Variant(const T &v): store(new Store::type>(v)) { } + Variant(const Variant &v): store(v.store ? v.store->clone() : nullptr) { } ~Variant() { delete store; } template Variant &operator=(const T &v) { delete store; - store = new Store::Type>(v); + store = new Store::type>(v); return *this; } Variant &operator=(const Variant &v) { delete store; - store = (v.store ? v.store->clone() : 0); + store = (v.store ? v.store->clone() : nullptr); return *this; } +private: template - T &value() const + Store::type> *get_typed_store() const { - typedef typename RemoveConst::Type NCT; + typedef typename std::remove_cv::type NCT; Store *s = dynamic_cast *>(store); if(!s) throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void))); - return s->data; + return s; + } + +public: + template + T &value() + { + return get_typed_store()->data; + } + + template + const T &value() const + { + return get_typed_store()->data; } template bool check_type() const { - return dynamic_cast::Type> *>(store)!=0; + return dynamic_cast::type> *>(store); } + bool check_same_type(const Variant &v) const + { return store && v.store && store->type_equals(*v.store); } + + bool operator==(const Variant &v) const + { return store && v.store && store->value_equals(*v.store); } + + bool operator!=(const Variant &v) const + { return !(operator==(v)); } + template operator T() const { return value(); }