Reputation: 3554
I am using a pattern like this, C++11:
class FooViewController {
void build() {
auto label = ...
network->doWork([] (const Result& r) {
label->setText(r.text);
});
}
}
FooViewController may deconstruct before doWork
completes, causing crashes. Looking at boost::signals2, I'm thinking of using boost::signals2::trackable
, which works great for my single threaded purposes, with the benefit that I do not have to hold and manage my connections directly, however I'm not sure how to get such a solution working with lambdas.
Here is a working lambda free version:
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
Two goals:
1-- I'd like to do something like:
signal.connect(boost::bind([] {
printf("Fired!");
}, &test));
Which would NOT call the lambda after test
is torn down.
2-- I do not want to directly manage the connection objects returned by .connect
.
Upvotes: 2
Views: 1254
Reputation: 472
As can be seen here: "Use of the trackable class is not recommended for new code"
Perhaps choose to go with scoped_connection
or track
instead.
Example:
#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
Upvotes: 2
Reputation: 3554
Found the answer referencing 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();
}
Output
Fire!
I'm dying...!
...and a multiple params example (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);
Output
Fire 1!
I'm dying...!
Upvotes: 1
Reputation: 393934
You can code something similar using shared/weak pointers:
#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();
}
Prints
Fire!
expired
The strictly c++11 version: Live On Coliru
Upvotes: 0