Skip to content

Commit fe01757

Browse files
author
Alexis Lopez Zubieta
committed
Allow creating a DLHandle from a list of sonames
1 parent fd7ee38 commit fe01757

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/libappimage/utils/DLHandle.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,37 @@ namespace appimage {
2525
* @param libName
2626
* @param mode
2727
*/
28-
explicit DLHandle(const std::string& libName, int mode) : libName(libName) {
28+
explicit DLHandle(const std::string& libName, int mode) : handle(nullptr), libName(libName) {
2929
handle = dlopen(libName.c_str(), mode);
3030

3131
if (!handle)
3232
throw DLHandleError("Unable to load " + libName);
3333
}
3434

35+
/**
36+
* Load one of the libraries listed in <libNames> with the given <mode> flags. For details about
37+
* the allowed flags see the dlopen doc.
38+
* @param libName
39+
* @param mode
40+
*/
41+
explicit DLHandle(std::initializer_list<const std::string> libNames, int mode) : handle(nullptr) {
42+
for (const auto& item: libNames) {
43+
handle = dlopen(item.c_str(), mode);
44+
if (handle) {
45+
libName = item;
46+
break;
47+
}
48+
}
49+
50+
if (!handle) {
51+
std::string libNamesStr;
52+
for (const auto& item: libNames)
53+
libNamesStr += " " + item;
54+
55+
throw DLHandleError("Unable to load any of: " + libNamesStr);
56+
}
57+
}
58+
3559
virtual ~DLHandle() {
3660
dlclose(handle);
3761
};

0 commit comments

Comments
 (0)