From: Mikko Rasa Date: Tue, 5 Aug 2008 06:33:56 +0000 (+0000) Subject: Handle constness in Variant X-Git-Tag: 1.0~2 X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=3553d02ef9cf66ae5ff16dd0b129815f144857a1 Handle constness in Variant Return a reference from Variant::value() Add meta.h for metaprogramming stuff --- diff --git a/source/core/meta.h b/source/core/meta.h new file mode 100644 index 0000000..f12aede --- /dev/null +++ b/source/core/meta.h @@ -0,0 +1,23 @@ +/* $Id$ + +This file is part of libmspcore +Copyright © 2008 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_CORE_META_H_ +#define MSP_CORE_META_H_ + +namespace Msp { + +template +struct RemoveConst +{ typedef T Type; }; + +template +struct RemoveConst +{ typedef T Type; }; + +} // namespace Msp + +#endif diff --git a/source/core/variant.h b/source/core/variant.h index b81bb50..6425fbf 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -9,6 +9,7 @@ Distributed under the LGPL #define MSP_CORE_VARIANT_H_ #include "except.h" +#include "meta.h" namespace Msp { @@ -33,21 +34,22 @@ private: public: Variant(): store(0) { } template - Variant(T v): store(new Store(v)) { } + Variant(const T &v): store(new Store::Type>(v)) { } ~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; } 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 +58,7 @@ public: template bool check_type() const { - return dynamic_cast *>(store)!=0; + return dynamic_cast::Type> *>(store)!=0; } template