#include <Corrade/PluginManager/Manager.h>
template<class T>
Manager class
Plugin manager.
Manages loading, instancing and unloading plugins. See Plugin management for a high-level introduction.
Plugin directories
Plugins are searched in the following directories, in order:
- If a non-empty
pluginDirectory
was passed to the Manager(Containers::StringView) constructor, plugins are searched there. If the directory doesn't exist, no search is performed and the process stops here. - Otherwise, it's expected that given plugin interface defined AbstractPlugin::
pluginSearchPaths(). The search goes through the entries and stops once an existing directory is found.
In both cases the path of Utility::
Besides the above, it's possible to call load() with a concrete path to a dynamic module file to load a plugin from outside of the plugin directory. The file is expected to be accompanied by its corresponding *.conf
metadata file (unless overridden in the plugin interface using AbstractPlugin::
Plugin loading, instantiation and unloading
A plugin is loaded by calling load() with given plugin name or alias. After that, you can get one or more instances using instantiate(). For convenience, loadAndInstantiate() combines these two operations into one.
Plugin is unloaded either by calling unload() or on plugin manager destruction. By default, all active plugin instances have to be deleted before a plugin can be unloaded. For hot reloading and other specific use cases it's possible to unload a plugin that still has active instances. For that to work, AbstractPlugin::true
for all active instances, otherwise unload() returns LoadState::
Plugin-specific data and configuration
Besides the API provided by a particular plugin interface after given plugin is instantiated, plugins can also define various additional metadata that can be accessed even if the plugin is not loaded. That can be used for example to provide extra information to users in a plugin listing UI. Plugin-specific data can be accessed via PluginMetadata::
In order to make it possible to configure behavior of specific plugins on top of what the general plugin interface provides, the AbstractPlugin::
See PluginMetadata for detailed description of the plugin metadata file.
Plugin dependencies
Plugins can depend on each other by listing then in the metadata file, as described in the PluginMetadata class documentation. From the user point-of-view the dependency handling is completely transparent — loading a plugin will also load all its dependencies, fail if some dependencies can't be found, and disallow the plugin from being unloaded when some other still needs it.
A special (and rarer) case is inter-manager dependencies. By default, to avoid shared mutable global state, plugin manager instances don't know about each other, so if a plugin depends on another from a different interface, you need to instantiate a manager for the other interface and register it with the original manager using registerExternalManager().
Multiple instances of the same manager
It's possible to have more than one instance of the same manager as well as loading and instantiating the same plugin in more than one manager at the same time. For static plugins there's no reason this could work, for dynamic both the Unix dlopen() and Windows LoadLibrary() load the library only once and maintain reference count to ensure it's unloaded only after it's not needed anymore. The only way you may run into problem is by loading the same plugin binary twice under different filenames, causing the loader to have symbol conflicts, violating ODR.
Custom plugin interfaces and template definitions
To avoid pulling in relatively big includes, the Corrade/
The easiest is to simply include Corrade/*.h
file in order to get the full definitions. If you however want to avoid the additional overhead of the template instantiations and includes it pulls in, it's recommended to include it only in the source file where you define the plugin interface and perform an explicit template instantiation:
#include <Corrade/PluginManager/Manager.hpp> namespace MyNamespace { class MyAbstractPlugin: public PluginManager::AbstractPlugin { … }; } namespace Corrade { namespace PluginManager { template class Manager<MyNamespace::MyAbstractPlugin>; }}
The namespace where you locate the template class
is important. Additionally, if the plugin interface is exposed in a shared library, you may need to export the template instantiation symbols for example with CORRADE_
Thread safety
Static plugins register themselves into a global storage. If done implicitly, the registration is executed before entering main()
and thus serially. If done explicitly via CORRADE_
On the other hand, all other functionality only reads from the global storage and thus is thread-safe.
Base classes
- class AbstractManager
- Base for plugin managers.
Constructors, destructors, conversion operators
-
Manager(Containers::
StringView pluginDirectory = {}) explicit - Constructor.
Public functions
-
auto instantiate(Containers::
StringView plugin) -> Containers:: Pointer<T> - Instantiate a plugin.
-
auto loadAndInstantiate(Containers::
StringView plugin) -> Containers:: Pointer<T> - Load and instantiate plugin.
-
template<class U>auto externalManager() -> Manager<U>* new in Git master
- Retrieve an external plugin manager.
Function documentation
template<class T>
Corrade:: PluginManager:: Manager<T>:: Manager(Containers:: StringView pluginDirectory = {}) explicit
Constructor.
Parameters | |
---|---|
pluginDirectory | Optional directory where plugins will be searched. See Plugin directories for more information. |
First goes through list of static plugins and finds ones that use the same interface as this manager instance. Then gets list of all dynamic plugins in given directory.
template<class T>
Containers:: Pointer<T> Corrade:: PluginManager:: Manager<T>:: instantiate(Containers:: StringView plugin)
Instantiate a plugin.
Returns new instance of given plugin. The plugin must be already successfully loaded by this manager. The returned value is never nullptr
.
template<class T>
Containers:: Pointer<T> Corrade:: PluginManager:: Manager<T>:: loadAndInstantiate(Containers:: StringView plugin)
Load and instantiate plugin.
Convenience alternative to calling both load() and instantiate(). If loading fails, nullptr
is returned.
As with load(), it's possible to pass a file path to plugin
. See its documentation for more information. The resulting plugin name is then loaded using instantiate() as usual.
template<class T>
template<class U>
Manager<U>* Corrade:: PluginManager:: Manager<T>:: externalManager() new in Git master
Retrieve an external plugin manager.
If a plugin manager of type U
was passed to registerExternalManager() earlier, returns a pointer to it, otherwise returns nullptr
. The manager type is matched using pluginInterface(), thus it's expected that the plugin defines it to a non-empty and unique value.