OpenLexocad  27.0
LibraryLoader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <windows.h>
4 
5 namespace Core
6 {
11 class LibraryLoader final
12 {
13 public:
14  HMODULE handle;
15 
16  inline LibraryLoader() : handle(nullptr) {}
17  inline LibraryLoader(const wchar_t* fileName) : handle(LoadLibrary(fileName)) {}
18  inline LibraryLoader(HMODULE h) : handle(h) {}
19  inline LibraryLoader(LibraryLoader&& ll) : handle(ll.handle) { ll.handle = nullptr; }
21  {
22  if (handle)
23  FreeLibrary(handle);
24  handle = ll.handle;
25  ll.handle = nullptr;
26  }
27  inline ~LibraryLoader()
28  {
29  if (handle)
30  FreeLibrary(handle);
31  }
32 
33  LibraryLoader(const LibraryLoader&) = delete;
34  LibraryLoader& operator=(const LibraryLoader&) = delete;
35 
36  inline bool valid() const { return handle != nullptr; }
37  inline bool load(const wchar_t* filename)
38  {
39  if (handle)
40  FreeLibrary(handle);
41  handle = LoadLibrary(filename);
42  return handle != nullptr;
43  }
44  inline void free()
45  {
46  if (handle)
47  {
48  FreeLibrary(handle);
49  handle = nullptr;
50  }
51  }
52  template <typename T>
53  inline T resolve(const char* funcName)
54  {
55  return reinterpret_cast<T>(GetProcAddress(handle, funcName));
56  }
57 };
58 
59 
60 } // namespace App
bool load(const wchar_t *filename)
Definition: LibraryLoader.h:37
LibraryLoader()
Definition: LibraryLoader.h:16
LibraryLoader(HMODULE h)
Definition: LibraryLoader.h:18
HMODULE handle
Definition: LibraryLoader.h:14
Core::PropertyText filename
Definition: CoreDocument.h:151
Definition: Base.h:19
bool valid() const
Definition: LibraryLoader.h:36
Definition: LibraryLoader.h:11
T resolve(const char *funcName)
Definition: LibraryLoader.h:53
LibraryLoader & operator=(LibraryLoader &&ll)
Definition: LibraryLoader.h:20
~LibraryLoader()
Definition: LibraryLoader.h:27
LibraryLoader(const wchar_t *fileName)
Definition: LibraryLoader.h:17
Base::String fileName
Definition: CoreDocument.h:161
void free()
Definition: LibraryLoader.h:44
LibraryLoader(LibraryLoader &&ll)
Definition: LibraryLoader.h:19