Here is how I have implemented it in my application.
It is not really efficient, because pluma currently lacks feature to have the address of the newly loaded plug-in.
template <class T>
T *getProvider(pluma::Pluma &plumConf, std::string const &location, std::string const &pluginName)
{
std::vector<T *> prevProviders, actualProviders;
plumConf.getProviders(prevProviders);
if(plumConf.load(location, pluginName))
{
// register loaded provider
decltype(actualProviders.size()) i = 0;
plumConf.getProviders(actualProviders);
//Locate the provider newly loaded
while(i < actualProviders.size() && prevProviders.end() != std::find(prevProviders.begin(), prevProviders.end(), actualProviders[i]))
++i;
// if it was found, return it.
if(i <= actualProviders.size())
return actualProviders[i];
}
return nullptr;
}
Some hints could be:
_ make "bool pluma::Pluma::load(const std::string &)" return the address of the new provider
_ implement a function/method like "Provider::getProvider(std::string const &plugName)"
The use of this feature is to be able to load only specific plug-ins when the application needs it, by example if the user disable some feature.
Of course, performances is not critical on plugin loading so the problem is not a very big one, but I like to keep things as lightweight as possible :)
The code you shown doesn't take in account that a plugin can supply more than one provider.
You can load plugins individually, you can tell the application to load some files and not others.
If you want to disable specific providers you can select the type of providers to load, or if they are all of the same type, use custom provider methods to decide what objects to instantiate (see here).
For example, add a method on the provider returning something that identifies it uniquely (a description for instance). Then a settings menu can get all descriptions from available providers and let user enable/disable them. Finally when populating objects from providers you can check if the description corresponds to an enabled option, and decide to populate it or not.