]> git.tdb.fi Git - libs/core.git/commitdiff
Add Variant class
authorMikko Rasa <tdb@tdb.fi>
Mon, 10 Mar 2008 00:52:03 +0000 (00:52 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 10 Mar 2008 00:52:03 +0000 (00:52 +0000)
Fix svn:keywords property on core/*

source/core/variant.h [new file with mode: 0644]

diff --git a/source/core/variant.h b/source/core/variant.h
new file mode 100644 (file)
index 0000000..b81bb50
--- /dev/null
@@ -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<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