From c6613812b3ab99bfb67090d942da4cef2c2ca368 Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Mon, 12 Mar 2007 16:44:43 +0000 Subject: [PATCH] Changed AnyError to inherit from std::exception, required a change of the signature of AnyError::what() --- src/error.cpp | 21 ++++++++++----------- src/error.hpp | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/error.cpp b/src/error.cpp index 7c48a053..e22d8c89 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -94,34 +94,33 @@ namespace Exiv2 { return idx; } - std::string Error::what() const + void Error::setMsg() { int idx = errorIdx(code_); - std::string msg = std::string(_(errMsg_[idx].message_)); + msg_ = std::string(_(errMsg_[idx].message_)); std::string::size_type pos; - pos = msg.find("%0"); + pos = msg_.find("%0"); if (pos != std::string::npos) { - msg.replace(pos, 2, toString(code_)); + msg_.replace(pos, 2, toString(code_)); } if (count_ > 0) { - pos = msg.find("%1"); + pos = msg_.find("%1"); if (pos != std::string::npos) { - msg.replace(pos, 2, arg1_); + msg_.replace(pos, 2, arg1_); } } if (count_ > 1) { - pos = msg.find("%2"); + pos = msg_.find("%2"); if (pos != std::string::npos) { - msg.replace(pos, 2, arg2_); + msg_.replace(pos, 2, arg2_); } } if (count_ > 2) { - pos = msg.find("%3"); + pos = msg_.find("%3"); if (pos != std::string::npos) { - msg.replace(pos, 2, arg3_); + msg_.replace(pos, 2, arg3_); } } - return msg; } } // namespace Exiv2 diff --git a/src/error.hpp b/src/error.hpp index 6e5178de..3218fea4 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -35,6 +35,7 @@ #include "types.hpp" // + standard includes +#include #include #include @@ -52,20 +53,23 @@ namespace Exiv2 { : code_(code), message_(message) { } - int code_; //!< Error code + int code_; //!< Error code const char* message_; //!< Error message }; /*! @brief Error class interface. Allows the definition and use of a hierarchy of error classes which can all be handled in one catch block. + Inherits from the standard exception base-class, to make life + easier for library users (they have the option of catching most + things via std::exception). */ - class AnyError { + class AnyError : public std::exception { public: //! @name Creators //@{ //! Virtual destructor. - virtual ~AnyError() + virtual ~AnyError() throw() { } //@} @@ -73,14 +77,7 @@ namespace Exiv2 { //! @name Accessors //@{ //! Return the error code. - virtual int code() const =0; - /*! - @brief Return the error message. Consider using the output operator - operator<<(std::ostream &os, const AnyError& error) instead. - @note Unlike std::exception::what(), this function returns an - std::string. - */ - virtual std::string what() const =0; + virtual int code() const throw() =0; }; // AnyError //! %AnyBase output operator @@ -101,12 +98,14 @@ namespace Exiv2 { explicit Error(int code) : code_(code), count_(0) { + setMsg(); } //! Constructor taking an error code and one argument template Error(int code, const A& arg1) : code_(code), count_(1), arg1_(toString(arg1)) { + setMsg(); } //! Constructor taking an error code and two arguments template @@ -114,23 +113,38 @@ namespace Exiv2 { : code_(code), count_(2), arg1_(toString(arg1)), arg2_(toString(arg2)) { + setMsg(); } //! Constructor taking an error code and three arguments template Error(int code, const A& arg1, const B& arg2, const C& arg3) : code_(code), count_(3), arg1_(toString(arg1)), arg2_(toString(arg2)), arg3_(toString(arg3)) + { + setMsg(); + } + //! Virtual destructor. + virtual ~Error() throw() { } //@} //! @name Accessors //@{ - virtual int code() const { return code_; } - virtual std::string what() const; + virtual int code() const throw() { return code_; } + /*! + @brief Return the error message. The pointer returned by what() + is valid only as long as the Error object exists. + */ + virtual const char* what() const throw() { return msg_.c_str(); } //@} private: + //! @name Manipulators + //@{ + void setMsg(); + //@} + static int errorIdx(int code); // DATA @@ -139,6 +153,7 @@ namespace Exiv2 { std::string arg1_; //!< First argument std::string arg2_; //!< Second argument std::string arg3_; //!< Third argument + std::string msg_; //!< Complete error message static const ErrMsg errMsg_[]; //!< List of error messages }; // class Error