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

Last change on this file since 23 was 16, checked in by sgosseli, 12 years ago

Passage by const-reference instead of value.

File size: 4.8 KB
Line 
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 */
10
11#ifndef COMPONENTMANAGER_H
12#define COMPONENTMANAGER_H
13
14#include <cstddef>
15
16#include <QMap>
17#include <QList>
18#include <QPluginLoader>
19
20#include "pacpus.h"
21#include "ComponentFactoryBase.h"
22#include "PacpusPluginInterface.h"
23#include "XmlConfigFile.h"
24
25namespace pacpus {
26
27class ComponentBase;
28
29typedef QMap<QString, ComponentBase *> ComponentMap;
30typedef QMap<QString, ComponentFactoryBase *> FactoryMap;
31
32/** ComponentManager
33 * @brief Singleton recording the components and managing them.
34 */
35class ComponentManager
36{
37 friend class ComponentBase;
38
39public:
40 /// @returns a pointer to the ComponentManager object
41 /// @deprecated use getInstance()
42 PACPUS_DEPRECATED_MSG( static PACPUSLIB_API ComponentManager * create(), "use 'getInstance()'" );
43
44 /** Get the singleton of the ComponentManager class.
45 * @return Pointer to the ComponentManager singleton.
46 */
47 static PACPUSLIB_API ComponentManager* getInstance();
48
49 /** Destroy the ComponentManager singleton.
50 * After this call, every pointer to the ComponentManager becomes invalid.
51 */
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
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 */
68 PACPUSLIB_API std::size_t loadComponents(const QString& file);
69
70 /** Start all the components
71 * @return True if all the component has been started, otherwise false.
72 */
73 PACPUSLIB_API int start();
74
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 */
79 PACPUSLIB_API int start(const QString& component);
80
81 /** Stop all the components
82 * @return True if all the component has been stopped, otherwise false.
83 */
84 PACPUSLIB_API int stop();
85
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 */
90 PACPUSLIB_API int stop(const QString& component);
91
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 */
96 PACPUSLIB_API ComponentBase* getComponent(const QString& name);
97
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 */
101 PACPUSLIB_API QStringList getAllComponentsName();
102
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 */
107 PACPUSLIB_API bool loadPlugin(const QString& filename);
108
109private:
110 /// Create a new component of type 'type' and with the name 'name'
111 bool createComponent(const QString& type, const QString& name);
112
113 bool registerComponent(ComponentBase* addr, const QString& name);
114 bool registerComponentFactory(ComponentFactoryBase* addr, const QString& type);
115
116 bool unRegisterComponent(const QString& name);
117 bool unRegisterComponentFactory(const QString& type);
118
119 // Allow 2 functions to access to private members of ComponentManager
120 friend void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, const QString & type);
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
129private:
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
151#endif // COMPONENTMANAGER_H
Note: See TracBrowser for help on using the repository browser.