[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 |
|
---|
[288] | 30 | namespace pacpus
|
---|
| 31 | {
|
---|
[89] | 32 |
|
---|
| 33 | class ComponentBase;
|
---|
| 34 |
|
---|
| 35 | /// @todo Documentation
|
---|
[288] | 36 | typedef QMap<QString, ComponentSharedPointer> ComponentMap;
|
---|
[89] | 37 | /// @todo Documentation
|
---|
[288] | 38 | typedef QMap<QString, ComponentFactoryBase* > FactoryMap;
|
---|
[89] | 39 |
|
---|
| 40 | /// Singleton recording the components and managing them.
|
---|
[146] | 41 | class PACPUSLIB_API ComponentManager
|
---|
[89] | 42 | {
|
---|
| 43 | friend class ComponentBase;
|
---|
| 44 |
|
---|
| 45 | public:
|
---|
| 46 | /// @returns a pointer to the ComponentManager object
|
---|
| 47 | /// @deprecated Use getInstance()
|
---|
[146] | 48 | PACPUS_DEPRECATED_MSG( static ComponentManager * create(), "use 'getInstance()'" );
|
---|
[89] | 49 |
|
---|
| 50 | /// Returns an instance of the singleton ComponentManager class.
|
---|
| 51 | /// @returns Pointer to the ComponentManager singleton.
|
---|
[146] | 52 | static ComponentManager* getInstance();
|
---|
[89] | 53 |
|
---|
| 54 | /// Destroys the ComponentManager singleton.
|
---|
| 55 | ///
|
---|
| 56 | /// After this call, every pointer to the ComponentManager becomes invalid.
|
---|
[146] | 57 | static void destroy();
|
---|
[89] | 58 |
|
---|
| 59 | /// Automatic deleter class.
|
---|
| 60 | struct destroyer {
|
---|
| 61 | /// Invokes ComponentManager::destroy() method if @b mgr pointer is not null.
|
---|
[288] | 62 | void operator()(ComponentManager* mgr) const
|
---|
[89] | 63 | {
|
---|
| 64 | if (!mgr) {
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 | mgr->destroy();
|
---|
| 68 | }
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | /** Load the components included in the XML config file.
|
---|
| 72 | * @param file Name of the XML file.
|
---|
| 73 | * @return Number of components loaded by the manager.
|
---|
| 74 | */
|
---|
[288] | 75 | std::size_t loadComponents(QString const& file);
|
---|
[89] | 76 |
|
---|
| 77 | /** Start all the components
|
---|
| 78 | * @return True if all the component has been started, otherwise false.
|
---|
| 79 | */
|
---|
[146] | 80 | bool start();
|
---|
[89] | 81 |
|
---|
| 82 | /** Start only the component passed in parameter.
|
---|
| 83 | * @param component Component to start.
|
---|
| 84 | * @return True if the component exists and has been started, otherwise false.
|
---|
| 85 | */
|
---|
[288] | 86 | bool start(QString const& component);
|
---|
[89] | 87 |
|
---|
| 88 | /** Stop all the components
|
---|
| 89 | * @return True if all the component has been stopped, otherwise false.
|
---|
| 90 | */
|
---|
[146] | 91 | bool stop();
|
---|
[89] | 92 |
|
---|
| 93 | /** Stop only the component passed in parameter.
|
---|
| 94 | * @param component Component to stop.
|
---|
| 95 | * @return True if the component has been stopped, otherwise false.
|
---|
| 96 | */
|
---|
[288] | 97 | bool stop(QString const& component);
|
---|
[89] | 98 |
|
---|
| 99 | /** Get a pointer to the component referred by @em name.
|
---|
| 100 | * @param name Name of the component.
|
---|
| 101 | * @return Pointer to the component if it exists, otherwise @em NULL.
|
---|
| 102 | */
|
---|
[288] | 103 | ComponentSharedPointer getComponent(QString const& name);
|
---|
[89] | 104 |
|
---|
| 105 | /** Get the list of all the names of the component known by the manager.
|
---|
| 106 | * @return List of all the component's name.
|
---|
| 107 | */
|
---|
[146] | 108 | QStringList getAllComponentsName() const;
|
---|
[89] | 109 |
|
---|
| 110 | /** Load a new plugin from the file filename (it may be a .so/.dll file)
|
---|
| 111 | * @param filename Name of the shared object or the dll.
|
---|
| 112 | * @return True if the plugin has been loaded, otherwise false.
|
---|
| 113 | */
|
---|
[288] | 114 | bool loadPlugin(QString const& filename);
|
---|
[89] | 115 |
|
---|
| 116 | private:
|
---|
[288] | 117 | bool stop(ComponentSharedPointer component) const;
|
---|
[146] | 118 |
|
---|
[89] | 119 | /// Create a new component of type 'type' and with the name 'name'
|
---|
[288] | 120 | bool createComponent(QString const& type, QString const& name);
|
---|
[89] | 121 |
|
---|
[288] | 122 | bool checkComponent(QString const& componentName);
|
---|
| 123 | bool checkComponentInput(QString const& componentName, QString const& inputName);
|
---|
| 124 | bool checkComponentOutput(QString const& componentName, QString const& outputName);
|
---|
[207] | 125 |
|
---|
[288] | 126 | bool createConnection(QString const& type, QString const& name, QString const& , int );
|
---|
[89] | 127 |
|
---|
[288] | 128 | bool registerComponent(ComponentSharedPointer addr, QString const& name);
|
---|
| 129 | bool registerComponentFactory(ComponentFactoryBase* addr, QString const& type);
|
---|
[89] | 130 |
|
---|
[288] | 131 | bool unregisterComponent(QString const& name);
|
---|
| 132 | bool unregisterComponentFactory(QString const& type);
|
---|
[89] | 133 |
|
---|
| 134 | // Allow 2 functions to access to private members of ComponentManager
|
---|
[288] | 135 | friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, QString const& type);
|
---|
| 136 | friend void ComponentFactoryBase::createComponent(QString const& name);
|
---|
[89] | 137 |
|
---|
| 138 | /// private constructor accessible only via static create() function
|
---|
| 139 | ComponentManager();
|
---|
| 140 |
|
---|
| 141 | /// private destructor accessible only via static destroy() function
|
---|
| 142 | ~ComponentManager();
|
---|
| 143 |
|
---|
| 144 | private:
|
---|
| 145 | /// The static pointer to this object (ComponentManager is a singleton)
|
---|
| 146 | static ComponentManager * mInstance;
|
---|
| 147 |
|
---|
| 148 | /// The map of available factories of component
|
---|
| 149 | FactoryMap factoryMap_;
|
---|
| 150 |
|
---|
| 151 | /// The map of loaded components
|
---|
| 152 | ComponentMap componentMap_;
|
---|
| 153 |
|
---|
| 154 | /// a pointer on the xml interface
|
---|
| 155 | pacpus::XmlConfigFile * xmlTree_;
|
---|
| 156 |
|
---|
| 157 | /// a list of QObject in which plugins are embedded
|
---|
| 158 | QObjectList pluginList_;
|
---|
| 159 |
|
---|
| 160 | /// The object used to load the plugins
|
---|
| 161 | QPluginLoader pluginLoader_;
|
---|
| 162 | };
|
---|
| 163 |
|
---|
| 164 | } // namespace pacpus
|
---|
| 165 |
|
---|
| 166 | #endif
|
---|