fix string management

- Adds the class name as a constructor parameter.
This commit is contained in:
Johann Dreo 2022-08-29 18:13:23 +02:00
commit c78ce64273

View file

@ -19,7 +19,7 @@
EXCEPTION(Exception,Buddhist_Observation); EXCEPTION(Exception,Buddhist_Observation);
creates an new exception class "Buddhist_Observation", derived from the base "Exception" creates an new exception class "Buddhist_Observation", derived from the base "Exception"
*/ */
#define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1 ) : Super (desc,func,f,l) {name = #Current;} } #define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1, const std::string & n=#Current ) : Super (desc,func,f,l,n) {} }
/** A shortcut to throw an exception without having to type ",E_INFOS" /** A shortcut to throw an exception without having to type ",E_INFOS"
@ -50,6 +50,9 @@ protected:
//! Line where the exception has been raised //! Line where the exception has been raised
int line; int line;
//! Assembled message
std::string message;
public: public:
//! Constructor of the exception //! Constructor of the exception
/*! /*!
@ -59,19 +62,21 @@ public:
Use the E_INFOS macro to raise the exception, for example : Use the E_INFOS macro to raise the exception, for example :
throw( Exception( "Shit evolves", E_INFOS ); throw( Exception( "Shit evolves", E_INFOS );
*/ */
Exception( const std::string & desc, const std::string & func, const std::string & f, const int l ) Exception( const std::string & desc, const std::string & func, const std::string & f, const int l, const std::string & n = "Exception" )
: description(desc), function(func), file(f), line(l) {} : description(desc), function(func), file(f), line(l), name(n)
{
std::ostringstream msg;
msg << "<" << name << "> " << description << "\t\t" << function << " @ " << file << ":" << line << "";
message = msg.str();
}
//! The destructor is not allowed to throw exceptions //! The destructor is not allowed to throw exceptions
virtual ~Exception() throw () {} virtual ~Exception() throw () {}
//! The method to use for printing the complete description of the exception //! The method to use for printing the complete description of the exception
std::string what() const const char* what() const noexcept
{ {
std::ostringstream msg; return message.c_str();
msg << description << " (<" << name << "> in " << function << " at " << file << ":" << line << ")";
return msg.str();
} }
}; };