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

Last change on this file since 288 was 288, checked in by Marek Kurdej, 10 years ago

Using boost::shared_ptr for storing components.

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