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

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

Update: fixed getDataSize(), getDataType() in connections.

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