TPDE
Loading...
Searching...
No Matches
base.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#pragma once
6#include <cassert>
7#include <cstdint>
8
9#ifdef TPDE_LOGGING
10 #include <spdlog/spdlog.h>
11 #define TPDE_LOG(level, ...) \
12 (spdlog::should_log(level) ? spdlog::log(level, __VA_ARGS__) : (void)0)
13 #ifndef NDEBUG
14 #define TPDE_LOG_TRACE(...) TPDE_LOG(spdlog::level::trace, __VA_ARGS__)
15 #define TPDE_LOG_DBG(...) TPDE_LOG(spdlog::level::debug, __VA_ARGS__)
16 #else
17 #define TPDE_LOG_TRACE(...)
18 #define TPDE_LOG_DBG(...)
19 #endif
20 #define TPDE_LOG_INFO(...) TPDE_LOG(spdlog::level::info, __VA_ARGS__)
21 #define TPDE_LOG_WARN(...) TPDE_LOG(spdlog::level::warn, __VA_ARGS__)
22 #define TPDE_LOG_ERR(...) TPDE_LOG(spdlog::level::err, __VA_ARGS__)
23#else
24 #define TPDE_LOG_TRACE(...)
25 #define TPDE_LOG_DBG(...)
26 #define TPDE_LOG_INFO(...)
27 #define TPDE_LOG_WARN(...)
28 #define TPDE_LOG_ERR(...)
29#endif
30
31#ifndef NDEBUG
32 #define TPDE_UNREACHABLE(msg) assert(0 && (msg))
33#else
34 #define TPDE_UNREACHABLE(msg) __builtin_unreachable()
35#endif
36#define TPDE_FATAL(msg) ::tpde::fatal_error(msg)
37
38#if __has_cpp_attribute(clang::lifetimebound)
39 #define TPDE_LIFETIMEBOUND [[clang::lifetimebound]]
40#else
41 #define TPDE_LIFETIMEBOUND
42#endif
43
44#if __has_builtin(__builtin_assume_separate_storage)
45 #define TPDE_NOALIAS(a, b) __builtin_assume_separate_storage(a, b)
46#else
47 #define TPDE_NOALIAS(a, b)
48#endif
49
50namespace tpde {
51// NOTE(ts): someone's gonna hate me...
52using u8 = uint8_t;
53using u16 = uint16_t;
54using u32 = uint32_t;
55using u64 = uint64_t;
56
57using i8 = int8_t;
58using i16 = int16_t;
59using i32 = int32_t;
60using i64 = int64_t;
61
62#ifndef NDEBUG
63constexpr bool WithAsserts = true;
64#else
65constexpr bool WithAsserts = false;
66#endif
67
68/// Abort program with a fatal error
69[[noreturn]] void fatal_error(const char *msg);
70
71} // namespace tpde