13 #ifndef __STOUT_POSIX_DYNAMICLIBRARY_HPP__ 14 #define __STOUT_POSIX_DYNAMICLIBRARY_HPP__ 33 : handle_(nullptr, [](void* handle) {
34 if (handle ==
nullptr) {
38 return dlclose(handle);
47 if (handle_ !=
nullptr) {
48 return Error(
"Library already opened");
51 handle_.reset(dlopen(path.c_str(), RTLD_NOW));
53 if (handle_ ==
nullptr) {
54 return Error(
"Could not load library '" + path +
"': " + dlerror());
64 if (handle_ ==
nullptr) {
65 return Error(
"Could not close library; handle was already `nullptr`");
68 if (dlclose(handle_.get()) != 0) {
70 "Could not close library '" +
71 (path_.
isSome() ? path_.
get() :
"") +
"': " + dlerror());
87 if (handle_ ==
nullptr) {
89 "Could not get symbol '" + name +
"'; library handle was `nullptr`");
92 void* symbol = dlsym(handle_.get(), name.c_str());
94 if (symbol ==
nullptr) {
96 "Error looking up symbol '" + name +
"' in '" +
97 (path_.
isSome() ? path_.
get() :
"") +
"' : " + dlerror());
104 std::unique_ptr<void, int (*)(void*)> handle_;
108 #endif // __STOUT_POSIX_DYNAMICLIBRARY_HPP__
Try< Nothing > open(const std::string &path)
Definition: dynamiclibrary.hpp:44
Definition: nothing.hpp:16
Definition: errorbase.hpp:36
virtual ~DynamicLibrary()=default
Try< void * > loadSymbol(const std::string &name)
Definition: dynamiclibrary.hpp:85
bool isSome() const
Definition: option.hpp:116
const T & get() const &
Definition: option.hpp:119
DynamicLibrary is a very simple wrapper around the programming interface to the dynamic linking loade...
Definition: dynamiclibrary.hpp:29
DynamicLibrary()
Definition: dynamiclibrary.hpp:32
Try< Nothing > close()
Definition: dynamiclibrary.hpp:62
constexpr const char * name
Definition: shell.hpp:41