Avara3D 0.2.0
C++ API reference
Assert.h
1//
2// Assert.h
3// avara3d
4//
5// Created by Morgan Davis on 1/5/26.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_ASSERT_H
10#define AVARA3D_ASSERT_H
11
12#include <cstddef>
13
14// ----------------------------------------------------------------------------
15// Branch prediction
16// ----------------------------------------------------------------------------
17#if defined(__clang__) || defined(__GNUC__)
18 #define A3D_LIKELY(x) __builtin_expect(!!(x), 1)
19 #define A3D_UNLIKELY(x) __builtin_expect(!!(x), 0)
20#else
21 #define A3D_LIKELY(x) (x)
22 #define A3D_UNLIKELY(x) (x)
23#endif
24
25// ----------------------------------------------------------------------------
26// Debug/Release detect
27// ----------------------------------------------------------------------------
28#ifndef A3D_DEBUG
29 #if defined(NDEBUG)
30 #define A3D_DEBUG 0
31 #else
32 #define A3D_DEBUG 1
33 #endif
34#endif
35
36namespace a3d::detail {
37
38 [[noreturn]] void assert_fail(const char* expr, const char* file, int line, const char* func);
39
40 [[noreturn]] void assert_fail_msg(const char* expr,
41 const char* file,
42 int line,
43 const char* func,
44 const char* msg);
45
46}
47
48// ----------------------------------------------------------------------------
49// Public macros
50// ----------------------------------------------------------------------------
51#if A3D_DEBUG
52
53 #define A3D_ASSERT(expr) \
54 do { \
55 if (A3D_UNLIKELY(!(expr))) { \
56 ::a3d::detail::assert_fail(#expr, __FILE__, __LINE__, __func__); \
57 } \
58 } while (0)
59
60// Message is a plain string here (keep it simple + dependency-free).
61// If you want formatting, see note below.
62 #define A3D_ASSERT_MSG(expr, msg) \
63 do { \
64 if (A3D_UNLIKELY(!(expr))) { \
65 ::a3d::detail::assert_fail_msg(#expr, __FILE__, __LINE__, __func__, (msg)); \
66 } \
67 } while (0)
68
69#else
70
71 #define A3D_ASSERT(expr) \
72 do { \
73 (void) sizeof(expr); \
74 } while (0)
75 #define A3D_ASSERT_MSG(expr, msg) \
76 do { \
77 (void) sizeof(expr); \
78 (void) sizeof(msg); \
79 } while (0)
80
81#endif
82
83#endif // AVARA3D_ASSERT_H