From 773a089db67a6293ac057e3160a43cceab8e5c1e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 10 Mar 2008 00:52:03 +0000 Subject: [PATCH] Add Variant class Fix svn:keywords property on core/* --- source/core/variant.h | 69 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 source/core/variant.h 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 -- 2.43.0