Eris  1.4.0
EventService.h
1 #ifndef ERIS_EVENT_SERVICE_H
2 #define ERIS_EVENT_SERVICE_H
3 
4 #include <sigc++/signal.h>
5 
6 #include <boost/asio/io_service.hpp>
7 #include <boost/asio/deadline_timer.hpp>
8 #include <boost/date_time/posix_time/ptime.hpp>
9 #include <boost/noncopyable.hpp>
10 
11 #include <queue>
12 #include <functional>
13 
14 namespace Eris
15 {
16 
17 class EventService;
18 class ActiveMarker;
19 
24 {
25 public:
26 
27  TimedEvent(EventService& eventService, const boost::posix_time::time_duration& duration, const std::function<void()>& callback);
28  ~TimedEvent();
29 
30 private:
31  boost::asio::deadline_timer* m_timer;
32 };
33 
34 template<typename T>
36 
43 class EventService : private boost::noncopyable
44 {
45 public:
46 
51  explicit EventService(boost::asio::io_service& io_service);
52 
56  ~EventService();
57 
67  void runOnMainThread(const std::function<void()>& handler,
68  std::shared_ptr<bool> activeMarker = std::make_shared<bool>(true));
69 
70 
77  void runOnMainThreadDelayed(const std::function<void()>& handler,
78  const boost::posix_time::time_duration& duration,
79  std::shared_ptr<bool> activeMarker = std::make_shared<bool>(true));
80 
88  size_t processAllHandlers();
89 
97  size_t processOneHandler();
98 
99 private:
100 
101  friend class TimedEvent;
102  boost::asio::io_service& m_io_service;
103  boost::asio::io_service::work* m_work;
104 
109  std::deque<std::function<void()>> m_handlers;
110 
117  WaitFreeQueue<std::function<void()>>* m_background_handlers_queue;
118 
123  boost::asio::deadline_timer* createTimer();
124 
128  size_t collectHandlersQueue();
129 
130 
131 };
132 
133 } // of namespace Eris
134 
135 #endif // of ERIS_EVENT_SERVICE_H
Class for things which occur after a period of time.
Definition: EventService.h:23
Every Eris class and type lives inside the Eris namespace; certain utility functions live in the Util...
Definition: Account.cpp:34
Handles polling of the IO system as well as making sure that registered handlers are run on the main ...
Definition: EventService.h:43
A queue optimized for insertion from background threads and consumption from one main thread...
Definition: EventService.h:35