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 |
|
---|
33 | #include <boost/program_options/options_description.hpp>
|
---|
34 | #include <boost/program_options/value_semantic.hpp>
|
---|
35 | #include <string>
|
---|
36 |
|
---|
37 | class QWidget;
|
---|
38 |
|
---|
39 | namespace boost {
|
---|
40 | namespace program_options {
|
---|
41 | class options_description_easy_init;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | namespace pacpus {
|
---|
46 |
|
---|
47 | // Forward declarations.
|
---|
48 | class ComponentManager;
|
---|
49 |
|
---|
50 | template <typename T, class C>
|
---|
51 | class InputInterface;
|
---|
52 |
|
---|
53 | template <typename T, class C>
|
---|
54 | class OutputInterface;
|
---|
55 |
|
---|
56 | /** ComponentBase
|
---|
57 | * @brief Base class of a Pacpus component.
|
---|
58 | */
|
---|
59 | class PACPUSLIB_API ComponentBase
|
---|
60 | {
|
---|
61 | friend class ComponentManager;
|
---|
62 |
|
---|
63 | public:
|
---|
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 |
|
---|
108 | InputInterfaceBase * getInput(QString name) const;
|
---|
109 |
|
---|
110 | OutputInterfaceBase * getOutput(QString name) const;
|
---|
111 |
|
---|
112 | protected:
|
---|
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;
|
---|
133 |
|
---|
134 | // Not Pure virtual, no need to implement if not needed !!!
|
---|
135 | virtual void addInputs() {}
|
---|
136 | virtual void addOutputs() {}
|
---|
137 |
|
---|
138 | boost::program_options::options_description_easy_init addParameters();
|
---|
139 |
|
---|
140 | protected:
|
---|
141 | typedef QMap<QString, InputInterfaceBase *> InputsMap;
|
---|
142 | typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
|
---|
143 |
|
---|
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 | }
|
---|
153 |
|
---|
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 | }
|
---|
161 |
|
---|
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 | }
|
---|
168 |
|
---|
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 | }
|
---|
175 |
|
---|
176 | bool isActive() const;
|
---|
177 | void setActive(bool isActive);
|
---|
178 | bool isRecording() const;
|
---|
179 | void setRecording(bool isRecording);
|
---|
180 |
|
---|
181 | InputsMap & inputs();
|
---|
182 | const InputsMap & inputs() const;
|
---|
183 | OutputsMap & outputs();
|
---|
184 | const OutputsMap & outputs() const;
|
---|
185 |
|
---|
186 | COMPONENT_CONFIGURATION configurationState() const;
|
---|
187 | void setConfigurationState(COMPONENT_CONFIGURATION state);
|
---|
188 |
|
---|
189 | const XmlComponentConfig xmlParameters() const;
|
---|
190 |
|
---|
191 | protected:
|
---|
192 | std::string mName;
|
---|
193 | std::string mTypeName;
|
---|
194 | bool mHasGui;
|
---|
195 | bool mVerbose;
|
---|
196 | int mVerbosityLevel;
|
---|
197 |
|
---|
198 | private:
|
---|
199 | /// Called by ComponentManager to handle parameters
|
---|
200 | /// @throws
|
---|
201 | void parseParameters(const XmlComponentConfig & cfg);
|
---|
202 |
|
---|
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 |
|
---|
209 | private:
|
---|
210 | boost::program_options::options_description mOptionsDescription;
|
---|
211 |
|
---|
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?
|
---|
222 | bool mIsRecording;
|
---|
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 |
|
---|
233 | /// store the state of the component
|
---|
234 | COMPONENT_STATE m_componentState;
|
---|
235 |
|
---|
236 | /// is the component configured (ie configureComponent method was called)
|
---|
237 | COMPONENT_CONFIGURATION m_configurationState;
|
---|
238 | };
|
---|
239 |
|
---|
240 | } // pacpus
|
---|
241 |
|
---|
242 | #endif // DEF_PACPUS_COMPONENTBASE_H
|
---|