iFun Engine API  1.0.0-b6053
Great Technology for Great Games
event.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2020 iFunFactory Inc. All Rights Reserved.
2 //
3 // This work is confidential and proprietary to iFunFactory Inc. and
4 // must not be used, disclosed, copied, or distributed without the prior
5 // consent of iFunFactory Inc.
6 
9 #ifndef INCLUDE_FUNAPI_SYSTEM_EVENT_H_
10 #define INCLUDE_FUNAPI_SYSTEM_EVENT_H_
11 
12 #include <boost/any.hpp>
13 #include <boost/bind.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <funapi/network/session.h>
16 #include <funapi/time/wall_clock.h>
17 #include <funapi/types.h>
18 #include <google/protobuf/message.h>
19 
20 #include <map>
21 #include <typeinfo>
22 
23 
24 namespace fun {
25 
27 typedef Uuid EventId;
28 
30 typedef Uuid EventTag;
31 FUNAPI_DLL_VISIBILITY extern const EventTag kNullEventTag;
32 
33 
35 class FUNAPI_DLL_VISIBILITY Event {
36  public:
37  typedef function<
38  void(const string &/*event_name*/,
39  const EventId &/*event_id*/,
40  const EventTag &/*event_tag*/,
41  const Ptr<Session> &/*associated_session*/)> TimeoutHandler;
42 
43  template <typename EventT>
44  struct EventTypeCaster {
45  typedef Ptr<const EventT> result_type;
46  Ptr<const EventT> operator()(const Ptr<const void> &t);
47  };
48 
51  template <typename EventT>
52  static void RegisterHandler(
53  const function<ChainAction(Ptr<const EventT>)> &h);
54 
56  template <typename EventT>
57  static void RegisterHandler(ChainAction (handler)(Ptr<const EventT>));
58 
60  static void RegisterTimeoutHandler(const TimeoutHandler &handler);
61 
63  template <typename EventT>
64  static bool Post(Ptr<EventT> event);
65  template <typename EventT>
66  static bool Post(Ptr<EventT> event, const EventTag &event_tag);
67  template <typename EventT>
68  static bool Post(Ptr<EventT> event, const string &event_name);
69  template <typename EventT>
70  static bool Post(Ptr<EventT> event, const string &event_name,
71  const EventTag &event_tag);
72 
74  static bool Invoke(const function<void()> &func);
75  static bool Invoke(const function<void()> &func, const EventTag &event_tag);
76  static bool Invoke(const function<void()> &func, const string &event_name);
77  static bool Invoke(const function<void()> &func, const string &event_name,
78  const EventTag &event_tag);
79 
80  static void Resume(const EventTag &event_tag);
81 
84  static void Abort();
85 
86  private:
87  typedef function<ChainAction (Ptr<const void>)> VoidedHandler;
88 
89  static void RegisterHandler(const std::type_info &, const VoidedHandler &);
90  static bool Post(const std::type_info &, Ptr<const void>);
91  static bool Post(const std::type_info &,
92  Ptr<const void>,
93  const EventTag &event_tag);
94  static bool Post(const std::type_info &, Ptr<const void>, const string &);
95  static bool Post(const std::type_info &,
96  Ptr<const void>,
97  const string &,
98  const EventTag &event_tag);
99 };
100 
101 
102 FUNAPI_DLL_VISIBILITY void ProcessNextEventAfterTimeout(const EventTag &tag);
103 
104 
105 FUNAPI_DLL_VISIBILITY const EventTag &GetCurrentEventTag();
106 
107 
108 FUNAPI_DLL_VISIBILITY void SetEventName(const string &name);
109 FUNAPI_DLL_VISIBILITY const string &GetEventName();
110 FUNAPI_DLL_VISIBILITY DEPRECATED(void DebugSetEventName(const string &name));
111 FUNAPI_DLL_VISIBILITY DEPRECATED(const string &DebugGetEventName());
112 
113 
114 FUNAPI_DLL_VISIBILITY WallClock::Duration GetEventQueueDelay();
115 
116 
117 template <typename EventT>
119  const Ptr<const void> &t) {
120  return boost::static_pointer_cast<const EventT, const void>(t);
121 }
122 
123 
124 template <typename EventT>
125 void Event::RegisterHandler(const function<ChainAction(Ptr<const EventT>)> &h) {
126 #ifndef _WIN32
127  VoidedHandler f = VoidedHandler(
128  boost::bind(h, boost::bind(EventTypeCaster<EventT>(), _1)));
129  RegisterHandler(typeid(EventT), f);
130 #else // _WIN32
131  RegisterHandler(typeid(EventT),
132  [h](Ptr<const void> p) {
133  const EventT *ep = static_cast<const EventT*>(p.get());
134  return h(Ptr<const EventT>(p, ep));
135  });
136 #endif // _WIN32
137 }
138 
139 
140 template <typename EventT>
141 void Event::RegisterHandler(ChainAction (handler)(Ptr<const EventT>)) {
142  RegisterHandler(typename EventT::Handler(handler));
143 }
144 
145 
146 template <typename EventT>
147 bool Event::Post(Ptr<EventT> event) {
148  return Post(typeid(EventT), event);
149 }
150 
151 
152 template <typename EventT>
153 bool Event::Post(Ptr<EventT> event, const EventTag &event_tag) {
154  return Post(typeid(EventT), event, event_tag);
155 }
156 
157 
158 template <typename EventT>
159 bool Event::Post(Ptr<EventT> event, const string &event_name) {
160  return Post(typeid(EventT), event_name, event);
161 }
162 
163 
164 template <typename EventT>
165 bool Event::Post(Ptr<EventT> event, const string &event_name,
166  const EventTag &event_tag) {
167  return Post(typeid(EventT), event, event_name, event_tag);
168 }
169 
170 } // namespace fun
171 
172 #endif // INCLUDE_FUNAPI_SYSTEM_EVENT_H_
Singleton class to access the Funapi&#39;s event subsystem.
Definition: event.h:35
Uuid EventId
Event Id.
Definition: event.h:27
Definition: event.h:44
Definition: json.h:18
boost::uuids::uuid Uuid
UUID type used throughout Funapi.
Definition: types.h:56
static bool Post(Ptr< EventT > event)
Posts an event so that listeners for the event can handle it.
Definition: event.h:147
static void RegisterHandler(const function< ChainAction(Ptr< const EventT >)> &h)
Listens to a particular event type.
Definition: event.h:125
Uuid EventTag
Event Tag.
Definition: event.h:30
ChainAction
Action types to specify behavior in chained operations (i.e.
Definition: types.h:67