Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19#define ASSERT_MODE_CASSERT 1
20#define ASSERT_MODE_RUNTIME_ERROR 2
21
22#ifdef SHAM_ASSERT_IS
23 #if SHAM_ASSERT_IS == ASSERT_MODE_RUNTIME_ERROR
24 #include "shambase/exception.hpp"
25 #endif
26 #if SHAM_ASSERT_IS == ASSERT_MODE_CASSERT
27 #include <cassert>
28 #endif
29#endif
30
31/*
32 * This file make heavy use of the legendary do while false trick
33 * do { ... } while (false) in order to force a compilation error
34 * for missing ; for the end of the macro call.
35 */
36
40#define SHAM_ASSERT_NAMED(message, condition) \
41 do { \
42 } while (false)
43
44#ifdef SHAM_ASSERT_IS
45 #undef SHAM_ASSERT_NAMED // we are about to redefine it
46
47 #if SHAM_ASSERT_IS == ASSERT_MODE_CASSERT
48 #define SHAM_ASSERT_NAMED(message, condition) \
49 do { \
50 assert(((void) message, condition)); \
51 } while (false)
52 #elif SHAM_ASSERT_IS == ASSERT_MODE_RUNTIME_ERROR
53 #define SHAM_ASSERT_NAMED(message, condition) \
54 do { \
55 if (!(condition)) { \
56 shambase::throw_with_loc<std::runtime_error>(message); \
57 } \
58 } while (false)
59 #else
60 #error \
61 "Unknown value for SHAM_ASSERT_IS. Possible values are: ASSERT_MODE_CASSERT,ASSERT_MODE_RUNTIME_ERROR"
62 #endif
63
64#endif
65
67#define SHAM_ASSERT(x) SHAM_ASSERT_NAMED(#x, x)
This header file contains utility functions related to exception handling in the code.