libzypp  17.38.7
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 
24 typedef struct _GMainContext GMainContext;
25 
26 namespace zyppng {
27 
28 class SocketNotifier;
29 class Timer;
30 ZYPP_FWD_DECL_TYPE_WITH_REFS( EventDispatcher );
31 ZYPP_FWD_DECL_TYPE_WITH_REFS( UnixSignalSource );
32 class EventDispatcherPrivate;
33 
42 class EventDispatcher : public Base
43 {
45  friend class AbstractEventSource;
46  friend class Timer;
47 
48 public:
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 
171 
172 protected:
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
virtual void removeTimer(Timer &timer)
static void unrefLater(T &&ptr)
static void invokeAfter(T &&callback, uint32_t timeout)
Convenience function to schedule a callback to be called later.
std::weak_ptr< Base > WeakPtr
Definition: base.h:67
std::function< bool()> IdleFunction
static void setThreadDispatcher(const std::shared_ptr< EventDispatcher > &disp)
Definition: ansi.h:854
std::function< bool()> TimeoutFunction
std::function< void(int, int)> WaitPidCallback
void * nativeDispatcherHandle() const
Returns the native dispatcher handle if the used implementation supports it.
static bool waitForFdEvent(const int fd, int events, int &revents, int &timeout)
void unrefLaterImpl(std::shared_ptr< void > &&ptr)
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:44
void trackChildProcess(int pid, std::function< void(int, int)> callback)
static void invokeOnIdle(IdleFunction &&callback)
Convenience function to schedule a callback to be called later.
EventDispatcher(void *ctx=nullptr)
virtual void updateEventSource(AbstractEventSource &notifier, int fd, int mode)
static std::shared_ptr< EventDispatcher > instance()
virtual void registerTimer(Timer &timer)
virtual void removeEventSource(AbstractEventSource &notifier, int fd=-1)
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:80
ZYPP_FWD_DECL_TYPE_WITH_REFS(EventDispatcher)
void invokeAfterImpl(TimeoutFunction &&callback, uint32_t timeout)
void invokeOnIdleImpl(IdleFunction &&callback)
UnixSignalSourceRef unixSignalSource()
struct _GMainContext GMainContext
std::shared_ptr< Base > Ptr
Definition: base.h:66