Skip to content

Commit

Permalink
exlib, refactor: refactor dl_func to remove the dependency on the val…
Browse files Browse the repository at this point in the history
…ue of func, only rely on the type of func.
  • Loading branch information
xicilion committed Oct 25, 2024
1 parent a170f18 commit 6fabae1
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions exlib/include/dl_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,31 @@ inline char* dlerror(void)

namespace exlib {

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

if (!handle) {
for (int i = 0; i < dl_count && !handle; i++) {
handle = dlopen(dl_name[i], RTLD_LAZY);
handle = dlopen(dl_name_array[i], RTLD_LAZY);
}
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
}
}

T func1 = (T)::dlsym(handle, func_name);
void* func1 = ::dlsym(handle, func_name);
if (!func1) {
fputs(dlerror(), stderr);
exit(1);
}

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, func, ...) static auto s_##func = exlib::dl_func(so, #func, func, __VA_ARGS__)
#define dl_def_func(so, func, ...) static auto s_##func = (decltype(func)*)exlib::dl_func(so, #func, __VA_ARGS__)

0 comments on commit 6fabae1

Please sign in to comment.