libzypp 17.37.17
|
#include <sigc++/trackable.h>
#include <sigc++/signal.h>
#include <sigc++/connection.h>
#include <sigc++/visit_each.h>
#include <sigc++/adaptors/adaptors.h>
#include <memory>
#include <utility>
#include <cassert>
#include <zypp-core/base/Exception.h>
#include <zypp-core/base/LogControl.h>
Go to the source code of this file.
Classes | |
class | zyppng::Signal< R(T...)> |
class | zyppng::MemSignal< SignalHost, ReturnType(Arguments...)> |
struct | zyppng::internal::lock_shared< T_functor, Lockers > |
class | zyppng::SignalProxy< R(T...)> |
Hides the signals emit function from external code. More... | |
struct | sigc::visitor< zyppng::internal::lock_shared< T_functor, Lockers... > > |
Namespaces | |
namespace | zyppng |
namespace | zyppng::internal |
namespace | sigc |
Typedefs | |
using | zyppng::connection = sigc::connection |
using | zyppng::trackable = sigc::trackable |
template<class R, class... T> | |
using | zyppng::SignalProxyBase = sigc::signal<R(T...)> |
Functions | |
template<typename T> | |
auto | zyppng::internal::lock_shared_makeLock (const T &locker) |
template<typename Functor, typename ... Obj> | |
decltype(auto) | zyppng::internal::locking_fun (const Functor &f, const Obj &... o) |
This file implements the signal and slot concept in libzypp.
Signals and slots are basically a implementation of the observer pattern which makes it possible for objects to work together without them having to know about each other by connecting the signals of one object to the slots of another. Whenever the object owning the signal emits said signal, all connected slots are executed. This is especially helpful in async environments where we need to react on specific events like timers or filedescriptor events that can happen at any time during execution.
Using signals and slots in libzypp requires the developer to make sure that all objects that are used in the signal chain can not be deleted during emission, for example a Socket emitting the closed() signal might result in its deletion. This is usually made sure by taking a reference to the sender object via shared_ptr, e.g. the EventLoop does this for all AbstractEventSource 's that are registered to receive events using shared_from_this.
To have signals in an Object, it is recommended to subclass the Type from zyppng::Base and always have the objects in a std::shared_ptr. zyppng::Base provides the zyppng::Base::connect and zyppng::Base::connectFunc helpers to connect signals and slots. Connections made with those helpers will make sure the receiving objects are properly locked.
The zyppng::Base::connect helper function can be used to connect a signal that is hosted by a zyppng::Base derived type to a slot hosted by a zyppng::Base derived type. No special care needs to be taken to make sure the receiver object is locked since the function is handling all the details about that:
The zyppng::Base::connect helper function can be used to connect a signal that is hosted by a zyppng::Base derived type to a lambda type slot, commonly used when the developer wants to have the code that is invoked by the signal right where the slot is connected, to make the code easier to read or when the slot code is not reused anywhere else.
In this example the receiver object uses a slot to invoke the actual function that handles the event. Since we only connect a lambda that invokes the writeOnTimeout function using a caputured this pointer we need to manually take care of locking the receiver object during the slot invocation by passing it as a extra argument after the lambda. While the code would compile without doing that we introduce a bug that is very hard to track down because the subsequent signal emitted from the receiver object causes it to be deleted, making the code fail afterwards.
Definition in file signals.h.