X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=eaf5a34e171ea3423018548a189f7fd8e250ba9a;hb=3bfc22d12b893d94cbb4697a77b7cababcbbd921;hp=b81bb506f300f8df2f215a63a427a7df38af7f7e;hpb=773a089db67a6293ac057e3160a43cceab8e5c1e;p=libs%2Fcore.git diff --git a/source/core/variant.h b/source/core/variant.h index b81bb50..eaf5a34 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,23 +1,31 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007 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 &); +}; + + class Variant { private: struct StoreBase { virtual ~StoreBase() { } + + 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 @@ -25,40 +33,88 @@ private: { T data; - Store(T d): data(d) { } + Store(const T &d): data(d) { } + + const std::type_info &type_id() const override { return typeid(T); } + StoreBase *clone() const override { return new Store(data); } + bool type_equals(const StoreBase &s) const override { return dynamic_cast *>(&s); } + bool value_equals(const StoreBase &s) const override { 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 = nullptr; public: - Variant(): store(0) { } + Variant() = default; template - Variant(T v): store(new Store(v)) { } + Variant(const T &v): store(new Store::type>(v)) { } + Variant(const Variant &v): store(v.store ? v.store->clone() : nullptr) { } ~Variant() { delete store; } template - Variant &operator=(T v) + Variant &operator=(const T &v) + { + delete store; + store = new Store::type>(v); + return *this; + } + + Variant &operator=(const Variant &v) { + if(&v==this) + return *this; + delete store; - store=new Store(v); + store = (v.store ? v.store->clone() : nullptr); return *this; } +private: template - T value() const + Store::type> *get_typed_store() const { - Store *s=dynamic_cast *>(store); + 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 *>(store)!=0; + return dynamic_cast::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 operator T() const { return value(); }