Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Assert.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Compiler.h" // SC_COMPILER_DEBUG_BREAK
5#include "../Foundation/LibC.h" // exit
6#include "../Foundation/Platform.h" // SC_CONFIGURATION_DEBUG
7
8namespace SC
9{
10struct SC_COMPILER_EXPORT Assert;
11}
14
17{
18 [[noreturn]] SC_COMPILER_FORCE_INLINE static void unreachable()
19 {
20#if SC_COMPILER_MSVC
21 __assume(false);
22#else
23 __builtin_unreachable();
24#endif
25 }
27 [[noreturn]] static void exit(int code);
28
30 static void printBacktrace(const char* expression, const native_char_t* filename, const char* function, int line);
31
32 private:
33 struct Internal;
34};
36
39#if SC_PLATFORM_WINDOWS
40#define WFILE WIDEN(__FILE__)
41#else
42#define WFILE __FILE__
43#endif
46#define SC_ASSERT_RELEASE(e) \
47 if (!(e)) \
48 SC_LANGUAGE_UNLIKELY \
49 { \
50 SC::Assert::printBacktrace(#e, WFILE, __func__, __LINE__); \
51 SC_COMPILER_DEBUG_BREAK; \
52 SC::Assert::exit(-1); \
53 } \
54 (void)0
55
58#if SC_CONFIGURATION_DEBUG
59#define SC_ASSERT_DEBUG(e) SC_ASSERT_RELEASE(e)
60#else
61#define SC_ASSERT_DEBUG(e) (void)0
62#endif
63
65#define SC_TRUST_RESULT(expression) SC_ASSERT_RELEASE(expression)
66
#define SC_COMPILER_FORCE_INLINE
Macro for forcing inline functions.
Definition Compiler.h:46
char native_char_t
The native char for the platform (wchar_t (4 bytes) on Windows, char (1 byte) everywhere else )
Definition PrimitiveTypes.h:34
Functions and macros to assert, exit() or abort() and capture backtraces.
Definition Assert.h:17
static void exit(int code)
Exits current process with the given code.
static void printBacktrace(const char *expression, const native_char_t *filename, const char *function, int line)
Prints an assertion to standard output.