OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PluginManagerTemplates.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
6 
9 
10 namespace OpenAB
11 {
12 template<typename __C, typename __P>
13 __C* PluginManager::getPluginInstance(const std::string& pluginName, const __P& params)
14 {
15  __C* newInstance = NULL;
16  if (NULL == OpenAB_Plugin::Factory< __C , __P >::factories[pluginName])
17  {
18  if (isPluginAvailable(pluginName))
19  {
20  LOG_INFO() << "Loading module "<<pluginsInfo[pluginName]<<std::endl;
21  loadModule(pluginsInfo[pluginName]);
22  }
23  }
24 
25  if (NULL != OpenAB_Plugin::Factory< __C , __P >::factories[pluginName])
26  {
27  loadedModulesInfo[pluginsInfo[pluginName]].refCount++;
28  newInstance = OpenAB_Plugin::Factory< __C , __P >::factories[pluginName]->newIstance(params);
29  if (newInstance)
30  {
31  pluginInstancesInfo.insert(std::pair<void*, std::string>(newInstance, pluginName));
32  }
33  }
34 
35  return newInstance;
36 }
37 
38 template<typename __C>
40 {
41  if (pluginInstancesInfo.find(instance) == pluginInstancesInfo.end())
42  {
43  LOG_ERROR() << "Plugin instance "<<(long long)instance<<" was not created using getPluginInstance"<<std::endl;
44  return;
45  }
46  std::string moduleName = getPluginModuleName(pluginInstancesInfo[instance]);
47  loadedModulesInfo[moduleName].refCount--;
48  pluginInstancesInfo.erase(instance);
49  delete instance;
50  if (loadedModulesInfo[moduleName].refCount <= 0)
51  {
52  LOG_INFO() << "Unloading module "<<moduleName<<std::endl;
53  // Find a way to unregister gobject types, so module can be safetly unloaded.
54  //unloadModule(moduleName);
55  }
56 }
57 
58 }
__C * getPluginInstance(const std::string &pluginName, const __P &params)
Creates instance of a plugin using plugin specific parameters to initialize it.
Definition: PluginManagerTemplates.hpp:13
virtual __C * newIstance(const __P &)=0
#define LOG_ERROR()
Definition: Log.hpp:117
Definition: Plugin.hpp:133
bool isPluginAvailable(const std::string &pluginName) const
Checks if given plugin is provided by any module.
Definition: PluginManager.cpp:121
void freePluginInstance(__C *instance)
Frees plugin instance created by getPluginInstance function. Apart from removing instance it will che...
Definition: PluginManagerTemplates.hpp:39
#define LOG_INFO()
Definition: Log.hpp:135