NeoFOAM
WIP Prototype of a modern OpenFOAM core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Concepts
Classes | Namespaces | Macros
error.hpp File Reference
#include <cstdlib>
#include <exception>
#include <iostream>
#include <string>
#include <sstream>
#include "info.hpp"

Go to the source code of this file.

Classes

class  NeoFOAM::NeoFOAMException
 Custom exception class for NeoFOAM. More...
 

Namespaces

namespace  NeoFOAM
 

Macros

#define NF_ERROR_MESSAGE(message)    "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n"
 Macro for generating an error message without debug information.
 
#define NF_ERROR_EXIT(message)
 Macro for printing an error message and aborting the program.
 
#define NF_THROW(message)    throw NeoFOAM::NeoFOAMException((std::stringstream() << NF_ERROR_MESSAGE(message)).str())
 Macro for throwing a NeoFOAMException with the specified error message.
 
#define NF_ASSERT(condition, message)
 Macro for asserting a condition and printing an error message if the condition is false.
 
#define NF_ASSERT_THROW(condition, message)
 Macro for asserting a condition and throwing a NeoFOAMException if the condition is false.
 
#define NF_DEBUG_ASSERT(condition, message)   ((void)0)
 Macro for asserting a condition and printing an error message if the condition is false (only in debug mode).
 
#define NF_DEBUG_ASSERT_THROW(condition, message)   ((void)0)
 Macro for asserting a condition and throwing a NeoFOAMException if the condition is false (only in debug mode).
 
#define NF_ASSERT_EQUAL(a, b)   NF_ASSERT(a == b, "Expected " << b << ", got " << a)
 Macro for asserting that two values are equal and printing an error message if they are not.
 
#define NF_DEBUG_ASSERT_EQUAL(a, b)   NF_DEBUG_ASSERT(a == b, "Expected " << b << ", got " << a)
 Macro for asserting that two values are equal and printing an error message if they are not (only in debug mode).
 
#define NF_ASSERT_EQUAL_THROW(a, b)   NF_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
 Macro for asserting that two values are equal and throwing a NeoFOAMException if they are not.
 
#define NF_DEBUG_ASSERT_EQUAL_THROW(a, b)    NF_DEBUG_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
 Macro for asserting that two values are equal and throwing a NeoFOAMException if they are not (only in debug mode).
 

Macro Definition Documentation

◆ NF_ASSERT

#define NF_ASSERT (   condition,
  message 
)
Value:
do \
{ \
if (!(condition)) [[unlikely]] \
{ \
NF_ERROR_EXIT("Assertion `" #condition "` failed.\n " << message); \
} \
} \
while (false)

Macro for asserting a condition and printing an error message if the condition is false.

This macro checks the specified condition and, if it evaluates to false, prints an error message to the standard error stream, including the current file name and current line number, and then calls std::abort() to terminate the program.

Parameters
conditionThe condition to be checked.
messageThe error message to be printed if the condition is false.

Definition at line 142 of file error.hpp.

◆ NF_ASSERT_EQUAL

#define NF_ASSERT_EQUAL (   a,
 
)    NF_ASSERT(a == b, "Expected " << b << ", got " << a)

Macro for asserting that two values are equal and printing an error message if they are not.

This macro checks that the two specified values are equal and, if they are not, prints an error message to the standard error stream, including the expected and actual values, the current file name, and the current line number, and then calls std::abort() to terminate the program.

Parameters
aThe actual value to be compared.
bThe expected value to be compared.

Definition at line 239 of file error.hpp.

◆ NF_ASSERT_EQUAL_THROW

#define NF_ASSERT_EQUAL_THROW (   a,
 
)    NF_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)

Macro for asserting that two values are equal and throwing a NeoFOAMException if they are not.

This macro checks that the two specified values are equal and, if they are not, constructs a std::stringstream to concatenate the expected and actual values with the current file name and current line number, and then throws a NeoFOAMException with the resulting string as the error message.

