Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline E57Exception #215

Merged
merged 1 commit into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 78 additions & 17 deletions include/E57Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -287,43 +292,99 @@ 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_;
const char *sourceFunctionName_;
int sourceLineNumber_;
/// @endcond
};

namespace Utilities
{
E57_DLL std::string errorCodeToString( ErrorCode ecode ) noexcept;
}
}
78 changes: 9 additions & 69 deletions src/E57Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include "E57Exception.h"

#include "Common.h"

namespace e57
{
/*!
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.

Expand All @@ -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
Expand All @@ -274,10 +218,6 @@ namespace e57

@throw No E57Exceptions.
*/
int E57Exception::sourceLineNumber() const noexcept
{
return sourceLineNumber_;
}

//=====================================================================================

Expand Down