source: pacpusframework/branches/0.1.x/include/Pacpus/kernel/ComponentBase.h@ 356

Last change on this file since 356 was 161, checked in by morasjul, 11 years ago

addInputs and addOutputs not yet Pure virtual, no need to implement if
not need, default implementation does nothing

  • Property svn:executable set to *
File size: 6.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/pacpus.h>
27#include <Pacpus/kernel/XmlComponentConfig.h>
28#include <Pacpus/kernel/InputOutputBase.h>
29
30#include <QString>
31#include <QMap>
32
33class QWidget;
34
35namespace pacpus {
36
37// Forward declarations.
38class ComponentManager;
39
40template <typename T, class C>
41class InputInterface;
42
43template <typename T, class C>
44class OutputInterface;
45
46/** ComponentBase
47 * @brief Base class of a Pacpus component.
48 */
49class PACPUSLIB_API ComponentBase
50{
51 friend class ComponentManager;
52
53public:
54 /**
55 * Enumeration of the state that can take a component, the three last states suppose
56 * that the component is started.
57 */
58 enum COMPONENT_STATE
59 {
60 STOPPED,
61 NOT_MONITORED,
62 MONITOR_OK,
63 MONITOR_NOK
64 };
65
66 /** Resulting state of a component after its configuration. */
67 enum COMPONENT_CONFIGURATION
68 {
69 CONFIGURED_OK,
70 NOT_CONFIGURED,
71 CONFIGURATION_DELAYED,
72 CONFIGURED_FAILED
73 };
74
75 /** Ctor of ComponentBase.
76 * @param name Name of your component.
77 */
78 ComponentBase(const QString& name);
79
80 /** Dtor of ComponentBase. */
81 virtual ~ComponentBase();
82
83 /** Return the state of the component.
84 * @return Value of the current state.
85 */
86 COMPONENT_STATE getState();
87
88 /** Check whether the component if configurer or not.
89 * @return True if the component is configured, otherwise false.
90 */
91 bool isConfigured() const;
92
93 /** Return the name of the component.
94 * @return Name of the component.
95 */
96 QString name() const;
97 QString getName() const;
98
99 InputInterfaceBase * getInput(QString name) const;
100
101 OutputInterfaceBase * getOutput(QString name) const;
102
103protected:
104 /** Change the state of the component.
105 * @param state New component state.
106 */
107 void setState(COMPONENT_STATE state);
108
109 /** Called when the component starts, you must override this function. */
110 virtual void startActivity() = 0;
111
112 /** Called when the component stops, you must override this function. */
113 virtual void stopActivity() = 0;
114
115 /** Called by the ComponentManager, it configure the component thanks a XML node.
116 * @param config Component's XML node.
117 * @return State of the configuration.
118 * FIXME: 'config' should be const, but we can't change the prototype without breaking
119 * old stuff.
120 */
121 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
122
123 // virtual QString getType() = 0;
124
125 // Not Pure virtual, no need to implement if not needed !!!
126 virtual void addInputs() {}
127 virtual void addOutputs() {}
128
129protected:
130 typedef QMap<QString, InputInterfaceBase *> InputsMap;
131 typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
132
133 // TODO: use std::function<void (const DataType &)>
134 // TODO: use std::mem_fun<void (const DataType &)>
135 template <typename DataType, class ComponentType, typename Function>
136 void addInput(const char * name, Function function)
137 {
138 typedef InputInterface<DataType, ComponentType> InputType;
139 InputType * connection = new InputType(name, dynamic_cast<ComponentType *>(this), function);
140 inputs().insert(name, connection);
141 }
142
143 template <typename DataType, class ComponentType>
144 void addOutput(const char * name)
145 {
146 typedef OutputInterface<DataType, ComponentType> OutputType;
147 OutputType * connection = new OutputType(name, dynamic_cast<ComponentType *>(this));
148 outputs().insert(name, connection);
149 }
150
151 template <typename DataType, class ComponentType>
152 InputInterface<DataType, ComponentType> *
153 getTypedInput(const char * name) const
154 {
155 return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
156 }
157
158 template <typename DataType, class ComponentType>
159 OutputInterface<DataType, ComponentType> *
160 getTypedOutput(const char * name) const
161 {
162 return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
163 }
164
165 bool isActive() const;
166 void setActive(bool isActive);
167 bool isRecording() const;
168 void setRecording(bool isRecording);
169
170 InputsMap & inputs();
171 const InputsMap & inputs() const;
172 OutputsMap & outputs();
173 const OutputsMap & outputs() const;
174
175 COMPONENT_CONFIGURATION configurationState() const;
176 void setConfigurationState(COMPONENT_CONFIGURATION state);
177
178 const XmlComponentConfig xmlParameters() const;
179
180private:
181 /// called by the ComponentManager to start the component
182 int startComponent();
183
184 /// called by the ComponentManager to stop the component
185 int stopComponent();
186
187private:
188 /// The XML node that is got in the configureComponent method
189 XmlComponentConfig param;
190
191 /// the name of the component. It is this one in the XML config file
192 QString m_componentName;
193
194 /// is the component active?
195 volatile bool m_isActive;
196
197 /// is the component is recording data?
198 bool m_isRecording;
199
200 /// a pointer to the manager of components
201 ComponentManager * m_manager;
202
203 InputsMap m_inputs;
204 OutputsMap m_outputs;
205
206 /// a pointer to an optional widget
207 QWidget * m_ui;
208
209 /// store the state of the component
210 COMPONENT_STATE m_componentState;
211
212 /// is the component configured (ie configureComponent method was called)
213 COMPONENT_CONFIGURATION m_configurationState;
214};
215
216} // pacpus
217
218#endif // DEF_PACPUS_COMPONENTBASE_H
Note: See TracBrowser for help on using the repository browser.