0

我在我们的一个项目中使用 Boost Signals2。在此我想要自动连接管理,为此我正在测试 Boost Signals2 跟踪,但我没有调用插槽。在我运行以下代码后,不会调用插槽。环境:VS 2010,windows 7,boost 1.54

    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <boost/bind.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/signals2/signal.hpp>
    #include <boost/shared_ptr.hpp>


    typedef boost::signals2::signal<void ()>  signal_test;
    using namespace boost;

    class SubjectTest
    {

    public:
        void Connect(const signal_test::slot_type &subscriber)
        {
            m_Signal.connect(subscriber);
            std::cout << "No of connections : " << m_Signal.num_slots() << std::endl;
        }
        void Notify()
        {
            m_Signal();
        }
    private:
        signal_test m_Signal;
    };

    class Listner 
    {
    public:
        Listner(){}
        ~Listner(){}
        Listner(std::string name)
        :m_name(name)
        {
        }
        void GotSignal()
        {
            std::cout << m_name <<  std::endl;
        }
        void ConnectWithTracking(SubjectTest *s)
        {
            shared_ptr<Listner> l = new Listner();
            s->Connect(signal_test::slot_type(&Listner::GotSignal,l.get()).track(l));

        }
        void ConnectNormal(SubjectTest *s)
        {
            s->Connect(bind(&Listner::GotSignal,this));

        }

    private:
        std::string m_name;
        shared_ptr<Listner> l;
    };

    void main()
    {
        Listner l2("First"); 
        SubjectTest sub;

        try
        {
        l2.ConnectWithTracking(&sub);
        //l2.ConnectNormal(&sub);
        sub.Notify();

        {
            Listner l3("Second"); 

            l3.ConnectWithTracking(&sub);
            //l3.ConnectNormal(&sub);
            sub.Notify();
        }

        sub.Notify();
        }
        catch(std::exception ex)
        {
            std::cout << ex.what() << std::endl;
        }

        std::cout << "Finish" <<std::endl;
    }

更新: *现在工作*

    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <boost/bind.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/signals2/signal.hpp>
    #include <boost/shared_ptr.hpp>


    typedef boost::signals2::signal<void ()>  signal_test;
    using namespace boost;

    class SubjectTest
    {

    public:
        void Connect(const signal_test::slot_type &subscriber)
        {
            m_Signal.connect(subscriber);
            std::cout << "No of connections : " << m_Signal.num_slots() << std::endl;
        }
        void Notify()
        {
            m_Signal();
        }
    private:
        signal_test m_Signal;
    };

    class Listner : public boost::enable_shared_from_this<Listner>
    {
    public:
        Listner(){}
        ~Listner(){}
        Listner(std::string name)
        :m_name(name)
        {
        }
        void GotSignal()
        {
            std::cout << m_name <<  std::endl;
        }
        void ConnectWithTracking(SubjectTest *s)
        {
            s->Connect(signal_test::slot_type(&Listner::GotSignal,shared_from_this().get()).track(shared_from_this()));
        }
        void ConnectNormal(SubjectTest *s)
        {
            s->Connect(bind(&Listner::GotSignal,this));
        }

    private:
        std::string m_name;

    };

    void main()
    {
        shared_ptr<Listner> l2(new Listner("First")); 

        SubjectTest sub;

        try
        {
        l2->ConnectWithTracking(&sub);

        sub.Notify();

        {
            shared_ptr<Listner> l3(new Listner("Second")); 

            l3->ConnectWithTracking(&sub);
            //l3.ConnectNormal(&sub);
            sub.Notify();
        }

        sub.Notify();
        }
        catch(std::exception ex)
        {
            std::cout << ex.what() << std::endl;
        }

        std::cout << "Finish" <<std::endl;
    }

现在这是 Signal2 自动连接管理的完整示例

4

1 回答 1

0
{
  shared_ptr<Listner> l(new Listner());
  s->Connect(signal_test::slot_type(&Listner::GotSignal,l.get()).track(l));
}

在上面的几行中,lpointee 在关闭时被销毁}- 这意味着您刚刚连接的插槽已过期。

跟踪的全部目的是传递给track()一个shared_ptr(或weak_ptr),它与插槽本身绑定(或紧密相关)。传递shared_ptr与插槽本身的寿命无关的寿命是没有意义的。

于 2014-02-27T07:22:23.873 回答