]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/variant.h
Add equality comparison for Variant
[libs/core.git] / source / core / variant.h
index 8d6db9a3de116e81433ac0e6fb706a3748adba50..2dd9732fc60dd4faf1a692eef1be63d9c83328cd 100644 (file)
@@ -1,25 +1,31 @@
-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_CORE_VARIANT_H_
 #define MSP_CORE_VARIANT_H_
 
-#include "except.h"
+#include <stdexcept>
+#include <typeinfo>
 #include "meta.h"
 
 namespace Msp {
 
+class type_mismatch: public std::runtime_error
+{
+public:
+       type_mismatch(const std::type_info &, const std::type_info &);
+       ~type_mismatch() throw() { }
+};
+
+
 class Variant
 {
 private:
        struct StoreBase
        {
                virtual ~StoreBase() { }
-               virtual StoreBase *clone() const =0;
+
+               virtual const std::type_info &type_id() const = 0;
+               virtual StoreBase *clone() const = 0;
+               virtual bool type_equals(const StoreBase &) const = 0;
+               virtual bool value_equals(const StoreBase &) const = 0;
        };
 
        template<typename T>
@@ -27,8 +33,20 @@ private:
        {
                T data;
 
-               Store(T d): data(d) { }
+               Store(const T &d): data(d) { }
+
+               virtual const std::type_info &type_id() const { return typeid(T); }
                virtual StoreBase *clone() const { return new Store<T>(data); }
+               virtual bool type_equals(const StoreBase &s) const { return dynamic_cast<const Store<T> *>(&s); }
+               virtual bool value_equals(const StoreBase &s) const { return value_equals_<T>(s); }
+
+               template<typename U>
+               typename EnableIf<IsEqualityComparable<U>::value, bool>::Yes value_equals_(const StoreBase &s) const
+               { const Store<T> *t = dynamic_cast<const Store<T> *>(&s); return (t && t->data==data); }
+
+               template<typename U>
+               typename EnableIf<IsEqualityComparable<U>::value, bool>::No value_equals_(const StoreBase &) const
+               { return false; }
        };
 
        StoreBase *store;
@@ -55,14 +73,28 @@ public:
                return *this;
        }
 
+private:
        template<typename T>
-       T &value() const
+       Store<typename RemoveConst<T>::Type> *get_typed_store() const
        {
                typedef typename RemoveConst<T>::Type NCT;
                Store<NCT> *s = dynamic_cast<Store<NCT> *>(store);
                if(!s)
-                       throw InvalidState("Type mismatch");
-               return s->data;
+                       throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void)));
+               return s;
+       }
+
+public:
+       template<typename T>
+       T &value()
+       {
+               return get_typed_store<T>()->data;
+       }
+
+       template<typename T>
+       const T &value() const
+       {
+               return get_typed_store<T>()->data;
        }
 
        template<typename T>
@@ -71,6 +103,15 @@ public:
                return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
        }
 
+       bool check_same_type(const Variant &v) const
+       { return store && v.store && store->type_equals(*v.store); }
+
+       bool operator==(const Variant &v) const
+       { return store && v.store && store->value_equals(*v.store); }
+
+       bool operator!=(const Variant &v) const
+       { return !(operator==(v)); }
+
        template<typename T>
        operator T() const
        { return value<T>(); }