Changeset 288 in pacpusframework for trunk/src/PacpusLib


Ignore:
Timestamp:
03/26/14 21:27:30 (10 years ago)
Author:
Marek Kurdej
Message:

Using boost::shared_ptr for storing components.

Location:
trunk/src/PacpusLib
Files:
4 edited

Legend:

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

    r286 r288  
    1717#include <boost/program_options/parsers.hpp>
    1818#include <boost/program_options/variables_map.hpp>
    19 #include <boost/thread/thread.hpp>
     19//#include <boost/thread/thread.hpp>
    2020#include <ostream>
    2121#include <string>
     
    7777DECLARE_STATIC_LOGGER("pacpus.core.ComponentBase");
    7878
    79 ComponentBase::ComponentBase(const QString & componentName)
     79ComponentBase::ComponentBase(QString const& componentName)
    8080    : m_componentName(componentName)
    8181    , m_isActive(false)
     
    131131}
    132132
    133 void ComponentBase::startComponentWithException(boost::exception_ptr& error)
    134 {
    135     try {
    136         startActivity();
    137         error = boost::exception_ptr();
    138     } catch (...) {
    139         error = boost::current_exception();
    140     }
    141 }
    142 
    143 void ComponentBase::startComponentInThread()
    144 {
    145     boost::exception_ptr error;
    146     boost::thread t(
    147         boost::bind(
    148             &ComponentBase::startComponentWithException,
    149             this,
    150             boost::ref(error)
    151         )
    152     );
    153     t.join();
    154     if (error) {
    155         try {
    156             boost::rethrow_exception(error);
    157         } catch (boost::exception& e) {
    158             LOG_FATAL("[" << getName() << "]" << "\tboost::exception thrown: " << boost::diagnostic_information(e));
    159             //throw;
    160         }
    161     }
    162 }
     133//void ComponentBase::startComponentWithException(boost::exception_ptr& error)
     134//{
     135//    try {
     136//        startActivity();
     137//        error = boost::exception_ptr();
     138//    } catch (...) {
     139//        error = boost::current_exception();
     140//    }
     141//}
     142//
     143//void ComponentBase::startComponentInThread()
     144//{
     145//    boost::exception_ptr error;
     146//    boost::thread t(
     147//        boost::bind(
     148//            &ComponentBase::startComponentWithException,
     149//            this,
     150//            boost::ref(error)
     151//        )
     152//    );
     153//    t.join();
     154//    if (error) {
     155//        try {
     156//            boost::rethrow_exception(error);
     157//        } catch (boost::exception& e) {
     158//            LOG_FATAL("[" << getName() << "]" << "\tboost::exception thrown: " << boost::diagnostic_information(e));
     159//            //throw;
     160//        }
     161//    }
     162//}
    163163
    164164int ComponentBase::startComponent()
     
    170170
    171171    setActive(true);
    172     boost::thread worker(&ComponentBase::startComponentInThread, this);
    173     //boost::thread worker(&ComponentBase::startActivity, this);
    174     //startActivity();
     172    //boost::thread worker(&ComponentBase::startComponentInThread, this);
     173    startActivity();
     174    //moveToThread(&mThread);
     175    //mThread.start();
    175176    return true;
    176177}
     
    185186    setActive(false);
    186187    stopActivity();
    187    
     188    //QMetaObject::invokeMethod(&mThread, "quit");
    188189    return true;
    189190}
  • trunk/src/PacpusLib/ComponentFactoryBase.cpp

    r89 r288  
    1010#include <Pacpus/kernel/Log.h>
    1111
    12 #include <cassert>
     12#include <boost/assert.hpp>
    1313#include <QString>
    1414
     
    2323    // get the adress of the ComponentManager instance
    2424    mgr_ = ComponentManager::getInstance();
     25    BOOST_ASSERT(mgr_);
    2526}
    2627
     
    3031}
    3132
    32 void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, const QString& type)
     33void ComponentFactoryBase::addFactory(ComponentFactoryBase* addr, QString const& type)
    3334{
    3435    LOG_DEBUG("addFactory(type="<< type << ")");
    3536
    36     assert(mgr_);
    3737    if (!mgr_->registerComponentFactory(addr, type)) {
    3838        /*
     
    4444}
    4545
    46 void ComponentFactoryBase::addComponent(const QString& name)
     46void ComponentFactoryBase::createComponent(QString const& name)
    4747{
    4848    LOG_DEBUG("addComponent(" << name << ")");
    4949
    50     // FIXME: instantiated component is never deleted!
    51     // who should do it? ComponentManager?
    52     ComponentBase * addr = instantiateComponent(name);
    53     assert(mgr_);
    54     if (!mgr_->registerComponent(addr, name)) {
    55         delete addr;
    56         addr = NULL;
    57     }
     50    ComponentSharedPointer component = instantiateComponent(name);
     51    mgr_->registerComponent(component, name);
    5852}
  • trunk/src/PacpusLib/ComponentManager.cpp

    r277 r288  
    125125}
    126126
    127 bool ComponentManager::registerComponentFactory(ComponentFactoryBase* addr, const QString& type)
     127bool ComponentManager::registerComponentFactory(ComponentFactoryBase* addr, QString const& type)
    128128{
    129129    LOG_TRACE("registerComponentFactory(type="<< type << ")");
     
    141141}
    142142
    143 bool ComponentManager::unregisterComponentFactory(const QString& type)
     143bool ComponentManager::unregisterComponentFactory(QString const& type)
    144144{
    145145    LOG_TRACE("unregisterComponentFactory(type="<< type << ")");
     
    156156}
    157157
    158 bool ComponentManager::registerComponent(ComponentBase* addr, const QString& name)
     158bool ComponentManager::registerComponent(boost::shared_ptr<ComponentBase> addr, QString const& name)
    159159{
    160160    LOG_TRACE("registerComponent(name="<< name << ")");
     
    171171}
    172172
    173 bool ComponentManager::unregisterComponent(const QString& name)
     173bool ComponentManager::unregisterComponent(QString const& name)
    174174{
    175175    LOG_TRACE("unregisterComponent(name="<< name << ")");
     
    180180    }
    181181
    182     // FIXME: delete component
    183     ComponentBase* component = componentMap_.value(name, NULL);
    184     //delete component;
    185 
     182    boost::shared_ptr<ComponentBase> component = componentMap_.value(name, NULL);
     183   
    186184    // FIXME: remove from map (causes segfault in QMap::qMapLessThanKey on Windows)
    187185    //componentMap_.remove(name);
    188186    LOG_INFO("unregistered component '" << name << "'");
    189 
    190     return true;
    191 }
    192 
    193 bool ComponentManager::createComponent(const QString& type, const QString& name)
     187    return true;
     188}
     189
     190bool ComponentManager::createComponent(QString const& type, QString const& name)
    194191{
    195192    LOG_TRACE("createComponent(type=" << type << ", " << "name="<< name << ")");
     
    198195        ComponentFactoryBase* factory = factoryMap_.value(type);
    199196        assert(factory);
    200         factory->addComponent(name);
     197        factory->createComponent(name);
    201198        return true;
    202199    }
     
    209206}
    210207
    211 bool ComponentManager::loadPlugin(const QString& filename)
     208bool ComponentManager::loadPlugin(QString const& filename)
    212209{
    213210    LOG_TRACE("loadPlugin(filename=" << filename << ")");
     
    236233}
    237234
    238 bool ComponentManager::checkComponent(const QString & componentName)
     235bool ComponentManager::checkComponent(QString const& componentName)
    239236{
    240237    if (NULL == getComponent(componentName)) {
     
    245242}
    246243
    247 bool ComponentManager::checkComponentInput(const QString & componentName, const QString & inputName)
     244bool ComponentManager::checkComponentInput(QString const& componentName, QString const& inputName)
    248245{
    249246    if (!checkComponent(componentName)) {
     
    257254}
    258255
    259 bool ComponentManager::checkComponentOutput(const QString & componentName, const QString & outputName)
     256bool ComponentManager::checkComponentOutput(QString const& componentName, QString const& outputName)
    260257{
    261258    if (!checkComponent(componentName)) {
     
    269266}
    270267
    271 bool ComponentManager::createConnection(const QString & outputSignature, const QString & inputSignature, const QString & type, int priority = 0)
     268bool ComponentManager::createConnection(QString const& outputSignature, QString const& inputSignature, QString const& type, int priority = 0)
    272269{
    273270    // FIXME: use 2 signatures (output component + output connection) instead of 1 separated by a (".") dot
     
    289286}
    290287
    291 std::size_t ComponentManager::loadComponents(const QString& configFilename)
     288std::size_t ComponentManager::loadComponents(QString const& configFilename)
    292289{
    293290    LOG_TRACE("loadComponents(filename=" << configFilename << ")");
     
    334331
    335332        // copy locally the config parameters of the component
    336         ComponentBase * component = getComponent(componentName);
     333        ComponentSharedPointer component = getComponent(componentName);
    337334        if (NULL == component) {
    338335            LOG_WARN("component '" << componentName << "' does not exist");
     
    356353        QString componentName = cfg.getComponentName();
    357354
    358         ComponentBase * component = getComponent(componentName);
    359         if (NULL == component) {
     355        ComponentSharedPointer component = getComponent(componentName);
     356        if (!component) {
    360357            LOG_WARN("component '" << componentName << "' does not exist");
    361358            continue;
     
    430427}
    431428
    432 bool ComponentManager::start(const QString & componentName)
     429bool ComponentManager::start(QString const& componentName)
    433430{
    434431    LOG_TRACE("start(component=" << componentName << ")");
    435432
    436     ComponentBase* component = getComponent(componentName);
     433    ComponentSharedPointer component = getComponent(componentName);
    437434    if (!component) {
    438435        LOG_WARN("cannot start component '" << componentName << "'.  It does not exist!");
     
    465462}
    466463
    467 bool ComponentManager::stop(ComponentBase * component) const
     464bool ComponentManager::stop(ComponentSharedPointer component) const
    468465{
    469466    if (!component) {
     
    477474}
    478475
    479 bool ComponentManager::stop(const QString & componentName)
     476bool ComponentManager::stop(QString const& componentName)
    480477{
    481478    LOG_TRACE("stop(component=" << componentName << ")");
    482479
    483     ComponentBase* component = getComponent(componentName);
     480    ComponentSharedPointer component = getComponent(componentName);
    484481    if (!component) {
    485482        LOG_WARN("cannot stop component '" << componentName << "'" << ". It does not exist");
     
    495492}
    496493
    497 ComponentBase * ComponentManager::getComponent(const QString & name)
     494ComponentSharedPointer ComponentManager::getComponent(QString const& name)
    498495{
    499496    LOG_TRACE("getComponent(name=" << name << ")");
  • trunk/src/PacpusLib/XmlComponentConfig.cpp

    r270 r288  
    2222static const char* kPropertyConnectionPriority = "priority";
    2323
    24 XmlComponentConfig::XmlComponentConfig(const QString& name)
     24XmlComponentConfig::XmlComponentConfig(QString const& name)
    2525{
    2626    LOG_TRACE("XmlComponentConfig(QString)");
     
    4343}
    4444
    45 void XmlComponentConfig::addProperty(const QString& name)
     45void XmlComponentConfig::addProperty(QString const& name)
    4646{
    4747    if (hasProperty(name)) {
     
    6262}
    6363
    64 int XmlComponentConfig::delProperty(const QString& name)
     64int XmlComponentConfig::delProperty(QString const& name)
    6565{
    6666    if (!hasProperty(name)) {
     
    8282}
    8383
    84 QString XmlComponentConfig::getProperty(const QString& name, const QString& defaultValue) const
     84QString XmlComponentConfig::getProperty(QString const& name, QString const& defaultValue) const
    8585{
    8686    if (!hasProperty(name))
     
    9797}
    9898
    99 bool XmlComponentConfig::getBoolProperty(const QString& name, bool defaultValue) const
     99bool XmlComponentConfig::getBoolProperty(QString const& name, bool defaultValue) const
    100100{
    101101  return hasProperty(name) ? getProperty(name) == "true" : defaultValue;
    102102}
    103103
    104 int XmlComponentConfig::getIntProperty(const QString& name, int defaultValue) const
     104int XmlComponentConfig::getIntProperty(QString const& name, int defaultValue) const
    105105{
    106106  return hasProperty(name) ? getProperty(name).toInt() : defaultValue;
    107107}
    108108
    109 double XmlComponentConfig::getDoubleProperty(const QString& name, double defaultValue) const
     109double XmlComponentConfig::getDoubleProperty(QString const& name, double defaultValue) const
    110110{
    111111  return hasProperty(name) ? getProperty(name).toDouble() : defaultValue;
    112112}
    113113
    114 void XmlComponentConfig::setProperty(const QString& name, const QString& value)
     114void XmlComponentConfig::setProperty(QString const& name, QString const& value)
    115115{
    116116    component_.setAttribute(name, value);
     
    121121}
    122122
    123 bool XmlComponentConfig::hasProperty(const QString& name) const
     123bool XmlComponentConfig::hasProperty(QString const& name) const
    124124{
    125125    return component_.hasAttribute(name);
Note: See TracChangeset for help on using the changeset viewer.