shengine pre-release
shengine by mrsinho
Loading...
Searching...
No Matches
shApplicationHost.h
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Platform-specific shared library loading functions.
4 *
5 * This file provides platform-specific macros and functions for loading shared libraries dynamically.
6 */
7
8#ifndef SH_SHARED_HOST_H
9#define SH_SHARED_HOST_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif//__cplusplus
14
15
16
17#ifdef _MSC_VER
18#pragma warning (disable: 4005 4996 4113)
19#endif//_MSC_VER
20
21
22#ifdef _WIN32
23#include <windows.h>
24
25/**
26 * @brief Macro to load a shared library on Windows.
27 *
28 * This macro uses the Windows API function `LoadLibrary` to load a shared library.
29 *
30 * @param path Path to the shared library.
31 * @return A handle to the loaded shared library.
32 */
33#define shLoadShared(path) LoadLibrary(path)
34
35/**
36 * @brief Macro to load a symbol from a shared library on Windows.
37 *
38 * This macro uses the Windows API function `GetProcAddress` to load a symbol from a shared library.
39 *
40 * @param handle Handle to the loaded shared library.
41 * @param symbol Symbol name to load.
42 * @return A pointer to the loaded symbol.
43 */
44#define shSharedLoadSymbol(handle, symbol) GetProcAddress(handle, symbol)
45
46/**
47 * @brief Macro to free a loaded shared library on Windows.
48 *
49 * This macro uses the Windows API function `FreeLibrary` to free a loaded shared library.
50 *
51 * @param handle Handle to the loaded shared library.
52 */
53#define shSharedFree(handle) FreeLibrary(handle)
54
55#else
56#include <dlfcn.h>
57
58/**
59 * @brief Macro to load a shared library on non-Windows platforms.
60 *
61 * This macro uses the POSIX function `dlopen` to load a shared library.
62 *
63 * @param path Path to the shared library.
64 * @return A handle to the loaded shared library.
65 */
66#define shLoadShared(path) dlopen(path, RTLD_LAZY)
67
68/**
69 * @brief Macro to load a symbol from a shared library on non-Windows platforms.
70 *
71 * This macro uses the POSIX function `dlsym` to load a symbol from a shared library.
72 *
73 * @param handle Handle to the loaded shared library.
74 * @param symbol Symbol name to load.
75 * @return A pointer to the loaded symbol.
76 */
77#define shSharedLoadSymbol(handle, symbol) dlsym(handle, symbol)
78
79/**
80 * @brief Macro to free a loaded shared library on non-Windows platforms.
81 *
82 * This macro uses the POSIX function `dlclose` to free a loaded shared library.
83 *
84 * @param handle Handle to the loaded shared library.
85 */
86#define shSharedFree(handle) dlclose(handle)
87
88#endif // _WIN32
89
90#include <stdint.h>
91#include <stddef.h>
92
93/**
94 * @typedef ShSharedHandle
95 * @brief Represents a handle to a loaded shared library.
96 */
97typedef void* ShSharedHandle;
98
99/**
100 * @typedef ShApplicationFunc
101 * @brief Represents the function signature for the application's main function.
102 *
103 * It takes A pointer as argument to manage some data that the application may use.
104 * @return An 8-bit unsigned integer representing the success or failure of the application.
105 */
106typedef uint8_t(ShApplicationFunc) (void*);
107
108/**
109 * @typedef ShApplicationThreadFunc
110 * @brief Represents the function signature for a thread within the application.
111 *
112 * It takes A pointer as argument to manage some data that the application may use.
113 * @return A 64-bit unsigned integer representing the success or failure of the thread.
114 */
115typedef uint64_t (ShApplicationThreadFunc) (void*);
116
117/**
118 * @struct ShApplicationHost
119 * @brief Represents the host for a shared application.
120 */
121typedef struct ShApplicationHost {
122 ShSharedHandle shared; /**< Handle to the loaded shared library. */
123 ShApplicationFunc* p_start; /**< Pointer to the application's start function. */
124 ShApplicationFunc* p_update; /**< Pointer to the application's update function. */
125 ShApplicationFunc* p_main_cmd_buffer; /**< Pointer to the application's main command buffer function. */
126 ShApplicationFunc* p_main_renderpass; /**< Pointer to the application's main render pass function. */
127 ShApplicationFunc* p_frame_resize; /**< Pointer to the application's frame resize function. */
128 ShApplicationFunc* p_close; /**< Pointer to the application's close function. */
130
131/**
132 * @brief Macro to handle errors in the shared host.
133 *
134 * This macro prints an error message and executes a failure expression if a condition is true.
135 *
136 * @param condition The condition to check.
137 * @param msg The error message.
138 * @param failure_expression The expression to execute in case of failure.
139 */
140#define shSharedHostError(condition, msg, failure_expression)\
141 if ((int)(condition)) { printf("shsharedhost error: %s\n", msg); failure_expression; }
142
143/**
144 * @brief Runs a shared application.
145 *
146 * This function runs the shared application by executing the provided application function.
147 *
148 * @param p_engine A pointer to the engine data.
149 * @param p_func The application function to run.
150 * @return A 64-bit unsigned integer representing the success or failure of the application.
151 */
152extern uint64_t shApplicationRun(
153 void* p_engine,
154 ShApplicationFunc* p_func
155);
156
157/**
158 * @brief Retrieves the shared application information.
159 *
160 * This function retrieves information about the shared application from the shared library.
161 *
162 * @param shared_name The name of the shared library.
163 * @param s_start The symbol name for the start function.
164 * @param s_update The symbol name for the update function.
165 * @param s_main_cmd_buffer The symbol name for the main command buffer function.
166 * @param s_main_renderpass The symbol name for the main render pass function.
167 * @param s_frame_resize The symbol name for the frame resize function.
168 * @param s_close The symbol name for the close function.
169 * @param p_application A pointer to the application host structure to fill.
170 * @return An 8-bit unsigned integer representing the success or failure of retrieving the shared application.
171 */
172extern uint8_t shGetSharedApplication(
173 const char* shared_name,
174 const char* s_start,
175 const char* s_update,
176 const char* s_main_cmd_buffer,
177 const char* s_main_renderpass,
178 const char* s_frame_resize,
179 const char* s_close,
180 ShApplicationHost* p_application
181);
182
183/**
184 * @brief Releases the shared library.
185 *
186 * This macro frees the loaded shared library if it is not NULL.
187 *
188 * @param p_shared A pointer to the shared library handle.
189 */
190#define shSharedRelease(p_shared)\
191 if ((p_shared) != NULL) { if (*(p_shared) != NULL) { shSharedFree(*(p_shared)); *(p_shared) = NULL; } }
192
193
194
195#ifdef __cplusplus
196}
197#endif//__cplusplus
198
199#endif//SH_SHARED_HOST_H
uint64_t() ShApplicationThreadFunc(void *)
Represents the function signature for a thread within the application.
Definition shApplicationHost.h:115
uint8_t shGetSharedApplication(const char *shared_name, const char *s_start, const char *s_update, const char *s_main_cmd_buffer, const char *s_main_renderpass, const char *s_frame_resize, const char *s_close, ShApplicationHost *p_application)
Retrieves the shared application information.
Definition shApplicationHost.c:27
uint8_t() ShApplicationFunc(void *)
Represents the function signature for the application's main function.
Definition shApplicationHost.h:106
void * ShSharedHandle
Represents a handle to a loaded shared library.
Definition shApplicationHost.h:97
uint64_t shApplicationRun(void *p_engine, ShApplicationFunc *p_func)
Runs a shared application.
Definition shApplicationHost.c:18
Represents the host for a shared application.
Definition shApplicationHost.h:121
ShSharedHandle shared
Definition shApplicationHost.h:122
ShApplicationFunc * p_close
Definition shApplicationHost.h:128
ShApplicationFunc * p_update
Definition shApplicationHost.h:124
ShApplicationFunc * p_frame_resize
Definition shApplicationHost.h:127
ShApplicationFunc * p_main_cmd_buffer
Definition shApplicationHost.h:125
ShApplicationFunc * p_start
Definition shApplicationHost.h:123
ShApplicationFunc * p_main_renderpass
Definition shApplicationHost.h:126