Files

295 lines
9.2 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Exception.h
*
* @brief Header file for the base exception class.
*/
#if (!defined ACTIV_BASE_EXCEPTION_H)
#define ACTIV_BASE_EXCEPTION_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/ActivBase.h"
#include ACTIV_BOOST_INCLUDE(exception/all.hpp)
#include <iostream>
//! Construct an exception.
#define ACTIV_EXCEPTION(t, e) t(__FILE__, __FUNCTION__, __LINE__, e)
/**
* @brief Raise an exception.
*
* Throw an exception of type t with error code e.
* The file, function and line number throwing the exception are automatically generated.
*/
#define ACTIV_THROW(t, e) throw ACTIV_EXCEPTION(t, e)
//! Rethrow an exception.
#define ACTIV_RETHROW(e) \
do \
{ \
e.AddException(new (std::nothrow) ACTIV_EXCEPTION(Exception, 0)); \
throw; \
} \
while (false)
//! Throw exception if result of expression tests true using operator against statusCode
#define ACTIV_THROW_ERROR_IF(statusCode, operator, expression, exception) \
do \
{ \
if (statusCode operator expression) \
throw exception; \
} \
while (false)
//! Construct an exception with a message.
#define ACTIV_EXCEPTION_WITH_MESSAGE(t, e, m) t(__FILE__, __FUNCTION__, __LINE__, e, m)
/**
* @brief Raise an exception with a message.
*
* Throw an exception of type t with error code e and message m.
* The file, function and line number throwing the exception are automatically generated.
*/
#define ACTIV_THROW_WITH_MESSAGE(t, e, m) throw ACTIV_EXCEPTION_WITH_MESSAGE(t, e, m)
// ---------------------------------------------------------------------------------------------------------------------------------
//! Activ namespace.
namespace Activ
{
//! Base Activ exception class.
class Exception
{
public:
/**
* @brief Constructor.
*
* @param file the file which raised the exception.
* @param function the function which raised the exception.
* @param line the line number which raised the exception.
* @param errorCode the exception specific error code.
* @param message the exception message.
*/
ACTIV_BASE_API Exception(const char *file, const char *function, const uint_fast32_t line, const uint_fast32_t errorCode = 0,
const String &message = "");
/**
* @brief Copy constructor.
*
* @param rhs
*/
ACTIV_BASE_API Exception(const Exception &rhs);
//! Destructor.
virtual ~Exception();
/**
* @brief Convert the exception error code to a string.
*
* Classes derived from Exception are required to implement ToString() to convert
* their error codes to a readable string.
*
* @return the error code as a string.
*/
ACTIV_BASE_API virtual String ToString() const;
/**
* @brief Stream output method.
*
* ToStream() allows writing of the exception to an ostream object. The file, function, line,
* error code and error string will be written to the ostream.
*
* @param os the ostream to write the exception to.
*/
ACTIV_BASE_API virtual void ToStream(std::ostream &os) const;
/**
* @brief Attach another exception to the chain.
*
* @param pException the exception to add to the chain.
*/
ACTIV_BASE_API void AddException(Exception *pException) const;
/**
* @brief Set the message.
*
* @param message the new message.
*/
void SetMessage(const String &message);
/**
* @brief Get the message.
*
* @return The message.
*/
const String &GetMessage() const;
const char * const m_file; ///< The file that raised the exception.
const char * const m_function; ///< The function that raised the exception.
const uint_fast32_t m_line; ///< The line number that raised the exception.
const uint_fast32_t m_errorCode; ///< The exception specific error code.
mutable String m_message; ///< The optional message.
private:
mutable const Exception * m_pNextException; ///< Can only be a heap allocated exception.
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline Exception::~Exception()
{
// For some reason, Emscripten refuses to resolve this if in the .cpp at "exe" link time.
delete m_pNextException;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Exception::SetMessage(const String &message)
{
m_message = message;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const String &Exception::GetMessage() const
{
return m_message;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper function to retrieve the file name of @a filePath.
*
* @param filePath
*
* @return An empty string or the file name.
*/
ACTIV_BASE_API String GetExceptionFileInfo(const String &filePath);
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Global operator<< to dump an exception to an ostream.
*
* operator<< allows writing of the exception to an ostream object. The file, function, line,
* error code and error string will be written to the ostream.
*
* @param os the ostream to write the exception to.
* @param e the exception to write.
*
* @return the ostream @a os.
*/
ACTIV_BASE_API std::ostream &operator<<(std::ostream &os, const Activ::Exception &e);
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Helper function to retrieve location of throw from an exception.
*
* @param e an exception.
*
* @return An empty string if the location isn't available, otherwise a string of the form "in <file> at line <line>".
*/
template<typename T>
String GetExceptionThrowLocationInfo(const T &e)
{
const char * const *pFile = Boost::get_error_info<Boost::throw_file>(e);
const int * const pLine = Boost::get_error_info<Boost::throw_line>(e);
if (pFile && pLine)
return "thrown from line " + TypeToString(*pLine) + " of " + GetExceptionFileInfo(*pFile);
else
return "";
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Template function to wrap a function call that can throw in a try-catch; log the exception and return a given value.
*
* @param fn a function object to call (e.g. via CxxYZ::bind).
* @param exceptionReturn value to return if an exception is caught.
* @param pFunctionName name of the function for display purposes if an exception is caught.
*
* @return The result of the wrapped function, or @a exceptionReturn in an exception was caught.
*/
#if (ACTIV_CPLUSPLUS >= ACTIV_CPLUSPLUS_17)
template <typename T> typename std::invoke_result_t<T>
WrapThrowingFunction(const T &fn, const typename std::invoke_result_t<T> exceptionReturn, const char * const pFunctionName)
#else
template <typename T> typename T::result_type
WrapThrowingFunction(const T &fn, const typename T::result_type exceptionReturn, const char * const pFunctionName)
#endif
{
try
{
return fn();
}
catch (const Exception &e)
{
OutputStringStream ss;
ss << pFunctionName << " caught exception " << e;
LogMessage(LOG_TYPE_WARNING, ss.str());
}
catch (const std::exception &e)
{
LogMessage(LOG_TYPE_WARNING, (String(pFunctionName) + " caught exception " + e.what()));
}
ACTIV_CATCH_ALL()
{
LogMessage(LOG_TYPE_WARNING, (String(pFunctionName) + " caught unknown exception"));
}
return exceptionReturn;
}
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Template function to wrap a void function call that can throw in a try-catch and log the exception.
*
* @param fn a function object to call (e.g. via CxxYZ::bind).
* @param pFunctionName name of the function for display purposes if an exception is caught.
*
* @return true if no exception was thrown, else false.
*/
template<typename T>
bool WrapThrowingFunction(const T &fn, const char * const pFunctionName)
{
try
{
fn();
return true;
}
catch (const Exception &e)
{
OutputStringStream ss;
ss << pFunctionName << " caught exception " << e;
LogMessage(LOG_TYPE_WARNING, ss.str());
}
catch (const std::exception &e)
{
LogMessage(LOG_TYPE_WARNING, (String(pFunctionName) + " caught exception " + e.what()));
}
ACTIV_CATCH_ALL()
{
LogMessage(LOG_TYPE_WARNING, (String(pFunctionName) + " caught unknown exception"));
}
return false;
}
} // namespace Activ
#endif // (!defined ACTIV_BASE_EXCEPTION_H)