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
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
[272]32#include <boost/program_options/options_description.hpp>
33#include <boost/program_options/value_semantic.hpp>
[89]34#include <QString>
35#include <QMap>
[176]36#include <string>
37
[110]38class QWidget;
39
[176]40namespace boost {
41namespace program_options {
42 class options_description_easy_init;
43}
44}
45
[89]46namespace pacpus {
47
48class ComponentManager;
49
[184]50class InputInterfaceBase;
51class OutputInterfaceBase;
52
[152]53template <typename T, class C>
54class InputInterface;
55template <typename T, class C>
56class OutputInterface;
57
[199]58using ::boost::program_options::value;
59
[201]60/// @brief Base class of a Pacpus component.
[89]61class PACPUSLIB_API ComponentBase
62{
63 friend class ComponentManager;
[152]64
[89]65public:
[201]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
[89]73 };
74
[201]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
[89]81 };
82
[201]83 /// Ctor of ComponentBase.
84 /// @param name Name of your component.
[206]85 ComponentBase(const QString & name);
[89]86
[201]87 /// Dtor of ComponentBase.
[89]88 virtual ~ComponentBase();
89
[201]90 /// Returns the state of the component.
91 /// @return Value of the current state.
[89]92 COMPONENT_STATE getState();
93
[201]94 /// Checks whether the component if configurer or not.
95 /// @return @b true if the component is configured, otherwise @b false.
[89]96 bool isConfigured() const;
97
[201]98 /// Returns the name of the component.
99 /// @return Name of the component.
[89]100 QString getName() const;
[206]101
102 /// @returns @b true if the component has been started and is active (working)
103 bool isActive() const;
[89]104
105protected:
[201]106 /// Changes the state of the component.
107 /// @param state New component state.
[89]108 void setState(COMPONENT_STATE state);
109
[201]110 /// Called when the component starts, you must override this function.
[89]111 virtual void startActivity() = 0;
112
[201]113 /// Called when the component stops, you must override this function.
[89]114 virtual void stopActivity() = 0;
115
[201]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.
[89]121 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
122
123 // virtual QString getType() = 0;
[120]124
[201]125 /// @todo FIXME: should be pure virtual, but it will be a breaking change
[161]126 virtual void addInputs() {}
127 virtual void addOutputs() {}
[120]128
[201]129 /// Returns an object permitting to add component parameters.
[199]130 ::boost::program_options::options_description_easy_init addParameters();
131
[89]132protected:
[152]133 typedef QMap<QString, InputInterfaceBase *> InputsMap;
134 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
[89]135
[152]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 }
[89]143
[152]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 }
[202]151
152 /// @todo DOC
153 InputInterfaceBase * getInput(QString name) const;
[89]154
[202]155 /// @todo DOC
156 OutputInterfaceBase * getOutput(QString name) const;
157
[152]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 }
[89]164
[152]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 }
[89]171
[152]172 void setActive(bool isActive);
173 bool isRecording() const;
174 void setRecording(bool isRecording);
[89]175
[152]176 InputsMap & inputs();
177 const InputsMap & inputs() const;
178 OutputsMap & outputs();
179 const OutputsMap & outputs() const;
[89]180
[152]181 COMPONENT_CONFIGURATION configurationState() const;
182 void setConfigurationState(COMPONENT_CONFIGURATION state);
[110]183
[152]184 const XmlComponentConfig xmlParameters() const;
[176]185
186protected:
187 std::string mName;
188 std::string mTypeName;
[152]189
[181]190 /// Whether to display or not the graphical interface (GUI)
191 bool hasGui() const;
192 bool isOutputVerbose() const;
193 int getVerbosityLevel() const;
194
[89]195private:
[176]196 /// Called by ComponentManager to handle parameters
197 /// @throws
198 void parseParameters(const XmlComponentConfig & cfg);
199
[89]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
[152]206private:
[181]207 bool mHasGui;
208 bool mVerbose;
209 int mVerbosityLevel;
210
[176]211 boost::program_options::options_description mOptionsDescription;
212
[152]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?
[177]223 bool mIsRecording;
[152]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
[89]234 /// store the state of the component
[152]235 COMPONENT_STATE m_componentState;
[89]236
237 /// is the component configured (ie configureComponent method was called)
[152]238 COMPONENT_CONFIGURATION m_configurationState;
[89]239};
240
241} // pacpus
242
243#endif // DEF_PACPUS_COMPONENTBASE_H
Note: See TracBrowser for help on using the repository browser.