]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/variant.h
Deprecate various meta-programming constructs in favor of std ones
[libs/core.git] / source / core / variant.h
index 4a0c89cf1a9a4b22661d28e62ba192a06004aabd..5109dbcc11811ac8297f71e3903a1eb8c4f36f36 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_CORE_VARIANT_H_
 
 #include <stdexcept>
+#include <type_traits>
 #include <typeinfo>
 #include "meta.h"
 
@@ -24,6 +25,8 @@ private:
 
                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>
@@ -31,10 +34,20 @@ private:
        {
                T data;
 
-               Store(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 std::enable_if<IsEqualityComparable<U>::value, bool>::type 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 std::enable_if<!IsEqualityComparable<U>::value, bool>::type value_equals_(const StoreBase &) const
+               { return false; }
        };
 
        StoreBase *store;
@@ -42,7 +55,7 @@ private:
 public:
        Variant(): store(0) { }
        template<typename T>
-       Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
+       Variant(const T &v): store(new Store<typename std::remove_cv<T>::type>(v)) { }
        Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
        ~Variant() { delete store; }
 
@@ -50,7 +63,7 @@ public:
        Variant &operator=(const T &v)
        {
                delete store;
-               store = new Store<typename RemoveConst<T>::Type>(v);
+               store = new Store<typename std::remove_cv<T>::type>(v);
                return *this;
        }
 
@@ -63,9 +76,9 @@ public:
 
 private:
        template<typename T>
-       Store<typename RemoveConst<T>::Type> *get_typed_store() const
+       Store<typename std::remove_cv<T>::type> *get_typed_store() const
        {
-               typedef typename RemoveConst<T>::Type NCT;
+               typedef typename std::remove_cv<T>::type NCT;
                Store<NCT> *s = dynamic_cast<Store<NCT> *>(store);
                if(!s)
                        throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void)));
@@ -88,9 +101,18 @@ public:
        template<typename T>
        bool check_type() const
        {
-               return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
+               return dynamic_cast<Store<typename std::remove_cv<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>(); }