1
0
Fork 0

Use unique_ptr instead of gsl::finally in Wayland helper

This commit is contained in:
Ilya Fedin 2021-06-26 11:25:27 +04:00 committed by John Preston
parent c8fa5ef714
commit 5b0a567240

View file

@ -11,8 +11,6 @@
#include <wayland-egl.h> #include <wayland-egl.h>
#include <wayland-cursor.h> #include <wayland-cursor.h>
#include <iostream>
#define LOAD_SYMBOL(handle, func) LoadSymbol(handle, #func, func) #define LOAD_SYMBOL(handle, func) LoadSymbol(handle, #func, func)
namespace Wayland { namespace Wayland {
@ -173,10 +171,16 @@ void wl_argument_from_va_list(
} }
} }
using Handle = void*; struct HandleDeleter {
void operator()(void *handle) {
dlclose(handle);
}
};
using Handle = std::unique_ptr<void, HandleDeleter>;
bool LoadLibrary(Handle &handle, const char *name) { bool LoadLibrary(Handle &handle, const char *name) {
handle = dlopen(name, RTLD_LAZY | RTLD_NODELETE); handle = Handle(dlopen(name, RTLD_LAZY | RTLD_NODELETE));
if (handle) { if (handle) {
return true; return true;
} }
@ -185,9 +189,9 @@ bool LoadLibrary(Handle &handle, const char *name) {
} }
template <typename Function> template <typename Function>
inline bool LoadSymbol(Handle handle, const char *name, Function &func) { inline bool LoadSymbol(const Handle &handle, const char *name, Function &func) {
func = handle func = handle
? reinterpret_cast<Function>(dlsym(handle, name)) ? reinterpret_cast<Function>(dlsym(handle.get(), name))
: nullptr; : nullptr;
if (const auto error = dlerror()) { if (const auto error = dlerror()) {
g_warning("Failed to load function '%s': %s", name, error); g_warning("Failed to load function '%s': %s", name, error);
@ -200,17 +204,6 @@ bool Resolve() {
auto egl = Handle(); auto egl = Handle();
auto cursor = Handle(); auto cursor = Handle();
auto client = Handle(); auto client = Handle();
const auto guard = gsl::finally([&] {
if (egl) {
dlclose(egl);
}
if (cursor) {
dlclose(cursor);
}
if (client) {
dlclose(client);
}
});
return LoadLibrary(egl, "libwayland-egl.so.1") return LoadLibrary(egl, "libwayland-egl.so.1")
&& LOAD_SYMBOL(egl, wl_egl_window_create) && LOAD_SYMBOL(egl, wl_egl_window_create)
&& LOAD_SYMBOL(egl, wl_egl_window_destroy) && LOAD_SYMBOL(egl, wl_egl_window_destroy)