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 | class QWidget;
|
---|
34 |
|
---|
35 | namespace pacpus {
|
---|
36 |
|
---|
37 | // Forward declarations.
|
---|
38 | class ComponentManager;
|
---|
39 |
|
---|
40 | template <typename T, class C>
|
---|
41 | class InputInterface;
|
---|
42 |
|
---|
43 | template <typename T, class C>
|
---|
44 | class OutputInterface;
|
---|
45 |
|
---|
46 | /** ComponentBase
|
---|
47 | * @brief Base class of a Pacpus component.
|
---|
48 | */
|
---|
49 | class PACPUSLIB_API ComponentBase
|
---|
50 | {
|
---|
51 | friend class ComponentManager;
|
---|
52 |
|
---|
53 | public:
|
---|
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 |
|
---|
103 | protected:
|
---|
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 | virtual void addInputs() = 0;
|
---|
126 | virtual void addOutputs() = 0;
|
---|
127 |
|
---|
128 | protected:
|
---|
129 | typedef QMap<QString, InputInterfaceBase *> InputsMap;
|
---|
130 | typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
|
---|
131 |
|
---|
132 | // TODO: use std::function<void (const DataType &)>
|
---|
133 | // TODO: use std::mem_fun<void (const DataType &)>
|
---|
134 | template <typename DataType, class ComponentType, typename Function>
|
---|
135 | void addInput(const char * name, Function function)
|
---|
136 | {
|
---|
137 | typedef InputInterface<DataType, ComponentType> InputType;
|
---|
138 | InputType * connection = new InputType(name, dynamic_cast<ComponentType *>(this), function);
|
---|
139 | inputs().insert(name, connection);
|
---|
140 | }
|
---|
141 |
|
---|
142 | template <typename DataType, class ComponentType>
|
---|
143 | void addOutput(const char * name)
|
---|
144 | {
|
---|
145 | typedef OutputInterface<DataType, ComponentType> OutputType;
|
---|
146 | OutputType * connection = new OutputType(name, dynamic_cast<ComponentType *>(this));
|
---|
147 | outputs().insert(name, connection);
|
---|
148 | }
|
---|
149 |
|
---|
150 | template <typename DataType, class ComponentType>
|
---|
151 | InputInterface<DataType, ComponentType> *
|
---|
152 | getTypedInput(const char * name) const
|
---|
153 | {
|
---|
154 | return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
|
---|
155 | }
|
---|
156 |
|
---|
157 | template <typename DataType, class ComponentType>
|
---|
158 | OutputInterface<DataType, ComponentType> *
|
---|
159 | getTypedOutput(const char * name) const
|
---|
160 | {
|
---|
161 | return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
|
---|
162 | }
|
---|
163 |
|
---|
164 | bool isActive() const;
|
---|
165 | void setActive(bool isActive);
|
---|
166 | bool isRecording() const;
|
---|
167 | void setRecording(bool isRecording);
|
---|
168 |
|
---|
169 | InputsMap & inputs();
|
---|
170 | const InputsMap & inputs() const;
|
---|
171 | OutputsMap & outputs();
|
---|
172 | const OutputsMap & outputs() const;
|
---|
173 |
|
---|
174 | COMPONENT_CONFIGURATION configurationState() const;
|
---|
175 | void setConfigurationState(COMPONENT_CONFIGURATION state);
|
---|
176 |
|
---|
177 | const XmlComponentConfig xmlParameters() const;
|
---|
178 |
|
---|
179 | private:
|
---|
180 | /// called by the ComponentManager to start the component
|
---|
181 | int startComponent();
|
---|
182 |
|
---|
183 | /// called by the ComponentManager to stop the component
|
---|
184 | int stopComponent();
|
---|
185 |
|
---|
186 | private:
|
---|
187 | /// The XML node that is got in the configureComponent method
|
---|
188 | XmlComponentConfig param;
|
---|
189 |
|
---|
190 | /// the name of the component. It is this one in the XML config file
|
---|
191 | QString m_componentName;
|
---|
192 |
|
---|
193 | /// is the component active?
|
---|
194 | volatile bool m_isActive;
|
---|
195 |
|
---|
196 | /// is the component is recording data?
|
---|
197 | bool m_isRecording;
|
---|
198 |
|
---|
199 | /// a pointer to the manager of components
|
---|
200 | ComponentManager * m_manager;
|
---|
201 |
|
---|
202 | InputsMap m_inputs;
|
---|
203 | OutputsMap m_outputs;
|
---|
204 |
|
---|
205 | /// a pointer to an optional widget
|
---|
206 | QWidget * m_ui;
|
---|
207 |
|
---|
208 | /// store the state of the component
|
---|
209 | COMPONENT_STATE m_componentState;
|
---|
210 |
|
---|
211 | /// is the component configured (ie configureComponent method was called)
|
---|
212 | COMPONENT_CONFIGURATION m_configurationState;
|
---|
213 | };
|
---|
214 |
|
---|
215 | } // pacpus
|
---|
216 |
|
---|
217 | #endif // DEF_PACPUS_COMPONENTBASE_H
|
---|