1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <boost/signals2.hpp>
struct Object {int n;};
struct EventArgs {int n;};
void MouseDown(const Object& object, const EventArgs& arg)
{
std::cout << "Global MouseDown: object = " << object.n
<< " arg = " << arg.n << '\n';
}
struct Test
{
void MouseDown(const Object& object, const EventArgs& arg)
{
std::cout << "Member MouseDown: object = " << object.n
<< " arg = " << arg.n << '\n';
}
};
typedef boost::signals2::signal<void(const Object&, const EventArgs&)> KeyEventH
int main() // main does not return void!
{
using std::placeholders::_1;
using std::placeholders::_2;
Object object = {1};
EventArgs arg = {2};
Test test;
KeyEventHandler KeyDown;
KeyDown.connect(MouseDown); // 1
auto c = KeyDown.connect(std::bind(&Test::MouseDown, &test, _1, _2)); // 2
KeyDown.connect(3, std::bind(&Test::MouseDown, &test, _1, _2)); //3
std::cout << "keydown #1\n";
KeyDown(object, arg);
// if you have an EqualityComparable object. you can delete it just so
KeyDown.disconnect(MouseDown); // disconnects #1
std::cout << "keydown #2\n";
KeyDown(object, arg);
// if you connected using a group id, disconnect the group
KeyDown.disconnect(3); // disconnects #3
std::cout << "keydown #3\n";
KeyDown(object, arg);
// if you kept the connection object, disconnect using that
c.disconnect(); // disconnects #2
std::cout << "keydown #4\n";
KeyDown(object, arg);
}
|