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

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

Update: minor.

  • Property svn:executable set to *
File size: 7.2 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>
26#include <Pacpus/kernel/pacpus.h>
27#include <Pacpus/kernel/XmlComponentConfig.h>
[138]28#include <Pacpus/kernel/InputOutputBase.h>
[89]29
30#include <QString>
31#include <QMap>
32
[176]33#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/value_semantic.hpp>
35#include <string>
36
[110]37class QWidget;
38
[176]39namespace boost {
40namespace program_options {
41 class options_description_easy_init;
42}
43}
44
[89]45namespace pacpus {
46
47// Forward declarations.
48class ComponentManager;
49
[152]50template <typename T, class C>
51class InputInterface;
52
53template <typename T, class C>
54class OutputInterface;
55
[89]56/** ComponentBase
57 * @brief Base class of a Pacpus component.
58 */
59class PACPUSLIB_API ComponentBase
60{
61 friend class ComponentManager;
[152]62
[89]63public:
64 /**
65 * Enumeration of the state that can take a component, the three last states suppose
66 * that the component is started.
67 */
68 enum COMPONENT_STATE
69 {
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 {
79 CONFIGURED_OK,
80 NOT_CONFIGURED,
81 CONFIGURATION_DELAYED,
82 CONFIGURED_FAILED
83 };
84
85 /** Ctor of ComponentBase.
86 * @param name Name of your component.
87 */
88 ComponentBase(const QString& name);
89
90 /** Dtor of ComponentBase. */
91 virtual ~ComponentBase();
92
93 /** Return the state of the component.
94 * @return Value of the current state.
95 */
96 COMPONENT_STATE getState();
97
98 /** Check whether the component if configurer or not.
99 * @return True if the component is configured, otherwise false.
100 */
101 bool isConfigured() const;
102
103 /** Return the name of the component.
104 * @return Name of the component.
105 */
106 QString getName() const;
107
[152]108 InputInterfaceBase * getInput(QString name) const;
[89]109
[152]110 OutputInterfaceBase * getOutput(QString name) const;
[89]111
112protected:
113 /** Change the state of the component.
114 * @param state New component state.
115 */
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 * FIXME: 'config' should be const, but we can't change the prototype without breaking
128 * old stuff.
129 */
130 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
131
132 // virtual QString getType() = 0;
[120]133
[161]134 // Not Pure virtual, no need to implement if not needed !!!
135 virtual void addInputs() {}
136 virtual void addOutputs() {}
[120]137
[176]138 boost::program_options::options_description_easy_init addParameters();
139
[89]140protected:
[152]141 typedef QMap<QString, InputInterfaceBase *> InputsMap;
142 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
[89]143
[152]144 // TODO: use std::function<void (const DataType &)>
145 // TODO: use std::mem_fun<void (const DataType &)>
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 }
[89]153
[152]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 }
[89]161
[152]162 template <typename DataType, class ComponentType>
163 InputInterface<DataType, ComponentType> *
164 getTypedInput(const char * name) const
165 {
166 return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
167 }
[89]168
[152]169 template <typename DataType, class ComponentType>
170 OutputInterface<DataType, ComponentType> *
171 getTypedOutput(const char * name) const
172 {
173 return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
174 }
[89]175
[152]176 bool isActive() const;
177 void setActive(bool isActive);
178 bool isRecording() const;
179 void setRecording(bool isRecording);
[89]180
[152]181 InputsMap & inputs();
182 const InputsMap & inputs() const;
183 OutputsMap & outputs();
184 const OutputsMap & outputs() const;
[89]185
[152]186 COMPONENT_CONFIGURATION configurationState() const;
187 void setConfigurationState(COMPONENT_CONFIGURATION state);
[110]188
[152]189 const XmlComponentConfig xmlParameters() const;
[176]190
191protected:
192 std::string mName;
193 std::string mTypeName;
[177]194 bool mHasGui;
195 bool mVerbose;
[176]196 int mVerbosityLevel;
[152]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:
[176]210 boost::program_options::options_description mOptionsDescription;
211
[152]212 /// The XML node that is got in the configureComponent method
213 XmlComponentConfig param;
214
215 /// the name of the component. It is this one in the XML config file
216 QString m_componentName;
217
218 /// is the component active?
219 volatile bool m_isActive;
220
221 /// is the component is recording data?
[177]222 bool mIsRecording;
[152]223
224 /// a pointer to the manager of components
225 ComponentManager * m_manager;
226
227 InputsMap m_inputs;
228 OutputsMap m_outputs;
229
230 /// a pointer to an optional widget
231 QWidget * m_ui;
232
[89]233 /// store the state of the component
[152]234 COMPONENT_STATE m_componentState;
[89]235
236 /// is the component configured (ie configureComponent method was called)
[152]237 COMPONENT_CONFIGURATION m_configurationState;
[89]238};
239
240} // pacpus
241
242#endif // DEF_PACPUS_COMPONENTBASE_H
Note: See TracBrowser for help on using the repository browser.