GemaCoreLib
The GeMA Core library
gmPluginLoader.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
24 #ifndef _GEMA_PLUGIN_LOADER_H_
25 #define _GEMA_PLUGIN_LOADER_H_
26 
27 #include "gmLog.h"
28 
29 #include <QStringList>
30 #include <QMap>
31 #include <assert.h>
32 
33 class QPluginLoader;
34 class GmPluginInfo;
35 class GmPluginObject;
36 class GmSimulationData;
38 
40 
43 {
44 public:
45  GmPluginLoader(const QStringList& pluginPaths);
46  ~GmPluginLoader();
47 
48  bool checkAndStoreLibVersion(const QStringList& libInfo, const QStringList& libDepInfo, QString& err);
49  bool loadPluginList();
50 
51  void printPluginVersionInfo(const GmLogCategory& logger, bool printDep);
52  static void printLibVersionInfo(const GmLogCategory& logger, QString libName, const QStringList& info);
53 
54  QList<GmPluginInfo*> pluginListByCategory(QString cat) const;
55  bool hasCategory(QString cat) const;
56  GmPluginInfo* pluginInfo(QString pluginName) const;
57 
58  GmPluginObject* instancePluginObject(QString objId, QString objTypeName, QString objDescription,
59  GmSimulationData* simData, QString category);
60 
61  GmPluginObject* loadPluginObject(LuaTable& globalTab, LuaTable& objTab,
62  GmSimulationData* simData, QString category);
63 
64  bool loadPluginObjectList(LuaTable& globalTab, GmSimulationData* simData,
65  QString category, QString listName,
66  bool required, GmPluginLoaderAdder* adder);
67 
68  GmPluginObjectFactory* objectFactory(QString pluginName, QString category = "", QString objType = "");
69 
71  const GmLogCategory& logger() { return _logger; }
72 
74  const QMap<QString, GmPluginInfo*>& pluginMap() const { return _pluginInfoMap; }
75 
77  const QMap<QString, GmPluginInfo*>& pluginDataAliasMap() const { return _pluginDataAliasMap; }
78 
79  bool checkPluginDataAttributes(LuaTable& globalTab, QString alias, const LuaTable& objTab);
80 
81 private:
82  bool recursiveLoad (QString pluginName, const GmPluginInfo* info, QString category);
83  bool loadPluginRsrcs (LuaTable& globalTab, const GmPluginInfo* info);
84  bool checkObjectAttributes(LuaTable& globalTab, const GmPluginInfo* info, LuaTable& objTable);
85 
92 
94 };
95 
98 {
99 public:
101  virtual bool addObject(GmPluginObject* obj) = 0;
102 };
103 
109 template <class T, class U> class GmPluginLoaderObjMemberAdder: public GmPluginLoaderAdder
110 {
111 public:
115  typedef bool (U::*UMember)(T* obj);
116 
121  : _containerObj(obj), _containerFunc(f)
122  {}
123 
124  // See coments on the base class
125  virtual bool addObject(GmPluginObject* obj)
126  {
127  // Converts obj to the desired type.
128  // Will we have problems with RTTI since obj was created in a shared library?
129  // If so, and we can't solve the problem with some linker magic, we might
130  // need to make every interface a QObject and use qobject_cast instead of
131  // dynamic_cast. Hoping not.
132  T* tobj = dynamic_cast<T*>(obj);
133  if(!tobj)
134  return false;
135 
136  // Save object in the container
137  bool ret = (_containerObj->*_containerFunc)(tobj);
138  assert(ret); Q_UNUSED(ret); // Previous validations should have ensured that obj->id() is unique among its peers
139  return true;
140  }
141 
142 private:
145 };
146 
147 #endif
148 
bool(U::* UMember)(T *obj)
Typedef for a member of U that receives an object 'obj' of type T* and inserts 'obj' in the U object,...
Definition: gmPluginLoader.h:115
const QMap< QString, GmPluginInfo * > & pluginMap() const
Returns a map with available plugin names and their information object.
Definition: gmPluginLoader.h:74
const GmLogCategory & logger()
Returns a reference to the internal logger used to emit error messages.
Definition: gmPluginLoader.h:71
virtual bool addObject(GmPluginObject *obj)
Virtual function used to add the object to its destination.
Definition: gmPluginLoader.h:125
QMap< QString, GmPluginInfo * > _pluginDataAliasMap
Map associating plugin data aliases with their corresponding plugins.
Definition: gmPluginLoader.h:91
GmLogCategory _logger
Logger object for plugin handling.
Definition: gmPluginLoader.h:93
Base class used to store information about a generic plugin.
Definition: gmPluginInfo.h:38
QMap< QString, GmPluginInfo * > _pluginInfoMap
Map associating plugin names with their information.
Definition: gmPluginLoader.h:87
Factory class exported by every plugin object.
Definition: gmPluginObjectFactory.h:36
QVector< QPluginLoader * > _pluginInstanceList
List storing the same pointers as _pluginInstanceMap, but in their load order.
Definition: gmPluginLoader.h:89
U * _containerObj
The object to which the data will be added.
Definition: gmPluginLoader.h:143
QMap< QString, QString > _revisionMap
Map associating plugin/library paths with their revisions.
Definition: gmPluginLoader.h:90
Auxiliar class used to store the complete set of simulation data.
Definition: gmSimulationData.h:51
QStringList _pathList
List of paths where plugins will be searched.
Definition: gmPluginLoader.h:86
GmPluginLoaderObjMemberAdder(U *obj, UMember f)
Constructor. Receives as parameters the container and the member function used to add an object to it...
Definition: gmPluginLoader.h:120
Class responsible for managing discovery and loading of plugins.
Definition: gmPluginLoader.h:42
Especialization of GmPluginLoaderAdder to add objects to a container 'U' by calling one member functi...
Definition: gmPluginLoader.h:109
Base interface class for all object plugins.
Definition: gmPluginObject.h:36
const QMap< QString, GmPluginInfo * > & pluginDataAliasMap() const
Returns a map with registered plugin data alias names and their information object.
Definition: gmPluginLoader.h:77
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
UMember _containerFunc
The method from _containerObj that will be called to add data to it.
Definition: gmPluginLoader.h:144
Auxiliar class used to add objects parsed by GmPluginLoader::loadPluginObjectList() to it's destiny.
Definition: gmPluginLoader.h:97
QMap< QString, QPluginLoader * > _pluginInstanceMap
Map associating plugin names with their implementation.
Definition: gmPluginLoader.h:88
Declaration of support functions and macros for information logging.