source: pacpusframework/trunk/include/Pacpus/kernel/ComponentManager.h@ 372

Last change on this file since 372 was 288, checked in by Marek Kurdej, 11 years ago

Using boost::shared_ptr for storing components.

  • Property svn:executable set to *
File size: 5.6 KB
Line 
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
20#include <Pacpus/kernel/ComponentFactoryBase.h>
21#include <Pacpus/kernel/PacpusLibConfig.h>
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
30namespace pacpus
31{
32
33class ComponentBase;
34
35/// @todo Documentation
36typedef QMap<QString, ComponentSharedPointer> ComponentMap;
37/// @todo Documentation
38typedef QMap<QString, ComponentFactoryBase* > FactoryMap;
39
40/// Singleton recording the components and managing them.
41class PACPUSLIB_API ComponentManager
42{
43 friend class ComponentBase;
44
45public:
46 /// @returns a pointer to the ComponentManager object
47 /// @deprecated Use getInstance()
48 PACPUS_DEPRECATED_MSG( static ComponentManager * create(), "use 'getInstance()'" );
49
50 /// Returns an instance of the singleton ComponentManager class.
51 /// @returns Pointer to the ComponentManager singleton.
52 static ComponentManager* getInstance();
53
54 /// Destroys the ComponentManager singleton.
55 ///
56 /// After this call, every pointer to the ComponentManager becomes invalid.
57 static void destroy();
58
59 /// Automatic deleter class.
60 struct destroyer {
61 /// Invokes ComponentManager::destroy() method if @b mgr pointer is not null.
62 void operator()(ComponentManager* mgr) const
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 */
75 std::size_t loadComponents(QString const& file);
76
77 /** Start all the components
78 * @return True if all the component has been started, otherwise false.
79 */
80 bool start();
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 */
86 bool start(QString const& component);
87
88 /** Stop all the components
89 * @return True if all the component has been stopped, otherwise false.
90 */
91 bool stop();
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 */
97 bool stop(QString const& component);
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 */
103 ComponentSharedPointer getComponent(QString const& name);
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 */
108 QStringList getAllComponentsName() const;
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 */
114 bool loadPlugin(QString const& filename);
115
116private:
117 bool stop(ComponentSharedPointer component) const;
118
119 /// Create a new component of type 'type' and with the name 'name'
120 bool createComponent(QString const& type, QString const& name);
121
122 bool checkComponent(QString const& componentName);
123 bool checkComponentInput(QString const& componentName, QString const& inputName);
124 bool checkComponentOutput(QString const& componentName, QString const& outputName);
125
126 bool createConnection(QString const& type, QString const& name, QString const& , int );
127
128 bool registerComponent(ComponentSharedPointer addr, QString const& name);
129 bool registerComponentFactory(ComponentFactoryBase* addr, QString const& type);
130
131 bool unregisterComponent(QString const& name);
132 bool unregisterComponentFactory(QString const& type);
133
134 // Allow 2 functions to access to private members of ComponentManager
135 friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, QString const& type);
136 friend void ComponentFactoryBase::createComponent(QString const& name);
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
144private:
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
Note: See TracBrowser for help on using the repository browser.