source: filezilla/trunk/fuentes/src/include/event_handler.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 4 years ago

First release to xenial

File size: 3.0 KB
Line 
1#ifndef FILEZILLA_ENGINE_EVENT_HANDLER
2#define FILEZILLA_ENGINE_EVENT_HANDLER
3
4class CEventBase;
5class CEventLoop;
6
7#include "event_loop.h"
8
9/*
10Simple handler for asynchronous event processing.
11
12Usage example:
13        struct foo_event_type{}; // Any uniquely named type that's not implicitly convertible
14        typedef CEvent<foo_event_type, int, std::string> CFooEvent;
15
16        struct bar_event_type{};
17        typedef CEvent<bar_event_type> CBarEvent;
18
19        class MyHandler final : public CEventHandler
20        {
21        public:
22                MyHandler(CEventLoop& loop)
23                        : CEventHandler(loop)
24                {}
25
26                virtual ~MyHandler()
27                {
28                        RemoveHandler();
29                }
30
31                void foo(int v, std::string const& s) {
32                        std::cout << "foo called with:" << s << v;
33                }
34
35                void bar() {
36                        std::cout << "bar called";
37                }
38
39                virtual void operator()(CEventBase const& ev) {
40                        // Tip: Put in order of decreasing frequency
41                        Dispatch<CFooEvent, CBarEvent>(ev, this, &MyHandler::foo, &MyHandler::bar);
42                }
43        };
44
45        CEventLoop loop;
46        MyHandler h(loop);
47        h.SendEvent<CFooEvent>(42, "Don't Panic");
48*/
49
50class CEventHandler
51{
52public:
53        CEventHandler() = delete;
54
55        explicit CEventHandler(CEventLoop& loop);
56        virtual ~CEventHandler();
57
58        CEventHandler(CEventHandler const&) = delete;
59        CEventHandler& operator=(CEventHandler const&) = delete;
60
61        // Deactivates handler, removes all pending events and stops all timers for this handler.
62        // When function returns, handler is not in its callback anymore.
63        //
64        // You _MUST_ call RemoveHandler no later than inside the destructor of the most derived class.
65        //
66        // This may deadlock if a handler removes itself inside its own callback
67        void RemoveHandler();
68
69        // Called by the event loop in the worker thread with the event to process
70        //
71        // Override in your derived class.
72        // Consider using the Dispatch function inside.
73        virtual void operator()(CEventBase const&) = 0;
74
75        // Sends the passed event asynchronously to the handler.
76        // Can be called from any thread.
77        // All events are processed in-order.
78        //
79        // See also operator()(CEventBase const&)
80        template<typename T, typename... Args>
81        void SendEvent(Args&&... args) {
82                event_loop_.SendEvent(this, new T(std::forward<Args>(args)...));
83        };
84
85        // Adds a timer, returns the timer id.
86        // One-shot timers are deleted automatically
87        //
88        // Once the interval expires, you get a timer event from the event loop.
89        // If multiple intervals expire before the timer fires, e.g. under heavy load,
90        // only one event is sent.
91        // The next timer event is scheduled ms_intervals into the future at the time right before
92        // the callback is called.
93        //
94        // If multiple timers have expired, the order in which the callback is executed is unspecified,
95        // there is no fairness guarantee.
96        // Timers take precedence over queued events.
97        // High-frequency timers doing heavy processing can starve other timers and queued events.
98        timer_id AddTimer(duration const& interval, bool one_shot);
99
100        // Stops the given timer.
101        // One-shot timers that have fired do not need to be stopped.
102        void StopTimer(timer_id id);
103
104        CEventLoop & event_loop_;
105private:
106        friend class CEventLoop;
107        bool removing_{};
108};
109
110#endif
Note: See TracBrowser for help on using the repository browser.