From: Mikko Rasa Date: Mon, 10 Mar 2008 00:52:03 +0000 (+0000) Subject: Add Variant class X-Git-Tag: 1.0~9 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=773a089db67a6293ac057e3160a43cceab8e5c1e;p=libs%2Fcore.git Add Variant class Fix svn:keywords property on core/* --- diff --git a/source/core/variant.h b/source/core/variant.h new file mode 100644 index 0000000..b81bb50 --- /dev/null +++ b/source/core/variant.h @@ -0,0 +1,69 @@ +/* $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" + +namespace Msp { + +class Variant +{ +private: + struct StoreBase + { + virtual ~StoreBase() { } + }; + + template + struct Store: public StoreBase + { + T data; + + Store(T d): data(d) { } + }; + + StoreBase *store; + +public: + Variant(): store(0) { } + template + Variant(T v): store(new Store(v)) { } + ~Variant() { delete store; } + + template + Variant &operator=(T v) + { + delete store; + store=new Store(v); + return *this; + } + + template + T value() const + { + Store *s=dynamic_cast *>(store); + if(!s) + throw InvalidState("Type mismatch"); + return s->data; + } + + template + bool check_type() const + { + return dynamic_cast *>(store)!=0; + } + + template + operator T() const + { return value(); } +}; + +} // namespace Msp + +#endif