[62] | 1 | // This file is part of the PACPUS framework distributed under the
|
---|
| 2 | // CECILL-C License, Version 1.0.
|
---|
| 3 | //
|
---|
| 4 | /// @author Gerald Dherbomez <firstname.surname@utc.fr>
|
---|
| 5 | /// @date Month, Year
|
---|
| 6 | /// @version $Id$
|
---|
| 7 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
| 8 | /// @brief Brief description.
|
---|
| 9 | ///
|
---|
| 10 | /// Detailed description.
|
---|
[3] | 11 |
|
---|
[31] | 12 | #ifndef DEF_PACPUS_COMPONENTMANAGER_H
|
---|
| 13 | #define DEF_PACPUS_COMPONENTMANAGER_H
|
---|
[3] | 14 |
|
---|
| 15 | #include <cstddef>
|
---|
[13] | 16 |
|
---|
[12] | 17 | #include <QMap>
|
---|
| 18 | #include <QList>
|
---|
| 19 | #include <QPluginLoader>
|
---|
[3] | 20 |
|
---|
[31] | 21 | #include <Pacpus/kernel/pacpus.h>
|
---|
| 22 | #include <Pacpus/kernel/ComponentFactoryBase.h>
|
---|
| 23 | #include <Pacpus/kernel/PacpusPluginInterface.h>
|
---|
| 24 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
[3] | 25 |
|
---|
| 26 | namespace pacpus {
|
---|
| 27 |
|
---|
| 28 | class ComponentBase;
|
---|
| 29 |
|
---|
| 30 | typedef QMap<QString, ComponentBase *> ComponentMap;
|
---|
| 31 | typedef QMap<QString, ComponentFactoryBase *> FactoryMap;
|
---|
| 32 |
|
---|
[13] | 33 | /** ComponentManager
|
---|
| 34 | * @brief Singleton recording the components and managing them.
|
---|
| 35 | */
|
---|
[3] | 36 | class ComponentManager
|
---|
| 37 | {
|
---|
| 38 | friend class ComponentBase;
|
---|
| 39 |
|
---|
| 40 | public:
|
---|
| 41 | /// @returns a pointer to the ComponentManager object
|
---|
| 42 | /// @deprecated use getInstance()
|
---|
| 43 | PACPUS_DEPRECATED_MSG( static PACPUSLIB_API ComponentManager * create(), "use 'getInstance()'" );
|
---|
| 44 |
|
---|
[13] | 45 | /** Get the singleton of the ComponentManager class.
|
---|
| 46 | * @return Pointer to the ComponentManager singleton.
|
---|
| 47 | */
|
---|
| 48 | static PACPUSLIB_API ComponentManager* getInstance();
|
---|
[3] | 49 |
|
---|
[13] | 50 | /** Destroy the ComponentManager singleton.
|
---|
| 51 | * After this call, every pointer to the ComponentManager becomes invalid.
|
---|
| 52 | */
|
---|
[3] | 53 | static PACPUSLIB_API void destroy();
|
---|
| 54 |
|
---|
| 55 | struct destroyer {
|
---|
| 56 | void operator()(ComponentManager * mgr) const
|
---|
| 57 | {
|
---|
| 58 | if (!mgr) {
|
---|
| 59 | return;
|
---|
| 60 | }
|
---|
| 61 | mgr->destroy();
|
---|
| 62 | }
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[13] | 65 | /** Load the components included in the XML config file.
|
---|
| 66 | * @param file Name of the XML file.
|
---|
| 67 | * @return Number of components loaded by the manager.
|
---|
| 68 | */
|
---|
[16] | 69 | PACPUSLIB_API std::size_t loadComponents(const QString& file);
|
---|
[3] | 70 |
|
---|
[13] | 71 | /** Start all the components
|
---|
| 72 | * @return True if all the component has been started, otherwise false.
|
---|
| 73 | */
|
---|
[48] | 74 | PACPUSLIB_API bool start();
|
---|
[3] | 75 |
|
---|
[13] | 76 | /** Start only the component passed in parameter.
|
---|
| 77 | * @param component Component to start.
|
---|
| 78 | * @return True if the component exists and has been started, otherwise false.
|
---|
| 79 | */
|
---|
[48] | 80 | PACPUSLIB_API bool start(const QString& component);
|
---|
[3] | 81 |
|
---|
[13] | 82 | /** Stop all the components
|
---|
| 83 | * @return True if all the component has been stopped, otherwise false.
|
---|
| 84 | */
|
---|
[48] | 85 | PACPUSLIB_API bool stop();
|
---|
[3] | 86 |
|
---|
[13] | 87 | /** Stop only the component passed in parameter.
|
---|
| 88 | * @param component Component to stop.
|
---|
| 89 | * @return True if the component has been stopped, otherwise false.
|
---|
| 90 | */
|
---|
[48] | 91 | PACPUSLIB_API bool stop(const QString& component);
|
---|
[3] | 92 |
|
---|
[13] | 93 | /** Get a pointer to the component referred by @em name.
|
---|
| 94 | * @param name Name of the component.
|
---|
| 95 | * @return Pointer to the component if it exists, otherwise @em NULL.
|
---|
| 96 | */
|
---|
[16] | 97 | PACPUSLIB_API ComponentBase* getComponent(const QString& name);
|
---|
[3] | 98 |
|
---|
[13] | 99 | /** Get the list of all the names of the component known by the manager.
|
---|
| 100 | * @return List of all the component's name.
|
---|
| 101 | */
|
---|
[48] | 102 | PACPUSLIB_API QStringList getAllComponentsName() const;
|
---|
[3] | 103 |
|
---|
[13] | 104 | /** Load a new plugin from the file filename (it may be a .so/.dll file)
|
---|
| 105 | * @param filename Name of the shared object or the dll.
|
---|
| 106 | * @return True if the plugin has been loaded, otherwise false.
|
---|
| 107 | */
|
---|
[16] | 108 | PACPUSLIB_API bool loadPlugin(const QString& filename);
|
---|
[3] | 109 |
|
---|
| 110 | private:
|
---|
| 111 | /// Create a new component of type 'type' and with the name 'name'
|
---|
[16] | 112 | bool createComponent(const QString& type, const QString& name);
|
---|
[3] | 113 |
|
---|
[16] | 114 | bool registerComponent(ComponentBase* addr, const QString& name);
|
---|
| 115 | bool registerComponentFactory(ComponentFactoryBase* addr, const QString& type);
|
---|
[3] | 116 |
|
---|
[16] | 117 | bool unRegisterComponent(const QString& name);
|
---|
| 118 | bool unRegisterComponentFactory(const QString& type);
|
---|
[3] | 119 |
|
---|
| 120 | // Allow 2 functions to access to private members of ComponentManager
|
---|
[16] | 121 | friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, const QString & type);
|
---|
[3] | 122 | friend void ComponentFactoryBase::addComponent(const QString & name);
|
---|
| 123 |
|
---|
| 124 | /// private constructor accessible only via static create() function
|
---|
| 125 | ComponentManager();
|
---|
| 126 |
|
---|
| 127 | /// private destructor accessible only via static destroy() function
|
---|
| 128 | ~ComponentManager();
|
---|
| 129 |
|
---|
| 130 | private:
|
---|
| 131 | /// The static pointer to this object (ComponentManager is a singleton)
|
---|
| 132 | static ComponentManager * mInstance;
|
---|
| 133 |
|
---|
| 134 | /// The map of available factories of component
|
---|
| 135 | FactoryMap factoryMap_;
|
---|
| 136 |
|
---|
| 137 | /// The map of loaded components
|
---|
| 138 | ComponentMap componentMap_;
|
---|
| 139 |
|
---|
| 140 | /// a pointer on the xml interface
|
---|
| 141 | pacpus::XmlConfigFile * xmlTree_;
|
---|
| 142 |
|
---|
| 143 | /// a list of QObject in which plugins are embedded
|
---|
| 144 | QObjectList pluginList_;
|
---|
| 145 |
|
---|
| 146 | /// The object used to load the plugins
|
---|
| 147 | QPluginLoader pluginLoader_;
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 | } // namespace pacpus
|
---|
| 151 |
|
---|
[31] | 152 | #endif
|
---|