]> git.tdb.fi Git - libs/core.git/commitdiff
Handle constness in Variant
authorMikko Rasa <tdb@tdb.fi>
Tue, 5 Aug 2008 06:33:56 +0000 (06:33 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 5 Aug 2008 06:33:56 +0000 (06:33 +0000)
Return a reference from Variant::value()
Add meta.h for metaprogramming stuff

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

diff --git a/source/core/meta.h b/source/core/meta.h
new file mode 100644 (file)
index 0000000..f12aede
--- /dev/null
@@ -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<typename T>
+struct RemoveConst
+{ typedef T Type; };
+
+template<typename T>
+struct RemoveConst<const T>
+{ typedef T Type; };
+
+} // namespace Msp
+
+#endif
index b81bb506f300f8df2f215a63a427a7df38af7f7e..6425fbfcbf7e4046d58105b445563f5e4d221acd 100644 (file)
@@ -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<typename T>
-       Variant(T v): store(new Store<T>(v)) { }
+       Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
        ~Variant() { delete store; }
 
        template<typename T>
-       Variant &operator=(v)
+       Variant &operator=(const T &v)
        {
                delete store;
-               store=new Store<T>(v);
+               store=new Store<typename RemoveConst<T>::Type>(v);
                return *this;
        }
 
        template<typename T>
-       T value() const
+       T &value() const
        {
-               Store<T> *s=dynamic_cast<Store<T> *>(store);
+               typedef typename RemoveConst<T>::Type NCT;
+               Store<NCT> *s=dynamic_cast<Store<NCT> *>(store);
                if(!s)
                        throw InvalidState("Type mismatch");
                return s->data;
@@ -56,7 +58,7 @@ public:
        template<typename T>
        bool check_type() const
        {
-               return dynamic_cast<Store<T> *>(store)!=0;
+               return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
        }
 
        template<typename T>