Lagrange
Loading...
Searching...
No Matches
assert.h
1/*
2 * Copyright 2022 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/api.h>
15#include <lagrange/utils/fmt/format.h>
16
17#include <string_view>
18
23
60
61namespace lagrange {
62
65
72LA_CORE_API void set_breakpoint_enabled(bool enabled);
73
80LA_CORE_API bool is_breakpoint_enabled();
81
85LA_CORE_API void trigger_breakpoint();
86
100[[noreturn]] LA_CORE_API bool assertion_failed(
101 const char* function,
102 const char* file,
103 unsigned int line,
104 const char* condition,
105 std::string_view message);
106
108
109} // namespace lagrange
110
111// -----------------------------------------------------------------------------
112
114
115#define LA_INTERNAL_EXPAND(x) x
116
117#define LA_INTERNAL_NARG2(...) \
118 LA_INTERNAL_EXPAND(LA_INTERNAL_NARG1(__VA_ARGS__, LA_INTERNAL_RSEQN()))
119#define LA_INTERNAL_NARG1(...) LA_INTERNAL_EXPAND(LA_INTERNAL_ARGSN(__VA_ARGS__))
120#define LA_INTERNAL_ARGSN(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
121#define LA_INTERNAL_RSEQN() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
122
123#define LA_INTERNAL_FUNC2(name, n) name##n
124#define LA_INTERNAL_FUNC1(name, n) LA_INTERNAL_FUNC2(name, n)
125
126#define LA_INTERNAL_GET_MACRO(func, ...) \
127 LA_INTERNAL_EXPAND( \
128 LA_INTERNAL_FUNC1(func, LA_INTERNAL_EXPAND(LA_INTERNAL_NARG2(__VA_ARGS__)))(__VA_ARGS__))
129
130// -----------------------------------------------------------------------------
131
132#if defined(__GNUC__) || defined(__clang__)
133 #define LA_ASSERT_FUNCTION __PRETTY_FUNCTION__
134#elif defined(_MSC_VER)
135 #define LA_ASSERT_FUNCTION __FUNCSIG__
136#elif defined(__SUNPRO_CC)
137 #define LA_ASSERT_FUNCTION __func__
138#else
139 #define LA_ASSERT_FUNCTION __FUNCTION__
140#endif
141
142// -----------------------------------------------------------------------------
143
144// Note: In order for the `la_xxx_assert()` macro to behave as a function, it needs to expand to an
145// expression. This means we cannot use `if` or `do ... while` statements. The only options are
146// ternary operators ?: or logical boolean using &&.
147#define LA_INTERNAL_ASSERT_ARGS_2(condition, message) \
148 ((void)(!static_cast<bool>((condition)) && ::lagrange::assertion_failed( \
149 LA_ASSERT_FUNCTION, \
150 __FILE__, \
151 __LINE__, \
152 #condition, \
153 message)))
154#define LA_INTERNAL_ASSERT_ARGS_1(condition) LA_INTERNAL_ASSERT_ARGS_2(condition, "")
155
156#define LA_INTERNAL_IGNORE_ARGS_2(condition, message) ((void)(0))
157#define LA_INTERNAL_IGNORE_ARGS_1(condition) LA_INTERNAL_IGNORE_ARGS_2(condition, "")
158
160
161// -----------------------------------------------------------------------------
162
174#ifndef la_runtime_assert
175 #define la_runtime_assert(...) \
176 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_ASSERT_ARGS_, __VA_ARGS__))
177#endif
178
190#ifndef la_debug_assert
191 #ifdef NDEBUG
192 #define la_debug_assert(...) \
193 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_IGNORE_ARGS_, __VA_ARGS__))
194 #else
195 #define la_debug_assert(...) \
196 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_ASSERT_ARGS_, __VA_ARGS__))
197 #endif // NDEBUG
198#endif // la_debug_assert
199
LA_CORE_API bool assertion_failed(const char *function, const char *file, unsigned int line, const char *condition, std::string_view message)
Definition assert.cpp:74
LA_CORE_API void trigger_breakpoint()
Call to explicitly trigger a debugger breakpoint.
Definition assert.cpp:66
LA_CORE_API bool is_breakpoint_enabled()
Returns whether to trigger a debugger breakpoint on assert failure.
Definition assert.cpp:61
LA_CORE_API void set_breakpoint_enabled(bool enabled)
Sets whether to trigger a debugger breakpoint on assert failure.
Definition assert.cpp:56
Main namespace for Lagrange.