Skip to content

Instantly share code, notes, and snippets.

@nekko1119
Last active January 1, 2016 10:29
Show Gist options
  • Save nekko1119/8132124 to your computer and use it in GitHub Desktop.
Save nekko1119/8132124 to your computer and use it in GitHub Desktop.
boost::signal2でshared_ptrを用いたトラッキングの方法
#include <iostream>
void hello()
{
std::cout << "Hello ";
}
struct World
{
void operator()()
{
std::cout << "World";
}
};
struct CoutChar
{
CoutChar(char c)
: letter(c)
{}
void print()
{
std::cout << letter;
}
char letter;
};
#include <boost/signals2.hpp>
int main()
{
World world;
CoutChar c('!');
using boost::signals2::signal;
signal<void()> s;
s.connect(&hello);
s.connect(world);
s.connect(std::bind(&CoutChar::print, c));
s();
std::cout << std::endl;
s.disconnect_all_slots();
s.connect(1, world);
s.connect(0, &hello);
s.connect(2, std::bind(&CoutChar::print, c));
s();
std::cout << std::endl;
s.disconnect_all_slots();
s.connect(1, world);
s.connect(0, &hello);
{
std::shared_ptr<CoutChar> c(new CoutChar('!'));
s.connect(2, signal<void()>::slot_type(&CoutChar::print, c.get()).track_foreign(c));
std::cout << s.num_slots();
std::cout << std::endl;
}
s();
std::cout << std::endl;
std::cout << s.num_slots();
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment