libfilezilla
event.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_EVENT_HEADER
2 #define LIBFILEZILLA_EVENT_HEADER
3 
4 #include "libfilezilla.hpp"
5 
6 #include <tuple>
7 #include <typeinfo>
8 
13 namespace fz {
14 
22 class FZ_PUBLIC_SYMBOL event_base
23 {
24 public:
25  event_base() = default;
26  virtual ~event_base() {}
27 
28  event_base(event_base const&) = delete;
29  event_base& operator=(event_base const&) = delete;
30 
46  virtual size_t derived_type() const = 0;
47 };
48 
50 size_t FZ_PUBLIC_SYMBOL get_unique_type_id(std::type_info const& id);
51 
62 template<typename UniqueType, typename...Values>
63 class simple_event final : public event_base
64 {
65 public:
66  typedef UniqueType unique_type;
67  typedef std::tuple<Values...> tuple_type;
68 
69  simple_event() = default;
70 
71  template<typename First_Value, typename...Remaining_Values>
72  explicit simple_event(First_Value&& value, Remaining_Values&& ...values)
73  : v_(std::forward<First_Value>(value), std::forward<Remaining_Values>(values)...)
74  {
75  }
76 
77  simple_event(simple_event const& op) = default;
78  simple_event& operator=(simple_event const& op) = default;
79 
81  inline static size_t type() {
82  // Exporting templates from DLLs is problematic to say the least. It breaks
83  // ODR, so we use this trick that goes over the type name.
84  static size_t const v = get_unique_type_id(typeid(UniqueType*));
85  return v;
86  }
87 
89  virtual size_t derived_type() const {
90  return type();
91  }
92 
97  mutable tuple_type v_;
98 };
99 
102 template<typename T>
103 bool same_type(event_base const& ev)
104 {
105  return ev.derived_type() == T::type();
106 }
107 
108 typedef unsigned long long timer_id;
109 
111 struct timer_event_type{};
112 
118 
119 }
120 
121 #endif
simple_event< timer_event_type, timer_id > timer_event
All timer events have this type.
Definition: event.hpp:117
virtual size_t derived_type() const
Simply returns type()
Definition: event.hpp:89
tuple_type v_
The event value, gets built from the arguments passed in the constructor.
Definition: event.hpp:97
static size_t type()
Returns a unique id for the type such that can be used directly in derived_type.
Definition: event.hpp:81
virtual size_t derived_type() const =0
This is the recommended event class.
Definition: event.hpp:63
bool same_type(event_base const &ev)
Definition: event.hpp:103
The namespace used by libfilezilla.
Definition: apply.hpp:17
Sets some global macros and further includes string.hpp.
Common base class for all events.
Definition: event.hpp:22