X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=8d6db9a3de116e81433ac0e6fb706a3748adba50;hb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd;hp=b81bb506f300f8df2f215a63a427a7df38af7f7e;hpb=773a089db67a6293ac057e3160a43cceab8e5c1e;p=libs%2Fcore.git diff --git a/source/core/variant.h b/source/core/variant.h index b81bb50..8d6db9a 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -9,6 +9,7 @@ Distributed under the LGPL #define MSP_CORE_VARIANT_H_ #include "except.h" +#include "meta.h" namespace Msp { @@ -18,6 +19,7 @@ private: struct StoreBase { virtual ~StoreBase() { } + virtual StoreBase *clone() const =0; }; template @@ -26,6 +28,7 @@ private: T data; Store(T d): data(d) { } + virtual StoreBase *clone() const { return new Store(data); } }; StoreBase *store; @@ -33,21 +36,30 @@ private: public: Variant(): store(0) { } template - Variant(T v): store(new Store(v)) { } + 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=(T v) + Variant &operator=(const T &v) { delete store; - store=new Store(v); + store = new Store::Type>(v); + return *this; + } + + Variant &operator=(const Variant &v) + { + delete store; + store = (v.store ? v.store->clone() : 0); return *this; } template - T value() const + T &value() const { - Store *s=dynamic_cast *>(store); + typedef typename RemoveConst::Type NCT; + Store *s = dynamic_cast *>(store); if(!s) throw InvalidState("Type mismatch"); return s->data; @@ -56,7 +68,7 @@ public: template bool check_type() const { - return dynamic_cast *>(store)!=0; + return dynamic_cast::Type> *>(store)!=0; } template