eris  1.4.0
A WorldForge client library.
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/steady_timer.hpp>
8 #include <boost/noncopyable.hpp>
9 
10 #include <queue>
11 #include <functional>
12 
13 namespace Eris
14 {
15 
16 class EventService;
17 class ActiveMarker;
18 
23 {
24 public:
25 
26  TimedEvent(EventService& eventService, const std::chrono::steady_clock::duration& duration, const std::function<void()>& callback);
27  ~TimedEvent();
28 
29 private:
30  std::unique_ptr<boost::asio::steady_timer> m_timer;
31 };
32 
33 template<typename T>
35 
42 class EventService : private boost::noncopyable
43 {
44 public:
45 
50  explicit EventService(boost::asio::io_service& io_service);
51 
55  ~EventService();
56 
66  void runOnMainThread(const std::function<void()>& handler,
67  std::shared_ptr<bool> activeMarker = std::make_shared<bool>(true));
68 
69 
76  void runOnMainThreadDelayed(const std::function<void()>& handler,
77  const std::chrono::steady_clock::duration& duration,
78  std::shared_ptr<bool> activeMarker = std::make_shared<bool>(true));
79 
87  size_t processAllHandlers();
88 
96  size_t processOneHandler();
97 
98 private:
99 
100  friend class TimedEvent;
101  boost::asio::io_service& m_io_service;
102  std::unique_ptr<boost::asio::io_service::work> m_work;
103 
108  std::deque<std::function<void()>> m_handlers;
109 
116  std::unique_ptr<WaitFreeQueue<std::function<void()>>> m_background_handlers_queue;
117 
122  std::unique_ptr<boost::asio::steady_timer> createTimer();
123 
127  size_t collectHandlersQueue();
128 
129 
130 };
131 
132 } // of namespace Eris
133 
134 #endif // of ERIS_EVENT_SERVICE_H
Eris::EventService
Handles polling of the IO system as well as making sure that registered handlers are run on the main ...
Definition: EventService.h:43
Eris::EventService::~EventService
~EventService()
Dtor.
Definition: EventService.cpp:34
Eris::EventService::runOnMainThreadDelayed
void runOnMainThreadDelayed(const std::function< void()> &handler, const std::chrono::steady_clock::duration &duration, std::shared_ptr< bool > activeMarker=std::make_shared< bool >(true))
Definition: EventService.cpp:57
Eris::TimedEvent
Class for things which occur after a period of time.
Definition: EventService.h:23
Eris
Definition: Account.cpp:33
Eris::WaitFreeQueue
A queue optimized for insertion from background threads and consumption from one main thread.
Definition: EventService.h:34
Eris::EventService::processAllHandlers
size_t processAllHandlers()
Processes all registered handlers.
Definition: EventService.cpp:70
Eris::EventService::runOnMainThread
void runOnMainThread(const std::function< void()> &handler, std::shared_ptr< bool > activeMarker=std::make_shared< bool >(true))
Adds a handler which will be run on the main thread.
Definition: EventService.cpp:47
Eris::EventService::EventService
EventService(boost::asio::io_service &io_service)
Ctor.
Definition: EventService.cpp:27
Eris::EventService::processOneHandler
size_t processOneHandler()
Processes one handler, if possible.
Definition: EventService.cpp:99