[89] | 1 | // %pacpus:license{
|
---|
| 2 | // This file is part of the PACPUS framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %pacpus:license}
|
---|
| 5 | /// @file
|
---|
| 6 | /// @author Gerald Dherbomez <firstname.surname@utc.fr>
|
---|
| 7 | /// @date January, 2006
|
---|
| 8 | /// @version $Id: ComponentManager.h 76 2013-01-10 17:05:10Z kurdejma $
|
---|
| 9 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
| 10 | /// @brief Brief description.
|
---|
| 11 | ///
|
---|
| 12 | /// Purpose: This class records the components and manages them
|
---|
| 13 | /// This class is a singleton
|
---|
| 14 | /// Use the static ComponentManager::create() function
|
---|
| 15 | /// to get a pointer on this object.
|
---|
| 16 |
|
---|
| 17 | #ifndef DEF_PACPUS_COMPONENTMANAGER_H
|
---|
| 18 | #define DEF_PACPUS_COMPONENTMANAGER_H
|
---|
| 19 |
|
---|
[198] | 20 | #include <Pacpus/kernel/ComponentFactoryBase.h>
|
---|
[197] | 21 | #include <Pacpus/kernel/PacpusLibConfig.h>
|
---|
[89] | 22 | #include <Pacpus/kernel/PacpusPluginInterface.h>
|
---|
| 23 | #include <Pacpus/kernel/XmlConfigFile.h>
|
---|
| 24 |
|
---|
| 25 | #include <cstddef>
|
---|
| 26 | #include <QList>
|
---|
| 27 | #include <QMap>
|
---|
| 28 | #include <QPluginLoader>
|
---|
| 29 |
|
---|
| 30 | namespace pacpus {
|
---|
| 31 |
|
---|
| 32 | class ComponentBase;
|
---|
| 33 |
|
---|
| 34 | /// @todo Documentation
|
---|
| 35 | typedef QMap<QString, ComponentBase *> ComponentMap;
|
---|
| 36 | /// @todo Documentation
|
---|
| 37 | typedef QMap<QString, ComponentFactoryBase *> FactoryMap;
|
---|
| 38 |
|
---|
| 39 | /// Singleton recording the components and managing them.
|
---|
[146] | 40 | class PACPUSLIB_API ComponentManager
|
---|
[89] | 41 | {
|
---|
| 42 | friend class ComponentBase;
|
---|
| 43 |
|
---|
| 44 | public:
|
---|
| 45 | /// @returns a pointer to the ComponentManager object
|
---|
| 46 | /// @deprecated Use getInstance()
|
---|
[146] | 47 | PACPUS_DEPRECATED_MSG( static ComponentManager * create(), "use 'getInstance()'" );
|
---|
[89] | 48 |
|
---|
| 49 | /// Returns an instance of the singleton ComponentManager class.
|
---|
| 50 | /// @returns Pointer to the ComponentManager singleton.
|
---|
[146] | 51 | static ComponentManager* getInstance();
|
---|
[89] | 52 |
|
---|
| 53 | /// Destroys the ComponentManager singleton.
|
---|
| 54 | ///
|
---|
| 55 | /// After this call, every pointer to the ComponentManager becomes invalid.
|
---|
[146] | 56 | static void destroy();
|
---|
[89] | 57 |
|
---|
| 58 | /// Automatic deleter class.
|
---|
| 59 | struct destroyer {
|
---|
| 60 | /// Invokes ComponentManager::destroy() method if @b mgr pointer is not null.
|
---|
| 61 | void operator()(ComponentManager * mgr) const
|
---|
| 62 | {
|
---|
| 63 | if (!mgr) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 | mgr->destroy();
|
---|
| 67 | }
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 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 | */
|
---|
[146] | 74 | std::size_t loadComponents(const QString& file);
|
---|
[89] | 75 |
|
---|
| 76 | /** Start all the components
|
---|
| 77 | * @return True if all the component has been started, otherwise false.
|
---|
| 78 | */
|
---|
[146] | 79 | bool start();
|
---|
[89] | 80 |
|
---|
| 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 | */
|
---|
[146] | 85 | bool start(const QString& component);
|
---|
[89] | 86 |
|
---|
| 87 | /** Stop all the components
|
---|
| 88 | * @return True if all the component has been stopped, otherwise false.
|
---|
| 89 | */
|
---|
[146] | 90 | bool stop();
|
---|
[89] | 91 |
|
---|
| 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 | */
|
---|
[146] | 96 | bool stop(const QString& component);
|
---|
[89] | 97 |
|
---|
| 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 | */
|
---|
[146] | 102 | ComponentBase* getComponent(const QString& name);
|
---|
[89] | 103 |
|
---|
| 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 | */
|
---|
[146] | 107 | QStringList getAllComponentsName() const;
|
---|
[89] | 108 |
|
---|
| 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 | */
|
---|
[146] | 113 | bool loadPlugin(const QString& filename);
|
---|
[89] | 114 |
|
---|
| 115 | private:
|
---|
[146] | 116 | bool stop(ComponentBase* component) const;
|
---|
| 117 |
|
---|
[89] | 118 | /// Create a new component of type 'type' and with the name 'name'
|
---|
| 119 | bool createComponent(const QString& type, const QString& name);
|
---|
| 120 |
|
---|
[184] | 121 | bool checkComponentConnection(const QString& componentName, const QString& connectionName);
|
---|
[89] | 122 | bool createConnection(const QString& type, const QString& name, const QString& , int );
|
---|
| 123 |
|
---|
| 124 | bool registerComponent(ComponentBase* addr, const QString& name);
|
---|
| 125 | bool registerComponentFactory(ComponentFactoryBase* addr, const QString& type);
|
---|
| 126 |
|
---|
[146] | 127 | bool unregisterComponent(const QString& name);
|
---|
| 128 | bool unregisterComponentFactory(const QString& type);
|
---|
[89] | 129 |
|
---|
| 130 | // Allow 2 functions to access to private members of ComponentManager
|
---|
| 131 | friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, const QString & type);
|
---|
| 132 | friend void ComponentFactoryBase::addComponent(const QString & name);
|
---|
| 133 |
|
---|
| 134 | /// private constructor accessible only via static create() function
|
---|
| 135 | ComponentManager();
|
---|
| 136 |
|
---|
| 137 | /// private destructor accessible only via static destroy() function
|
---|
| 138 | ~ComponentManager();
|
---|
| 139 |
|
---|
| 140 | private:
|
---|
| 141 | /// The static pointer to this object (ComponentManager is a singleton)
|
---|
| 142 | static ComponentManager * mInstance;
|
---|
| 143 |
|
---|
| 144 | /// The map of available factories of component
|
---|
| 145 | FactoryMap factoryMap_;
|
---|
| 146 |
|
---|
| 147 | /// The map of loaded components
|
---|
| 148 | ComponentMap componentMap_;
|
---|
| 149 |
|
---|
| 150 | /// a pointer on the xml interface
|
---|
| 151 | pacpus::XmlConfigFile * xmlTree_;
|
---|
| 152 |
|
---|
| 153 | /// a list of QObject in which plugins are embedded
|
---|
| 154 | QObjectList pluginList_;
|
---|
| 155 |
|
---|
| 156 | /// The object used to load the plugins
|
---|
| 157 | QPluginLoader pluginLoader_;
|
---|
| 158 | };
|
---|
| 159 |
|
---|
| 160 | } // namespace pacpus
|
---|
| 161 |
|
---|
| 162 | #endif
|
---|