X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=2e9caaf9b79440bacff303975a208321f6cdcd22;hb=5763dd6e8089c97699cbcbd221afb7fe1841bcdd;hp=8d6db9a3de116e81433ac0e6fb706a3748adba50;hpb=d5dd704b2576f878809e87dbb8ff8591b9bdbce4;p=libs%2Fcore.git diff --git a/source/core/variant.h b/source/core/variant.h index 8d6db9a..2e9caaf 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,25 +1,32 @@ -/* $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 +#include +#include #include "meta.h" namespace Msp { +class type_mismatch: public std::runtime_error +{ +public: + type_mismatch(const std::type_info &, const std::type_info &); + virtual ~type_mismatch() throw() = default; +}; + + 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 @@ -27,16 +34,28 @@ 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(data); } + virtual bool type_equals(const StoreBase &s) const { return dynamic_cast *>(&s); } + virtual bool value_equals(const StoreBase &s) const { return _value_equals(s); } + + template + typename std::enable_if::value, bool>::type _value_equals(const StoreBase &s) const + { const Store *t = dynamic_cast *>(&s); return (t && t->data==data); } + + template + typename std::enable_if::value, bool>::type _value_equals(const StoreBase &) const + { return false; } }; - StoreBase *store; + StoreBase *store = 0; public: - Variant(): store(0) { } + Variant() = default; template - Variant(const T &v): store(new Store::Type>(v)) { } + Variant(const T &v): store(new Store::type>(v)) { } Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { } ~Variant() { delete store; } @@ -44,7 +63,7 @@ public: Variant &operator=(const T &v) { delete store; - store = new Store::Type>(v); + store = new Store::type>(v); return *this; } @@ -55,22 +74,45 @@ public: return *this; } +private: template - T &value() const + Store::type> *get_typed_store() const { - typedef typename RemoveConst::Type NCT; + typedef typename std::remove_cv::type NCT; Store *s = dynamic_cast *>(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 + T &value() + { + return get_typed_store()->data; + } + + template + const T &value() const + { + return get_typed_store()->data; } template bool check_type() const { - return dynamic_cast::Type> *>(store)!=0; + return dynamic_cast::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 operator T() const { return value(); }