libzypp: zyppng::EventDispatcher Class Reference

libzypp 17.38.6

zyppng::EventDispatcher Class Reference

`#include <zypp-core/ng/base/eventdispatcher.h>`

Inheritance diagram for zyppng::EventDispatcher:

## Public Types
using | Ptr = EventDispatcherRef
using | WeakPtr = EventDispatcherWeakRef
using | IdleFunction = std::function<bool ()>
using | TimeoutFunction = std::function<bool ()>
using | WaitPidCallback = std::function<void(int, int)>
Public Types inherited from zyppng::Base
using | Ptr = std::shared_ptr<Base>
using | WeakPtr = std::weak_ptr<Base>

## Public Member Functions
| ~EventDispatcher () override
virtual bool | run_once ()
void | clearUnrefLaterList ()
ulong | runningTimers () const
void * | nativeDispatcherHandle () const
| Returns the native dispatcher handle if the used implementation supports it.
void | trackChildProcess (int pid, std::function< void(int, int)> callback)
bool | untrackChildProcess (int pid)
UnixSignalSourceRef | unixSignalSource ()
GMainContext * | glibContext ()
Public Member Functions inherited from zyppng::Base
| Base ()
virtual | ~Base ()
WeakPtr | parent () const
void | addChild (const Base::Ptr &child)
void | removeChild (const Ptr &child)
const std::unordered_set< Ptr > & | children () const
std::thread::id | threadId () const
template<typename T>
std::vector< std::weak_ptr< T > > | findChildren () const
template<typename T>
std::shared_ptr< T > | shared_this () const
template<typename T>
std::shared_ptr< T > | shared_this ()
template<typename T>
std::weak_ptr< T > | weak_this () const
template<typename T>
std::weak_ptr< T > | weak_this ()
template<typename SenderFunc, typename ReceiverFunc>
auto | connect (SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
template<typename SenderFunc, typename ReceiverFunc, typename ... Tracker>
std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > | connectFunc (SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
void | setData (uint32_t quark, zypp::AutoDispose< void * > data)
void * | data (uint32_t quark)
void | clearData (uint32_t quark)

## Static Public Member Functions
static void | invokeOnIdle (IdleFunction &&callback)
| Convenience function to schedule a callback to be called later.
template<typename T = TimeoutFunction>
static void | invokeAfter (T &&callback, uint32_t timeout)
| Convenience function to schedule a callback to be called later.
template<typename T>
static void | unrefLater (T &&ptr)
static std::shared_ptr< EventDispatcher > | instance ()
static void | setThreadDispatcher (const std::shared_ptr< EventDispatcher > &disp)
static bool | waitForFdEvent (const int fd, int events, int &revents, int &timeout)
Static Public Member Functions inherited from zyppng::Base
template<typename Obj, typename Functor>
static decltype(auto) | make_base_slot (Obj *o, Functor &&f)
template<typename SenderFunc, typename ReceiverFunc>
static auto | connect (typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
template<typename SenderFunc, typename ReceiverFunc, typename ... Tracker>
static auto | connectFunc (typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)

## Protected Member Functions
| EventDispatcher (void *ctx=nullptr)
void | unrefLaterImpl (std::shared_ptr< void > &&ptr)
void | invokeOnIdleImpl (IdleFunction &&callback)
void | invokeAfterImpl (TimeoutFunction &&callback, uint32_t timeout)
virtual void | updateEventSource (AbstractEventSource &notifier, int fd, int mode)
virtual void | removeEventSource (AbstractEventSource &notifier, int fd=-1)
virtual void | registerTimer (Timer &timer)
virtual void | removeTimer (Timer &timer)
Protected Member Functions inherited from zyppng::Base
| Base (BasePrivate &dd)

## Friends
class | AbstractEventSource
class | Timer

## Additional Inherited Members
Protected Attributes inherited from zyppng::Base
std::unique_ptr< BasePrivate > | d_ptr

## Detailed Description

The EventDispatcher class implements the libzypp event loop native backend.

Libzypp is using a thread local dispatcher, which means each thread has its own unique disptacher. The only special case is when we need to work together with an already exisiting event loop, for example in a Qt application. The default implementation however uses the glib eventloop, just like Qt and GTK, so integrating libzypp here is just a matter of passing the default main context to the constructor of EventDispatcher.

Definition at line 42 of file eventdispatcher.h.

## Member Typedef Documentation

## ◆ Ptr

using zyppng::EventDispatcher::Ptr = EventDispatcherRef

Definition at line 50 of file eventdispatcher.h.

## ◆ WeakPtr

using zyppng::EventDispatcher::WeakPtr = EventDispatcherWeakRef

Definition at line 51 of file eventdispatcher.h.

## ◆ IdleFunction

using zyppng::EventDispatcher::IdleFunction = std::function<bool ()>

Definition at line 52 of file eventdispatcher.h.

## ◆ TimeoutFunction

using zyppng::EventDispatcher::TimeoutFunction = std::function<bool ()>

Definition at line 53 of file eventdispatcher.h.

## ◆ WaitPidCallback

using zyppng::EventDispatcher::WaitPidCallback = std::function<void(int, int)>

Definition at line 148 of file eventdispatcher.h.

## Constructor & Destructor Documentation

## ◆ ~EventDispatcher()

zyppng::EventDispatcher::~EventDispatcher | ( |  | ) |
| override

Definition at line 391 of file eventdispatcher_glib.cc.

## ◆ EventDispatcher()

zyppng::EventDispatcher::EventDispatcher | ( | void * | ctx = nullptr | ) |
| protected

Create a new instance of the EventDispatcher, if ctx is given it is used as the new context for the eventloop

Definition at line 386 of file eventdispatcher_glib.cc.

## Member Function Documentation

## ◆ run_once()

bool zyppng::EventDispatcher::run_once | ( |  | ) |
| virtual

Enters the eventloop once and dequeues all pending events, once the event queue is empty the function returns

Definition at line 589 of file eventdispatcher_glib.cc.

## ◆ invokeOnIdle()

void zyppng::EventDispatcher::invokeOnIdle | ( | IdleFunction && | callback | ) |
| inlinestatic

Convenience function to schedule a callback to be called later.

Parameters

  callback | a std::function that is called after all other events have been processed

Note
  these can be starved if there are constantly events in the event loop. ONLY use this if it is fine that they might not be called until all other things have been processed.

Definition at line 71 of file eventdispatcher.h.

## ◆ invokeAfter()

template<typename T = TimeoutFunction>

void zyppng::EventDispatcher::invokeAfter | ( | T && | callback,
|  | uint32_t | timeout )
| inlinestatic

Convenience function to schedule a callback to be called later.

Parameters

  callback | a std::function that is called after all other events have been processed

Definition at line 83 of file eventdispatcher.h.

## ◆ unrefLater()

template<typename T>

void zyppng::EventDispatcher::unrefLater | ( | T && | ptr | ) |
| inlinestatic

Schedules a

See also
  std::shared_ptr to be unreferenced in the next idle phase of the event loop.

In some cases it might be required to delay the cleanup of a ressource until the current event loop iteration was finished, in case there are more pending events for the ressource.

Note
  Normally this should be handled by using correct shared_ptr semantics, and always owning a reference to a object were functions are called on. Only if that is not possible unrefLater should be used.

Definition at line 102 of file eventdispatcher.h.

## ◆ clearUnrefLaterList()

void zyppng::EventDispatcher::clearUnrefLaterList | ( |  | ) |

Immediately clears the list of all shared_ptr's that were registered to be unreferenced later. Mainly used to be called when a event loop exits, otherwise we might have weird side effects when the main loop instance preserves pointers that should be cleared at that point already. Like child items still trying to access the parent object that was already cleaned up before the MainLoop instance.

Definition at line 619 of file eventdispatcher_glib.cc.

## ◆ runningTimers()

ulong zyppng::EventDispatcher::runningTimers | ( |  | ) | const

Returns the number of the currently active timers

Definition at line 624 of file eventdispatcher_glib.cc.

## ◆ instance()

std::shared_ptr< EventDispatcher > zyppng::EventDispatcher::instance | ( |  | ) |
| static

Returns the EventDispatcher instance for the current thread.

Definition at line 629 of file eventdispatcher_glib.cc.

## ◆ setThreadDispatcher()

void zyppng::EventDispatcher::setThreadDispatcher | ( | const std::shared_ptr< EventDispatcher > & | disp | ) |
| static

Registers the given event dispatcher as the default for the current thread. The reference count for the shared pointer will not be increased, so the application is responsible to keep it until the application exits.

Note
  This must be called before the default dispatcher is registered, otherwise its ignored.

Definition at line 634 of file eventdispatcher_glib.cc.

## ◆ nativeDispatcherHandle()

void * zyppng::EventDispatcher::nativeDispatcherHandle | ( |  | ) | const

Returns the native dispatcher handle if the used implementation supports it.

Note
  the glib backend will return the used glib GMainContext

Definition at line 506 of file eventdispatcher_glib.cc.

## ◆ waitForFdEvent()

bool zyppng::EventDispatcher::waitForFdEvent | ( | const int | fd,
|  | int | events,
|  | int & | revents,
|  | int & | timeout )
| static

Waits until one of the requested events in events happens on the file descriptor. Use AbstractEventSource::EventTypes to define for which events should be polled. Returns true on success, revents will contain the bitwise combination of AbstractEventSource::EventTypes that triggered the wakeup.

Definition at line 511 of file eventdispatcher_glib.cc.

## ◆ trackChildProcess()

void zyppng::EventDispatcher::trackChildProcess | ( | int | pid,
|  | std::function< void(int, int)> | callback )

Tracks a child process until its execution did end. Callback is called when the child exits, the callback arguments are pid and status

Definition at line 549 of file eventdispatcher_glib.cc.

## ◆ untrackChildProcess()

bool zyppng::EventDispatcher::untrackChildProcess | ( | int | pid | ) |

Removes a child process from the internal sources, the process will not be reaped anymore if it is still running!

Returns
  true if the pid was removed

Definition at line 560 of file eventdispatcher_glib.cc.

## ◆ unixSignalSource()

UnixSignalSourceRef zyppng::EventDispatcher::unixSignalSource | ( |  | ) |

Returns the currently active UnixSignalSource for this EventDispatcher. It is required to keep the reference alive as long as signals need to be catched.

Definition at line 571 of file eventdispatcher_glib.cc.

## ◆ glibContext()

GMainContext * zyppng::EventDispatcher::glibContext | ( |  | ) |

Definition at line 584 of file eventdispatcher_glib.cc.

## ◆ unrefLaterImpl()

void zyppng::EventDispatcher::unrefLaterImpl | ( | std::shared_ptr< void > && | ptr | ) |
| protected

See also
  unrefLater

Definition at line 612 of file eventdispatcher_glib.cc.

## ◆ invokeOnIdleImpl()

void zyppng::EventDispatcher::invokeOnIdleImpl | ( | EventDispatcher::IdleFunction && | callback | ) |
| protected

See also
  invokeOnIdle

Definition at line 594 of file eventdispatcher_glib.cc.

## ◆ invokeAfterImpl()

void zyppng::EventDispatcher::invokeAfterImpl | ( | TimeoutFunction && | callback,
|  | uint32_t | timeout )
| protected

See also
  invokeAfter

Definition at line 601 of file eventdispatcher_glib.cc.

## ◆ updateEventSource()

void zyppng::EventDispatcher::updateEventSource | ( | AbstractEventSource & | notifier,
|  | int | fd,
|  | int | mode )
| protectedvirtual

Updates or registeres a event source in the event loop

Parameters

  notifier | The event source implementation that should receive the event notification
  fd | The file descriptor that is added to the internal watchlist
  mode | The watch mode for the given file desriptor

See also
  zyppng::AbstractEventSource::EventTypes

Definition at line 395 of file eventdispatcher_glib.cc.

## ◆ removeEventSource()

void zyppng::EventDispatcher::removeEventSource | ( | zyppng::AbstractEventSource & | notifier,
|  | int | fd = -1 )
| protectedvirtual

Removes a file descriptor from the internal watchlist, if fd is set to -1 all file descriptors associated with the

See also
  AbstractEventSource are removed

Parameters

  notifier | The

See also
  AbstractEventSource parent of the file descriptor

Parameters

  fd | The file descriptor to be removed, set to -1 to remove all descriptors for a

See also
  AbstractEventSource

Definition at line 437 of file eventdispatcher_glib.cc.

## ◆ registerTimer()

void zyppng::EventDispatcher::registerTimer | ( | Timer & | timer | ) |
| protectedvirtual

Adds a new Timer instance to the internal timer list

Definition at line 476 of file eventdispatcher_glib.cc.

## ◆ removeTimer()

void zyppng::EventDispatcher::removeTimer | ( | Timer & | timer | ) |
| protectedvirtual

Removes a timer from the internal timer list, once a Timer is removed it does not fire anymore

Definition at line 492 of file eventdispatcher_glib.cc.

## Friends And Related Symbol Documentation

## ◆ AbstractEventSource

friend class AbstractEventSource
| friend

Definition at line 45 of file eventdispatcher.h.

## ◆ Timer

friend class Timer
| friend

Definition at line 46 of file eventdispatcher.h.

The documentation for this class was generated from the following files:

- zypp-core/ng/base/eventdispatcher.h
- zypp-core/ng/base/eventdispatcher_glib.cc