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

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

Minor: fixed warnings.

  • Property svn:executable set to *
File size: 7.8 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
[286]32#include <boost/noncopyable.hpp>
[272]33#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/value_semantic.hpp>
[89]35#include <QString>
36#include <QMap>
[176]37#include <string>
38
[110]39class QWidget;
40
[281]41namespace boost
42{
43class exception_ptr;
44
45namespace program_options
46{
[176]47 class options_description_easy_init;
[281]48} // namespace program_options
[176]49
[281]50} // namespace boost
51
[286]52namespace pacpus
53{
[89]54
55class ComponentManager;
56
[184]57class InputInterfaceBase;
58class OutputInterfaceBase;
59
[152]60template <typename T, class C>
61class InputInterface;
62template <typename T, class C>
63class OutputInterface;
64
[286]65using boost::program_options::value;
[199]66
[201]67/// @brief Base class of a Pacpus component.
[89]68class PACPUSLIB_API ComponentBase
[286]69 : boost::noncopyable
[89]70{
71 friend class ComponentManager;
[152]72
[89]73public:
[201]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
[89]81 };
82
[201]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]89 };
90
[201]91 /// Ctor of ComponentBase.
92 /// @param name Name of your component.
[206]93 ComponentBase(const QString & name);
[89]94
[201]95 /// Dtor of ComponentBase.
[89]96 virtual ~ComponentBase();
97
[201]98 /// Returns the state of the component.
99 /// @return Value of the current state.
[89]100 COMPONENT_STATE getState();
101
[201]102 /// Checks whether the component if configurer or not.
103 /// @return @b true if the component is configured, otherwise @b false.
[89]104 bool isConfigured() const;
105
[201]106 /// Returns the name of the component.
107 /// @return Name of the component.
[89]108 QString getName() const;
[206]109
110 /// @returns @b true if the component has been started and is active (working)
111 bool isActive() const;
[89]112
113protected:
[201]114 /// Changes the state of the component.
115 /// @param state New component state.
[89]116 void setState(COMPONENT_STATE state);
117
[201]118 /// Called when the component starts, you must override this function.
[89]119 virtual void startActivity() = 0;
120
[201]121 /// Called when the component stops, you must override this function.
[89]122 virtual void stopActivity() = 0;
123
[201]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.
[89]129 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
130
131 // virtual QString getType() = 0;
[120]132
[201]133 /// @todo FIXME: should be pure virtual, but it will be a breaking change
[161]134 virtual void addInputs() {}
135 virtual void addOutputs() {}
[120]136
[201]137 /// Returns an object permitting to add component parameters.
[286]138 boost::program_options::options_description_easy_init addParameters();
[199]139
[89]140protected:
[152]141 typedef QMap<QString, InputInterfaceBase *> InputsMap;
142 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
[89]143
[152]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 }
[89]151
[152]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 }
[202]159
160 /// @todo DOC
161 InputInterfaceBase * getInput(QString name) const;
[89]162
[202]163 /// @todo DOC
164 OutputInterfaceBase * getOutput(QString name) const;
165
[152]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 }
[89]172
[152]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 }
[89]179
[152]180 void setActive(bool isActive);
181 bool isRecording() const;
182 void setRecording(bool isRecording);
[89]183
[152]184 InputsMap & inputs();
185 const InputsMap & inputs() const;
186 OutputsMap & outputs();
187 const OutputsMap & outputs() const;
[89]188
[152]189 COMPONENT_CONFIGURATION configurationState() const;
190 void setConfigurationState(COMPONENT_CONFIGURATION state);
[110]191
[152]192 const XmlComponentConfig xmlParameters() const;
[176]193
194protected:
195 std::string mName;
196 std::string mTypeName;
[152]197
[181]198 /// Whether to display or not the graphical interface (GUI)
199 bool hasGui() const;
200 bool isOutputVerbose() const;
201 int getVerbosityLevel() const;
202
[89]203private:
[176]204 /// Called by ComponentManager to handle parameters
205 /// @throws
206 void parseParameters(const XmlComponentConfig & cfg);
207
[89]208 /// called by the ComponentManager to start the component
209 int startComponent();
[281]210 void startComponentInThread();
211 void startComponentWithException(boost::exception_ptr& error);
[89]212
213 /// called by the ComponentManager to stop the component
214 int stopComponent();
215
[152]216private:
[181]217 bool mHasGui;
218 bool mVerbose;
219 int mVerbosityLevel;
220
[176]221 boost::program_options::options_description mOptionsDescription;
222
[152]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?
[177]233 bool mIsRecording;
[152]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
[89]244 /// store the state of the component
[152]245 COMPONENT_STATE m_componentState;
[89]246
247 /// is the component configured (ie configureComponent method was called)
[152]248 COMPONENT_CONFIGURATION m_configurationState;
[89]249};
250
251} // pacpus
252
253#endif // DEF_PACPUS_COMPONENTBASE_H
Note: See TracBrowser for help on using the repository browser.