| 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 | /// @version $Id: ComponentBase.cpp 76 2013-01-10 17:05:10Z kurdejma $
|
|---|
| 6 |
|
|---|
| 7 | #include <Pacpus/kernel/ComponentBase.h>
|
|---|
| 8 |
|
|---|
| 9 | #include <Pacpus/kernel/ComponentManager.h>
|
|---|
| 10 | #include <Pacpus/kernel/Log.h>
|
|---|
| 11 | #include <Pacpus/kernel/PacpusException.h>
|
|---|
| 12 |
|
|---|
| 13 | #include <boost/program_options/parsers.hpp>
|
|---|
| 14 | #include <boost/program_options/variables_map.hpp>
|
|---|
| 15 | #include <ostream>
|
|---|
| 16 | #include <string>
|
|---|
| 17 | #include <vector>
|
|---|
| 18 |
|
|---|
| 19 | namespace po = boost::program_options;
|
|---|
| 20 | using namespace pacpus;
|
|---|
| 21 | using namespace std;
|
|---|
| 22 |
|
|---|
| 23 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
|
|---|
| 24 |
|
|---|
| 25 | namespace std
|
|---|
| 26 | {
|
|---|
| 27 |
|
|---|
| 28 | template <typename _Elem, typename _Traits>
|
|---|
| 29 | std::basic_ostream<_Elem, _Traits> & operator<<(std::basic_ostream<_Elem, _Traits> & os, const boost::program_options::variables_map & vm)
|
|---|
| 30 | {
|
|---|
| 31 | for (po::variables_map::const_iterator i = vm.begin(); i != vm.end(); ++i) {
|
|---|
| 32 | const po::variable_value & v = i->second;
|
|---|
| 33 | if (v.empty()) {
|
|---|
| 34 | continue;
|
|---|
| 35 | }
|
|---|
| 36 | const type_info & type = v.value().type();
|
|---|
| 37 | if (type == typeid(string)) {
|
|---|
| 38 | const string & val = v.as<string>();
|
|---|
| 39 | os << i->first << "=" << val;
|
|---|
| 40 | } else if (type == typeid(long)) {
|
|---|
| 41 | int val = v.as<long>();
|
|---|
| 42 | os << i->first << "=" << val;
|
|---|
| 43 | } else if (type == typeid(int)) {
|
|---|
| 44 | int val = v.as<int>();
|
|---|
| 45 | os << i->first << "=" << val;
|
|---|
| 46 | } else if (type == typeid(unsigned long)) {
|
|---|
| 47 | int val = v.as<unsigned long>();
|
|---|
| 48 | os << i->first << "=" << val;
|
|---|
| 49 | } else if (type == typeid(unsigned int)) {
|
|---|
| 50 | int val = v.as<unsigned int>();
|
|---|
| 51 | os << i->first << "=" << val;
|
|---|
| 52 | } else if (type == typeid(double)) {
|
|---|
| 53 | int val = v.as<double>();
|
|---|
| 54 | os << i->first << "=" << val;
|
|---|
| 55 | } else if (type == typeid(float)) {
|
|---|
| 56 | int val = v.as<float>();
|
|---|
| 57 | os << i->first << "=" << val;
|
|---|
| 58 | } else if (type == typeid(bool)) {
|
|---|
| 59 | int val = v.as<bool>();
|
|---|
| 60 | os << i->first << "=" << val;
|
|---|
| 61 | } else {
|
|---|
| 62 | // unknown value type
|
|---|
| 63 | os << i->first;
|
|---|
| 64 | }
|
|---|
| 65 | os << "\n";
|
|---|
| 66 | }
|
|---|
| 67 | return os;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | } // namespace std
|
|---|
| 71 |
|
|---|
| 72 | DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
|
|---|
| 73 |
|
|---|
| 74 | ComponentBase::ComponentBase(const QString & componentName)
|
|---|
| 75 | : m_componentName(componentName)
|
|---|
| 76 | , m_isActive(false)
|
|---|
| 77 | , mIsRecording(true)
|
|---|
| 78 | , m_manager(NULL)
|
|---|
| 79 | , m_ui(NULL)
|
|---|
| 80 | , m_componentState(NOT_MONITORED)
|
|---|
| 81 | , mOptionsDescription("Component parameters")
|
|---|
| 82 | {
|
|---|
| 83 | LOG_TRACE("constructor");
|
|---|
| 84 | // Get a pointer on the instance of ComponentManager.
|
|---|
| 85 | m_manager = ComponentManager::getInstance();
|
|---|
| 86 | LOG_INFO("component " << getName() << " was created");
|
|---|
| 87 |
|
|---|
| 88 | addParameters()
|
|---|
| 89 | ("name", po::value<string>(&mName)->required(), "component name")
|
|---|
| 90 | ("type", po::value<string>(&mTypeName)->required(), "component type")
|
|---|
| 91 | ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
|
|---|
| 92 | ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
|
|---|
| 93 | ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
|
|---|
| 94 | ("recording", po::value<bool>(&mIsRecording)->default_value(false), "whether to record data")
|
|---|
| 95 | ;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | ComponentBase::~ComponentBase()
|
|---|
| 99 | {
|
|---|
| 100 | LOG_TRACE("destructor");
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool ComponentBase::isActive() const
|
|---|
| 104 | {
|
|---|
| 105 | return m_isActive;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void ComponentBase::setActive(bool isActive)
|
|---|
| 109 | {
|
|---|
| 110 | m_isActive = isActive;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | bool ComponentBase::isRecording() const
|
|---|
| 114 | {
|
|---|
| 115 | return mIsRecording;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void ComponentBase::setRecording(bool isRecording)
|
|---|
| 119 | {
|
|---|
| 120 | mIsRecording = isRecording;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | const XmlComponentConfig ComponentBase::xmlParameters() const
|
|---|
| 124 | {
|
|---|
| 125 | return param;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int ComponentBase::startComponent()
|
|---|
| 129 | {
|
|---|
| 130 | if (isActive()) {
|
|---|
| 131 | LOG_DEBUG("component already started, cannot (re-)start");
|
|---|
| 132 | return false;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | setActive(true);
|
|---|
| 136 | startActivity();
|
|---|
| 137 |
|
|---|
| 138 | return true;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | int ComponentBase::stopComponent()
|
|---|
| 142 | {
|
|---|
| 143 | if (!isActive()) {
|
|---|
| 144 | LOG_DEBUG("component already stopped, cannot (re-)stop");
|
|---|
| 145 | return false;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | setActive(false);
|
|---|
| 149 | stopActivity();
|
|---|
| 150 |
|
|---|
| 151 | return true;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void ComponentBase::setState(const COMPONENT_STATE state)
|
|---|
| 155 | {
|
|---|
| 156 | m_componentState = state;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // FIXME: this should be const.
|
|---|
| 160 | ComponentBase::COMPONENT_STATE ComponentBase::getState()
|
|---|
| 161 | {
|
|---|
| 162 | COMPONENT_STATE state = m_componentState;
|
|---|
| 163 | if (ComponentBase::NOT_MONITORED != m_componentState) {
|
|---|
| 164 | m_componentState = ComponentBase::MONITOR_NOK;
|
|---|
| 165 | }
|
|---|
| 166 | return state;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | ComponentBase::COMPONENT_CONFIGURATION ComponentBase::configurationState() const
|
|---|
| 170 | {
|
|---|
| 171 | return m_configurationState;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void ComponentBase::setConfigurationState(COMPONENT_CONFIGURATION state)
|
|---|
| 175 | {
|
|---|
| 176 | m_configurationState = state;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | bool ComponentBase::isConfigured() const
|
|---|
| 180 | {
|
|---|
| 181 | return (m_configurationState == CONFIGURED_OK);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | QString ComponentBase::getName() const
|
|---|
| 185 | {
|
|---|
| 186 | return m_componentName;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | ComponentBase::InputsMap & ComponentBase::inputs()
|
|---|
| 190 | {
|
|---|
| 191 | return m_inputs;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | const ComponentBase::InputsMap & ComponentBase::inputs() const
|
|---|
| 195 | {
|
|---|
| 196 | return m_inputs;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | ComponentBase::OutputsMap & ComponentBase::outputs()
|
|---|
| 200 | {
|
|---|
| 201 | return m_outputs;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | const ComponentBase::OutputsMap & ComponentBase::outputs() const
|
|---|
| 205 | {
|
|---|
| 206 | return m_outputs;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | InputInterfaceBase * ComponentBase::getInput(QString inputName) const
|
|---|
| 210 | {
|
|---|
| 211 | if (inputs().contains(inputName)) {
|
|---|
| 212 | return inputs()[inputName];
|
|---|
| 213 | }
|
|---|
| 214 | LOG_WARN("Component " << getName() << " does not contain input " << inputName);
|
|---|
| 215 | return NULL;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
|
|---|
| 219 | {
|
|---|
| 220 | if (outputs().contains(outputName)) {
|
|---|
| 221 | return outputs()[outputName];
|
|---|
| 222 | }
|
|---|
| 223 | LOG_WARN("Component " << getName() << " does not contain output " << outputName);
|
|---|
| 224 | return NULL;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | bool ComponentBase::hasGui() const
|
|---|
| 228 | {
|
|---|
| 229 | return mHasGui;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | bool ComponentBase::isOutputVerbose() const
|
|---|
| 233 | {
|
|---|
| 234 | return mVerbose || (getVerbosityLevel() > 0);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | int ComponentBase::getVerbosityLevel() const
|
|---|
| 238 | {
|
|---|
| 239 | return mVerbosityLevel;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | po::options_description_easy_init ComponentBase::addParameters()
|
|---|
| 243 | {
|
|---|
| 244 | return mOptionsDescription.add_options();
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | class DomElementParser
|
|---|
| 248 | {
|
|---|
| 249 | public:
|
|---|
| 250 | DomElementParser(QDomElement const& args);
|
|---|
| 251 |
|
|---|
| 252 | /** Sets options descriptions to use. */
|
|---|
| 253 | DomElementParser& options(const boost::program_options::options_description& desc);
|
|---|
| 254 |
|
|---|
| 255 | /** Parses the options and returns the result of parsing.
|
|---|
| 256 | Throws on error.
|
|---|
| 257 | */
|
|---|
| 258 | boost::program_options::basic_parsed_options<char> run();
|
|---|
| 259 |
|
|---|
| 260 | /** Specifies that unregistered options are allowed and should
|
|---|
| 261 | be passed though. For each command like token that looks
|
|---|
| 262 | like an option but does not contain a recognized name, an
|
|---|
| 263 | instance of basic_option<charT> will be added to result,
|
|---|
| 264 | with 'unrecognized' field set to 'true'. It's possible to
|
|---|
| 265 | collect all unrecognized options with the 'collect_unrecognized'
|
|---|
| 266 | funciton.
|
|---|
| 267 | */
|
|---|
| 268 | DomElementParser& allow_unregistered();
|
|---|
| 269 |
|
|---|
| 270 | private:
|
|---|
| 271 | boost::program_options::basic_parsed_options<char> parseDomElement(
|
|---|
| 272 | const QDomElement& dom_element,
|
|---|
| 273 | const boost::program_options::options_description& desc,
|
|---|
| 274 | bool allow_unregistered = false);
|
|---|
| 275 |
|
|---|
| 276 | private:
|
|---|
| 277 | const boost::program_options::options_description* m_desc;
|
|---|
| 278 | const QDomElement& m_dom_element;
|
|---|
| 279 | bool m_allow_unregistered;
|
|---|
| 280 | };
|
|---|
| 281 |
|
|---|
| 282 | DomElementParser::DomElementParser(const QDomElement& dom_element)
|
|---|
| 283 | : m_dom_element(dom_element)
|
|---|
| 284 | , m_allow_unregistered(false)
|
|---|
| 285 | {
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | DomElementParser& DomElementParser::options(const boost::program_options::options_description& desc)
|
|---|
| 289 | {
|
|---|
| 290 | m_desc = &desc;
|
|---|
| 291 | return *this;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | DomElementParser& DomElementParser::allow_unregistered()
|
|---|
| 295 | {
|
|---|
| 296 | m_allow_unregistered = true;
|
|---|
| 297 | return *this;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | #include <boost/iterator/iterator_facade.hpp>
|
|---|
| 301 | #include <boost/program_options/errors.hpp>
|
|---|
| 302 |
|
|---|
| 303 | namespace detail
|
|---|
| 304 | {
|
|---|
| 305 | template <typename charT>
|
|---|
| 306 | class basic_dom_element_iterator
|
|---|
| 307 | : public boost::iterator_facade<
|
|---|
| 308 | basic_dom_element_iterator<charT>
|
|---|
| 309 | , const boost::program_options::option
|
|---|
| 310 | , boost::random_access_traversal_tag
|
|---|
| 311 | >
|
|---|
| 312 | {
|
|---|
| 313 | public:
|
|---|
| 314 | typedef boost::program_options::option ValueType;
|
|---|
| 315 |
|
|---|
| 316 | basic_dom_element_iterator<charT>()
|
|---|
| 317 | : m_dom_element(NULL)
|
|---|
| 318 | , m_at_eof(true)
|
|---|
| 319 | {
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | basic_dom_element_iterator<charT>(const QDomElement& dom_element,
|
|---|
| 323 | const std::set<std::string>& allowed_options,
|
|---|
| 324 | bool allow_unregistered = false)
|
|---|
| 325 | : m_dom_element(&dom_element)
|
|---|
| 326 | , m_allowed_options(allowed_options)
|
|---|
| 327 | , m_allow_unregistered(allow_unregistered)
|
|---|
| 328 | , m_i(0)
|
|---|
| 329 | {
|
|---|
| 330 | m_attrs = m_dom_element->attributes();
|
|---|
| 331 | m_at_eof = !(m_i < m_attrs.size());
|
|---|
| 332 | if (!m_at_eof) {
|
|---|
| 333 | get();
|
|---|
| 334 | }
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | private:
|
|---|
| 338 | friend class ::boost::iterator_core_access;
|
|---|
| 339 |
|
|---|
| 340 | bool equal(const basic_dom_element_iterator<charT>& other) const
|
|---|
| 341 | {
|
|---|
| 342 | if (m_at_eof && other.m_at_eof) {
|
|---|
| 343 | return true;
|
|---|
| 344 | }
|
|---|
| 345 | return false;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | void increment()
|
|---|
| 349 | {
|
|---|
| 350 | ++m_i;
|
|---|
| 351 | m_at_eof = !(m_i < m_attrs.size());
|
|---|
| 352 | if (!m_at_eof) {
|
|---|
| 353 | get();
|
|---|
| 354 | }
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | const ValueType& dereference() const
|
|---|
| 358 | {
|
|---|
| 359 | return m_value;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | void advance(size_t n)
|
|---|
| 363 | {
|
|---|
| 364 | m_i += n;
|
|---|
| 365 | m_at_eof = !(m_i < m_attrs.size());
|
|---|
| 366 | if (!m_at_eof) {
|
|---|
| 367 | get();
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | difference_type distance_to(const basic_dom_element_iterator<charT>& other) const
|
|---|
| 372 | {
|
|---|
| 373 | return other.m_i - this->m_i;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | private:
|
|---|
| 377 | ValueType& value()
|
|---|
| 378 | {
|
|---|
| 379 | return m_value;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | void get()
|
|---|
| 383 | {
|
|---|
| 384 | using namespace boost::program_options;
|
|---|
| 385 |
|
|---|
| 386 | QDomNode node = m_attrs.item(m_i);
|
|---|
| 387 | QDomAttr attr = node.toAttr();
|
|---|
| 388 |
|
|---|
| 389 | string name = attr.name().toStdString();
|
|---|
| 390 | string value = attr.value().toStdString();
|
|---|
| 391 |
|
|---|
| 392 | bool registered = allowed_option(name);
|
|---|
| 393 | if (!registered && !m_allow_unregistered) {
|
|---|
| 394 | boost::throw_exception(unknown_option(name));
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | this->value().string_key = name;
|
|---|
| 398 | this->value().value.clear();
|
|---|
| 399 | this->value().value.push_back(value);
|
|---|
| 400 | this->value().unregistered = !registered;
|
|---|
| 401 | this->value().original_tokens.push_back(name);
|
|---|
| 402 | this->value().original_tokens.push_back(value);
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | bool allowed_option(const std::string& s) const
|
|---|
| 406 | {
|
|---|
| 407 | set<string>::const_iterator i = m_allowed_options.find(s);
|
|---|
| 408 | if (i != m_allowed_options.end()) {
|
|---|
| 409 | return true;
|
|---|
| 410 | }
|
|---|
| 411 | return false;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | private:
|
|---|
| 415 | const QDomElement* m_dom_element;
|
|---|
| 416 | std::set<std::string> m_allowed_options;
|
|---|
| 417 | bool m_allow_unregistered;
|
|---|
| 418 |
|
|---|
| 419 | QDomNamedNodeMap m_attrs;
|
|---|
| 420 | int m_i;
|
|---|
| 421 | bool m_at_eof;
|
|---|
| 422 | ValueType m_value;
|
|---|
| 423 | };
|
|---|
| 424 |
|
|---|
| 425 | typedef basic_dom_element_iterator<char> dom_element_iterator;
|
|---|
| 426 | typedef basic_dom_element_iterator<wchar_t> wdom_element_iterator;
|
|---|
| 427 |
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | boost::program_options::basic_parsed_options<char> DomElementParser::run()
|
|---|
| 431 | {
|
|---|
| 432 | assert(m_desc);
|
|---|
| 433 | return parseDomElement(m_dom_element, *m_desc, m_allow_unregistered);
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | boost::program_options::basic_parsed_options<char> DomElementParser::parseDomElement(
|
|---|
| 437 | const QDomElement& dom_element,
|
|---|
| 438 | const boost::program_options::options_description& desc,
|
|---|
| 439 | bool allow_unregistered)
|
|---|
| 440 | {
|
|---|
| 441 | // TODO: use XPath paths
|
|---|
| 442 |
|
|---|
| 443 | typedef char charT;
|
|---|
| 444 |
|
|---|
| 445 | using boost::program_options::error;
|
|---|
| 446 | using boost::shared_ptr;
|
|---|
| 447 | using namespace boost::program_options;
|
|---|
| 448 | using ::detail::basic_dom_element_iterator;
|
|---|
| 449 |
|
|---|
| 450 | set<string> allowed_options;
|
|---|
| 451 |
|
|---|
| 452 | const vector<shared_ptr<option_description> >& options = desc.options();
|
|---|
| 453 | for (unsigned i = 0; i < options.size(); ++i) {
|
|---|
| 454 | const option_description& d = *options[i];
|
|---|
| 455 |
|
|---|
| 456 | if (d.long_name().empty()) {
|
|---|
| 457 | boost::throw_exception(
|
|---|
| 458 | error("abbreviated option names are not permitted when parsing DOM elements"));
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | allowed_options.insert(d.long_name());
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | // Parser returns char strings
|
|---|
| 465 | parsed_options result(&desc);
|
|---|
| 466 | copy(basic_dom_element_iterator<charT>(dom_element, allowed_options, allow_unregistered),
|
|---|
| 467 | basic_dom_element_iterator<charT>(),
|
|---|
| 468 | back_inserter(result.options));
|
|---|
| 469 |
|
|---|
| 470 | // Convert char strings into desired type.
|
|---|
| 471 | return basic_parsed_options<charT>(result);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | DomElementParser parseDomElement();
|
|---|
| 475 |
|
|---|
| 476 | /** Creates instance of 'command_line_parser', passes parameters to it,
|
|---|
| 477 | and returns the result of calling the 'run' method.
|
|---|
| 478 | */
|
|---|
| 479 | boost::program_options::basic_parsed_options<char>
|
|---|
| 480 | parseDomElement(QDomElement const& domElement, const boost::program_options::options_description&);
|
|---|
| 481 |
|
|---|
| 482 | boost::program_options::basic_parsed_options<char>
|
|---|
| 483 | parseDomElement(QDomElement const& domElement, const boost::program_options::options_description& desc)
|
|---|
| 484 | {
|
|---|
| 485 | return DomElementParser(domElement)
|
|---|
| 486 | .options(desc)
|
|---|
| 487 | .run();
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | void ComponentBase::parseParameters(XmlComponentConfig const& cfg)
|
|---|
| 491 | {
|
|---|
| 492 | LOG_INFO("Parsing parameters...");
|
|---|
| 493 | LOG_INFO(mOptionsDescription);
|
|---|
| 494 |
|
|---|
| 495 | po::variables_map vm;
|
|---|
| 496 | try {
|
|---|
| 497 | po::store(
|
|---|
| 498 | DomElementParser(cfg.getDomElement())
|
|---|
| 499 | .options(mOptionsDescription)
|
|---|
| 500 | .allow_unregistered() // FIXME: temporary only, at term all the components specify all parameters
|
|---|
| 501 | .run()
|
|---|
| 502 | , vm);
|
|---|
| 503 | po::notify(vm);
|
|---|
| 504 | } catch (po::error& e) {
|
|---|
| 505 | LOG_WARN(e.what());
|
|---|
| 506 | throw PacpusException(e.what());
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | LOG_INFO("Parsed parameter values:\n" << vm);
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
|
|---|
| 513 | {
|
|---|
| 514 | vector<string> xargs;
|
|---|
| 515 | xargs.reserve(attributes.size());
|
|---|
| 516 |
|
|---|
| 517 | for (int i = 0; i < attributes.size(); ++i) {
|
|---|
| 518 | QDomAttr parameter = attributes.item(i).toAttr();
|
|---|
| 519 | if (parameter.isNull()) {
|
|---|
| 520 | LOG_WARN("node is not a parameter");
|
|---|
| 521 | continue;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | QString arg = QString("--") + parameter.name() + "=";
|
|---|
| 525 |
|
|---|
| 526 | bool shouldAddQuotes = parameter.value().contains(' ');
|
|---|
| 527 | if (shouldAddQuotes) {
|
|---|
| 528 | arg += '\"';
|
|---|
| 529 | arg += parameter.value();
|
|---|
| 530 | arg += '\"';
|
|---|
| 531 | } else {
|
|---|
| 532 | arg += parameter.value();
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | LOG_DEBUG("parameter: " << arg);
|
|---|
| 536 | xargs.push_back(arg.toStdString());
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | return xargs;
|
|---|
| 540 | }
|
|---|