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