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
13namespace Eris
14{
15
16class EventService;
17class ActiveMarker;
18
23{
24public:
25
26 TimedEvent(EventService& eventService, const std::chrono::steady_clock::duration& duration, const std::function<void()>& callback);
28
29private:
30 std::unique_ptr<boost::asio::steady_timer> m_timer;
31};
32
33template<typename T>
34class WaitFreeQueue;
35
42class EventService : private boost::noncopyable
43{
44public:
45
50 explicit EventService(boost::asio::io_service& io_service);
51
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
98private:
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
Handles polling of the IO system as well as making sure that registered handlers are run on the main ...
Definition: EventService.h:43
size_t processAllHandlers()
Processes all registered handlers.
size_t processOneHandler()
Processes one handler, if possible.
void runOnMainThreadDelayed(const std::function< void()> &handler, const std::chrono::steady_clock::duration &duration, std::shared_ptr< bool > activeMarker=std::make_shared< bool >(true))
EventService(boost::asio::io_service &io_service)
Ctor.
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.
Class for things which occur after a period of time.
Definition: EventService.h:23
A queue optimized for insertion from background threads and consumption from one main thread.
Definition: WaitFreeQueue.h:40
Definition: Account.cpp:33