Lagrange
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
16#include <string_view>
17
22
59
60namespace lagrange {
61
64
71LA_CORE_API void set_breakpoint_enabled(bool enabled);
72
79LA_CORE_API bool is_breakpoint_enabled();
80
84LA_CORE_API void trigger_breakpoint();
85
99[[noreturn]] LA_CORE_API bool assertion_failed(
100 const char* function,
101 const char* file,
102 unsigned int line,
103 const char* condition,
104 std::string_view message);
105
107
108} // namespace lagrange
109
110// -----------------------------------------------------------------------------
111
113
114#define LA_INTERNAL_EXPAND(x) x
115
116#define LA_INTERNAL_NARG2(...) \
117 LA_INTERNAL_EXPAND(LA_INTERNAL_NARG1(__VA_ARGS__, LA_INTERNAL_RSEQN()))
118#define LA_INTERNAL_NARG1(...) LA_INTERNAL_EXPAND(LA_INTERNAL_ARGSN(__VA_ARGS__))
119#define LA_INTERNAL_ARGSN(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
120#define LA_INTERNAL_RSEQN() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
121
122#define LA_INTERNAL_FUNC2(name, n) name##n
123#define LA_INTERNAL_FUNC1(name, n) LA_INTERNAL_FUNC2(name, n)
124
125#define LA_INTERNAL_GET_MACRO(func, ...) \
126 LA_INTERNAL_EXPAND( \
127 LA_INTERNAL_FUNC1(func, LA_INTERNAL_EXPAND(LA_INTERNAL_NARG2(__VA_ARGS__)))(__VA_ARGS__))
128
129// -----------------------------------------------------------------------------
130
131#if defined(__GNUC__) || defined(__clang__)
132#define LA_ASSERT_FUNCTION __PRETTY_FUNCTION__
133#elif defined(_MSC_VER)
134#define LA_ASSERT_FUNCTION __FUNCSIG__
135#elif defined(__SUNPRO_CC)
136#define LA_ASSERT_FUNCTION __func__
137#else
138#define LA_ASSERT_FUNCTION __FUNCTION__
139#endif
140
141// -----------------------------------------------------------------------------
142
143// Note: In order for the `la_xxx_assert()` macro to behave as a function, it needs to expand to an
144// expression. This means we cannot use `if` or `do ... while` statements. The only options are
145// ternary operators ?: or logical boolean using &&.
146#define LA_INTERNAL_ASSERT_ARGS_2(condition, message) \
147 ((void)(!static_cast<bool>((condition)) && ::lagrange::assertion_failed(LA_ASSERT_FUNCTION, __FILE__, __LINE__, #condition, message)))
148#define LA_INTERNAL_ASSERT_ARGS_1(condition) LA_INTERNAL_ASSERT_ARGS_2(condition, "")
149
150#define LA_INTERNAL_IGNORE_ARGS_2(condition, message) ((void)(0))
151#define LA_INTERNAL_IGNORE_ARGS_1(condition) LA_INTERNAL_IGNORE_ARGS_2(condition, "")
152
154
155// -----------------------------------------------------------------------------
156
168#ifndef la_runtime_assert
169#define la_runtime_assert(...) \
170 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_ASSERT_ARGS_, __VA_ARGS__))
171#endif
172
184#ifndef la_debug_assert
185#ifdef NDEBUG
186#define la_debug_assert(...) \
187 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_IGNORE_ARGS_, __VA_ARGS__))
188#else
189#define la_debug_assert(...) \
190 LA_INTERNAL_EXPAND(LA_INTERNAL_GET_MACRO(LA_INTERNAL_ASSERT_ARGS_, __VA_ARGS__))
191#endif // NDEBUG
192#endif // la_debug_assert
193
LA_CORE_API bool assertion_failed(const char *function, const char *file, unsigned int line, const char *condition, std::string_view message)
Called in case of an assertion failure.
Definition: assert.cpp:73
LA_CORE_API void trigger_breakpoint()
Call to explicitly trigger a debugger breakpoint.
Definition: assert.cpp:65
LA_CORE_API bool is_breakpoint_enabled()
Returns whether to trigger a debugger breakpoint on assert failure.
Definition: assert.cpp:60
LA_CORE_API void set_breakpoint_enabled(bool enabled)
Sets whether to trigger a debugger breakpoint on assert failure.
Definition: assert.cpp:55
Main namespace for Lagrange.
Definition: AABBIGL.h:30