neosigslot

neosigslot is a lightweight C++ "signals and slots" library. The latest version of neosigslot is actually a thin deprecated wrapper around a new event system that is designed for the cross platform C++ GUI library neoGFX.  This new event system is documented here and has the following features:

You can download the implementation here (v3.0) (changelog).

The following example demonstrates re-entrancy and an event handler being destroyed whilst it is being notified of a signal and also the use of local 'sink' objects for controlled event handler de-registration.  The example uses the new event system API:

#include <neolib/neolib.hpp>
#include <memory>
#include <iostream>
#include <string>
#include <neolib/event.hpp>

class greetings
{
public:
	neolib::event<const std::string&> hello_and_goodbye;
public:
	greetings()
	{
		iSink += hello_and_goodbye([this](const std::string& greeting) { handle_hello_and_goodbye(greeting); });
	}
	~greetings()
	{
		std::cout << "Destroying greetings object..." << std::endl;
	}
public:
	void handle_hello_and_goodbye(const std::string& greeting)
	{
		static int phase;
		std::cout << "In handler, phase = " << phase << ", argument = '" << greeting << "'" << std::endl;
		switch (phase)
		{
		case 0:
			++phase;
			hello_and_goodbye.trigger(", world!");
			break;
		case 1:
			delete this;
			break;
		}
	}
private:
	neolib::sink iSink; // performs the traditional role of slot handler de-registration
};

class counter
{
public:
	neolib::event<int> new_integer;
public:
	void count(int n)
	{
		for (int i = 1; i <= n; ++i)
			new_integer.trigger(i);
	}
};

int main()
{
	neolib::async_task mainTask;
neolib::async_thread mainThread{ mainTask, "neolib::event unit test(s)", true }; { // we shall use 'new' instead of smart pointer maker as greetings object will delete itself in event handler... auto object = new greetings{}; object->hello_and_goodbye.trigger("Hello"); } { counter c; { neolib::sink localSink; localSink += c.new_integer([](int n) { std::cout << n << std::endl; }); c.count(10); } c.count(10); // shouldn't print anything as 'localSink' has been destroyed deregistering the handler } }

The output of which is:

In handler, phase = 0, argument = 'Hello'
In handler, phase = 1, argument = ', world!'
Destroying greetings object...
1
2
3
4
5
6
7
8
9
10

You can e-mail comments to the author.

Back to project page