Changeset 176 in pacpusframework for trunk/src/PacpusLib/ComponentBase.cpp


Ignore:
Timestamp:
10/11/13 14:10:06 (11 years ago)
Author:
Marek Kurdej
Message:

Added: addParameters() method in ComponentBase using Boost.Program_Options.
Each component can declare its parameters and they will be read automatically before configureComponent() method.
See example ProducerConsumerExample constructors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PacpusLib/ComponentBase.cpp

    r152 r176  
    99#include <Pacpus/kernel/Log.h>
    1010
     11#include <boost/program_options/parsers.hpp>
     12#include <boost/program_options/variables_map.hpp>
     13#include <string>
     14#include <vector>
     15
     16namespace po = boost::program_options;
    1117using namespace pacpus;
     18using namespace std;
     19
     20std::vector<std::string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes);
     21void logVariablesMap(boost::program_options::variables_map vm);
    1222
    1323DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
     
    2030  , m_ui(NULL)
    2131  , m_componentState(NOT_MONITORED)
     32  , mOptionsDescription("Component parameters")
    2233{
    2334    LOG_TRACE("constructor");
    2435    // Get a pointer on the instance of ComponentManager.
    2536    m_manager = ComponentManager::getInstance();
    26     LOG_INFO("component " << name() << " was created");
     37    LOG_INFO("component " << getName() << " was created");
     38
     39    addParameters()
     40        ("name", po::value<string>(&mName)->required(), "component name")
     41        ("type", po::value<string>(&mTypeName)->required(), "component type")
     42        ("ui", po::value<bool>(&mHasGui)->default_value(false), "whether to show GUI")
     43        ("verbose", po::value<bool>(&mVerbose)->default_value(false), "set output verbose")
     44        ("verbosity-level", po::value<int>(&mVerbosityLevel)->default_value(0), "set verbosity level")
     45        ("recording", po::value<bool>(&m_isRecording)->default_value(false), "whether to record data")
     46    ;
    2747}
    2848
     
    113133}
    114134
    115 QString ComponentBase::name() const
    116 {
    117     return m_componentName;
    118 }
    119 
    120135QString ComponentBase::getName() const
    121136{
     
    148163        return inputs()[inputName];
    149164    }
    150     LOG_WARN("Component " << name() << " does not contain input " << inputName);
     165    LOG_WARN("Component " << getName() << " does not contain input " << inputName);
    151166    return NULL;
    152167}
     
    161176        return outputs()[outputName];
    162177    }
    163     LOG_WARN("Component " << name() << " does not containt output " << outputName);
     178    LOG_WARN("Component " << getName() << " does not containt output " << outputName);
    164179    return NULL;
    165180}
     181
     182po::options_description_easy_init ComponentBase::addParameters()
     183{
     184    return mOptionsDescription.add_options();
     185}
     186
     187void ComponentBase::parseParameters(const XmlComponentConfig & cfg)
     188{
     189    LOG_INFO("Parsing parameters...");
     190    LOG_INFO(mOptionsDescription);
     191   
     192    vector<string> xargs = convertAttributesToArgumentVector(cfg.getProperties());
     193   
     194    boost::program_options::variables_map vm;
     195    try {
     196        po::store(
     197            po::command_line_parser(xargs)
     198                .options(mOptionsDescription)
     199                //.allow_unregistered()
     200                .run()
     201        , vm);
     202        po::notify(vm);
     203    } catch (po::error & e) {
     204        LOG_ERROR(e.what());
     205        throw;
     206    }
     207
     208    logVariablesMap(vm);
     209}
     210
     211void logVariablesMap(boost::program_options::variables_map vm)
     212{
     213    for (po::variables_map::iterator i = vm.begin(); i != vm.end(); ++i) {
     214        const po::variable_value& v = i->second;
     215        if (v.empty()) {
     216            continue;
     217        }
     218        const type_info & type = v.value().type();
     219        if (type == typeid(string)) {
     220            const string & val = v.as<string>();
     221            LOG_INFO(i->first << "=" << val);
     222        } else if (type == typeid(int)) {
     223            int val = v.as<int>();
     224            LOG_INFO(i->first << "=" << val);
     225        } else if (type == typeid(bool)) {
     226            int val = v.as<bool>();
     227            LOG_INFO(i->first << "=" << val);
     228        }
     229    }
     230}
     231
     232vector<string> convertAttributesToArgumentVector(const QDomNamedNodeMap & attributes)
     233{
     234    vector<string> xargs;
     235    xargs.reserve(attributes.size());
     236
     237    for (int i = 0; i < attributes.size(); ++i) {
     238        QDomAttr parameter = attributes.item(i).toAttr();
     239        if (parameter.isNull()) {
     240            LOG_WARN("node is not a parameter");
     241            continue;
     242        }
     243       
     244        QString arg = QString("--") + parameter.name() + "=";
     245       
     246        bool shouldAddQuotes = parameter.value().contains(' ');
     247        if (shouldAddQuotes) {
     248            arg += '\"';
     249            arg += parameter.value();
     250            arg += '\"';
     251        } else {
     252            arg += parameter.value();
     253        }
     254       
     255        LOG_DEBUG("parameter: " << arg);
     256        xargs.push_back(arg.toStdString());
     257    }
     258
     259    return xargs;
     260}
Note: See TracChangeset for help on using the changeset viewer.