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

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

Major: cleaned connection interfaces.

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