Tulip  5.7.4
Large graphs analysis and drawing
PluginLister.h
1 /*
2  *
3  * This file is part of Tulip (https://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TULIP_PLUGINLISTER_H
21 #define TULIP_PLUGINLISTER_H
22 
23 #include <list>
24 #include <string>
25 #include <map>
26 
27 #include <tulip/Iterator.h>
28 #include <tulip/Plugin.h>
29 #include <tulip/PluginLoader.h>
30 #include <tulip/Observable.h>
31 
32 namespace tlp {
33 class PluginContext;
34 
35 /**
36  * @ingroup Plugins
37  *
38  * @brief The PluginLister class is a singleton used to list plugins currently loaded into Tulip and
39  * retrieve information about them.
40  *
41  * This class holds various methods to check information about plugins currently loaded into Tulip.
42  * You can use it to list plugins, get dependencies information or create an instance of a plugin.
43  *
44  * @note Since a plugin name is unique, Plugins are mainly identified by their name
45  * (tlp::Plugin::name()) when interfaced with the plugin lister.
46  *
47  * @see tlp::Plugin
48  * @see tlp::PluginLoader
49  * @see tlp::PluginLibraryLoader
50  */
51 class TLP_SCOPE PluginLister {
52 private:
53  static tlp::Plugin *registeredPluginObject(const std::string &name);
54 
55  static Iterator<Plugin *> *registeredPluginObjects();
56 
57 public:
58  /**
59  * @brief Checks if all registered plug-ins have their dependencies met.
60  *
61  * @param loader If there are errors, the loader is informed about them so they can be displayed.
62  * @return void
63  **/
65 
66  /**
67  * @brief Constructs a plug-in.
68  *
69  * @param name The name of the plug-in to instantiate.
70  * @param p The context to give to the plug-in.
71  * @return ObjectType* The newly constructed plug-in.
72  **/
73  static tlp::Plugin *getPluginObject(const std::string &name,
74  tlp::PluginContext *context = nullptr);
75 
76  /**
77  * @brief Checks if a plugin of a given type is loaded
78  * This method checks the plugin "pluginName" is currently loaded into Tulip and if it's of type
79  * PluginType.
80  * @param PluginType the class type of the plugin
81  * @param pluginName the name of the plugin
82  * @return true if a matching plugin is currently loaded into Tulip.
83  */
84  template <typename PluginType>
85  static bool pluginExists(const std::string &pluginName) {
86  Plugin *plugin = registeredPluginObject(pluginName);
87  return dynamic_cast<PluginType *>(plugin) != nullptr;
88  }
89 
90  /**
91  * @brief Similar to tlp::PluginLister::getPluginObject() but returns a typed instance
92  *
93  * This method instantiate a plugin from its name and returns it casted into the given type.
94  *
95  * @param name The plugin's name
96  * @param context The context to give to the plugin
97  *
98  * @return The plugin instance. If there is no such plugin or if the plugin does not match the
99  * required type, this method returns nullptr
100  */
101  template <typename PluginType>
102  static PluginType *getPluginObject(const std::string &name,
103  tlp::PluginContext *context = nullptr) {
104  auto plugin = getPluginObject(name, context);
105  return dynamic_cast<PluginType *>(plugin);
106  }
107 
108  /**
109  * @brief Gets the list of plugins
110  * @return A std::list<std::string> containing plugin names.
111  **/
112  static std::list<std::string> availablePlugins();
113 
114  /**
115  * @brief Gets the list of plugins of a given type (template parameter).
116  * @return A std::list<std::string> containing plugin names.
117  **/
118  template <typename PluginType>
119  static std::list<std::string> availablePlugins() {
120  std::list<std::string> keys;
121 
122  for (auto plugin : registeredPluginObjects()) {
123  if (dynamic_cast<PluginType *>(plugin))
124  keys.push_back(plugin->name());
125  }
126 
127  return keys;
128  }
129 
130  /**
131  * @brief Gets more detailed information about one specific plug-in.
132  *
133  * @param name The name of the plugin to retrieve information for.
134  * @return :const Plugin& The information on the plugin.
135  **/
136  static const Plugin &pluginInformation(const std::string &name);
137 
138  /**
139  * @brief Checks if a given name is registered in this factory.
140  *
141  * @param pluginName The name of the plug-in to look for.
142  * @return bool Whether there is a plug-in with the given name registered in this factory.
143  **/
144  static bool pluginExists(const std::string &pluginName);
145 
146  /**
147  * @brief Gets the list of parameters for the given plug-in.
148  *
149  * @param name The name of the plug-in to retrieve the parameters of.
150  * @return :ParameterDescriptionList The parameters of the plug-in.
151  **/
152  static const ParameterDescriptionList &getPluginParameters(const std::string &name);
153 
154  /**
155  * @brief Gets the dependencies of a plug-in.
156  *
157  * @param name The name of the plug-in to retrieve the dependencies of.
158  * @return :list< tlp::Dependency, std::allocator< tlp::Dependency > > The list of dependencies of
159  *the plug-in.
160  **/
161  static const std::list<tlp::Dependency> &getPluginDependencies(const std::string &name);
162 
163  /**
164  * @brief Gets the library from which a plug-in has been loaded.
165  *
166  * @param name The name of the plug-in to retrieve the library of.
167  * @return std::string The library from which the plug-in was loaded.
168  **/
169  static std::string getPluginLibrary(const std::string &name);
170 
171  /**
172  * @brief Removes a plug-in from this factory.
173  * This is useful when a plug-in has unmet dependencies, or appears more than once.
174  *
175  * @param name The name of the plug-in to remove.
176  * @return void
177  **/
178  static void removePlugin(const std::string &name);
179 
180  /**
181  * @brief Registers a plugin into Tulip
182  *
183  * @warning This method should only be called by tlp::PluginFactory subclasses
184  * @see PLUGIN
185  */
186  static void registerPlugin(PluginFactory *objectFactory);
187 
188  ///@cond DOXYGEN_HIDDEN
189 protected:
190  /**
191  * @brief Gets the release number of the given plug-in.
192  *
193  * @param name The name of the plug-in to retrieve the version number of.
194  * @return :string The version number, usually formatted as X[.Y], where X is the major, and Y
195  *the minor.
196  **/
197  static std::string getPluginRelease(const std::string &name);
198  ///@endcond
199 };
200 
201 ///@cond DOXYGEN_HIDDEN
202 class TLP_SCOPE PluginEvent : public Event {
203 public:
204  enum PluginEventType { TLP_ADD_PLUGIN = 0, TLP_REMOVE_PLUGIN = 1 };
205 
206  // constructor for node/edge events
207  PluginEvent(const Observable &sender, PluginEventType pluginEvtType,
208  const std::string &pluginName)
209  : Event(sender, Event::TLP_MODIFICATION), evtType(pluginEvtType), pluginName(pluginName) {}
210 
211  PluginEventType getType() const {
212  return evtType;
213  }
214 
215  std::string getPluginName() const {
216  return pluginName;
217  }
218 
219  static void addListener(Observable *);
220 
221 protected:
222  PluginEventType evtType;
223  std::string pluginName;
224 };
225 ///@endcond
226 } // namespace tlp
227 
228 #endif // TULIP_PLUGINLISTER_H
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
Contains runtime parameters for a plugin.
Definition: PluginContext.h:42
Top-level interface for plug-ins.
Definition: Plugin.h:86
The PluginLister class is a singleton used to list plugins currently loaded into Tulip and retrieve i...
Definition: PluginLister.h:51
static std::list< std::string > availablePlugins()
Gets the list of plugins.
static std::string getPluginLibrary(const std::string &name)
Gets the library from which a plug-in has been loaded.
static const std::list< tlp::Dependency > & getPluginDependencies(const std::string &name)
Gets the dependencies of a plug-in.
static bool pluginExists(const std::string &pluginName)
Checks if a given name is registered in this factory.
static const ParameterDescriptionList & getPluginParameters(const std::string &name)
Gets the list of parameters for the given plug-in.
static void checkLoadedPluginsDependencies(tlp::PluginLoader *loader)
Checks if all registered plug-ins have their dependencies met.
static void registerPlugin(PluginFactory *objectFactory)
Registers a plugin into Tulip.
static std::list< std::string > availablePlugins()
Gets the list of plugins of a given type (template parameter).
Definition: PluginLister.h:119
static PluginType * getPluginObject(const std::string &name, tlp::PluginContext *context=nullptr)
Similar to tlp::PluginLister::getPluginObject() but returns a typed instance.
Definition: PluginLister.h:102
static const Plugin & pluginInformation(const std::string &name)
Gets more detailed information about one specific plug-in.
static bool pluginExists(const std::string &pluginName)
Checks if a plugin of a given type is loaded This method checks the plugin "pluginName" is currently ...
Definition: PluginLister.h:85
static tlp::Plugin * getPluginObject(const std::string &name, tlp::PluginContext *context=nullptr)
Constructs a plug-in.
static void removePlugin(const std::string &name)
Removes a plug-in from this factory. This is useful when a plug-in has unmet dependencies,...
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:74
A callback class when loading plugins into Tulip.
Definition: PluginLoader.h:41