X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=5109dbcc11811ac8297f71e3903a1eb8c4f36f36;hp=f2d2ee563f339bb8b64eb020b516c3792b1db08f;hb=641a71730a0135fe647f6a230e9704d8b677f2c5;hpb=ab956fd92f2110197c8e035b12dafa30a18b8ae1 diff --git a/source/core/variant.h b/source/core/variant.h index f2d2ee5..5109dbc 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,14 +1,8 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2008 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #ifndef MSP_CORE_VARIANT_H_ #define MSP_CORE_VARIANT_H_ #include +#include #include #include "meta.h" @@ -31,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 @@ -38,10 +34,20 @@ 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; @@ -49,7 +55,7 @@ private: public: Variant(): store(0) { } template - Variant(const T &v): store(new Store::Type>(v)) { } + Variant(const T &v): store(new Store::type>(v)) { } Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { } ~Variant() { delete store; } @@ -57,7 +63,7 @@ public: Variant &operator=(const T &v) { delete store; - store = new Store::Type>(v); + store = new Store::type>(v); return *this; } @@ -68,22 +74,45 @@ public: 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)!=0; } + 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(); }