diff --git a/include/E57Exception.h b/include/E57Exception.h index c56570f..4bfd45b 100644 --- a/include/E57Exception.h +++ b/include/E57Exception.h @@ -35,6 +35,11 @@ #include "E57Export.h" +#ifndef E57_DEBUG +// Used to mark unused parameters to indicate intent and supress warnings. +#define UNUSED( expr ) (void)( expr ) +#endif + // C++14 does not support the [[deprecated]] attribute on enumerators. // Turn on enumerator deprecation notices if we are compiling with C++17 or later. #if ( ( defined( _MSVC_LANG ) && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L ) @@ -287,33 +292,94 @@ namespace e57 "Will be removed in 4.0. Use ErrorInvarianceViolation." ) = ErrorInvarianceViolation, }; - class E57_DLL E57Exception : public std::exception + namespace Utilities + { + E57_DLL std::string errorCodeToString( ErrorCode ecode ) noexcept; + } + + class E57Exception : public std::exception { public: - const char *what() const noexcept override; + const char *what() const noexcept override + { + return "E57 exception"; + } void report( const char *reportingFileName = nullptr, int reportingLineNumber = 0, const char *reportingFunctionName = nullptr, - std::ostream &os = std::cout ) const noexcept; + std::ostream &os = std::cout ) const noexcept + { + os << "**** Got an e57 exception: " << errorStr() << std::endl; + +#ifdef E57_DEBUG + os << " Debug info: " << std::endl; + os << " context: " << context_ << std::endl; + os << " sourceFunctionName: " << sourceFunctionName_ << std::endl; + if ( reportingFunctionName != nullptr ) + { + os << " reportingFunctionName: " << reportingFunctionName << std::endl; + } - ErrorCode errorCode() const noexcept; - std::string errorStr() const noexcept; + /*** Add a line in error message that a smart editor (gnu emacs) can + * interpret as a link to the source code: */ + os << sourceFileName_ << "(" << sourceLineNumber_ << ") : error C" << errorCode_ + << ": <--- occurred on" << std::endl; + if ( reportingFileName != nullptr ) + { + os << reportingFileName << "(" << reportingLineNumber + << ") : error C0: <--- reported on" << std::endl; + } +#else + UNUSED( reportingFileName ); + UNUSED( reportingLineNumber ); + UNUSED( reportingFunctionName ); +#endif + } - std::string context() const noexcept; + ErrorCode errorCode() const noexcept + { + return errorCode_; + } + + std::string errorStr() const noexcept + { + return Utilities::errorCodeToString( errorCode_ ); + } + + std::string context() const noexcept + { + return context_; + } // For debugging purposes: - const char *sourceFileName() const noexcept; - const char *sourceFunctionName() const noexcept; - int sourceLineNumber() const noexcept; + const char *sourceFileName() const noexcept + { + return sourceFileName_.c_str(); + } + + const char *sourceFunctionName() const noexcept + { + return sourceFunctionName_; + } - /// @cond documentNonPublic The following isn't part of the API, and isn't documented. + int sourceLineNumber() const noexcept + { + return sourceLineNumber_; + } + + /// @cond documentNonPublic The following isn't part of the API, and isn't documented. E57Exception() = delete; E57Exception( ErrorCode ecode, std::string context, const char *srcFileName = nullptr, - int srcLineNumber = 0, const char *srcFunctionName = nullptr ); + int srcLineNumber = 0, const char *srcFunctionName = nullptr ) : + errorCode_( ecode ), + context_( std::move( context ) ), sourceFileName_( srcFileName ), + sourceFunctionName_( srcFunctionName ), sourceLineNumber_( srcLineNumber ) + { + } /// @endcond private: - /// @cond documentNonPublic The following isn't part of the API, and isn't documented. + /// @cond documentNonPublic The following isn't part of the API, and isn't documented. ErrorCode errorCode_; std::string context_; std::string sourceFileName_; @@ -321,9 +387,4 @@ namespace e57 int sourceLineNumber_; /// @endcond }; - - namespace Utilities - { - E57_DLL std::string errorCodeToString( ErrorCode ecode ) noexcept; - } } diff --git a/src/E57Exception.cpp b/src/E57Exception.cpp index 33decb4..5c6738c 100644 --- a/src/E57Exception.cpp +++ b/src/E57Exception.cpp @@ -27,8 +27,6 @@ #include "E57Exception.h" -#include "Common.h" - namespace e57 { /*! @@ -96,17 +94,8 @@ namespace e57 Production code will likely have catch handlers for these exceptions as well. */ - /// @cond documentNonPublic The following isn't part of the API, and isn't documented. - E57Exception::E57Exception( ErrorCode ecode, std::string context, const char *srcFileName, - int srcLineNumber, const char *srcFunctionName ) : - errorCode_( ecode ), - context_( std::move( context ) ), sourceFileName_( srcFileName ), - sourceFunctionName_( srcFunctionName ), sourceLineNumber_( srcLineNumber ) - { - } - /// @endcond - /*! + @fn const char *E57Exception::what() @brief Get string description of exception category. @details @@ -118,12 +107,10 @@ namespace e57 @throw No E57Exceptions. */ - const char *E57Exception::what() const noexcept - { - return "E57 exception"; - } /*! + @fn void E57Exception::report( const char *reportingFileName, int reportingLineNumber, + const char *reportingFunctionName, std::ostream &os ) @brief Print error information on a given output stream. @param [in] reportingFileName Name of file where catch statement caught the exception. NULL if @@ -145,37 +132,9 @@ namespace e57 @see ErrorCode */ - void E57Exception::report( const char *reportingFileName, int reportingLineNumber, - const char *reportingFunctionName, std::ostream &os ) const noexcept - { - os << "**** Got an e57 exception: " << errorStr() << std::endl; - -#ifdef E57_DEBUG - os << " Debug info: " << std::endl; - os << " context: " << context_ << std::endl; - os << " sourceFunctionName: " << sourceFunctionName_ << std::endl; - if ( reportingFunctionName != nullptr ) - { - os << " reportingFunctionName: " << reportingFunctionName << std::endl; - } - - /*** Add a line in error message that a smart editor (gnu emacs) can - * interpret as a link to the source code: */ - os << sourceFileName_ << "(" << sourceLineNumber_ << ") : error C" << errorCode_ - << ": <--- occurred on" << std::endl; - if ( reportingFileName != nullptr ) - { - os << reportingFileName << "(" << reportingLineNumber << ") : error C0: <--- reported on" - << std::endl; - } -#else - UNUSED( reportingFileName ); - UNUSED( reportingLineNumber ); - UNUSED( reportingFunctionName ); -#endif - } /*! + @fn ErrorCode E57Exception::errorCode() @brief Get numeric ::ErrorCode associated with the exception. @post No visible state is modified. @@ -186,12 +145,9 @@ namespace e57 @see E57Exception::errorStr, Utilities::errorCodeToString, ErrorCode */ - ErrorCode E57Exception::errorCode() const noexcept - { - return errorCode_; - } /*! + @fn std::string E57Exception::errorStr() @brief Get error string associated with the exception. @post No visible state is modified. @@ -200,12 +156,9 @@ namespace e57 @throw No E57Exceptions. */ - std::string E57Exception::errorStr() const noexcept - { - return Utilities::errorCodeToString( errorCode_ ); - } /*! + @fn std::string E57Exception::context() @brief Get human-readable string that describes the context of the error. @details @@ -219,12 +172,9 @@ namespace e57 @throw No E57Exceptions. */ - std::string E57Exception::context() const noexcept - { - return context_; - } /*! + @fn const char *E57Exception::sourceFileName() @brief Get name of source file where exception occurred, for debugging. @details @@ -237,12 +187,9 @@ namespace e57 @throw No E57Exceptions. */ - const char *E57Exception::sourceFileName() const noexcept - { - return sourceFileName_.c_str(); - } /*! + @fn const char *E57Exception::sourceFunctionName() @brief Get name of function in source code where the error occurred , for debugging. @@ -256,12 +203,9 @@ namespace e57 @throw No E57Exceptions. */ - const char *E57Exception::sourceFunctionName() const noexcept - { - return sourceFunctionName_; - } /*! + @fn int E57Exception::sourceLineNumber() @brief Get line number in source code file where exception occurred, for debugging. @details @@ -274,10 +218,6 @@ namespace e57 @throw No E57Exceptions. */ - int E57Exception::sourceLineNumber() const noexcept - { - return sourceLineNumber_; - } //=====================================================================================