使用boost :: signals2 :: trackable with lambdas

时间:2015-08-17 22:10:23

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

我正在使用这样的模式,C ++ 11:

class FooViewController {
    void build() {
        auto label = ...

        network->doWork([] (const Result& r) {
             label->setText(r.text);
        });
    }
}

FooViewController可能会在doWork完成之前解构,从而导致崩溃。看看boost :: signals2,我正在考虑使用boost::signals2::trackable,这对于我的单线程用途非常有用,其好处是我不必直接保存和管理我的连接,但是我不确定如何使用lambdas获得这样的解决方案。

这是一个有效的lambda免费版本:

class Foo : public boost::signals2::trackable {
public:
    void bar() {
        printf("Fire!");
    }
};


Usage:

    boost::signals2::signal<void()> signal;
    {
        Foo test;
        signal.connect(boost::bind(&Foo::bar, &test));
        signal();
    }
    signal();

Output:

    Fired!
    // Note a second 'Fired!' did not occur, which is correct behavior

两个目标:

1--我想做类似的事情:

signal.connect(boost::bind([] {
    printf("Fired!");
}, &test));

test被拆除后不会调用lambda。

2--我不想直接管理.connect返回的连接对象。

3 个答案:

答案 0 :(得分:1)

可以看到here&#34;不建议在新代码中使用可跟踪类&#34;

或许选择使用scoped_connectiontrack代替。

示例:

#include <iostream>
#include <memory>

#include <boost/signals2.hpp>


struct short_lived : public boost::signals2::scoped_connection {
public:
   short_lived(const boost::signals2::connection &conn) : boost::signals2::scoped_connection{conn}
   {   }
   ~short_lived() {
      std::cout << "I'm dying...1!" << std::endl;
   }

};

int main() {
   typedef boost::signals2::signal<void()> sig_type;
   sig_type s1;

   {
      /* boost::signals2::scoped_connection */ short_lived conn{s1.connect([]() {
                  std::cout << "Fire1!" << std::endl;
               })};
      s1();
   }
   s1();
   std::cout << std::endl;

   {
      auto lam = []() {
         std::cout << "Fire2!" << std::endl;
      };

      /* shared_ptr with custom deleter that does not delete (since we didn't use new),
         but prints a message */
      std::shared_ptr<decltype(lam)> sptr{&lam, [](decltype(lam) *) { std::cout << "I'm dying...2!" << std::endl; }};
      s1.connect(sig_type::slot_type(lam).track_foreign(sptr));
      s1();
   }
   s1();

   return 0;
}

http://melpon.org/wandbox/permlink/c8LHGIp8ArkKsnWA

答案 1 :(得分:0)

您可以使用共享/弱指针编写类似的代码:

<强> Live On Coliru

#include <boost/signals2.hpp>
#include <boost/make_shared.hpp>
#include <boost/weak_ptr.hpp>
#include <iostream>

class Foo : public boost::signals2::trackable {
public:
    void bar() {
        printf("Fire!\n");
    }
};

int main() {
    boost::signals2::signal<void()> signal;
    {
        auto test = boost::make_shared<Foo>();

        signal.connect([wp = boost::weak_ptr<Foo>(test)] 
                { 
                    if (auto sp = wp.lock()) 
                        sp->bar(); 
                    else
                        std::cout << "expired\n";
                }
            );

        signal();
    }
    signal();
}

打印

Fire!
expired

严格的c ++ 11版本: Live On Coliru

答案 2 :(得分:0)

找到引用trackable_test.cpp的答案:

struct short_lived : public boost::signals2::trackable {
    ~short_lived() {
        cout << "I'm dying...!" << std::endl;
    }
};

void main() {
    typedef boost::signals2::signal<void()> sig_type;
    sig_type s1;

    short_lived* shorty = new short_lived();
    s1.connect(boost::bind<void>([](const short_lived*) {
        cout << "Fire!" << std::endl;
    }, shorty));
    s1();
    delete shorty;

    s1();
}

输出

Fire!
I'm dying...!

...和一个多参数示例(boost :: bind refresher):

typedef boost::signals2::signal<void(int)> sig_type;

// ...same...

s1.connect(boost::bind<void>([](const short_lived*, int cannon) {
    cout << "Fire " << cannon << "!" << std::endl;
}, shorty, _1));
s(1);
delete shorty;
s(2);

输出

Fire 1!
I'm dying...!