Parameters
aThe actual value to be compared.
bThe expected value to be compared.

Definition at line 266 of file error.hpp.

◆ NF_ASSERT_THROW

#define NF_ASSERT_THROW (   condition,
  message 
)
Value:
do \
{ \
if (!(condition)) [[unlikely]] \
{ \
NF_THROW("Assertion `" #condition "` failed.\n " << message); \
} \
} \
while (false)

Macro for asserting a condition and throwing a NeoFOAMException if the condition is false.

This macro checks the specified condition and, if it evaluates to false, constructs a std::stringstream to concatenate the specified error message with the current file name and current line number, and then throws a NeoFOAMException with the resulting string as the error message.

Parameters
conditionThe condition to be checked.
messageThe error message to be included in the exception if the condition is false.

Definition at line 165 of file error.hpp.

◆ NF_DEBUG_ASSERT

#define NF_DEBUG_ASSERT (   condition,
  message 
)    ((void)0)

Macro for asserting a condition and printing an error message if the condition is false (only in debug mode).

This macro does nothing in release mode.

Parameters
conditionThe condition to be checked.
messageThe error message to be printed if the condition is false.

Definition at line 211 of file error.hpp.

◆ NF_DEBUG_ASSERT_EQUAL

#define NF_DEBUG_ASSERT_EQUAL (   a,
 
)    NF_DEBUG_ASSERT(a == b, "Expected " << b << ", got " << a)

Macro for asserting that two values are equal and printing an error message if they are not (only in debug mode).

This macro is equivalent to NF_ASSERT_EQUAL in debug mode.

Parameters
aThe actual value to be compared.
bThe expected value to be compared.

Definition at line 251 of file error.hpp.

◆ NF_DEBUG_ASSERT_EQUAL_THROW

#define NF_DEBUG_ASSERT_EQUAL_THROW (   a,
 
)     NF_DEBUG_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)

Macro for asserting that two values are equal and throwing a NeoFOAMException if they are not (only in debug mode).

This macro is equivalent to NF_ASSERT_EQUAL_THROW in debug mode.

Parameters
aThe actual value to be compared.
bThe expected value to be compared.

Definition at line 278 of file error.hpp.

◆ NF_DEBUG_ASSERT_THROW

#define NF_DEBUG_ASSERT_THROW (   condition,
  message 
)    ((void)0)

Macro for asserting a condition and throwing a NeoFOAMException if the condition is false (only in debug mode).

This macro does nothing in release mode.

Parameters
conditionThe condition to be checked.
messageThe error message to be included in the exception if the condition is false.

Definition at line 224 of file error.hpp.

◆ NF_ERROR_EXIT

#define NF_ERROR_EXIT (   message)
Value:
do \
{ \
std::cerr << NF_ERROR_MESSAGE(message); \
std::exit(1); \
} \
while (false)
#define NF_ERROR_MESSAGE(message)
Macro for generating an error message without debug information.
Definition error.hpp:85

Macro for printing an error message and aborting the program.

This macro prints the specified error message to the standard error stream, including the current file name and current line number, and then calls std::abort() to terminate the program.

Parameters
messageThe error message to be printed.

Definition at line 108 of file error.hpp.

◆ NF_ERROR_MESSAGE

#define NF_ERROR_MESSAGE (   message)     "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n"

Macro for generating an error message without debug information.

This macro generates an error message with the specified message, current file name, and current line number.

Parameters
messageThe error message to be included in the generated message.
Returns
std::string The generated error message.

Definition at line 85 of file error.hpp.

◆ NF_THROW

#define NF_THROW (   message)     throw NeoFOAM::NeoFOAMException((std::stringstream() << NF_ERROR_MESSAGE(message)).str())

Macro for throwing a NeoFOAMException with the specified error message.

This macro constructs a std::stringstream to concatenate the specified error message with the current file name and current line number, and then throws a NeoFOAMException with the resulting string as the error message.

Parameters
messageThe error message to be included in the exception.

Definition at line 127 of file error.hpp.