--- /dev/null
+/* $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<typename T>
+ struct Store: public StoreBase
+ {
+ T data;
+
+ Store(T d): data(d) { }
+ };
+
+ StoreBase *store;
+
+public:
+ Variant(): store(0) { }
+ template<typename T>
+ Variant(T v): store(new Store<T>(v)) { }
+ ~Variant() { delete store; }
+
+ template<typename T>
+ Variant &operator=(T v)
+ {
+ delete store;
+ store=new Store<T>(v);
+ return *this;
+ }
+
+ template<typename T>
+ T value() const
+ {
+ Store<T> *s=dynamic_cast<Store<T> *>(store);
+ if(!s)
+ throw InvalidState("Type mismatch");
+ return s->data;
+ }
+
+ template<typename T>
+ bool check_type() const
+ {
+ return dynamic_cast<Store<T> *>(store)!=0;
+ }
+
+ template<typename T>
+ operator T() const
+ { return value<T>(); }
+};
+
+} // namespace Msp
+
+#endif