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

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

Minor: fixed warnings.

  • 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/noncopyable.hpp>
33#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/value_semantic.hpp>
35#include <QString>
36#include <QMap>
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 ComponentManager;
56
57class InputInterfaceBase;
58class OutputInterfaceBase;
59
60template <typename T, class C>
61class InputInterface;
62template <typename T, class C>
63class OutputInterface;
64
65using boost::program_options::value;
66
67/// @brief Base class of a Pacpus component.
68class PACPUSLIB_API ComponentBase
69 : boost::noncopyable
70{
71 friend class ComponentManager;
72
73public:
74 /// Enumeration of the state that can take a component, the three last states suppose
75 /// that the component is started.
76 enum COMPONENT_STATE {
77 STOPPED,
78 NOT_MONITORED,
79 MONITOR_OK,
80 MONITOR_NOK
81 };
82
83 /// Resulting state of a component after its configuration.
84 enum COMPONENT_CONFIGURATION {
85 CONFIGURED_OK,
86 NOT_CONFIGURED,
87 CONFIGURATION_DELAYED,
88 CONFIGURED_FAILED
89 };
90
91 /// Ctor of ComponentBase.
92 /// @param name Name of your component.
93 ComponentBase(const QString & name);
94
95 /// Dtor of ComponentBase.
96 virtual ~ComponentBase();
97
98 /// Returns the state of the component.
99 /// @return Value of the current state.
100 COMPONENT_STATE getState();
101
102 /// Checks whether the component if configurer or not.
103 /// @return @b true if the component is configured, otherwise @b false.
104 bool isConfigured() const;
105
106 /// Returns the name of the component.
107 /// @return Name of the component.
108 QString getName() const;
109
110 /// @returns @b true if the component has been started and is active (working)
111 bool isActive() const;
112
113protected:
114 /// Changes the state of the component.
115 /// @param state New component state.
116 void setState(COMPONENT_STATE state);
117
118 /// Called when the component starts, you must override this function.
119 virtual void startActivity() = 0;
120
121 /// Called when the component stops, you must override this function.
122 virtual void stopActivity() = 0;
123
124 /// Called by the ComponentManager, it configure the component thanks a XML node.
125 /// @param config Component's XML node.
126 /// @return State of the configuration.
127 /// @todo FIXME: 'config' should be const, but it will be a breaking change
128 /// old stuff.
129 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
130
131 // virtual QString getType() = 0;
132
133 /// @todo FIXME: should be pure virtual, but it will be a breaking change
134 virtual void addInputs() {}
135 virtual void addOutputs() {}
136
137 /// Returns an object permitting to add component parameters.
138 boost::program_options::options_description_easy_init addParameters();
139
140protected:
141 typedef QMap<QString, InputInterfaceBase *> InputsMap;
142 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
143
144 template <typename DataType, class ComponentType, typename Function>
145 void addInput(const char * name, Function function)
146 {
147 typedef InputInterface<DataType, ComponentType> InputType;
148 InputType * connection = new InputType(name, dynamic_cast<ComponentType *>(this), function);
149 inputs().insert(name, connection);
150 }
151
152 template <typename DataType, class ComponentType>
153 void addOutput(const char * name)
154 {
155 typedef OutputInterface<DataType, ComponentType> OutputType;
156 OutputType * connection = new OutputType(name, dynamic_cast<ComponentType *>(this));
157 outputs().insert(name, connection);
158 }
159
160 /// @todo DOC
161 InputInterfaceBase * getInput(QString name) const;
162
163 /// @todo DOC
164 OutputInterfaceBase * getOutput(QString name) const;
165
166 template <typename DataType, class ComponentType>
167 InputInterface<DataType, ComponentType> *
168 getTypedInput(const char * name) const
169 {
170 return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
171 }
172
173 template <typename DataType, class ComponentType>
174 OutputInterface<DataType, ComponentType> *
175 getTypedOutput(const char * name) const
176 {
177 return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
178 }
179
180 void setActive(bool isActive);
181 bool isRecording() const;
182 void setRecording(bool isRecording);
183
184 InputsMap & inputs();
185 const InputsMap & inputs() const;
186 OutputsMap & outputs();
187 const OutputsMap & outputs() const;
188
189 COMPONENT_CONFIGURATION configurationState() const;
190 void setConfigurationState(COMPONENT_CONFIGURATION state);
191
192 const XmlComponentConfig xmlParameters() const;
193
194protected:
195 std::string mName;
196 std::string mTypeName;
197
198 /// Whether to display or not the graphical interface (GUI)
199 bool hasGui() const;
200 bool isOutputVerbose() const;
201 int getVerbosityLevel() const;
202
203private:
204 /// Called by ComponentManager to handle parameters
205 /// @throws
206 void parseParameters(const XmlComponentConfig & cfg);
207
208 /// called by the ComponentManager to start the component
209 int startComponent();
210 void startComponentInThread();
211 void startComponentWithException(boost::exception_ptr& error);
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.