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

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

Added: PacpusException - base class for all exceptions. DbiteExceptions inherits from it.
Added: PacpusLibConfig.h - dllimport/dllexport clauses separated from pacpus.h.
Update: comments.

  • Property svn:keywords set to Id
File size: 5.4 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 116 2013-06-25 11:44:25Z 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/pacpus.h>
22#include <Pacpus/kernel/PacpusLibConfig.h>
23#include <Pacpus/kernel/PacpusPluginInterface.h>
24#include <Pacpus/kernel/XmlConfigFile.h>
25
26#include <cstddef>
27#include <QList>
28#include <QMap>
29#include <QPluginLoader>
30
31namespace pacpus {
32
33class ComponentBase;
34
35/// @todo Documentation
36typedef QMap<QString, ComponentBase *> ComponentMap;
37/// @todo Documentation
38typedef QMap<QString, ComponentFactoryBase *> FactoryMap;
39
40/// Singleton recording the components and managing them.
41class ComponentManager
42{
43 friend class ComponentBase;
44
45 // Allow 2 functions to access to private members of ComponentManager
46 friend void ComponentFactoryBase::addFactory(ComponentFactoryBase * addr, const QString & type);
47 friend void ComponentFactoryBase::addComponent(const QString & name);
48
49public:
50 /// @returns a pointer to the ComponentManager object
51 /// @deprecated Use getInstance()
52 PACPUS_DEPRECATED_MSG( static PACPUSLIB_API ComponentManager * create(), "use 'getInstance()'" );
53
54 /// Returns an instance of the singleton ComponentManager class.
55 /// @returns Pointer to the ComponentManager singleton.
56 static PACPUSLIB_API ComponentManager* getInstance();
57
58 /// Destroys the ComponentManager singleton.
59 ///
60 /// After this call, every pointer to the ComponentManager becomes invalid.
61 static PACPUSLIB_API void destroy();
62
63 /// Automatic deleter class.
64 struct destroyer {
65 /// Invokes ComponentManager::destroy() method if @b mgr pointer is not null.
66 void operator()(ComponentManager * mgr) const
67 {
68 if (!mgr) {
69 return;
70 }
71 mgr->destroy();
72 }
73 };
74
75 /// Loads the components included in the XML config file.
76 /// @param file Name of the XML file.
77 /// @returns Number of components loaded by the manager.
78 PACPUSLIB_API std::size_t loadComponents(const QString& file);
79
80 /// Starts all the components
81 /// @return @b true if all the component has been started, @b false otherwise.
82 PACPUSLIB_API bool start();
83
84 /// Starts only the component passed in parameter.
85 /// @param component Component to start.
86 /// @returns @b true if the component exists and has been started, @b false otherwise.
87 PACPUSLIB_API bool start(const QString& component);
88
89 /// Stops all the components
90 /// @returns @b true if all the component has been stopped, @b false otherwise.
91 PACPUSLIB_API bool stop();
92
93 /// Stops only the component passed in parameter.
94 /// @param component Component to stop.
95 /// @returns @b true if the component has been stopped, @b false otherwise.
96 PACPUSLIB_API bool stop(const QString& component);
97
98 /// Gets a pointer to the component referred by @em name.
99 ///
100 /// @param name Name of the component.
101 /// @returns Pointer to the component if it exists, @b NULL otherwise.
102 PACPUSLIB_API ComponentBase* getComponent(const QString& name);
103
104 /// Gets the list of all the names of the component known by the manager.
105 /// @returns List of all the component's name.
106 PACPUSLIB_API QStringList getAllComponentsName() const;
107
108 /// Loads a new plugin from the file filename (it may be a .so/.dll file)
109 /// @param filename Name of the shared object or the dll.
110 /// @returns @b true if the plugin has been loaded, @b false otherwise .
111 PACPUSLIB_API bool loadPlugin(const QString& filename);
112
113private:
114 /// Create a new component of type 'type' and with the name 'name'
115 bool createComponent(const QString& type, const QString& name);
116
117 bool registerComponent(ComponentBase* addr, const QString& name);
118 bool registerComponentFactory(ComponentFactoryBase * addr, const QString& type);
119
120 bool unRegisterComponent(const QString& name);
121 bool unRegisterComponentFactory(const QString& type);
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
Note: See TracBrowser for help on using the repository browser.