source: pacpusframework/trunk/include/Pacpus/kernel/ComponentBase.h@ 184

Last change on this file since 184 was 184, checked in by morasjul, 11 years ago

Minor fixes.

  • Property svn:executable set to *
File size: 7.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 Dhermobez <firstname.surname@utc.fr>
7/// @author Marek Kurdej <firstname.surname@utc.fr>
8/// @author Samuel Gosselin <firstname.surname@utc.fr>
9/// @author Julien Moras <firstname.surname@utc.fr>
10/// @date February, 2006
11/// @version $Id: ComponentBase.h 76 2013-01-10 17:05:10Z kurdejma $
12/// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
13/// @brief Generic ComponentBase class. This is an abstract class.
14///
15/// Detailed description.
16/// @todo - see if some methods can be private with ComponentManager
17/// friendship
18/// - include the copy of Xml node in param here
19/// - see if there is a possibility to avoid the constraint
20/// on parameters in the constructor of derived class
21
22#ifndef DEF_PACPUS_COMPONENTBASE_H
23#define DEF_PACPUS_COMPONENTBASE_H
24
25#include <Pacpus/kernel/ComponentManager.h>
26#include <Pacpus/kernel/InputOutputBase.h>
27#include <Pacpus/kernel/pacpus.h>
28#include <Pacpus/kernel/XmlComponentConfig.h>
29
30#include <QString>
31#include <QMap>
32
33#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/value_semantic.hpp>
35#include <string>
36
37class QWidget;
38
39namespace boost {
40namespace program_options {
41 class options_description_easy_init;
42}
43}
44
45namespace pacpus {
46
47class ComponentManager;
48
49class InputInterfaceBase;
50class OutputInterfaceBase;
51
52template <typename T, class C>
53class InputInterface;
54template <typename T, class C>
55class OutputInterface;
56
57/** ComponentBase
58 * @brief Base class of a Pacpus component.
59 */
60class PACPUSLIB_API ComponentBase
61{
62 friend class ComponentManager;
63
64public:
65 /**
66 * Enumeration of the state that can take a component, the three last states suppose
67 * that the component is started.
68 */
69 enum COMPONENT_STATE
70 {
71 STOPPED,
72 NOT_MONITORED,
73 MONITOR_OK,
74 MONITOR_NOK
75 };
76
77 /** Resulting state of a component after its configuration. */
78 enum COMPONENT_CONFIGURATION
79 {
80 CONFIGURED_OK,
81 NOT_CONFIGURED,
82 CONFIGURATION_DELAYED,
83 CONFIGURED_FAILED
84 };
85
86 /** Ctor of ComponentBase.
87 * @param name Name of your component.
88 */
89 ComponentBase(const QString& name);
90
91 /** Dtor of ComponentBase. */
92 virtual ~ComponentBase();
93
94 /** Return the state of the component.
95 * @return Value of the current state.
96 */
97 COMPONENT_STATE getState();
98
99 /** Check whether the component if configurer or not.
100 * @return True if the component is configured, otherwise false.
101 */
102 bool isConfigured() const;
103
104 /** Return the name of the component.
105 * @return Name of the component.
106 */
107 QString getName() const;
108
109 InputInterfaceBase * getInput(QString name) const;
110
111 OutputInterfaceBase * getOutput(QString name) const;
112
113protected:
114 /** Change the state of the component.
115 * @param state New component state.
116 */
117 void setState(COMPONENT_STATE state);
118
119 /** Called when the component starts, you must override this function. */
120 virtual void startActivity() = 0;
121
122 /** Called when the component stops, you must override this function. */
123 virtual void stopActivity() = 0;
124
125 /** Called by the ComponentManager, it configure the component thanks a XML node.
126 * @param config Component's XML node.
127 * @return State of the configuration.
128 * FIXME: 'config' should be const, but we can't change the prototype without breaking
129 * old stuff.
130 */
131 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
132
133 // virtual QString getType() = 0;
134
135 // Not Pure virtual, no need to implement if not needed !!!
136 virtual void addInputs() {}
137 virtual void addOutputs() {}
138
139 boost::program_options::options_description_easy_init addParameters();
140
141protected:
142 typedef QMap<QString, InputInterfaceBase *> InputsMap;
143 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
144
145 // TODO: use std::function<void (const DataType &)>
146 // TODO: use std::mem_fun<void (const DataType &)>
147 template <typename DataType, class ComponentType, typename Function>
148 void addInput(const char * name, Function function)
149 {
150 typedef InputInterface<DataType, ComponentType> InputType;
151 InputType * connection = new InputType(name, dynamic_cast<ComponentType *>(this), function);
152 inputs().insert(name, connection);
153 }
154
155 template <typename DataType, class ComponentType>
156 void addOutput(const char * name)
157 {
158 typedef OutputInterface<DataType, ComponentType> OutputType;
159 OutputType * connection = new OutputType(name, dynamic_cast<ComponentType *>(this));
160 outputs().insert(name, connection);
161 }
162
163 template <typename DataType, class ComponentType>
164 InputInterface<DataType, ComponentType> *
165 getTypedInput(const char * name) const
166 {
167 return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
168 }
169
170 template <typename DataType, class ComponentType>
171 OutputInterface<DataType, ComponentType> *
172 getTypedOutput(const char * name) const
173 {
174 return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
175 }
176
177 bool isActive() const;
178 void setActive(bool isActive);
179 bool isRecording() const;
180 void setRecording(bool isRecording);
181
182 InputsMap & inputs();
183 const InputsMap & inputs() const;
184 OutputsMap & outputs();
185 const OutputsMap & outputs() const;
186
187 COMPONENT_CONFIGURATION configurationState() const;
188 void setConfigurationState(COMPONENT_CONFIGURATION state);
189
190 const XmlComponentConfig xmlParameters() const;
191
192protected:
193 std::string mName;
194 std::string mTypeName;
195
196 /// Whether to display or not the graphical interface (GUI)
197 bool hasGui() const;
198 bool isOutputVerbose() const;
199 int getVerbosityLevel() const;
200
201private:
202 /// Called by ComponentManager to handle parameters
203 /// @throws
204 void parseParameters(const XmlComponentConfig & cfg);
205
206 /// called by the ComponentManager to start the component
207 int startComponent();
208
209 /// called by the ComponentManager to stop the component
210 int stopComponent();
211
212private:
213 bool mHasGui;
214 bool mVerbose;
215 int mVerbosityLevel;
216
217 boost::program_options::options_description mOptionsDescription;
218
219 /// The XML node that is got in the configureComponent method
220 XmlComponentConfig param;
221
222 /// the name of the component. It is this one in the XML config file
223 QString m_componentName;
224
225 /// is the component active?
226 volatile bool m_isActive;
227
228 /// is the component is recording data?
229 bool mIsRecording;
230
231 /// a pointer to the manager of components
232 ComponentManager * m_manager;
233
234 InputsMap m_inputs;
235 OutputsMap m_outputs;
236
237 /// a pointer to an optional widget
238 QWidget * m_ui;
239
240 /// store the state of the component
241 COMPONENT_STATE m_componentState;
242
243 /// is the component configured (ie configureComponent method was called)
244 COMPONENT_CONFIGURATION m_configurationState;
245};
246
247} // pacpus
248
249#endif // DEF_PACPUS_COMPONENTBASE_H
Note: See TracBrowser for help on using the repository browser.