libzypp 17.38.5
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
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
24namespace zyppng {
25
26class SocketNotifier;
27class Timer;
28
32
42{
44 friend class AbstractEventSource;
45 friend class Timer;
46
47public:
48
51 using IdleFunction = std::function<bool ()>;
52 using TimeoutFunction = std::function<bool ()>;
53
54
55 ~EventDispatcher() override;
56
61 virtual bool run_once();
62
70 template< typename T = IdleFunction >
71 static void invokeOnIdle ( T &&callback )
72 {
73 auto ev = instance();
74 if ( ev )
75 ev->invokeOnIdleImpl( std::forward<T>(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
167 UnixSignalSourceRef unixSignalSource ();
168
169protected:
170
175 EventDispatcher( void *ctx = nullptr );
176
180 void unrefLaterImpl ( std::shared_ptr<void> &&ptr );
181
186
187
191 void invokeAfterImpl (TimeoutFunction &&callback, uint32_t timeout );
192
199 virtual void updateEventSource ( AbstractEventSource &notifier, int fd, int mode );
200
207 virtual void removeEventSource ( AbstractEventSource &notifier, int fd = -1 );
208
212 virtual void registerTimer ( Timer &timer );
213
217 virtual void removeTimer ( Timer &timer );
218
219};
220
221}
222
223#endif
friend class AbstractEventSource
static void unrefLater(T &&ptr)
static void invokeAfter(T &&callback, uint32_t timeout)
Convenience function to schedule a callback to be called later.
UnixSignalSourceRef unixSignalSource()
virtual void registerTimer(Timer &timer)
std::function< void(int, int)> WaitPidCallback
std::shared_ptr< EventDispatcher > WeakPtr
void unrefLaterImpl(std::shared_ptr< void > &&ptr)
std::function< bool()> TimeoutFunction
std::function< bool()> IdleFunction
EventDispatcher(void *ctx=nullptr)
virtual void removeTimer(Timer &timer)
void trackChildProcess(int pid, std::function< void(int, int)> callback)
virtual void updateEventSource(AbstractEventSource &notifier, int fd, int mode)
virtual void removeEventSource(AbstractEventSource &notifier, int fd=-1)
static std::shared_ptr< EventDispatcher > instance()
void invokeOnIdleImpl(IdleFunction &&callback)
void invokeAfterImpl(TimeoutFunction &&callback, uint32_t timeout)
std::shared_ptr< EventDispatcher > Ptr
static void invokeOnIdle(T &&callback)
Convenience function to schedule a callback to be called later.
The Timer class provides repetitive and single-shot timers.
Definition timer.h:45
Definition ansi.h:855
Callbacks light.
Definition Callback.h:146
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:87
#define LIBZYPP_NG_EXPORT
Definition zyppglobal.h:8
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:126