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