Skip to content

Commit

Permalink
exlib, refactor: support multiple dynamic library names in dl_func.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Oct 6, 2024
1 parent 2b7a00d commit 2262107
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions exlib/include/dl_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ inline char* dlerror(void)
namespace exlib {

template <typename T>
T dl_func(void*& handle, const char* dl_name, const char* func_name, T func)
T dl_func(void*& handle, const char** dl_name, int dl_count, const char* func_name, T func)
{
if (!handle) {
handle = dlopen(dl_name, RTLD_LAZY);
for (int i = 0; i < dl_count && !handle; i++) {
handle = dlopen(dl_name[i], RTLD_LAZY);
}
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
Expand All @@ -85,6 +87,12 @@ T dl_func(void*& handle, const char* dl_name, const char* func_name, T func)

return func1;
}
template <typename T, typename... Args>
T dl_func(void*& handle, const char* func_name, T func, Args... dl_names)
{
const char* dl_name_array[] = { dl_names... };
return dl_func(handle, dl_name_array, sizeof...(dl_names), func_name, func);
}
}

#define dl_def_func(so, so_name, func) static auto s_##func = exlib::dl_func(so, so_name, #func, func)
#define dl_def_func(so, func, ...) static auto s_##func = exlib::dl_func(so, #func, func, __VA_ARGS__)

0 comments on commit 2262107

Please sign in to comment.