]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/variant.h
Style updates
[libs/core.git] / source / core / variant.h
index 6425fbfcbf7e4046d58105b445563f5e4d221acd..8d6db9a3de116e81433ac0e6fb706a3748adba50 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspcore
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -19,6 +19,7 @@ private:
        struct StoreBase
        {
                virtual ~StoreBase() { }
+               virtual StoreBase *clone() const =0;
        };
 
        template<typename T>
@@ -27,6 +28,7 @@ private:
                T data;
 
                Store(T d): data(d) { }
+               virtual StoreBase *clone() const { return new Store<T>(data); }
        };
 
        StoreBase *store;
@@ -35,13 +37,21 @@ public:
        Variant(): store(0) { }
        template<typename T>
        Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
+       Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
        ~Variant() { delete store; }
 
        template<typename T>
        Variant &operator=(const T &v)
        {
                delete store;
-               store=new Store<typename RemoveConst<T>::Type>(v);
+               store = new Store<typename RemoveConst<T>::Type>(v);
+               return *this;
+       }
+
+       Variant &operator=(const Variant &v)
+       {
+               delete store;
+               store = (v.store ? v.store->clone() : 0);
                return *this;
        }
 
@@ -49,7 +59,7 @@ public:
        T &value() const
        {
                typedef typename RemoveConst<T>::Type NCT;
-               Store<NCT> *s=dynamic_cast<Store<NCT> *>(store);
+               Store<NCT> *s = dynamic_cast<Store<NCT> *>(store);
                if(!s)
                        throw InvalidState("Type mismatch");
                return s->data;