Loading...
Searching...
No Matches
PluginManager.h
Go to the documentation of this file.
1//
2// PluginManager.h
3//
4
5#ifndef PLUGINHOST_H
6#define PLUGINHOST_H
7
12
13#ifdef _WIN32
14 #include <windows.h>
15 #define LIB_HANDLE HMODULE
16 #define LOAD_LIB(path) LoadLibraryA(path)
17 #define GET_FUNC(handle,name) GetProcAddress(handle, name)
18 #define CLOSE_LIB(handle) FreeLibrary(static_cast<HMODULE>(handle))
19#else
20 #include <dlfcn.h>
21 #define LIB_HANDLE void*
22 #define LOAD_LIB(path) dlopen(path, RTLD_NOW | RTLD_GLOBAL)
23 #define GET_FUNC(handle,name) dlsym(handle, name)
24 #define CLOSE_LIB(handle) dlclose(handle)
25#endif
26
27namespace fs = std::filesystem;
28
29namespace ml
30{
78 class PluginManager : public DeferredOperationsManager<PluginManager>
79 {
80 struct PluginData
81 {
82 Plugin* plugin;
83 LIB_HANDLE handle;
84 };
85
86 std::vector<PluginData> _plugins;
87
88 std::vector<std::string> getPluginFiles(const std::string& directory);
89 std::vector<std::string> getFilesInDirectory(const std::string& directory,
90 const std::string& extension = "");
91
92 public:
99 PluginManager(const std::string& pluginPath = "");
100
113 Plugin* loadPlugin(const std::string& path);
114
123 void loadPluginsFromDirectory(const std::string& dir);
124
134 std::vector<Plugin*> getPlugins();
135
153 void unloadPlugin(Plugin* plugin);
154
166 std::vector<PluginInfo> scanPlugins(const std::string& dir);
167
168 private:
177 void doUnloadPlugin(Plugin* plugin);
178 };
179
180} // namespace ml
181
182#endif // PLUGINHOST_H
#define LIB_HANDLE
CRTP base that gives a manager safe deferred-operation support.
Abstract base class for all Malena plugins.
Definition Plugin.h:77
void unloadPlugin(Plugin *plugin)
Unload a plugin and close its shared library.
Plugin * loadPlugin(const std::string &path)
Load a single plugin from a shared library path.
void loadPluginsFromDirectory(const std::string &dir)
Load all plugin libraries found in a directory.
std::vector< Plugin * > getPlugins()
Return all currently loaded plugins.
PluginManager(const std::string &pluginPath="")
Construct a PluginManager with a default plugin search path.
std::vector< PluginInfo > scanPlugins(const std::string &dir)
Probe a directory for plugins without keeping them loaded.
Definition Component.h:18