cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
macro.hpp
Go to the documentation of this file.
1#pragma once
2
6
22#define CPP_TOOLBOX_STATIC_ASSERT(Condition, Message) \
23 static_assert(Condition, Message)
24
37#ifdef CPP_TOOLBOX_DEBUG
38# define CPP_TOOLBOX_ASSERT(Condition, Message) \
39 do { \
40 if (!(Condition)) { \
41 std::cerr << "Assertion failed: " << Message << std::endl; \
42 std::abort(); \
43 } \
44 } while (false)
45#else
46# define CPP_TOOLBOX_ASSERT(Condition, Message) ((void)0)
47#endif
48
60#define CPP_TOOLBOX_UNREACHABLE() \
61 do { \
62 std::cerr << "Unreachable code reached" << std::endl; \
63 std::abort(); \
64 } while (false)
65
77#define CPP_TOOLBOX_LIKELY(Condition) __builtin_expect(!!(Condition), true)
78#define CPP_TOOLBOX_UNLIKELY(Condition) __builtin_expect(!!(Condition), false)
79
89#if defined(CPP_TOOLBOX_COMPILER_MSVC)
90# define CPP_TOOLBOX_FORCE_INLINE __forceinline
91#elif defined(CPP_TOOLBOX_COMPILER_CLANG) || defined(CPP_TOOLBOX_COMPILER_GCC)
92# define CPP_TOOLBOX_FORCE_INLINE __attribute__((always_inline)) inline
93#else
94# define CPP_TOOLBOX_FORCE_INLINE inline
95#endif
96
100#if defined(CPP_TOOLBOX_COMPILER_MSVC)
101# define CPP_TOOLBOX_FUNCTION_NAME __FUNCTION__
102#elif defined(CPP_TOOLBOX_COMPILER_CLANG) || defined(CPP_TOOLBOX_COMPILER_GCC)
103# define CPP_TOOLBOX_FUNCTION_NAME __func__
104#endif
105
114#define CPP_TOOLBOX_ALIGNAS(Alignment) alignas(Alignment)
115
126#define CPP_TOOLBOX_UNUSED(Variable) ((void)(Variable))
127
137#define CPP_TOOLBOX_NODISCARD [[nodiscard]]
138
153#define CPP_TOOLBOX_FALLTHROUGH [[fallthrough]]
154
162#if defined(CPP_TOOLBOX_DEBUG)
163# define CPP_TOOLBOX_LOG_DEBUG(fmt, ...) \
164 LOG_DEBUG_S << __FILE__ << ":" << __LINE__ << " " \
165 << CPP_TOOLBOX_FUNCTION_NAME << " " << fmt << __VA_ARGS__
166#else
167# define CPP_TOOLBOX_LOG_DEBUG(fmt, ...) ((void)0)
168#endif
169
177#define CPP_TOOLBOX_COUNT_ARGS(...) \
178 CPP_TOOLBOX_COUNT_ARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
179#define CPP_TOOLBOX_COUNT_ARGS_IMPL( \
180 _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, Count, ...) \
181 Count
182
191#define CPP_TOOLBOX_REPEAT_2(x) x x
192#define CPP_TOOLBOX_REPEAT_3(x) CPP_TOOLBOX_REPEAT_2(x) x
193#define CPP_TOOLBOX_REPEAT_4(x) CPP_TOOLBOX_REPEAT_2(x) CPP_TOOLBOX_REPEAT_2(x)
194#define CPP_TOOLBOX_REPEAT_5(x) CPP_TOOLBOX_REPEAT_4(x) x
195#define CPP_TOOLBOX_REPEAT(n, x) CPP_TOOLBOX_REPEAT_##n(x)
196
206#define CPP_TOOLBOX_STRING_CONCAT(a, b) a##b
207
216#define CPP_TOOLBOX_STRINGIZE(x) CPP_TOOLBOX_STRINGIZE_IMPL(x)
217#define CPP_TOOLBOX_STRINGIZE_IMPL(x) #x
218
232#define CPP_TOOLBOX_SAFE_CALL(Func) \
233 try { \
234 Func(); \
235 } catch (const std::exception& e) { \
236 LOG_ERROR_S << __FILE__ << ":" << __LINE__ << " " \
237 << CPP_TOOLBOX_FUNCTION_NAME << " " \
238 << "Exception: " << e.what(); \
239 }
240
244#if defined(CPP_TOOLBOX_PLATFORM_WINDOWS) && defined(CPP_TOOLBOX_COMPILER_MSVC)
245# define __CURRENT_FUNCTION__ __FUNCSIG__
246#else
247# define __CURRENT_FUNCTION__ __PRETTY_FUNCTION__
248#endif