From 226210706adc03b268ee6550f2ee4f195a06d03d Mon Sep 17 00:00:00 2001 From: xicilion Date: Mon, 7 Oct 2024 01:34:53 +0800 Subject: [PATCH] exlib, refactor: support multiple dynamic library names in dl_func. --- exlib/include/dl_func.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/exlib/include/dl_func.h b/exlib/include/dl_func.h index 17986d4cf..eafdc37fb 100644 --- a/exlib/include/dl_func.h +++ b/exlib/include/dl_func.h @@ -67,10 +67,12 @@ inline char* dlerror(void) namespace exlib { template -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); @@ -85,6 +87,12 @@ T dl_func(void*& handle, const char* dl_name, const char* func_name, T func) return func1; } +template +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__)