NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023-2024 NeoFOAM authors
3#pragma once
4
5#include <cstdlib>
6#include <exception>
7#include <iostream>
8#include <string>
9#include <sstream>
10#include <iostream>
11
12#ifdef NF_WITH_MPI_SUPPORT
13#include <mpi.h>
14#endif
15
16// compiling with clang and cuda fails to
17// find source location
18// #include <source_location>
19// #include <experimental/source_location>
20
21#include "info.hpp"
22
23#ifdef NF_DEBUG_MESSAGING
24#include "cpptrace/cpptrace.hpp"
25#endif
26
27
28namespace NeoFOAM
29{
30
37class NeoFOAMException : public std::exception
38{
39public:
40
45 explicit NeoFOAMException(const std::string& message) : message_(message) {}
46
51 const char* what() const noexcept override { return message_.c_str(); }
52
53private:
54
55 std::string message_;
56};
57
58} // namespace NeoFOAM
59
60#ifdef NF_DEBUG_MESSAGING
71#define NF_ERROR_MESSAGE(message) \
72 "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n" \
73 << cpptrace::generate_trace().to_string() << "\n"
74#else
85#define NF_ERROR_MESSAGE(message) \
86 "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n"
87#endif
88
99#ifdef NF_WITH_MPI_SUPPORT
100#define NF_ERROR_EXIT(message) \
101 do \
102 { \
103 std::cerr << NF_ERROR_MESSAGE(message); \
104 MPI_Abort(MPI_COMM_WORLD, 1); \
105 } \
106 while (false)
107#else
108#define NF_ERROR_EXIT(message) \
109 do \
110 { \
111 std::cerr << NF_ERROR_MESSAGE(message); \
112 std::exit(1); \
113 } \
114 while (false)
115#endif
116
127#define NF_THROW(message) \
128 throw NeoFOAM::NeoFOAMException((std::stringstream() << NF_ERROR_MESSAGE(message)).str())
129
142#define NF_ASSERT(condition, message) \
143 do \
144 { \
145 if (!(condition)) [[unlikely]] \
146 { \
147 NF_ERROR_EXIT("Assertion `" #condition "` failed.\n " << message); \
148 } \
149 } \
150 while (false)
151
165#define NF_ASSERT_THROW(condition, message) \
166 do \
167 { \
168 if (!(condition)) [[unlikely]] \
169 { \
170 NF_THROW("Assertion `" #condition "` failed.\n " << message); \
171 } \
172 } \
173 while (false)
174
175#ifdef NF_DEBUG
186#define NF_DEBUG_ASSERT(condition, message) NF_ASSERT(condition, message)
187
199#define NF_DEBUG_ASSERT_THROW(condition, message) NF_ASSERT_THROW(condition, message)
200#else
211#define NF_DEBUG_ASSERT(condition, message) ((void)0)
212
224#define NF_DEBUG_ASSERT_THROW(condition, message) ((void)0)
225#endif
226
239#define NF_ASSERT_EQUAL(a, b) NF_ASSERT(a == b, "Expected " << b << ", got " << a)
240
251#define NF_DEBUG_ASSERT_EQUAL(a, b) NF_DEBUG_ASSERT(a == b, "Expected " << b << ", got " << a)
252
266#define NF_ASSERT_EQUAL_THROW(a, b) NF_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
267
278#define NF_DEBUG_ASSERT_EQUAL_THROW(a, b) \
279 NF_DEBUG_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
Custom exception class for NeoFOAM.
Definition error.hpp:38
const char * what() const noexcept override
Returns the error message associated with the exception.
Definition error.hpp:51
NeoFOAMException(const std::string &message)
Constructs a NeoFOAMException object with the given error message.
Definition error.hpp:45