]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/variant.h
Use nullptr instead of 0 for pointers
[libs/core.git] / source / core / variant.h
index 515350de6ae5543158e20b6e2c20d4ef786163fb..443befb481620b4242e178a4b377192179af1adc 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_CORE_VARIANT_H_
 
 #include <stdexcept>
+#include <type_traits>
 #include <typeinfo>
 #include "meta.h"
 
@@ -11,7 +12,7 @@ class type_mismatch: public std::runtime_error
 {
 public:
        type_mismatch(const std::type_info &, const std::type_info &);
-       ~type_mismatch() throw() { }
+       virtual ~type_mismatch() throw() = default;
 };
 
 
@@ -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>
@@ -35,37 +38,47 @@ private:
 
                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;
+       StoreBase *store = nullptr;
 
 public:
-       Variant(): store(0) { }
+       Variant() = default;
        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(const T &v): store(new Store<typename std::remove_cv<T>::type>(v)) { }
+       Variant(const Variant &v): store(v.store ? v.store->clone() : nullptr) { }
        ~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 std::remove_cv<T>::type>(v);
                return *this;
        }
 
        Variant &operator=(const Variant &v)
        {
                delete store;
-               store = (v.store ? v.store->clone() : 0);
+               store = (v.store ? v.store->clone() : nullptr);
                return *this;
        }
 
 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);
        }
 
+       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>(); }