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

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

First release to xenial

File size: 2.1 KB
Line 
1#ifndef FILEZILLA_ENGINE_EVENT_HEADER
2#define FILEZILLA_ENGINE_EVENT_HEADER
3
4/*
5Common base class for all events.
6
7If possible, use CEvent<> below instead of deriving from CEventBase directly.
8
9Keep events as simple as possible. Avoid mutexes in your events.
10*/
11class CEventBase
12{
13public:
14        CEventBase() = default;
15        virtual ~CEventBase() {}
16
17        CEventBase(CEventBase const&) = delete;
18        CEventBase& operator=(CEventBase const&) = delete;
19
20        /*
21        The returned pointer must be unique for the derived type such that:
22                CEventBase& a = ...
23                CEventBase& b = ...
24                assert((a.derived_type() == b.derived_type()) == (typeid(a) == typeid(b)));
25
26        Beware: Using &typeid is tempting, but unspecifined (sic)
27
28        Best solution is to have your derived type return the address of a static data member of it
29        */
30        virtual void const* derived_type() const = 0;
31};
32
33/*
34This is the recommended event class.
35
36Instanciate the template with a unique type to identify the type of the event and a number of types for the values.
37
38Keep the values simple, in particular avoid mutexes in your values.
39
40See event_handler.h for usage example.
41*/
42template<typename UniqueType, typename...Values>
43class CEvent final : public CEventBase
44{
45public:
46        typedef UniqueType unique_type;
47        typedef std::tuple<Values...> tuple_type;
48
49        CEvent() = default;
50
51        template<typename First_Value, typename...Remaining_Values>
52        explicit CEvent(First_Value&& value, Remaining_Values&& ...values)
53                : v_(std::forward<First_Value>(value), std::forward<Remaining_Values>(values)...)
54        {
55        }
56
57        CEvent(CEvent const& op) = default;
58        CEvent& operator=(CEvent const& op) = default;
59
60        // Returns a unique pointer for the type such that can be used directly in derived_type.
61        static void const* type() {
62                static const char* f = 0;
63                return &f;
64        }
65
66        virtual void const* derived_type() const {
67                return type();
68        }
69
70        tuple_type v_;
71};
72
73// Returns true iff T& t = ...; t.derived_type() == ev.derived_type()
74template<typename T>
75bool same_type(CEventBase const& ev)
76{
77        return ev.derived_type() == T::type();
78}
79
80typedef unsigned long long timer_id;
81struct timer_event_type{};
82typedef CEvent<timer_event_type, timer_id> CTimerEvent;
83
84#endif
Note: See TracBrowser for help on using the repository browser.