libzypp  17.22.0
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/zyppng/base/Signals>
19 #include <zypp/zyppng/base/Base>
20 #include <zypp/zyppng/base/AbstractEventSource>
21 #include <memory>
22 #include <functional>
23 
24 namespace zyppng {
25 
26 class SocketNotifier;
27 class Timer;
28 class EventDispatcher;
29 
30 
31 class EventDispatcherPrivate;
32 
50 {
52  friend class AbstractEventSource;
53  friend class Timer;
54 
55 public:
56 
57  using Ptr = std::shared_ptr<EventDispatcher>;
58  using WeakPtr = std::shared_ptr<EventDispatcher>;
59  using IdleFunction = std::function<bool ()>;
60 
68  static std::shared_ptr<EventDispatcher> createMain ( );
69 
78  static std::shared_ptr<EventDispatcher> createForThread ( );
79 
80  virtual ~EventDispatcher();
81 
86  virtual bool run_once();
87 
91  virtual void run ();
92 
96  virtual void quit ();
97 
102  template< typename T = IdleFunction >
103  static void invokeOnIdle ( T &&callback )
104  {
105  auto ev = instance();
106  if ( ev )
107  ev->invokeOnIdleImpl( std::forward<T>(callback) );
108  }
109 
117  template< typename T >
118  static void unrefLater ( T &&ptr ) {
119  auto ev = instance();
120  if ( ev )
121  ev->unrefLaterImpl( std::static_pointer_cast<void>( std::forward<T>(ptr) ) );
122  }
123 
127  ulong runningTimers() const;
128 
132  static std::shared_ptr<EventDispatcher> instance();
133 
134 protected:
135 
140  EventDispatcher( void *ctx = nullptr );
141 
145  void unrefLaterImpl ( std::shared_ptr<void> &&ptr );
146 
150  void invokeOnIdleImpl ( IdleFunction &&callback );
151 
158  virtual void updateEventSource ( AbstractEventSource *notifier, int fd, int mode );
159 
166  virtual void removeEventSource ( AbstractEventSource *notifier, int fd = -1 );
167 
171  virtual void registerTimer ( Timer *timer );
172 
176  virtual void removeTimer ( Timer *timer );
177 };
178 
179 }
180 
181 #endif
static void unrefLater(T &&ptr)
std::weak_ptr< Base > WeakPtr
Definition: base.h:39
std::function< bool()> IdleFunction
Definition: Arch.h:347
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:42
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:21
#define LIBZYPP_NG_EXPORT
Definition: zyppglobal.h:7
std::shared_ptr< Base > Ptr
Definition: base.h:38