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