Zth (libzth)
Loading...
Searching...
No Matches
indirection.h
Go to the documentation of this file.
1#ifndef ZTH_INDIRECTION_H
2#define ZTH_INDIRECTION_H
3/*
4 * SPDX-FileCopyrightText: 2019-2026 Jochem Rutgers
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 */
8
22#include <libzth/macros.h>
23
24#include <libzth/fiber.h>
25#include <libzth/init.h>
26#include <libzth/util.h>
27
28#if ZTH_SHARED_LIB
29# if defined(ZTH_OS_WINDOWS)
30# include <windows.h>
31# elif defined(ZTH_HAVE_DL)
32# include <dlfcn.h>
33# endif
34#endif
35
36#ifdef __cplusplus
37extern "C" {
38#endif // __cplusplus
39
40typedef void(zth_logv_t)(char const* fmt, va_list arg);
41typedef int(zth_main_fiber_t)(int argc, char** argv);
42typedef void(zth_assert_handler_t)(char const* file, int line, char const* expr);
43typedef int(zth_postdeinit_t)();
44typedef void(zth_preinit_t)();
45typedef void(zth_terminate_t)();
46
47typedef struct {
54
55#if ZTH_SHARED_LIB
56 // For a shared library, we cannot determine at compile time if the application overrides
57 // the functions. Library functions may be wrapped by the compiler. In that case, we check
58 // for recursion on the indirection call upon the first time.
59 zth_logv_t* zth_logv_check;
60 zth_main_fiber_t* main_fiber_check;
61 zth_assert_handler_t* zth_assert_handler_check;
62 zth_postdeinit_t* zth_postdeinit_check;
63 zth_preinit_t* zth_preinit_check;
64 zth_terminate_t* zth_terminate_check;
65#endif // ZTH_SHARED_LIB
67
69
70ZTH_EXPORT void zth_logv_indirect(zth_logv_t* func);
71ZTH_EXPORT void zth_main_fiber_indirect(zth_main_fiber_t* func);
73ZTH_EXPORT void zth_postdeinit_indirect(zth_postdeinit_t* func);
74ZTH_EXPORT void zth_preinit_indirect(zth_preinit_t* func);
75ZTH_EXPORT void zth_terminate_indirect(zth_terminate_t* func);
76
77ZTH_EXPORT void zth_indirect(zth_indirection_t const* indirection);
78
79#if ZTH_SHARED_LIB
80void zth_logv_indirect_check(char const* fmt, va_list arg);
81int zth_main_fiber_indirect_check(int argc, char** argv);
82void zth_assert_handler_indirect_check(char const* file, int line, char const* expr);
83int zth_postdeinit_indirect_check();
84void zth_preinit_indirect_check();
85void zth_terminate_indirect_check();
86
94inline void zth_indirect_auto()
95{
97 zth_indirection.zth_logv = zth_logv_indirect_check;
98 zth_indirection.zth_logv_check = zth_logv;
99 }
100
102 zth_indirection.main_fiber = zth_main_fiber_indirect_check;
103 zth_indirection.main_fiber_check = main_fiber;
104 }
105
107 zth_indirection.zth_assert_handler = zth_assert_handler_indirect_check;
108 zth_indirection.zth_assert_handler_check = zth_assert_handler;
109 }
110
112 zth_indirection.zth_postdeinit = zth_postdeinit_indirect_check;
113 zth_indirection.zth_postdeinit_check = zth_postdeinit;
114 }
115
117 zth_indirection.zth_preinit = zth_preinit_indirect_check;
118 zth_indirection.zth_preinit_check = zth_preinit;
119 }
120
122 zth_indirection.zth_terminate = zth_terminate_indirect_check;
123 zth_indirection.zth_terminate_check = zth_terminate;
124 }
125}
126
127ZTH_APP_INIT_CALL(zth_indirect_auto)
128#endif // ZTH_SHARED_LIB
129
130#ifdef __cplusplus
131# define ZTH_INDIRECT_PROLOGUE(func, ...) \
132 if(::zth_indirection.func) \
133 return ::zth_indirection.func(__VA_ARGS__);
134
135# define ZTH_INDIRECT_PROLOGUEV(func, ...) \
136 if(::zth_indirection.func) { \
137 ::zth_indirection.func(__VA_ARGS__); \
138 return; \
139 }
140
141} // extern "C"
142#endif // __cplusplus
143#endif // ZTH_INDIRECTION_H
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.
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
zth_indirection_t zth_indirection
void() zth_terminate_t()
Definition indirection.h:45
#define ZTH_APP_INIT_CALL(f)
Definition init.h:91
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.