Workspace 6.21.5
Classes | Namespaces | Macros | Functions
errorchecks.h File Reference
#include <functional>
#include <stdexcept>
#include <QFileInfo>
#include <QString>
#include "Workspace/api_workspace.h"
#include <csignal>
Include dependency graph for errorchecks.h:
This graph shows which files directly or indirectly include this file:

Classes

class  Assert
 

Namespaces

namespace  CSIRO
 Top level namespace for all Workspace code.
 

Macros

#define WS_ASSERT_FAIL(msg)
 
#define WS_ASSERT_LOGIC(cond)
 
#define WS_ASSERT_LOGIC_MSG(cond, msg)
 
#define WS_ASSERT_RUNTIME(cond)
 
#define WS_ASSERT_RUNTIME_MSG(cond, msg)
 
#define WS_HALT()   std::raise(SIGTRAP)
 
#define WS_VERIFY_RUNTIME(cond)
 
#define WS_VERIFY_RUNTIME_MSG(cond, msg)
 

Functions

template<typename T >
std::enable_if_t< std::is_convertible< T, bool >::value > validateExpression ()
 
template<typename T >
std::enable_if_t< std::is_convertible< T, QString >::value > validateString ()
 

Macro Definition Documentation

◆ WS_ASSERT_FAIL

#define WS_ASSERT_FAIL (   msg)
Value:
do \
{ \
if (CSIRO::Assert::reportFailure(CSIRO::Assert::Logic, nullptr, __FILE__, __LINE__, (msg)) == CSIRO::Assert::Halt) \
{ \
WS_HALT(); \
} \
} while (0)
@ Halt
Definition: errorchecks.h:48
static FailBehaviour reportFailure(FailType type, const char *condition, const char *file, int line, const char *message)
Definition: errorchecks.cpp:134
@ Logic
Definition: errorchecks.h:54

Use this macro to protect sections of the code that should not be entered.

This macro is only active in debug (i.e. when NDEBUG is not defined). In release, the macro evaluates to nothing.

◆ WS_ASSERT_LOGIC

#define WS_ASSERT_LOGIC (   cond)
Value:
do \
{ \
if (!(cond)) \
{ \
if (CSIRO::Assert::reportFailure(CSIRO::Assert::Logic, #cond, __FILE__, __LINE__, nullptr) == CSIRO::Assert::Halt) \
{ \
WS_HALT(); \
} \
} \
} while (0)

Use this macro to assert behaviour related to the internal logic of a program. Errors of this type are assumed to be "detectable" before the program executes.

This macro is only active in debug (i.e. when NDEBUG is not defined). In release, the macro evaluates to nothing.

◆ WS_ASSERT_LOGIC_MSG

#define WS_ASSERT_LOGIC_MSG (   cond,
  msg 
)
Value:
do \
{ \
if (!(cond)) \
{ \
if (CSIRO::Assert::reportFailure(CSIRO::Assert::Logic, #cond, __FILE__, __LINE__, (msg)) == CSIRO::Assert::Halt) \
{ \
WS_HALT(); \
} \
} \
} while (0)

Identical to WS_ASSERT_LOGIC but with a message that will be printed if the assert fails.

This macro is only active in debug (i.e. when NDEBUG is not defined). In release, the macro evaluates to nothing.

The msg passed in has to be of the type const char*.

◆ WS_ASSERT_RUNTIME

#define WS_ASSERT_RUNTIME (   cond)
Value:
do \
{ \
if (!(cond)) \
{ \
if (CSIRO::Assert::reportFailure(CSIRO::Assert::Runtime, #cond, __FILE__, __LINE__, nullptr) == CSIRO::Assert::Halt) \
{ \
WS_HALT(); \
} \
} \
} while (0)
@ Runtime
Definition: errorchecks.h:55

Use this macro to assert conditions that can only be validated at runtime. Assertions of this type are usually used to guard against impossible user input or data variations.

This macro is only active in debug (i.e. when NDEBUG is not defined). In release, the macro evaluates to nothing.

◆ WS_ASSERT_RUNTIME_MSG

#define WS_ASSERT_RUNTIME_MSG (   cond,
  msg 
)
Value:
do \
{ \
if (!(cond)) \
{ \
if (CSIRO::Assert::reportFailure(CSIRO::Assert::Runtime, #cond, __FILE__, __LINE__, (msg)) == CSIRO::Assert::Halt) \
{ \
WS_HALT(); \
} \
} \
} while (0)

Identical to WS_ASSERT_RUNTIME but with a message that will be printed if the assert fails.

This macro is only active in debug (i.e. when NDEBUG is not defined). In release, the macro evaluates to nothing.

The msg passed in has to be of the type const char*.

◆ WS_HALT

#define WS_HALT ( )    std::raise(SIGTRAP)

◆ WS_VERIFY_RUNTIME

#define WS_VERIFY_RUNTIME (   cond)
Value:
do \
{ \
if (!(cond)) \
{ \
QString msgText = CSIRO::Assert::generateDefaultErrorText(QString("Verify Failed"), CSIRO::Assert::Runtime, #cond, nullptr, __FILE__, __LINE__); \
throw std::runtime_error(msgText.toStdString()); \
} \
} while (0)
static QString generateDefaultErrorText(FailType type, const char *condition, const char *message, const char *file, int line)
Definition: errorchecks.cpp:81

This macro is identical to WS_ASSERT_RUNTIME except that it is evaluated even in release mode.

◆ WS_VERIFY_RUNTIME_MSG

#define WS_VERIFY_RUNTIME_MSG (   cond,
  msg 
)
Value:
do \
{ \
if (!(cond)) \
{ \
QString msgText = CSIRO::Assert::generateDefaultErrorText(QString("Verify Failed"), CSIRO::Assert::Runtime, #cond, (msg), __FILE__, __LINE__); \
throw std::runtime_error(msgText.toStdString()); \
} \
} while (0)

This macro is identical to WS_VERIFY_RUNTIME except that it is prints the specified message when the verification fails. It is evaluated even in release mode. The msg passed in could be formatted QString or anything QString::arg accept, be aware that formatting a QString may throw std::bad_alloc if memory allocation fails.

Sample usages:

WS_VERIFY_RUNTIME_MSG(ptr != nullptr, "A pointer that is not expected to be null."); // exception thrown when ptr is nullptr. ptr->foo(); // ptr is guaranteed to be not nullptr.

WS_VERIFY_RUNTIME_MSG(value > 0, QString("A value expected to be positive, but turned out to be %1.").arg(value));