Ocean
Loading...
Searching...
No Matches
PluginManager.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_BASE_PLUGIN_MANAGER_H
9#define META_OCEAN_BASE_PLUGIN_MANAGER_H
10
11#include "ocean/base/Base.h"
14
15#include <set>
16#include <vector>
17
18namespace Ocean
19{
20
21/**
22 * This class implements a manager for all plugins available for the Ocean framework.
23 * @ingroup base
24 */
25class OCEAN_BASE_EXPORT PluginManager : public Singleton<PluginManager>
26{
27 friend class Singleton<PluginManager>;
28
29 public:
30
31 /**
32 * Definition of different plugin types.
33 */
35 {
36 /// Unknown plugin type.
37 TYPE_UNKNOWN = 0,
38 /// Device plugin.
39 TYPE_DEVICE = 1 << 0,
40 /// Interaction plugin.
41 TYPE_INTERACTION = 1 << 1,
42 /// Media plugin.
43 TYPE_MEDIA = 1 << 2,
44 /// Rendering plugin.
45 TYPE_RENDERING = 1 << 3,
46 /// Scene description plugin.
47 TYPE_SCENEDESCRIPTION = 1 << 4,
48 /// Physics plugin.
49 TYPE_PHYSICS = 1 << 5,
50 /// Any plugin
51 TYPE_ANY = TYPE_DEVICE | TYPE_INTERACTION | TYPE_MEDIA | TYPE_RENDERING | TYPE_SCENEDESCRIPTION | TYPE_PHYSICS
52 };
53
54 /**
55 * Definition of different plugin load priority values.
56 * Higher priority means that the plugin will be loaded earlier than plugins with lower priority.
57 */
59 {
60 /// Undefined plugin load priority.
62 /// Low plugin load priority.
64 /// Medium plugin load priority.
66 /// High plugin load priority.
68 /// Very high plugin load priority.
69 PRIORITY_VERY_HIGH
70 };
71
72 /**
73 * Definition of a set holding plugin types.
74 */
75 typedef std::set<PluginType> PluginTypeSet;
76
77 /**
78 * Definition of a vector holding plugin names.
79 */
80 typedef std::vector<std::string> Names;
81
82 private:
83
84 /**
85 * This class implements a plugin interface.
86 */
87 class OCEAN_BASE_EXPORT Plugin
88 {
89 protected:
90
91 /**
92 * Definition of a function pointer for plugin load functions.
93 */
94 typedef bool (*PluginLoadFunction)();
95
96 /**
97 * Definition of a function pointer for plugin unload functions.
98 */
99 typedef bool (*PluginUnloadFunction)();
100
101 /**
102 * Definition of a function pointer for plugin version information functions.
103 */
104 typedef const char* (*PluginVersionFunction)();
105
106 public:
107
108 /**
109 * Creates an empty plugin object.
110 */
111 Plugin() = default;
112
113 /**
114 * Creates a new plugin object.
115 * @param filename Plugin filename
116 * @param name Plugin name
117 * @param description Plugin description
118 * @param type Plugin type
119 * @param priority Plugin load priority, the higher the priority the earlier the plugin will be loaded
120 * @param dependencySet Set of ocean plugin types this plugin depends on.
121 * @param thirdpartyDependences 3rd party dependences
122 * @param thirdpartyDescription 3rd party description
123 */
124 Plugin(const std::string& filename, const std::string& name, const std::string& description, const PluginType type, const PluginPriority priority, const PluginTypeSet& dependencySet, const std::string& thirdpartyDependences, const std::string& thirdpartyDescription);
125
126 /**
127 * Returns the filename of the plugin.
128 * @return Plugin filename
129 */
130 inline const std::string& filename() const;
131
132 /**
133 * Returns the name of the plugin.
134 * @return Plugin name
135 */
136 inline const std::string& name() const;
137
138 /**
139 * Returns the description of the plugin.
140 * @return Plugin description
141 */
142 inline const std::string& description() const;
143
144 /**
145 * Returns the version information of the used 3rd party libraries.
146 * @return Version information
147 */
148 inline const std::string& thirdpartyInformation() const;
149
150 /**
151 * Returns the type of this plugin.
152 * @return Plugin type
153 */
154 inline PluginType type() const;
155
156 /**
157 * Returns the 3rd party dependences of this plugin.
158 * @return 3rd party dependences
159 */
160 inline const std::string& thirdpartyDependences() const;
161
162 /**
163 * Returns the description about the 3rd party dependences of this plugin.
164 * @return 3rd party description
165 */
166 inline const std::string& thirdpartyDescription() const;
167
168 /**
169 * Loads the plugin.
170 * @return True, if succeeded
171 */
172 bool load() const;
173
174#if defined(__APPLE__)
175
176 /**
177 * Loads the plugin (specialization for Apple platforms)
178 * @sa load()
179 * @return True, if succeeded
180 */
181 bool loadApple() const;
182
183#endif // defined(__APPLE__)
184
185 /**
186 * Tries to unload the plugin.
187 * All resources using this plugin have to be released to unload the plugin.
188 * @return True, if succeeded
189 */
190 bool unload() const;
191
192#if defined(__APPLE__)
193
194 /**
195 * Tries to unload the plugin. (specialization for Apple platforms)
196 * All resources using this plugin have to be released to unload the plugin.
197 * @sa unload()
198 * @return True, if succeeded
199 */
200 bool unloadApple() const;
201
202#endif // defined(__APPLE__)
203
204 /**
205 * Returns whether the plugin is successfully loaded.
206 * @return True, if so
207 */
208 explicit operator bool() const;
209
210 /**
211 * Returns whether the left plugin has to be loaded before the right one.
212 * @param right Right plugin
213 * @return True, if so
214 */
215 bool operator<(const Plugin& right) const;
216
217 protected:
218
219 /// Filename of the plugin.
220 std::string filename_;
221
222 /// Name of the plugin.
223 std::string name_;
224
225 /// Description of the plugin.
226 std::string description_;
227
228 /// 3rd party library version information.
230
231 /// Platform specific plugin handle.
232 mutable void* handle_ = nullptr;
233
234 /// Plugin type.
235 PluginType type_ = TYPE_UNKNOWN;
236
237 /// Set of plugin types this plugin depends on.
239
240 /// Load priority of this plugin.
241 PluginPriority priority_ = PRIORITY_UNDEFINED;
242
243 /// 3rd party dependences.
245
246 /// 3rd party description.
248
249 /// Plugin load function.
250 mutable PluginLoadFunction loadFunction_ = nullptr;
251
252 /// Plugin unload function.
253 mutable PluginUnloadFunction unloadFunction_ = nullptr;
254 };
255
256 /**
257 * Definition of a vector holding plugin objects.
258 */
259 typedef std::vector<Plugin> Plugins;
260
261 /**
262 * Definition of a set holding plugin objects.
263 */
264 typedef std::set<Plugin> PluginSet;
265
266 public:
267
268 /**
269 * Returns the file extension for plugin files.
270 * @return File extension of plugins
271 */
272 inline const std::string& fileExtension() const;
273
274 /**
275 * Sets the file extension for plugin files.
276 * @param extension File extension to be set
277 * @return True, if succeeded
278 */
279 bool setPluginFileExtension(const std::string& extension);
280
281 /**
282 * Collects all Ocean supported plugins available in a given directory.
283 * @param directory Plugin directory
284 * @param removeAlreadyCollected State determining whether previous collected (but not loaded) plugins will be remove before
285 * @return Number of collected plugins
286 */
287 unsigned int collectPlugins(const std::string& directory, const bool removeAlreadyCollected = true);
288
289 /**
290 * Loads a specific plugin.
291 * @param name The name of the plugin to load.
292 * @return True, if succeeded
293 */
294 bool loadPlugin(const std::string& name);
295
296 /**
297 * Loads several plugins and uses the internal dependency order.
298 * @param names Names of the plugins to load
299 * @return True, if at least one plugin has been loaded
300 */
301 bool loadPlugins(const Names& names);
302
303 /**
304 * Loads all plugins with a specified type.
305 * @param type Plugin type to load, can be a combination of all defined plugin types
306 * @return True, if at least one plugin has been loaded
307 */
308 bool loadPlugins(const PluginType type);
309
310 /**
311 * Loads all available plugins.
312 * @return True, if at least one plugin has been loaded
313 */
315
316 /**
317 * Unloads all loaded plugins.
318 * @return True, if all plugins could be unloaded successfully
319 */
321
322 /**
323 * Returns all available plugins.
324 * @return Plugin names
325 */
326 Names plugins() const;
327
328 /**
329 * Returns all loaded plugins.
330 * @return Plugin names
331 */
333
334 /**
335 * Returns all not loaded plugins.
336 * @return Plugin names
337 */
339
340 /**
341 * Releases the plugin manager and unloads all plugins.
342 */
343 void release();
344
345 private:
346
347 /**
348 * Creates a new plugin manager object.
349 */
351
352 /**
353 * Destructs a plugin manager object.
354 */
356
357 /**
358 * Determines whether a given file is a plugin.
359 * @param filename Filename of the potential plugin
360 * @param plugin Resulting plugin object, if succeeded
361 * @return True, if succeeded
362 */
363 static bool determinePlugin(const std::string& filename, Plugin& plugin);
364
365 #if defined(__APPLE__)
366
367 /**
368 * Determines whether a given file is a plugin (specialization for Apple platforms)
369 * @sa determinePlugin()
370 * @param filename Filename of the potential plugin
371 * @param plugin Resulting plugin object, if succeeded
372 * @return True, if succeeded
373 */
374 static bool determinePluginApple(const std::string& filename, Plugin& plugin);
375
376 #endif // defined(__APPLE__)
377
378 /**
379 * Translates a plugin type string to a plugin type id.
380 * @param type Plugin type string to translate
381 * @return Plugin type id
382 */
383 static PluginType translateType(const std::string& type);
384
385 private:
386
387 /// Vector holding all collected plugins.
389
390 /// Vector holding all loaded plugins.
392
393 /// File extension for plugins.
395
396 /// Lock for the manager.
397 mutable Lock lock_;
398};
399
400inline const std::string& PluginManager::Plugin::filename() const
401{
402 return filename_;
403}
404
405inline const std::string& PluginManager::Plugin::name() const
406{
407 return name_;
408}
409
410inline const std::string& PluginManager::Plugin::description() const
411{
412 return description_;
413}
414
415inline const std::string& PluginManager::Plugin::thirdpartyInformation() const
416{
417 return thirdpartyInformation_;
418}
419
421{
422 return type_;
423}
424
425inline const std::string& PluginManager::Plugin::thirdpartyDependences() const
426{
427 return thirdpartyDependences_;
428}
429
430inline const std::string& PluginManager::Plugin::thirdpartyDescription() const
431{
432 return thirdpartyDescription_;
433}
434
435inline const std::string& PluginManager::fileExtension() const
436{
438}
439
440}
441
442#endif // META_OCEAN_BASE_PLUGIN_MANAGER_H
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a plugin interface.
Definition PluginManager.h:88
const std::string & thirdpartyDependences() const
Returns the 3rd party dependences of this plugin.
Definition PluginManager.h:425
std::string name_
Name of the plugin.
Definition PluginManager.h:223
std::string thirdpartyDependences_
3rd party dependences.
Definition PluginManager.h:244
bool unloadApple() const
Tries to unload the plugin.
const std::string & description() const
Returns the description of the plugin.
Definition PluginManager.h:410
bool unload() const
Tries to unload the plugin.
std::string description_
Description of the plugin.
Definition PluginManager.h:226
PluginType type() const
Returns the type of this plugin.
Definition PluginManager.h:420
const std::string & filename() const
Returns the filename of the plugin.
Definition PluginManager.h:400
const std::string & thirdpartyDescription() const
Returns the description about the 3rd party dependences of this plugin.
Definition PluginManager.h:430
Plugin(const std::string &filename, const std::string &name, const std::string &description, const PluginType type, const PluginPriority priority, const PluginTypeSet &dependencySet, const std::string &thirdpartyDependences, const std::string &thirdpartyDescription)
Creates a new plugin object.
bool loadApple() const
Loads the plugin (specialization for Apple platforms)
bool operator<(const Plugin &right) const
Returns whether the left plugin has to be loaded before the right one.
bool load() const
Loads the plugin.
const std::string & name() const
Returns the name of the plugin.
Definition PluginManager.h:405
std::string thirdpartyDescription_
3rd party description.
Definition PluginManager.h:247
PluginTypeSet dependencySet_
Set of plugin types this plugin depends on.
Definition PluginManager.h:238
Plugin()=default
Creates an empty plugin object.
const std::string & thirdpartyInformation() const
Returns the version information of the used 3rd party libraries.
Definition PluginManager.h:415
std::string thirdpartyInformation_
3rd party library version information.
Definition PluginManager.h:229
std::string filename_
Filename of the plugin.
Definition PluginManager.h:220
This class implements a manager for all plugins available for the Ocean framework.
Definition PluginManager.h:26
Plugins loadedPlugins_
Vector holding all loaded plugins.
Definition PluginManager.h:391
Names unloadedPlugins() const
Returns all not loaded plugins.
std::vector< std::string > Names
Definition of a vector holding plugin names.
Definition PluginManager.h:80
std::vector< Plugin > Plugins
Definition of a vector holding plugin objects.
Definition PluginManager.h:259
Lock lock_
Lock for the manager.
Definition PluginManager.h:397
bool unloadAllPlugins()
Unloads all loaded plugins.
bool loadPlugins(const PluginType type)
Loads all plugins with a specified type.
bool loadAllPlugins()
Loads all available plugins.
~PluginManager()
Destructs a plugin manager object.
static bool determinePluginApple(const std::string &filename, Plugin &plugin)
Determines whether a given file is a plugin (specialization for Apple platforms)
PluginPriority
Definition of different plugin load priority values.
Definition PluginManager.h:59
@ PRIORITY_LOW
Low plugin load priority.
Definition PluginManager.h:63
@ PRIORITY_MEDIUM
Medium plugin load priority.
Definition PluginManager.h:65
@ PRIORITY_UNDEFINED
Undefined plugin load priority.
Definition PluginManager.h:61
@ PRIORITY_HIGH
High plugin load priority.
Definition PluginManager.h:67
bool setPluginFileExtension(const std::string &extension)
Sets the file extension for plugin files.
Plugins collectedPlugins_
Vector holding all collected plugins.
Definition PluginManager.h:388
Names loadedPlugins() const
Returns all loaded plugins.
Names plugins() const
Returns all available plugins.
void release()
Releases the plugin manager and unloads all plugins.
bool loadPlugins(const Names &names)
Loads several plugins and uses the internal dependency order.
static bool determinePlugin(const std::string &filename, Plugin &plugin)
Determines whether a given file is a plugin.
const std::string & fileExtension() const
Returns the file extension for plugin files.
Definition PluginManager.h:435
PluginType
Definition of different plugin types.
Definition PluginManager.h:35
bool loadPlugin(const std::string &name)
Loads a specific plugin.
std::set< Plugin > PluginSet
Definition of a set holding plugin objects.
Definition PluginManager.h:264
std::set< PluginType > PluginTypeSet
Definition of a set holding plugin types.
Definition PluginManager.h:75
std::string pluginFileExtension_
File extension for plugins.
Definition PluginManager.h:394
PluginManager()
Creates a new plugin manager object.
static PluginType translateType(const std::string &type)
Translates a plugin type string to a plugin type id.
unsigned int collectPlugins(const std::string &directory, const bool removeAlreadyCollected=true)
Collects all Ocean supported plugins available in a given directory.
This template class is the base class for all singleton objects.
Definition Singleton.h:71
The namespace covering the entire Ocean framework.
Definition Accessor.h:15