modulemanager 1.0.0
Finds, loads and keeps track of modules at runtime
Loading...
Searching...
No Matches
SharedLibrary.hpp
Go to the documentation of this file.
1
8#ifndef SHARED_LIBRARY_HPP
9#define SHARED_LIBRARY_HPP
10
11#include <stdexcept>
12#include <string>
13
14#if defined(_WIN32)
15 #include <windows.h>
16#else
17 #include <dlfcn.h>
18#endif
19
20
26 public:
27 #if defined(_WIN32)
28 using Handle = HMODULE;
29 #else
30 using Handle = void *;
31 #endif
32
33 static const char *extension() {
34 #if defined(_WIN32)
35 return ".dll";
36 #elif defined(__APPLE__)
37 return ".dylib";
38 #else
39 return ".so";
40 #endif
41 }
42
43 explicit SharedLibrary(std::string path) : _path(std::move(path)) {
44 #if defined(_WIN32)
45 _handle = ::LoadLibraryA(_path.c_str());
46 #else
47 _handle = ::dlopen(_path.c_str(), RTLD_NOW | RTLD_LOCAL);
48 #endif
49 if (!_handle)
50 throw std::runtime_error("SharedLibrary: " + _path);
51 }
52
54 #if defined(_WIN32)
55 if (_handle) ::FreeLibrary(_handle);
56 #else
57 if (_handle) ::dlclose(_handle);
58 #endif
59 }
60
61 SharedLibrary(const SharedLibrary &) = delete;
63
64 template <typename T>
65 T symbol(const char *name) const {
66 #if defined(_WIN32)
67 return reinterpret_cast<T>(::GetProcAddress(_handle, name));
68 #else
69 return reinterpret_cast<T>(::dlsym(_handle, name));
70 #endif
71 }
72
73 const std::string &path() const { return _path; }
74 Handle handle() const { return _handle; }
75
76 private:
77 Handle _handle = nullptr;
78 std::string _path;
79};
80
83#endif /* !SHARED_LIBRARY_HPP */
Encapsule le handle d'une bibliotheque dynamique. Rien de plus.
Definition SharedLibrary.hpp:25
SharedLibrary & operator=(const SharedLibrary &)=delete
SharedLibrary(const SharedLibrary &)=delete
void * Handle
Definition SharedLibrary.hpp:30
Handle handle() const
Definition SharedLibrary.hpp:74
~SharedLibrary()
Definition SharedLibrary.hpp:53
SharedLibrary(std::string path)
Definition SharedLibrary.hpp:43
T symbol(const char *name) const
Definition SharedLibrary.hpp:65
static const char * extension()
Definition SharedLibrary.hpp:33
const std::string & path() const
Definition SharedLibrary.hpp:73