[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 67 2013-01-09 18:17:44Z 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 |
|
---|
[31] | 19 | #include <Pacpus/kernel/pacpus.h>
|
---|
| 20 | #include <Pacpus/kernel/ComponentFactoryBase.h>
|
---|
| 21 | #include <Pacpus/kernel/PacpusPluginInterface.h>
|
---|
| 22 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
[3] | 23 |
|
---|
[67] | 24 | #include <cstddef>
|
---|
| 25 | #include <QList>
|
---|
| 26 | #include <QMap>
|
---|
| 27 | #include <QPluginLoader>
|
---|
| 28 |
|
---|
[3] | 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 |
|
---|
[67] | 58 | /// Automatic deleter class.
|
---|
[3] | 59 | struct destroyer {
|
---|
[67] | 60 | /// Invokes ComponentManager::destroy() method if @b mgr pointer is not null.
|
---|
[3] | 61 | void operator()(ComponentManager * mgr) const
|
---|
| 62 | {
|
---|
| 63 | if (!mgr) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 | mgr->destroy();
|
---|
| 67 | }
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[13] | 70 | /** Load the components included in the XML config file.
|
---|
| 71 | * @param file Name of the XML file.
|
---|
| 72 | * @return Number of components loaded by the manager.
|
---|
| 73 | */
|
---|
[16] | 74 | PACPUSLIB_API std::size_t loadComponents(const QString& file);
|
---|
[3] | 75 |
|
---|
[13] | 76 | /** Start all the components
|
---|
| 77 | * @return True if all the component has been started, otherwise false.
|
---|
| 78 | */
|
---|
[48] | 79 | PACPUSLIB_API bool start();
|
---|
[3] | 80 |
|
---|
[13] | 81 | /** Start only the component passed in parameter.
|
---|
| 82 | * @param component Component to start.
|
---|
| 83 | * @return True if the component exists and has been started, otherwise false.
|
---|
| 84 | */
|
---|
[48] | 85 | PACPUSLIB_API bool start(const QString& component);
|
---|
[3] | 86 |
|
---|
[13] | 87 | /** Stop all the components
|
---|
| 88 | * @return True if all the component has been stopped, otherwise false.
|
---|
| 89 | */
|
---|
[48] | 90 | PACPUSLIB_API bool stop();
|
---|
[3] | 91 |
|
---|
[13] | 92 | /** Stop only the component passed in parameter.
|
---|
| 93 | * @param component Component to stop.
|
---|
| 94 | * @return True if the component has been stopped, otherwise false.
|
---|
| 95 | */
|
---|
[48] | 96 | PACPUSLIB_API bool stop(const QString& component);
|
---|
[3] | 97 |
|
---|
[13] | 98 | /** Get a pointer to the component referred by @em name.
|
---|
| 99 | * @param name Name of the component.
|
---|
| 100 | * @return Pointer to the component if it exists, otherwise @em NULL.
|
---|
| 101 | */
|
---|
[16] | 102 | PACPUSLIB_API ComponentBase* getComponent(const QString& name);
|
---|
[3] | 103 |
|
---|
[13] | 104 | /** Get the list of all the names of the component known by the manager.
|
---|
| 105 | * @return List of all the component's name.
|
---|
| 106 | */
|
---|
[48] | 107 | PACPUSLIB_API QStringList getAllComponentsName() const;
|
---|
[3] | 108 |
|
---|
[13] | 109 | /** Load a new plugin from the file filename (it may be a .so/.dll file)
|
---|
| 110 | * @param filename Name of the shared object or the dll.
|
---|
| 111 | * @return True if the plugin has been loaded, otherwise false.
|
---|
| 112 | */
|
---|
[16] | 113 | PACPUSLIB_API bool loadPlugin(const QString& filename);
|
---|
[3] | 114 |
|
---|
| 115 | private:
|
---|
| 116 | /// Create a new component of type 'type' and with the name 'name'
|
---|
[16] | 117 | bool createComponent(const QString& type, const QString& name);
|
---|
[3] | 118 |
|
---|
[16] | 119 | bool registerComponent(ComponentBase* addr, const QString& name);
|
---|
| 120 | bool registerComponentFactory(ComponentFactoryBase* addr, const QString& type);
|
---|
[3] | 121 |
|
---|
[16] | 122 | bool unRegisterComponent(const QString& name);
|
---|
| 123 | bool unRegisterComponentFactory(const QString& type);
|
---|
[3] | 124 |
|
---|
| 125 | // Allow 2 functions to access to private members of ComponentManager
|
---|
[16] | 126 | friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, const QString & type);
|
---|
[3] | 127 | friend void ComponentFactoryBase::addComponent(const QString & name);
|
---|
| 128 |
|
---|
| 129 | /// private constructor accessible only via static create() function
|
---|
| 130 | ComponentManager();
|
---|
| 131 |
|
---|
| 132 | /// private destructor accessible only via static destroy() function
|
---|
| 133 | ~ComponentManager();
|
---|
| 134 |
|
---|
| 135 | private:
|
---|
| 136 | /// The static pointer to this object (ComponentManager is a singleton)
|
---|
| 137 | static ComponentManager * mInstance;
|
---|
| 138 |
|
---|
| 139 | /// The map of available factories of component
|
---|
| 140 | FactoryMap factoryMap_;
|
---|
| 141 |
|
---|
| 142 | /// The map of loaded components
|
---|
| 143 | ComponentMap componentMap_;
|
---|
| 144 |
|
---|
| 145 | /// a pointer on the xml interface
|
---|
| 146 | pacpus::XmlConfigFile * xmlTree_;
|
---|
| 147 |
|
---|
| 148 | /// a list of QObject in which plugins are embedded
|
---|
| 149 | QObjectList pluginList_;
|
---|
| 150 |
|
---|
| 151 | /// The object used to load the plugins
|
---|
| 152 | QPluginLoader pluginLoader_;
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | } // namespace pacpus
|
---|
| 156 |
|
---|
[31] | 157 | #endif
|
---|