Zth (libzth)
Loading...
Searching...
No Matches
indirection.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 */
6
8
9// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
11
12#if ZTH_SHARED_LIB
13# define ZTH_INDIRECT_CHECKV(f, ...) \
14 static bool recursive = false; \
15 \
16 zth_assert(zth_indirection.f##_check); \
17 \
18 if(recursive) { \
19 zth_indirection.f = zth_indirection.f##_check = nullptr; \
20 f(__VA_ARGS__); \
21 } else { \
22 recursive = true; \
23 zth_indirection.f##_check(__VA_ARGS__); \
24 zth_indirection.f = zth_indirection.f##_check; \
25 }
26
27# define ZTH_INDIRECT_CHECK(f, ret, ...) \
28 static bool recursive = false; \
29 \
30 zth_assert(zth_indirection.f##_check); \
31 \
32 if(recursive) { \
33 zth_indirection.f = zth_indirection.f##_check = nullptr; \
34 return f(__VA_ARGS__); \
35 } else { \
36 recursive = true; \
37 ret res = zth_indirection.f##_check(__VA_ARGS__); \
38 zth_indirection.f = zth_indirection.f##_check; \
39 return res; \
40 }
41
42void zth_logv_indirect_check(char const* fmt, va_list arg)
43{
44 // NOLINTNEXTLINE(clang-diagnostic-format-nonliteral)
45 ZTH_INDIRECT_CHECKV(zth_logv, fmt, arg)
46}
47
48int zth_main_fiber_indirect_check(int argc, char** argv)
49{
50 ZTH_INDIRECT_CHECK(main_fiber, int, argc, argv)
51}
52
53void zth_assert_handler_indirect_check(char const* file, int line, char const* expr)
54{
55 ZTH_INDIRECT_CHECKV(zth_assert_handler, file, line, expr)
56}
57
58int zth_postdeinit_indirect_check()
59{
60 ZTH_INDIRECT_CHECK(zth_postdeinit, int)
61}
62
63void zth_preinit_indirect_check()
64{
65 ZTH_INDIRECT_CHECKV(zth_preinit)
66}
67
68void zth_terminate_indirect_check()
69{
70 ZTH_INDIRECT_CHECKV(zth_terminate)
71}
72#endif // ZTH_SHARED_LIB
73
82
91
100
109
118
127
133void zth_indirect(zth_indirection_t const* indirection)
134{
135 if(indirection) {
136 zth_indirection = *indirection;
137 } else {
138 zth_indirection_t i = {};
139 zth_indirection = i;
140 }
141}
int main_fiber(int argc, char **argv)
void zth_logv(char const *fmt, va_list arg)
Prints the given printf()-like formatted string to stdout.
Definition zth_logv.cpp:19
void zth_preinit_indirect(zth_preinit_t *func)
Set the function pointer for zth_preinit.
void zth_postdeinit_indirect(zth_postdeinit_t *func)
Set the function pointer for zth_postdeinit.
void zth_assert_handler_indirect(zth_assert_handler_t *func)
Set the function pointer for zth_assert_handler.
void zth_logv_indirect(zth_logv_t *func)
Set the function pointer for zth_logv.
void zth_main_fiber_indirect(zth_main_fiber_t *func)
Set the function pointer for main_fiber.
void zth_terminate_indirect(zth_terminate_t *func)
Set the function pointer for zth_terminate.
void zth_indirect(zth_indirection_t const *indirection)
Set all function indirection pointers.
zth_indirection_t zth_indirection
int() zth_main_fiber_t(int argc, char **argv)
Definition indirection.h:41
void() zth_preinit_t()
Definition indirection.h:44
int() zth_postdeinit_t()
Definition indirection.h:43
void() zth_logv_t(char const *fmt, va_list arg)
Definition indirection.h:40
void() zth_assert_handler_t(char const *file, int line, char const *expr)
Definition indirection.h:42
void() zth_terminate_t()
Definition indirection.h:45
int zth_postdeinit()
Initialization function to be called by the default-supplied main(), just before shutting down.
void zth_preinit()
Initialization function to be called by the default-supplied main(), before doing anything else.
zth_logv_t * zth_logv
Definition indirection.h:48
zth_main_fiber_t * main_fiber
Definition indirection.h:49
zth_terminate_t * zth_terminate
Definition indirection.h:53
zth_assert_handler_t * zth_assert_handler
Definition indirection.h:50
zth_postdeinit_t * zth_postdeinit
Definition indirection.h:51
zth_preinit_t * zth_preinit
Definition indirection.h:52
void zth_assert_handler(char const *file, int line, char const *expr)
void zth_terminate()
Terminate immediately.