From ab956fd92f2110197c8e035b12dafa30a18b8ae1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 25 May 2011 23:32:24 +0300 Subject: [PATCH] Improve error reporting in Variant --- source/core/variant.cpp | 13 +++++++++++++ source/core/variant.h | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 source/core/variant.cpp diff --git a/source/core/variant.cpp b/source/core/variant.cpp new file mode 100644 index 0000000..1cdc0a5 --- /dev/null +++ b/source/core/variant.cpp @@ -0,0 +1,13 @@ +#include +#include +#include "variant.h" + +using namespace std; + +namespace Msp { + +type_mismatch::type_mismatch(const type_info &e, const type_info &a): + runtime_error(format("expected: %s\nactual: %s", Debug::demangle(e.name()), Debug::demangle(a.name()))) +{ } + +} diff --git a/source/core/variant.h b/source/core/variant.h index 8d6db9a..f2d2ee5 100644 --- a/source/core/variant.h +++ b/source/core/variant.h @@ -8,18 +8,29 @@ 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 +39,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 +74,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; } -- 2.43.0