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