Skip to content

listenable

Dmitry Koplyarov edited this page Apr 9, 2017 · 7 revisions

Back to reference

An alternative notification mechanism. The important difference between signal and listenable is that listenable does not support the asynchronous handlers. As for the rest, which one to use depends on your personal preferences.
Here is the tutorial section.

Defined in wigwag/listenable.hpp

template < typename ListenerType_, typename... Policies_ >
class listenable
{
public:
	using listener_type = ListenerType_;

	template < typename... Args_ >
	listenable(Args_&&... args);

	listenable(const listenable&) = delete;
	listenable& operator = (const listenable&) = delete;

	auto lock_primitive() const -> decltype(_impl->get_lock_primitive().get_primitive());

	token connect(ListenerType_ listener, handler_attributes attributes = handler_attributes::none) const;

	template < typename InvokeListenerFunc_ >
	void invoke(const InvokeListenerFunc_& invoke_listener_func);
};

Table of contents

Template arguments

ListenerType_

The listener type. :)

Policies_...

The custom policies list. You can read more in this tutorial section.

Supported policies:

Nested types

listener_type

using listener_type = ListenerType_;

The listener type. The same type as the ListenerType_ template argument.

Constructors

variadic arguments constructor

template < typename... Args_ >
listenable(Args_&&... args);

Forwards the arguments to the policies' internals You can read more in the tutorial.

Arguments:

  • args... - forwarded to the policies' internals

copy constructor

listenable(const listenable&) = delete;

The copy constructor is prohibited

Methods

connect

token connect(ListenerType_ listener, handler_attributes attributes = handler_attributes::none) const;

Connects a listener to a listenable.

Thread safety: This is a thread-safe method for the default listenable version. If you use custom policies, it is thread-safe as long as the threading policy assures that.

Arguments:

  • listener - a listener_type object to be connected to a listenable
  • attributes - handler_attributes enum flags

Return value: a RAII token object that manages the connection to a listenable.

lock_primitive

auto lock_primitive() const -> decltype(_storage.get_lock_primitive().get_primitive());

Return value: a reference to the lock primitive (std::recursive_mutex for the default listenable version). The actual type is the same as that of threading policy's lock_primitive::get_primitive() method.

invoke

template < typename InvokeListenerFunc_ >
void invoke(const InvokeListenerFunc_& invoke_listener_func);

Invokes the listenable.
Please note that in general case you should lock the listenable's lock primitive in the code that calls this method. You can get more information here.

Thread safety: This is a thread-safe method for the default listenable version. If you use custom policies, it MAY OR MAY NOT BE a thread-safe method even with a thread-safe threading policy (e.g. with all policies that use a non-recursive mutex)!

Arguments:

  • invoke_listener_func - a function object of a signature void(const listener_type&) that is invoked for each connected listener

Operators

operator =

signal& operator = (const signal&) = delete;

The assignment operator is prohibited

Clone this wiki locally