Skip to content

TutorialBasic

Dmitry Koplyarov edited this page Feb 10, 2016 · 4 revisions

Back to tutorial

Table of contents

Hello, World!

void HelloWorld()
{
	std::cout << "Hello, World!" << std::endl;
}

// ...

// A signal with no arguments
wigwag::signal<void()> s;

// Connecting to the signal
wigwag::token t = s.connect(&HelloWorld);

// Invoking the signal
s();

Output:

Hello, World!

There are two essential classes in wigwag: signal and token. The first is apparently the signal itself, and the second represents a connection between a handler and a signal. The handler remains connected to a signal while the token object is alive.

Signal arguments

void PrintSum(int x, int y)
{
	std::cout << "The sum is " << x + y << std::endl;
}

void PrintDifference(int x, int y)
{
	std::cout << "The difference is " << x - y << std::endl;
}

// ...

wigwag::signal<void(int, int)> s;
wigwag::token t1 = s.connect(&PrintSum);
wigwag::token t2 = s.connect(&PrintDifference);
s(5, 2);

Output:

The sum is 7
The difference is 2

Please note: there is no certain order in which the handlers are invoked! You should not use any assumptions in your code.

token_pool

wigwag::signal<void()> s;
token_pool p;

p += s.connect([] { std::cout << "handler 1" << std::endl; });
p += s.connect([] { std::cout << "handler 2" << std::endl; });
s();

std::cout << "releasing the tokens";
p.release();

p.add_token(s.connect([] { std::cout << "handler 3" << std::endl; }));
s();

Output:

handler 1
handler 2
releasing the tokens
handler 3

The token_pool object stores a set of tokens, that are released by the token_pool::release() method or by its destructor.
It is useful if you connect an object to a bunch of signals and do not need to manage the connections separately.

Clone this wiki locally