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#include "../Foundation/Result.h" // exit
8
9namespace SC
10{
13
15struct SC_COMPILER_EXPORT Assert
16{
17 [[noreturn]] SC_COMPILER_FORCE_INLINE static void unreachable()
18 {
19#if SC_COMPILER_MSVC
20 __assume(false);
21#else
22 __builtin_unreachable();
23#endif
24 }
26 [[noreturn]] static void exit(int code);
27
29 static void printBacktrace(const char* expression, Result result, const native_char_t* filename,
30 const char* function, int line);
31 static void printBacktrace(const char* expression, bool result, const native_char_t* filename, const char* function,
32 int line);
33
34 private:
35 struct Internal;
36};
38
41#if SC_PLATFORM_WINDOWS
42#define WFILE WIDEN(__FILE__)
43#else
44#define WFILE __FILE__
45#endif
48#define SC_ASSERT_RELEASE(e) \
49 if (!(e)) \
50 SC_LANGUAGE_UNLIKELY \
51 { \
52 SC::Assert::printBacktrace(#e, e, WFILE, __func__, __LINE__); \
53 SC_COMPILER_DEBUG_BREAK; \
54 SC::Assert::exit(-1); \
55 } \
56 (void)0
57
60#if SC_CONFIGURATION_DEBUG
61#define SC_ASSERT_DEBUG(e) SC_ASSERT_RELEASE(e)
62#else
63#define SC_ASSERT_DEBUG(e) (void)0
64#endif
65
67#define SC_TRUST_RESULT(expression) SC_ASSERT_RELEASE(expression)
68
70} // namespace SC
#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:16
static void exit(int code)
Exits current process with the given code.
static void printBacktrace(const char *expression, Result result, const native_char_t *filename, const char *function, int line)
Prints an assertion to standard output.
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13