X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=2dd9732fc60dd4faf1a692eef1be63d9c83328cd;hp=6425fbfcbf7e4046d58105b445563f5e4d221acd;hb=90fc7638aee23270fe005f86dd1b492d67015c13;hpb=3553d02ef9cf66ae5ff16dd0b129815f144857a1 diff --git a/source/core/variant.h b/source/core/variant.h index 6425fbf..2dd9732 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,24 +1,31 @@ -/* $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 "meta.h" namespace Msp { +class type_mismatch: public std::runtime_error +{ +public: + type_mismatch(const std::type_info &, const std::type_info &); + ~type_mismatch() throw() { } +}; + + class Variant { private: struct StoreBase { virtual ~StoreBase() { } + + 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 @@ -26,7 +33,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 EnableIf::value, bool>::Yes value_equals_(const StoreBase &s) const + { const Store *t = dynamic_cast *>(&s); return (t && t->data==data); } + + template + typename EnableIf::value, bool>::No value_equals_(const StoreBase &) const + { return false; } }; StoreBase *store; @@ -35,24 +55,46 @@ 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; } 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); + return *this; + } + +private: template - T &value() const + Store::Type> *get_typed_store() const { typedef typename RemoveConst::Type NCT; - Store *s=dynamic_cast *>(store); + Store *s = dynamic_cast *>(store); if(!s) - throw InvalidState("Type mismatch"); - return s->data; + throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void))); + return s; + } + +public: + template + T &value() + { + return get_typed_store()->data; + } + + template + const T &value() const + { + return get_typed_store()->data; } template @@ -61,6 +103,15 @@ public: 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(); }