From 269539741ce3e94b1e89260ed67b94b343e7d794 Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 29 Aug 2022 17:13:59 +0200 Subject: [PATCH 1/4] fix: move header in subdir --- README.md | 2 +- example.cpp | 2 +- exceptions.h => exceptions/exceptions.h | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename exceptions.h => exceptions/exceptions.h (100%) diff --git a/README.md b/README.md index 6aa3bb9..ecb8503 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Example #include #include -#include "exceptions.h" +#include // Use this macro to build up your hierarchy of exceptions EXCEPTION( Exception, Existential_Observation ); diff --git a/example.cpp b/example.cpp index 5b624c5..96d570e 100644 --- a/example.cpp +++ b/example.cpp @@ -5,7 +5,7 @@ #include #include -#include "exceptions.h" +#include "exceptions/exceptions.h" // Use this macro to build up your hierarchy of exceptions EXCEPTION( Exception, Existential_Observation ); diff --git a/exceptions.h b/exceptions/exceptions.h similarity index 100% rename from exceptions.h rename to exceptions/exceptions.h From 2f9d1b7a1fb27c0ff4f2dcb08d849197e0cc6503 Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 29 Aug 2022 17:26:25 +0200 Subject: [PATCH 2/4] fix const correctness --- exceptions/exceptions.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exceptions/exceptions.h b/exceptions/exceptions.h index e634df7..b54ddb6 100644 --- a/exceptions/exceptions.h +++ b/exceptions/exceptions.h @@ -59,14 +59,14 @@ public: Use the E_INFOS macro to raise the exception, for example : 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 ) : description(desc), function(func), file(f), line(l) {} //! The destructor is not allowed to throw exceptions virtual ~Exception() throw () {} //! The method to use for printing the complete description of the exception - std::string what() + std::string what() const { std::ostringstream msg; msg << description << " (<" << name << "> in " << function << " at " << file << ":" << line << ")"; From c78ce64273a290bd8c31a6b19fc5c4f1052344d6 Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 29 Aug 2022 18:13:23 +0200 Subject: [PATCH 3/4] fix string management - Adds the class name as a constructor parameter. --- exceptions/exceptions.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/exceptions/exceptions.h b/exceptions/exceptions.h index b54ddb6..021a989 100644 --- a/exceptions/exceptions.h +++ b/exceptions/exceptions.h @@ -19,7 +19,7 @@ EXCEPTION(Exception,Buddhist_Observation); 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" @@ -50,6 +50,9 @@ protected: //! Line where the exception has been raised int line; + //! Assembled message + std::string message; + public: //! Constructor of the exception /*! @@ -59,19 +62,21 @@ public: Use the E_INFOS macro to raise the exception, for example : throw( Exception( "Shit evolves", E_INFOS ); */ - Exception( const std::string & desc, const std::string & func, const std::string & f, const int l ) - : description(desc), function(func), file(f), line(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), name(n) + { + std::ostringstream msg; + msg << "<" << name << "> " << description << "\t\t" << function << " @ " << file << ":" << line << ""; + message = msg.str(); + } //! The destructor is not allowed to throw exceptions virtual ~Exception() throw () {} //! The method to use for printing the complete description of the exception - std::string what() const + const char* what() const noexcept { - std::ostringstream msg; - msg << description << " (<" << name << "> in " << function << " at " << file << ":" << line << ")"; - - return msg.str(); + return message.c_str(); } }; From 97b60f5cb93aa2f063a8708f9247bb38b6be281e Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 5 Sep 2022 18:53:25 +0200 Subject: [PATCH 4/4] fix reserved macro, reorder members, adds virtual destructor --- exceptions/exceptions.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/exceptions/exceptions.h b/exceptions/exceptions.h index 021a989..5596974 100644 --- a/exceptions/exceptions.h +++ b/exceptions/exceptions.h @@ -1,5 +1,6 @@ -#ifndef __EXCEPTIONS_H__ -#define __EXCEPTIONS_H__ +#pragma once +#ifndef EXCEPTIONS_H +#define EXCEPTIONS_H #include #include @@ -35,8 +36,6 @@ class Exception : public std::exception { protected: - //! Name of the current exception class - std::string name; //! Description of the exception std::string description; @@ -50,6 +49,9 @@ protected: //! Line where the exception has been raised int line; + //! Name of the current exception class + std::string name; + //! Assembled message std::string message; @@ -71,7 +73,7 @@ public: } //! The destructor is not allowed to throw exceptions - virtual ~Exception() throw () {} + virtual ~Exception() noexcept {} //! The method to use for printing the complete description of the exception const char* what() const noexcept @@ -80,5 +82,5 @@ public: } }; -#endif // __EXCEPTIONS_H__ +#endif // EXCEPTIONS_H