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

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

Added: OutputInterface::checkedSend.
Added: ComponentManager: prints InputsMap, OutputsMap.

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