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