lfilesystem  0.0.1
C++ filesystem library
lfilesystem_DynamicLibrary.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #if defined(_WIN32) || defined(WIN32)
18 # include <windows.h>
19 #endif
20 
21 #include <string_view>
22 #include <string>
23 #include <atomic>
24 #include <vector>
25 #include "lfilesystem/lfilesystem_Export.h"
28 
35 namespace limes::files
36 {
37 
57 class LFILE_EXPORT DynamicLibrary final
58 {
59 public:
62 
64  DynamicLibrary() = default;
65 
69  explicit DynamicLibrary (const std::string_view& nameOrPath) noexcept;
70 
72 
77 
78  DynamicLibrary (DynamicLibrary&& other) noexcept;
79  DynamicLibrary& operator= (DynamicLibrary&& other) noexcept;
80 
81  DynamicLibrary (const DynamicLibrary&) = delete;
82  DynamicLibrary& operator= (const DynamicLibrary&) = delete;
83 
84 #if (defined(_WIN32) || defined(WIN32)) && ! defined(DOXYGEN)
85  using Handle = HMODULE;
86 #else
87  using Handle = void*;
88 #endif
89 
97  [[nodiscard]] bool operator== (const DynamicLibrary& other) const noexcept;
98 
100  [[nodiscard]] bool operator!= (const DynamicLibrary& other) const noexcept;
101 
105  [[nodiscard]] bool isOpen() const noexcept;
106 
108  [[nodiscard]] Handle getHandle() const noexcept;
109 
111  [[nodiscard]] void* findFunction (const std::string_view& functionName) noexcept;
112 
122  bool open (const std::string_view& nameOrPath) noexcept;
123 
132  void close();
133 
143  bool reload();
144 
150  [[nodiscard]] File getFile() const;
151 
153  [[nodiscard]] std::string getName() const;
154 
161  class LFILE_EXPORT Reloader final : public FileWatcher
162  {
163  public:
165  explicit Reloader (DynamicLibrary& libraryToReload);
166 
167  private:
168  void fileDeleted (const FilesystemEntry&) final;
169  void fileModified (const FilesystemEntry&) final;
170 
171  DynamicLibrary& library;
172  };
173 
180  class LFILE_EXPORT Listener
181  {
182  public:
184  explicit Listener (DynamicLibrary& library);
185 
187  virtual ~Listener();
188 
195  virtual void libraryOpened (bool /*wasSuccessful*/) { }
196 
203  virtual void libraryClosed() { }
204 
212  virtual void libraryReloaded (bool /*wasSuccessful*/) { }
213 
214  private:
215  DynamicLibrary& lib;
216  };
217 
218 private:
219  std::atomic<Handle> handle { nullptr };
220 
221  std::vector<Listener*> listeners;
222 
223  bool suppressNotifs { false };
224 };
225 
226 } // namespace limes::files
227 
228 namespace std
229 {
230 
236 template <>
237 struct LFILE_EXPORT hash<limes::files::DynamicLibrary> final
238 {
239  size_t operator() (const limes::files::DynamicLibrary& l) const noexcept;
240 };
241 
242 } // namespace std
This class listens for events to a DynamicLibrary and receives a callback when the library is opened,...
virtual void libraryOpened(bool)
Called when the library is opened.
Listener(DynamicLibrary &library)
Constructs a listener.
virtual void libraryReloaded(bool)
Called when DynamicLibrary::reload() is called.
virtual void libraryClosed()
Called when the library is closed.
This class watches a dynamic library file for changes on disk.
Reloader(DynamicLibrary &libraryToReload)
Creates a Reloader object watching the specified library.
This class represents a dynamically loaded library.
DynamicLibrary()=default
Creates an unopened library object.
void * Handle
The type of platform-specific handle used for dynamic libraries.
bool isOpen() const noexcept
Returns true if the library is currently open.
DynamicLibrary(const std::string_view &nameOrPath) noexcept
Creates a library object and attempts to open the specified library.
This class listens for changes to or operations on a certain file, and receives callbacks to be notif...
This class represents a file on the filesystem.
The base class for any kind of object on the filesystem.
This file defines the files::FileWatcher class.
This file defines the File and TempFile classes.
Filesystem utilities.