Signals2 connect()用于模板

时间:2013-08-22 16:59:27

标签: c++ boost c++11 boost-signals2

我正在尝试为一些重复的函数创建模板类,这些函数是发送不同类型的数据所必需的。但是,我的问题(我认为)实际上是使用 boost :: signals2 :: signal ::的 InterfacePublisher :: addSubscription() 函数。连接()的。

似乎 connect 函数正在获取基类的位置,即使派生类覆盖它们。我相信会有一个解决方法,但现在已经坚持了很长一段时间。

我正在粘贴下面的代码。我们的想法是将 StringPublisher 中的字符串传递给 StringSubscriber ,而无需通过模板对类名进行硬编码:

#include <string>
#include <iostream>

#include <boost/lambda/lambda.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/signals2/signal_base.hpp>
#include <boost/signals2/slot.hpp>
#include <boost/signals2/slot_base.hpp>

template <class T>
class InterfaceSubscriber
{
public:
    InterfaceSubscriber(const std::string& name)
        : mName         (name) {}

    virtual void onData (const std::string&   source, T& data)
    {
        std::cout << "InterfaceSubscriber::onData::BASE SHOULD BE IGNORED\n";
    }
protected:
    const std::string mName;
};




template <class T>
class InterfacePublisher
{
public:
    InterfacePublisher(const std::string& publisherName)
        : mPublisherName         (publisherName)
    {
    }

    void publish(T& data)
    {
        mSignalArgs(mPublisherName, data);
    }

    void addSubscription (InterfaceSubscriber<T>* subsc)
    {
        // The section where I think problem is. There is where the solution should be
        mSignalArgs.connect( std::bind (InterfaceSubscriber<T>::onData , *subsc, std::placeholders::_1, std::placeholders::_2) );
    }

protected:
    boost::signals2::signal<void (const std::string& publisherName, T& data)> mSignalArgs;
    const std::string mPublisherName;
};

class StringSubscriber : public InterfaceSubscriber<std::string>
{
public:
    StringSubscriber (const std::string& subscName) : InterfaceSubscriber(subscName) {}
    void onData (const std::string&   source, std::string&        data) override
    {
        std::cout << mName << ":[" << source << "]Received string of value: " << data << std::endl;
    }
};


class StringPublisher : public InterfacePublisher<std::string>
{
public:
    StringPublisher (const std::string& name) : InterfacePublisher(name) {}
};


int main()
{
    StringSubscriber subscriber1("String_Subscriber_1");
    StringSubscriber subscriber2("String_Subscriber_2");
    StringPublisher publisher("Publisher_Of_String");
    publisher.addSubscription(&subscriber1);
    publisher.addSubscription(&subscriber2);
    std::string str = "Hello World";

    // This should lead to StringSubscriber::onData being called, but instead ends up calling InterfaceSubscriber<T>::onData
    publisher.publish(str);

}

1 个答案:

答案 0 :(得分:0)

在构造StringSubscriber仿函数期间,

std::bind被切片,导致InterfaceSubscriber<T>::onData()在运行时类型为InterfaceSubscriber<T>的对象上执行而不是运行时类型提供给InterfacePublisher<T>::addSubscription()的对象。

void addSubscription(InterfaceSubscriber<T>* subsc)
{
  mSignalArgs.connect(std::bind(&InterfaceSubscriber<T>::onData, 
                                *subsc, ...);
                             // ^~~ sliced
}

要解决此问题,请直接传递指针或传递std::ref对象作为实例。

void addSubscription(InterfaceSubscriber<T>* subsc)
{
  mSignalArgs.connect(std::bind(&InterfaceSubscriber<T>::onData,
                                subsc, ...);
                             // ^~~ pointer
}

void addSubscription(InterfaceSubscriber<T>* subsc)
{
  mSignalArgs.connect(std::bind(&InterfaceSubscriber<T>::onData, 
                                std::ref(*subsc), ...);
                             // ^~~ reference
}