[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 |
|
---|
| 32 | #include <QString>
|
---|
| 33 | #include <QMap>
|
---|
| 34 |
|
---|
[176] | 35 | #include <boost/program_options/options_description.hpp>
|
---|
| 36 | #include <boost/program_options/value_semantic.hpp>
|
---|
| 37 | #include <string>
|
---|
| 38 |
|
---|
[110] | 39 | class QWidget;
|
---|
| 40 |
|
---|
[176] | 41 | namespace boost {
|
---|
| 42 | namespace program_options {
|
---|
| 43 | class options_description_easy_init;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[89] | 47 | namespace pacpus {
|
---|
| 48 |
|
---|
| 49 | class ComponentManager;
|
---|
| 50 |
|
---|
[184] | 51 | class InputInterfaceBase;
|
---|
| 52 | class OutputInterfaceBase;
|
---|
| 53 |
|
---|
[152] | 54 | template <typename T, class C>
|
---|
| 55 | class InputInterface;
|
---|
| 56 | template <typename T, class C>
|
---|
| 57 | class OutputInterface;
|
---|
| 58 |
|
---|
[89] | 59 | /** ComponentBase
|
---|
| 60 | * @brief Base class of a Pacpus component.
|
---|
| 61 | */
|
---|
| 62 | class PACPUSLIB_API ComponentBase
|
---|
| 63 | {
|
---|
| 64 | friend class ComponentManager;
|
---|
[152] | 65 |
|
---|
[89] | 66 | public:
|
---|
| 67 | /**
|
---|
| 68 | * Enumeration of the state that can take a component, the three last states suppose
|
---|
| 69 | * that the component is started.
|
---|
| 70 | */
|
---|
| 71 | enum COMPONENT_STATE
|
---|
| 72 | {
|
---|
| 73 | STOPPED,
|
---|
| 74 | NOT_MONITORED,
|
---|
| 75 | MONITOR_OK,
|
---|
| 76 | MONITOR_NOK
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | /** Resulting state of a component after its configuration. */
|
---|
| 80 | enum COMPONENT_CONFIGURATION
|
---|
| 81 | {
|
---|
| 82 | CONFIGURED_OK,
|
---|
| 83 | NOT_CONFIGURED,
|
---|
| 84 | CONFIGURATION_DELAYED,
|
---|
| 85 | CONFIGURED_FAILED
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | /** Ctor of ComponentBase.
|
---|
| 89 | * @param name Name of your component.
|
---|
| 90 | */
|
---|
| 91 | ComponentBase(const QString& name);
|
---|
| 92 |
|
---|
| 93 | /** Dtor of ComponentBase. */
|
---|
| 94 | virtual ~ComponentBase();
|
---|
| 95 |
|
---|
| 96 | /** Return the state of the component.
|
---|
| 97 | * @return Value of the current state.
|
---|
| 98 | */
|
---|
| 99 | COMPONENT_STATE getState();
|
---|
| 100 |
|
---|
| 101 | /** Check whether the component if configurer or not.
|
---|
| 102 | * @return True if the component is configured, otherwise false.
|
---|
| 103 | */
|
---|
| 104 | bool isConfigured() const;
|
---|
| 105 |
|
---|
| 106 | /** Return the name of the component.
|
---|
| 107 | * @return Name of the component.
|
---|
| 108 | */
|
---|
| 109 | QString getName() const;
|
---|
| 110 |
|
---|
[152] | 111 | InputInterfaceBase * getInput(QString name) const;
|
---|
[89] | 112 |
|
---|
[152] | 113 | OutputInterfaceBase * getOutput(QString name) const;
|
---|
[89] | 114 |
|
---|
| 115 | protected:
|
---|
| 116 | /** Change the state of the component.
|
---|
| 117 | * @param state New component state.
|
---|
| 118 | */
|
---|
| 119 | void setState(COMPONENT_STATE state);
|
---|
| 120 |
|
---|
| 121 | /** Called when the component starts, you must override this function. */
|
---|
| 122 | virtual void startActivity() = 0;
|
---|
| 123 |
|
---|
| 124 | /** Called when the component stops, you must override this function. */
|
---|
| 125 | virtual void stopActivity() = 0;
|
---|
| 126 |
|
---|
| 127 | /** Called by the ComponentManager, it configure the component thanks a XML node.
|
---|
| 128 | * @param config Component's XML node.
|
---|
| 129 | * @return State of the configuration.
|
---|
| 130 | * FIXME: 'config' should be const, but we can't change the prototype without breaking
|
---|
| 131 | * old stuff.
|
---|
| 132 | */
|
---|
| 133 | virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config) = 0;
|
---|
| 134 |
|
---|
| 135 | // virtual QString getType() = 0;
|
---|
[120] | 136 |
|
---|
[161] | 137 | // Not Pure virtual, no need to implement if not needed !!!
|
---|
| 138 | virtual void addInputs() {}
|
---|
| 139 | virtual void addOutputs() {}
|
---|
[120] | 140 |
|
---|
[176] | 141 | boost::program_options::options_description_easy_init addParameters();
|
---|
| 142 |
|
---|
[89] | 143 | protected:
|
---|
[152] | 144 | typedef QMap<QString, InputInterfaceBase *> InputsMap;
|
---|
| 145 | typedef QMap<QString, OutputInterfaceBase *> OutputsMap;
|
---|
[89] | 146 |
|
---|
[152] | 147 | // TODO: use std::function<void (const DataType &)>
|
---|
| 148 | // TODO: use std::mem_fun<void (const DataType &)>
|
---|
| 149 | template <typename DataType, class ComponentType, typename Function>
|
---|
| 150 | void addInput(const char * name, Function function)
|
---|
| 151 | {
|
---|
| 152 | typedef InputInterface<DataType, ComponentType> InputType;
|
---|
| 153 | InputType * connection = new InputType(name, dynamic_cast<ComponentType *>(this), function);
|
---|
| 154 | inputs().insert(name, connection);
|
---|
| 155 | }
|
---|
[89] | 156 |
|
---|
[152] | 157 | template <typename DataType, class ComponentType>
|
---|
| 158 | void addOutput(const char * name)
|
---|
| 159 | {
|
---|
| 160 | typedef OutputInterface<DataType, ComponentType> OutputType;
|
---|
| 161 | OutputType * connection = new OutputType(name, dynamic_cast<ComponentType *>(this));
|
---|
| 162 | outputs().insert(name, connection);
|
---|
| 163 | }
|
---|
[89] | 164 |
|
---|
[152] | 165 | template <typename DataType, class ComponentType>
|
---|
| 166 | InputInterface<DataType, ComponentType> *
|
---|
| 167 | getTypedInput(const char * name) const
|
---|
| 168 | {
|
---|
| 169 | return dynamic_cast<InputInterface<DataType, ComponentType> *>(getInput(name));
|
---|
| 170 | }
|
---|
[89] | 171 |
|
---|
[152] | 172 | template <typename DataType, class ComponentType>
|
---|
| 173 | OutputInterface<DataType, ComponentType> *
|
---|
| 174 | getTypedOutput(const char * name) const
|
---|
| 175 | {
|
---|
| 176 | return dynamic_cast<OutputInterface<DataType, ComponentType> *>(getOutput(name));
|
---|
| 177 | }
|
---|
[89] | 178 |
|
---|
[152] | 179 | bool isActive() const;
|
---|
| 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 |
|
---|
| 194 | protected:
|
---|
| 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] | 203 | private:
|
---|
[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();
|
---|
| 210 |
|
---|
| 211 | /// called by the ComponentManager to stop the component
|
---|
| 212 | int stopComponent();
|
---|
| 213 |
|
---|
[152] | 214 | private:
|
---|
[181] | 215 | bool mHasGui;
|
---|
| 216 | bool mVerbose;
|
---|
| 217 | int mVerbosityLevel;
|
---|
| 218 |
|
---|
[176] | 219 | boost::program_options::options_description mOptionsDescription;
|
---|
| 220 |
|
---|
[152] | 221 | /// The XML node that is got in the configureComponent method
|
---|
| 222 | XmlComponentConfig param;
|
---|
| 223 |
|
---|
| 224 | /// the name of the component. It is this one in the XML config file
|
---|
| 225 | QString m_componentName;
|
---|
| 226 |
|
---|
| 227 | /// is the component active?
|
---|
| 228 | volatile bool m_isActive;
|
---|
| 229 |
|
---|
| 230 | /// is the component is recording data?
|
---|
[177] | 231 | bool mIsRecording;
|
---|
[152] | 232 |
|
---|
| 233 | /// a pointer to the manager of components
|
---|
| 234 | ComponentManager * m_manager;
|
---|
| 235 |
|
---|
| 236 | InputsMap m_inputs;
|
---|
| 237 | OutputsMap m_outputs;
|
---|
| 238 |
|
---|
| 239 | /// a pointer to an optional widget
|
---|
| 240 | QWidget * m_ui;
|
---|
| 241 |
|
---|
[89] | 242 | /// store the state of the component
|
---|
[152] | 243 | COMPONENT_STATE m_componentState;
|
---|
[89] | 244 |
|
---|
| 245 | /// is the component configured (ie configureComponent method was called)
|
---|
[152] | 246 | COMPONENT_CONFIGURATION m_configurationState;
|
---|
[89] | 247 | };
|
---|
| 248 |
|
---|
| 249 | } // pacpus
|
---|
| 250 |
|
---|
| 251 | #endif // DEF_PACPUS_COMPONENTBASE_H
|
---|