From: Mikko Rasa Date: Wed, 6 Aug 2008 16:29:57 +0000 (+0000) Subject: Add a "where" property to Exception class X-Git-Tag: 1.0~1 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7c5ced26f18d29bad4aa7fa14c6c8feba2e269ab;p=libs%2Fcore.git Add a "where" property to Exception class --- diff --git a/source/core/except.cpp b/source/core/except.cpp index ddd2cb5..0ae2e59 100644 --- a/source/core/except.cpp +++ b/source/core/except.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -16,24 +16,27 @@ using namespace std; namespace Msp { -Exception::Exception(const string &w_): - w(w_) +Exception::Exception(const string &w): + wot(w) { #ifdef WITH_EXCEPTION_BACKTRACE bt=Debug::Backtrace::create(); #endif } +Exception &Exception::at(const std::string &w) throw() +{ + wer=w; + wot=wer+": "+wot; + return *this; +} + + SystemError::SystemError(const string &w_, int e): Exception(build_what(w_, e)), err(e) { } -KeyError::KeyError(const string &w_, const string &k): - Exception(w_+" ("+k+")"), - key(k) -{ } - string SystemError::build_what(const string &w, int e) { ostringstream buf; @@ -50,4 +53,10 @@ string SystemError::build_what(const string &w, int e) return buf.str(); } + +KeyError::KeyError(const string &w_, const string &k): + Exception(w_+" ("+k+")"), + key(k) +{ } + } // namespace Msp diff --git a/source/core/except.h b/source/core/except.h index 5dfbe8d..fc6fcc6 100644 --- a/source/core/except.h +++ b/source/core/except.h @@ -1,9 +1,10 @@ /* $Id$ This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ + #ifndef MSP_CORE_ERROR_H_ #define MSP_CORE_ERROR_H_ @@ -22,10 +23,13 @@ public: Exception(const std::string &); ~Exception() throw() { } - const char *what() const throw() { return w.c_str(); } + const char *what() const throw() { return wot.c_str(); } + Exception &at(const std::string &) throw(); + const char *where() const throw() { return wer.c_str(); } const Debug::Backtrace &get_backtrace() const throw() { return bt; } private: - std::string w; + std::string wot; + std::string wer; Debug::Backtrace bt; }; @@ -88,6 +92,10 @@ private: static std::string build_what(const std::string &, int); }; +template +void throw_at(E e, const std::string &a) +{ e.at(a); throw e; } + } // namespace Msp #endif