Repository navigation
TutorialAdvanced
The wigwag signals are highly customizable due to the use of policy-based design.
Signal class has four policies to configure:
You may get more detailed information at the reference page.
Often, you may find a more suitable implementation for some policy. A good example is exception handling. The default policy rethrows all exceptions, so an error in one handler may prevent other from being invoked. It's much better to catch exceptions and log them. print_to_stderr implementation fits you perfectly if the stderr logging is OK for you.
signal<void(), exception_handling::print_to_stderr> s;
token_pool tokens;
tokens += s.connect([] { throw std::runtime_error("test exception"); });
tokens += s.connect([] { throw 42; }); //Sorry :(
s();Output:
std::exception in signal handler: test exception
Unknown exception in signal handler!
Sometimes you may want to make your own policy implementation. E.g. if there is no state associated with a signal it might be (but not necessarily!) better to use a read-write lock threading policy. This allows you to invoke the signal simultaneously from different threads without being blocked.
struct read_write_lock
{
class lock_primitive
{
private:
mutable boost::shared_mutex _mutex;
public:
boost::shared_mutex& get_primitive() const { return _mutex; }
void lock_connect() const { _mutex.lock(); }
void unlock_connect() const { _mutex.unlock(); }
void lock_invoke() const { _mutex.lock_shared(); }
void unlock_invoke() const { _mutex.unlock_shared(); }
};
};
// ...
wigwag::signal<void(), wigwag::exception_handling::default_, read_write_lock> s;
s(); // Use the signalPlease note: You should not use read-write locks if you do have an associated state. In this case you modify the state right before invoking the signal, so the read-only lock is not enough for you.
Usually, you don't really need a variety of signals in your project, so it's convenient to use template aliases for several configurations that you use.
namespace my_project
{
template < typename Signature_>
using signal = wigwag::signal <
Signature_,
wigwag::exception_handling::print_to_stderr
>;
template < typename Signature_>
using ui_signal = wigwag::signal <
Signature_,
wigwag::exception_handling::print_to_stderr,
wigwag::threading::none,
wigwag::state_populating::none,
wigwag::handlers_state_container::default_,
wigwag::life_assurance::none
>;
}Now you have my_project::signal to use in the multithreaded my_project core and myproject::ui_signal that is perfectly suitable for UI due to minimal overhead.
class Resource;
using ResourcePtr = std::shared_ptr<Resource>;
class ResourceRequestSession
{
public:
wigwag::signal<void(const ResourcePtr&)> OnResourceGranted;
// ...
};
class ResourceUser
{
private:
ResourceRequestSession _resourceRequest;
ResourcePtr _resource;
wigwag::token _token;
public:
ResourceUser()
{
// Atomically connecting and obtaining the resource if it has already been granted
_token = _resourceRequest.OnResourceGranted.connect(
std::bind(&ResourceUser::ResourceHandler, this, std::placeholders::_1));
}
~ResourceUser()
{
// But we have a race condition here
_token.reset();
ResourceHandler(nullptr);
}
private:
void ResourceHandler(const ResourcePtr& resource)
{
_resource = resource;
if (_resource)
std::cout << "Resource granted" << std::endl;
else
std::cout << "Resource withdrawed" << std::endl;
}
};Race here
~ResourceUser()
{
_token.reset();
ResourceHandler(nullptr);
}and race there
~ResourceUser()
{
ResourceHandler(nullptr);
_token.reset();
}Withdrawer
~ResourceUser()
{
_token.reset();
}using populator_and_withdrawer = wigwag::state_populating::populator_and_withdrawer;
template < typename Signature_ >
using resource_signal = wigwag::signal<
Signature_,
wigwag::exception_handling::default_,
wigwag::threading::default_,
populator_and_withdrawer
>;
class ResourceRequestSession
{
private:
ResourcePtr _resource;
public:
ResourceRequestSession()
: OnResourceGranted(populator_and_withdrawer(
[](const std::function<void(const ResourcePtr&)>& h) { h(_resource); },
[](const std::function<void(const ResourcePtr&)>& h) { h(nullptr); }
))
{ }
resource_signal<void(const ResourcePtr&)> OnResourceGranted;
// ...
};