NeoN
A framework for CFD software
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <cstdlib>
8#include <exception>
9#include <iostream>
10#include <string>
11#include <sstream>
12#include <iostream>
13
14#ifdef NF_WITH_MPI_SUPPORT
15#include <mpi.h>
16#endif
17
18// compiling with clang and cuda fails to
19// find source location
20// #include <source_location>
21// #include <experimental/source_location>
22
23#include "info.hpp"
24
25#ifdef NF_DEBUG_MESSAGING
26#include "cpptrace/cpptrace.hpp"
27#endif
28
29
30namespace NeoN
31{
32
39class NeoNException : public std::exception
40{
41public:
42
47 explicit NeoNException(const std::string& message) : message_(message) {}
48
53 const char* what() const noexcept override { return message_.c_str(); }
54
55private:
56
57 std::string message_;
58};
59
60} // namespace NeoN
61
62#ifdef NF_DEBUG_MESSAGING
73#define NF_ERROR_MESSAGE(message) \
74 "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n" \
75 << cpptrace::generate_trace().to_string() << "\n"
76#else
87#define NF_ERROR_MESSAGE(message) \
88 "Error: " << message << "\nFile: " << __FILE__ << "\nLine: " << __LINE__ << "\n"
89#endif
90
101#ifdef NF_WITH_MPI_SUPPORT
102#define NF_ERROR_EXIT(message) \
103 do \
104 { \
105 std::cerr << NF_ERROR_MESSAGE(message); \
106 MPI_Abort(MPI_COMM_WORLD, 1); \
107 } \
108 while (false)
109#else
110#define NF_ERROR_EXIT(message) \
111 do \
112 { \
113 std::cerr << NF_ERROR_MESSAGE(message); \
114 std::exit(1); \
115 } \
116 while (false)
117#endif
118
129#define NF_THROW(message) \
130 throw NeoN::NeoNException((std::stringstream() << NF_ERROR_MESSAGE(std::string(message))).str())
131
144#define NF_ASSERT(condition, message) \
145 do \
146 { \
147 if (!(condition)) [[unlikely]] \
148 { \
149 NF_ERROR_EXIT("Assertion `" #condition "` failed.\n " << message); \
150 } \
151 } \
152 while (false)
153
167#define NF_ASSERT_THROW(condition, message) \
168 do \
169 { \
170 if (!(condition)) [[unlikely]] \
171 { \
172 NF_THROW("Assertion `" #condition "` failed.\n " << message); \
173 } \
174 } \
175 while (false)
176
177#ifdef NF_DEBUG
188#define NF_DEBUG_ASSERT(condition, message) NF_ASSERT(condition, message)
189
201#define NF_DEBUG_ASSERT_THROW(condition, message) NF_ASSERT_THROW(condition, message)
202#else
213#define NF_DEBUG_ASSERT(condition, message) ((void)0)
214
226#define NF_DEBUG_ASSERT_THROW(condition, message) ((void)0)
227#endif
228
241#define NF_ASSERT_EQUAL(a, b) NF_ASSERT(a == b, "Expected " << b << ", got " << a)
242
253#define NF_DEBUG_ASSERT_EQUAL(a, b) NF_DEBUG_ASSERT(a == b, "Expected " << b << ", got " << a)
254
268#define NF_ASSERT_EQUAL_THROW(a, b) NF_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
269
280#define NF_DEBUG_ASSERT_EQUAL_THROW(a, b) \
281 NF_DEBUG_ASSERT_THROW(a == b, "Expected " << b << ", got " << a)
Custom exception class for NeoN.
Definition error.hpp:40
const char * what() const noexcept override
Returns the error message associated with the exception.
Definition error.hpp:53
NeoNException(const std::string &message)
Constructs a NeoNException object with the given error message.
Definition error.hpp:47
Definition array.hpp:20