libzypp: zypp-core/ng/base/eventdispatcher.h Source File

libzypp 17.38.6

eventdispatcher.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_BASE_EVENTDISPATCHER_DEFINED
15#define ZYPP_BASE_EVENTDISPATCHER_DEFINED
16
17#include <zypp-core/ng/base/zyppglobal.h>
18#include <zypp-core/ng/base/Signals>
19#include <zypp-core/ng/base/Base>
20#include <zypp-core/ng/base/AbstractEventSource>
21#include <memory>
22#include <functional>
23
24 typedef struct _GMainContext GMainContext;
25
26namespace zyppng {
27
28class SocketNotifier;
29class Timer;
30 ZYPP_FWD_DECL_TYPE_WITH_REFS( EventDispatcher );
31 ZYPP_FWD_DECL_TYPE_WITH_REFS( UnixSignalSource );
32class EventDispatcherPrivate;
33
42 class EventDispatcher : public Base
43{
44 ZYPP_DECLARE_PRIVATE(EventDispatcher)
45 friend class AbstractEventSource;
46 friend class Timer;
47
48public:
49
50 using Ptr = EventDispatcherRef;
51 using WeakPtr = EventDispatcherWeakRef;
52 using IdleFunction = std::function<bool ()>;
53 using TimeoutFunction = std::function<bool ()>;
54
55
56 ~EventDispatcher() override;
57
62 virtual bool run_once();
63
71 static void invokeOnIdle ( IdleFunction &&callback )
72 {
73 auto ev = instance();
74 if ( ev )
75 ev->invokeOnIdleImpl( std::move(callback) );
76 }
77
82 template< typename T = TimeoutFunction >
83 static void invokeAfter ( T &&callback, uint32_t timeout )
84 {
85 auto ev = instance();
86 if ( ev )
87 ev->invokeAfterImpl( std::forward<T>(callback), timeout );
88 }
89
90
101 template< typename T >
102 static void unrefLater ( T &&ptr ) {
103 auto ev = instance();
104 if ( ev )
105 ev->unrefLaterImpl( std::static_pointer_cast<void>( std::forward<T>(ptr) ) );
106 }
107
115 void clearUnrefLaterList ();
116
120 ulong runningTimers() const;
121
125 static std::shared_ptr<EventDispatcher> instance();
126
132 static void setThreadDispatcher ( const std::shared_ptr<EventDispatcher> &disp );
133
138 void *nativeDispatcherHandle () const;
139
146 static bool waitForFdEvent ( const int fd, int events, int &revents, int &timeout );
147
148 using WaitPidCallback = std::function<void(int, int)>;
149
154 void trackChildProcess ( int pid, std::function<void(int, int)> callback );
155
161 bool untrackChildProcess ( int pid );
162
163
168 UnixSignalSourceRef unixSignalSource ();
169
170 GMainContext * glibContext();
171
172protected:
173
178 EventDispatcher( void *ctx = nullptr );
179
183 void unrefLaterImpl ( std::shared_ptr<void> &&ptr );
184
188 void invokeOnIdleImpl ( IdleFunction &&callback );
189
190
194 void invokeAfterImpl (TimeoutFunction &&callback, uint32_t timeout );
195
202 virtual void updateEventSource ( AbstractEventSource &notifier, int fd, int mode );
203
210 virtual void removeEventSource ( AbstractEventSource &notifier, int fd = -1 );
211
215 virtual void registerTimer ( Timer &timer );
216
220 virtual void removeTimer ( Timer &timer );
221
222};
223
224}
225
226#endif
zyppng::Base::Base
Base()
Definition base.cc:20
zyppng::EventDispatcherPrivate
Definition eventdispatcher_glib_p.h:80
zyppng::EventDispatcher::AbstractEventSource
friend class AbstractEventSource
Definition eventdispatcher.h:45
zyppng::EventDispatcher::unrefLater
static void unrefLater(T &&ptr)
Definition eventdispatcher.h:102
zyppng::EventDispatcher::invokeAfter
static void invokeAfter(T &&callback, uint32_t timeout)
Convenience function to schedule a callback to be called later.
Definition eventdispatcher.h:83
zyppng::EventDispatcher::run_once
virtual bool run_once()
Definition eventdispatcher_glib.cc:589
zyppng::EventDispatcher::unixSignalSource
UnixSignalSourceRef unixSignalSource()
Definition eventdispatcher_glib.cc:571
zyppng::EventDispatcher::WeakPtr
EventDispatcherWeakRef WeakPtr
Definition eventdispatcher.h:51
zyppng::EventDispatcher::registerTimer
virtual void registerTimer(Timer &timer)
Definition eventdispatcher_glib.cc:476
zyppng::EventDispatcher::WaitPidCallback
std::function< void(int, int)> WaitPidCallback
Definition eventdispatcher.h:148
zyppng::EventDispatcher::clearUnrefLaterList
void clearUnrefLaterList()
Definition eventdispatcher_glib.cc:619
zyppng::EventDispatcher::runningTimers
ulong runningTimers() const
Definition eventdispatcher_glib.cc:624
zyppng::EventDispatcher::Timer
friend class Timer
Definition eventdispatcher.h:46
zyppng::EventDispatcher::unrefLaterImpl
void unrefLaterImpl(std::shared_ptr< void > &&ptr)
Definition eventdispatcher_glib.cc:612
zyppng::EventDispatcher::TimeoutFunction
std::function< bool()> TimeoutFunction
Definition eventdispatcher.h:53
zyppng::EventDispatcher::nativeDispatcherHandle
void * nativeDispatcherHandle() const
Returns the native dispatcher handle if the used implementation supports it.
Definition eventdispatcher_glib.cc:506
zyppng::EventDispatcher::glibContext
GMainContext * glibContext()
Definition eventdispatcher_glib.cc:584
zyppng::EventDispatcher::IdleFunction
std::function< bool()> IdleFunction
Definition eventdispatcher.h:52
zyppng::EventDispatcher::EventDispatcher
EventDispatcher(void *ctx=nullptr)
Definition eventdispatcher_glib.cc:386
zyppng::EventDispatcher::waitForFdEvent
static bool waitForFdEvent(const int fd, int events, int &revents, int &timeout)
Definition eventdispatcher_glib.cc:511
zyppng::EventDispatcher::removeTimer
virtual void removeTimer(Timer &timer)
Definition eventdispatcher_glib.cc:492
zyppng::EventDispatcher::trackChildProcess
void trackChildProcess(int pid, std::function< void(int, int)> callback)
Definition eventdispatcher_glib.cc:549
zyppng::EventDispatcher::untrackChildProcess
bool untrackChildProcess(int pid)
Definition eventdispatcher_glib.cc:560
zyppng::EventDispatcher::updateEventSource
virtual void updateEventSource(AbstractEventSource &notifier, int fd, int mode)
Definition eventdispatcher_glib.cc:395
zyppng::EventDispatcher::removeEventSource
virtual void removeEventSource(AbstractEventSource &notifier, int fd=-1)
Definition eventdispatcher_glib.cc:437
zyppng::EventDispatcher::invokeOnIdle
static void invokeOnIdle(IdleFunction &&callback)
Convenience function to schedule a callback to be called later.
Definition eventdispatcher.h:71
zyppng::EventDispatcher::setThreadDispatcher
static void setThreadDispatcher(const std::shared_ptr< EventDispatcher > &disp)
Definition eventdispatcher_glib.cc:634
zyppng::EventDispatcher::instance
static std::shared_ptr< EventDispatcher > instance()
Definition eventdispatcher_glib.cc:629
zyppng::EventDispatcher::invokeOnIdleImpl
void invokeOnIdleImpl(IdleFunction &&callback)
Definition eventdispatcher_glib.cc:594
zyppng::EventDispatcher::invokeAfterImpl
void invokeAfterImpl(TimeoutFunction &&callback, uint32_t timeout)
Definition eventdispatcher_glib.cc:601
zyppng::EventDispatcher::Ptr
EventDispatcherRef Ptr
Definition eventdispatcher.h:50
zyppng::UnixSignalSource
Definition unixsignalsource.h:13
GMainContext
struct _GMainContext GMainContext
Definition eventdispatcher.h:24
std
Definition ansi.h:855
zypp::callback
Callbacks light.
Definition Callback.h:146
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppglobal.h
ZYPP_DECLARE_PRIVATE
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:80
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119