![]() |
ImFusion C++ SDK 4.4.0
|
#include <ImFusion/Core/PluginManager.h>
Global singleton for loading ImFusion plugins from shared libraries and initializing them. More...
Global singleton for loading ImFusion plugins from shared libraries and initializing them.
ImFusion plugins use a two-phase initialization:
For convenience, you can use the loadPlugin() and loadPlugins() functions that combine both steps into a single function call.
Classes | |
| struct | PluginInfo |
| Record collecting metadata about registered plugins. More... | |
Public Types | |
| enum class | PluginStatus { Unknown , Registered , Incompatible , Initialized , Failed } |
| Enumeration of the set of states an ImFusion plugin can be in during its lifetime. More... | |
| enum class | Status { Success = 0 , FileNotFound , MissingSymbol , Skipped , Incompatible , PluginIdConflict , LicenseCheckFailed , DependencyInitFailed , UnknownPluginId , WrongPluginStatus , Unsupported , InvalidPluginVersion , UnknownError } |
| Enumeration of status/error codes for the member functions of PluginManager. More... | |
Public Member Functions | |
| std::pair< std::string, Status > | registerPlugin (Filesystem::Path filename) |
| Load the shared library from filename and check whether it is compatible with the host application. | |
| std::vector< std::tuple< Filesystem::Path, std::string, Status > > | registerPlugins (const std::vector< Filesystem::Path > &scanDirectories=defaultPluginSearchDirectories(), std::function< bool(const Filesystem::Path &)> filterFunction=nullptr, bool performMagicStringDetection=true) |
| Scans scanDirectories for shared library files and calls registerPlugin() for each suitable file. | |
| Status | initPlugin (const std::string &pluginId) |
| Initialize the given plugin and its dependencies by calling PluginBase::init(). | |
| std::vector< std::pair< std::string, Status > > | initAllRegisteredPlugins (bool logAvailablePlugins=true) |
| Initialize all currently registered plugins by calling PluginBase::init(). | |
| std::pair< std::string, Status > | loadPlugin (const Filesystem::Path &filename) |
| Convenience function combining registerPlugin() and initPlugin() into a single function call. | |
| std::vector< std::tuple< Filesystem::Path, std::string, Status > > | loadPlugins (const std::vector< Filesystem::Path > &scanDirectories=defaultPluginSearchDirectories(), std::function< bool(const Filesystem::Path &)> filterFunction=nullptr, bool performMagicStringDetection=true) |
| Convenience function first calling registerPlugins() and subsequently calling initPlugin() for each previously registered plugin. | |
| void | addLibraryNameToBlacklist (const std::string &libraryName) |
| Add libraryName to the list of blacklisted library names to prevent it from being loaded via loadPlugins(). | |
| bool | isCompatibleImFusionPlugin (const Filesystem::Path &filename) const |
| Checks if filename looks like a library that contains a valid and compatible plugin without actually loading it via the OS. | |
| PluginStatus | pluginStatus (const std::string &pluginId) |
| Returns the current status of the given plugin. | |
| PluginBase * | pluginInstance (const std::string &pluginId) |
| Returns the PluginBase instance with status PluginStatus::Initialized and the given pluginId, or nullptr if no such plugin known, the plugin is not yet initialized, or initialization has failed. | |
| template<typename T> | |
| T * | pluginInstance () |
| Returns the first found PluginBase instance with status PluginStatus::Initialized that is of type T, or nullptr if no such plugin known, the plugin is not yet initialized, or initialization has failed. | |
| const std::vector< PluginInfo > & | allPlugins () const |
| Returns the list of all known plugins, including plugins that are not yet initialized or failed to do so. | |
| void | deinit () |
| Explicitly unload all plugins and deinitialize PluginManager. | |
Static Public Member Functions | |
| static PluginManager & | get () |
| Return the singleton instance. | |
| static std::vector< Filesystem::Path > | defaultPluginSearchDirectories () |
| Returns a list of default search directories for plugins: | |
|
strong |
Enumeration of the set of states an ImFusion plugin can be in during its lifetime.
| Enumerator | |
|---|---|
| Unknown | Plugin has not yet been registered. |
| Registered | Plugin library matches the requirements for an ImFusion plugin and was successfully registered. |
| Incompatible | Plugin was attempted to be registered but its library does not match the necessary requirements. |
| Initialized | Plugin was successfully initialized and ready to be used. |
| Failed | Plugin initialization has failed and can not be used. |
|
strong |
Enumeration of status/error codes for the member functions of PluginManager.
| Enumerator | |
|---|---|
| Success | Operation succeeded without errors. |
| FileNotFound | The library file was not found. |
| MissingSymbol | The library does not export the necessary symbols. |
| Skipped | The library was skipped because it was blacklisted. |
| Incompatible | The library contains a plugin that is not compatible with the host application. |
| PluginIdConflict | A different library file already registered a plugin with the same ID. |
| LicenseCheckFailed | A license check failed. |
| DependencyInitFailed | A plugin dependency could not be initialized. |
| UnknownPluginId | The given plugin ID is unknown. |
| WrongPluginStatus | The current PluginStatus is not suitable for the requested operation. |
| Unsupported | The requested operation is unsupported by the plugin. |
| InvalidPluginVersion | The version of the plugin is not a valid Utils::Version. |
| UnknownError | An unknown error error occurred during the requested operation. |
|
static |
Returns a list of default search directories for plugins:
| std::pair< std::string, Status > ImFusion::PluginManager::registerPlugin | ( | Filesystem::Path | filename | ) |
Load the shared library from filename and check whether it is compatible with the host application.
Plugin state must be PluginStatus::Unknown. Afterwards, the plugin state will either be PluginStatus::Registered on success or PluginStatus::Incompatible in case of failure.
| std::vector< std::tuple< Filesystem::Path, std::string, Status > > ImFusion::PluginManager::registerPlugins | ( | const std::vector< Filesystem::Path > & | scanDirectories = defaultPluginSearchDirectories(), |
| std::function< bool(const Filesystem::Path &)> | filterFunction = nullptr, | ||
| bool | performMagicStringDetection = true ) |
Scans scanDirectories for shared library files and calls registerPlugin() for each suitable file.
This function will only consider files with the platform-specific default extension for shared libraries (Windows: .dll, Linux: .so, MacOS: .dylib), and files that are not blacklisted (cf. addLibraryNameToBlacklist()). Furthermore, each file's content will be scanned for the presence of a magic string before attempting to register it. This avoids loading unrelated libraries into the process memory and the OS executing their static initializers only to have them being unloaded again after realizing that they do not implement the necessary interface.
| scanDirectories | List of local filesystem directories to scan for suitable shared libraries. |
| filterFunction | Optional predicate function to override the built-in library blacklist (cf. addLibraryNameToBlacklist()) with a custom filter function and register only those files found in scanDirectories where the function returns true and ignore files where it returns false. |
| performMagicStringDetection | Some platforms (e.g. MacOS) support optional encryption of the text sections in shared libraries. In this case the detection of the magic string in will fail. You can set this to false as a workaround when working with encrypted libraries. |
| Status ImFusion::PluginManager::initPlugin | ( | const std::string & | pluginId | ) |
Initialize the given plugin and its dependencies by calling PluginBase::init().
Plugin state must be PluginStatus::Registered. Afterwards, the plugin state will either be PluginStatus::Initialized on success or PluginStatus::Failed in case of failure.
| pluginId | Unique plugin ID as returned by the corresponding PluginBase::id() function. |
| std::vector< std::pair< std::string, Status > > ImFusion::PluginManager::initAllRegisteredPlugins | ( | bool | logAvailablePlugins = true | ) |
Initialize all currently registered plugins by calling PluginBase::init().
Only plugins with state PluginStatus::Registered are considered. Afterwards, their plugin state will either be PluginStatus::Initialized on success or PluginStatus::Failed in case of failure.
| logAvailablePlugins | Flag whether to emit a log message with a list of all available plugins at the end of this function call using Log::Level::Info. |
| std::pair< std::string, Status > ImFusion::PluginManager::loadPlugin | ( | const Filesystem::Path & | filename | ) |
Convenience function combining registerPlugin() and initPlugin() into a single function call.
Plugin state must be PluginStatus::Unknown. Afterwards, the plugin state will either be PluginStatus::Initialized on success, or PluginStatus::Incompatible or PluginStatus::Failed in case of failure.
| std::vector< std::tuple< Filesystem::Path, std::string, Status > > ImFusion::PluginManager::loadPlugins | ( | const std::vector< Filesystem::Path > & | scanDirectories = defaultPluginSearchDirectories(), |
| std::function< bool(const Filesystem::Path &)> | filterFunction = nullptr, | ||
| bool | performMagicStringDetection = true ) |
Convenience function first calling registerPlugins() and subsequently calling initPlugin() for each previously registered plugin.
| void ImFusion::PluginManager::addLibraryNameToBlacklist | ( | const std::string & | libraryName | ) |
Add libraryName to the list of blacklisted library names to prevent it from being loaded via loadPlugins().
A library is considered blacklisted if any substring of the filename is part of the list. The blacklist is default-initialized by the value of the IMFUSION_PLUGIN_BLACKLIST environment variable.
| bool ImFusion::PluginManager::isCompatibleImFusionPlugin | ( | const Filesystem::Path & | filename | ) | const |
Checks if filename looks like a library that contains a valid and compatible plugin without actually loading it via the OS.
Internally, this function simply memory-maps the file and searches for the presence of a magic string containing the plugin descriptor. If found it will then check the descriptor for compatibility with the host application.
| PluginStatus ImFusion::PluginManager::pluginStatus | ( | const std::string & | pluginId | ) |
Returns the current status of the given plugin.
| pluginId | Unique plugin ID as returned by the corresponding PluginBase::id() function. |
| PluginBase * ImFusion::PluginManager::pluginInstance | ( | const std::string & | pluginId | ) |
Returns the PluginBase instance with status PluginStatus::Initialized and the given pluginId, or nullptr if no such plugin known, the plugin is not yet initialized, or initialization has failed.
| pluginId | Unique plugin ID as returned by the corresponding PluginBase::id() function. |
| T * ImFusion::PluginManager::pluginInstance | ( | ) |
Returns the first found PluginBase instance with status PluginStatus::Initialized that is of type T, or nullptr if no such plugin known, the plugin is not yet initialized, or initialization has failed.
| T | Concrete type of the plugin instance to dynamic_cast for. |
| void ImFusion::PluginManager::deinit | ( | ) |
Explicitly unload all plugins and deinitialize PluginManager.
Call this function to avoid deinitialization happening during static teardown time (PluginManager singleton destructor).