iFun Engine API  1.0.0-b3582
Great Technology for Great Games
 All Classes Files Functions Variables Typedefs Macros
event.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 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/types.h>
17 #include <google/protobuf/message.h>
18 
19 #include <map>
20 #include <typeinfo>
21 
22 
23 namespace fun {
24 
26 typedef Uuid EventId;
27 
29 typedef Uuid EventTag;
30 extern const EventTag kNullEventTag;
31 
32 
34 class Event {
35  public:
36  typedef function<
37  void(const string &/*event_name*/,
38  const EventId &/*event_id*/,
39  const EventTag &/*event_tag*/,
40  const Ptr<Session> &/*associated_session*/)> TimeoutHandler;
41 
42 
45  template <typename EventT>
46  static void RegisterHandler(
47  const function<ChainAction(Ptr<const EventT>)> &h) {
48  VoidedHandler f =
49  boost::bind(h,
50  boost::bind(
51  &boost::static_pointer_cast<const EventT, const void>,
52  _1));
53 
54  RegisterHandler(typeid(EventT), f);
55  }
56 
57 
59  template <typename EventT>
60  static void RegisterHandler(
61  ChainAction (handler)(Ptr<const EventT>)) {
62  RegisterHandler(typename EventT::Handler(handler));
63  }
64 
65 
67  static void RegisterTimeoutHandler(const TimeoutHandler &handler);
68 
69 
71  template <typename EventT>
72  static bool Post(Ptr<EventT> event) {
73  return Post(typeid(EventT), event);
74  }
75  template <typename EventT>
76  static bool Post(Ptr<EventT> event, const EventTag &event_tag) {
77  return Post(typeid(EventT), event, event_tag);
78  }
79 
81  static bool Invoke(const function<void()> &func);
82  static bool Invoke(const function<void()> &func,
83  const EventTag &event_tag);
84 
85  static void Resume(const EventTag &event_tag);
86 
89  static void Abort();
90 
91  private:
92  typedef function<ChainAction (Ptr<const void>)> VoidedHandler;
93 
94  static void RegisterHandler(const std::type_info &, const VoidedHandler &);
95  static bool Post(const std::type_info &, Ptr<const void>);
96  static bool Post(const std::type_info &,
97  Ptr<const void>,
98  const EventTag &event_tag);
99 };
100 
101 
102 void SetCommonEventTag(const EventTag &tag);
103 bool IsCommonEventTag(const EventTag &tag);
104 const EventTag &GetCurrentEventTag();
105 
106 
107 void DebugSetEventName(const string &name);
108 const string &DebugGetEventName();
109 
110 } // namespace fun
111 
112 #endif // INCLUDE_FUNAPI_SYSTEM_EVENT_H_
static void RegisterHandler(ChainAction(handler)(Ptr< const EventT >))
Registers a handler for a particular event type.
Definition: event.h:60
static void RegisterTimeoutHandler(const TimeoutHandler &handler)
Registers the event timeout handler.
static bool Post(Ptr< EventT > event)
Posts an event so that listeners for the event can handle it.
Definition: event.h:72
Singleton class to access the Funapi's event subsystem.
Definition: event.h:34
static void RegisterHandler(const function< ChainAction(Ptr< const EventT >)> &h)
Listens to a particular event type.
Definition: event.h:46
static bool Invoke(const function< void()> &func)
Executes a function as an event.
static void Abort()
Aborts current event and nullify effects.