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

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

Minor: formatting.

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