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

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

Transporting and catching exceptions in threads.

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