imodule 1.0.0
The contract every loadable module answers to
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
28 public:
29 #if defined(_WIN32)
30 using Handle = HMODULE;
31 #else
32 using Handle = void *;
33 #endif
34
36 static const char *extension() {
37 #if defined(_WIN32)
38 return ".dll";
39 #elif defined(__APPLE__)
40 return ".dylib";
41 #else
42 return ".so";
43 #endif
44 }
45
46 explicit SharedLibrary(std::string path) : _path(std::move(path)) {
47 #if defined(_WIN32)
48 _handle = ::LoadLibraryA(_path.c_str());
49 #else
50 _handle = ::dlopen(_path.c_str(), RTLD_NOW | RTLD_LOCAL);
51 #endif
52 if (!_handle)
53 throw std::runtime_error("SharedLibrary: " + _path);
54 }
55
57 #if defined(_WIN32)
58 if (_handle) ::FreeLibrary(_handle);
59 #else
60 if (_handle) ::dlclose(_handle);
61 #endif
62 }
63
64 SharedLibrary(const SharedLibrary &) = delete;
66
73 template <typename T>
74 T symbol(const char *name) const {
75 #if defined(_WIN32)
76 return reinterpret_cast<T>(::GetProcAddress(_handle, name));
77 #else
78 return reinterpret_cast<T>(::dlsym(_handle, name));
79 #endif
80 }
81
83 const std::string &path() const { return _path; }
84
86 Handle handle() const { return _handle; }
87
88 private:
89 Handle _handle = nullptr;
90 std::string _path;
91};
92
95#endif /* !SHARED_LIBRARY_HPP */
Une bibliotheque dynamique ouverte : elle se ferme avec l'objet.
Definition SharedLibrary.hpp:27
SharedLibrary & operator=(const SharedLibrary &)=delete
SharedLibrary(const SharedLibrary &)=delete
void * Handle
Definition SharedLibrary.hpp:32
Handle handle() const
Le handle systeme, pour qui en a besoin.
Definition SharedLibrary.hpp:86
~SharedLibrary()
Definition SharedLibrary.hpp:56
SharedLibrary(std::string path)
Definition SharedLibrary.hpp:46
T symbol(const char *name) const
Un symbole exporte, rendu comme un T.
Definition SharedLibrary.hpp:74
static const char * extension()
L'extension des bibliotheques de cette plateforme.
Definition SharedLibrary.hpp:36
const std::string & path() const
Le chemin dont elle a ete ouverte.
Definition SharedLibrary.hpp:83