X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvariant.h;h=e00885c94210ef720c3d48c42769d99a7d3182f8;hb=154d5f43f7a6633a4937499039fc1b1b713e0c0c;hp=8d6db9a3de116e81433ac0e6fb706a3748adba50;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd;p=libs%2Fcore.git diff --git a/source/core/variant.h b/source/core/variant.h index 8d6db9a..e00885c 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -1,25 +1,29 @@ -/* $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 "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; }; template @@ -28,6 +32,8 @@ private: T data; Store(T d): data(d) { } + + virtual const std::type_info &type_id() const { return typeid(T); } virtual StoreBase *clone() const { return new Store(data); } }; @@ -61,7 +67,7 @@ public: typedef typename RemoveConst::Type NCT; Store *s = dynamic_cast *>(store); if(!s) - throw InvalidState("Type mismatch"); + throw type_mismatch(typeid(T), (store ? store->type_id() : typeid(void))); return s->data; }