Leviathan  0.8.0.0
Leviathan game engine
Leviathan::EventHandler Class Reference

Allows object to register for events that can be fired from anywhere. More...

#include <EventHandler.h>

+ Inheritance diagram for Leviathan::EventHandler:

Public Member Functions

DLLEXPORT EventHandler ()
 
DLLEXPORT ~EventHandler ()
 
DLLEXPORT bool Init ()
 
DLLEXPORT void Release ()
 
DLLEXPORT void CallEvent (Event *event)
 
DLLEXPORT void CallEvent (const Event::pointer &event)
 
DLLEXPORT void CallEvent (GenericEvent *event)
 
DLLEXPORT void CallEvent (const GenericEvent::pointer &event)
 
DLLEXPORT bool RegisterForEvent (CallableObject *toregister, EVENT_TYPE totype)
 
DLLEXPORT bool RegisterForEvent (CallableObject *toregister, const std::string &genericname)
 
DLLEXPORT void Unregister (CallableObject *caller, EVENT_TYPE type, bool all=false)
 
DLLEXPORT void Unregister (CallableObject *caller, const std::string &genericname, bool all=false)
 
- Public Member Functions inherited from Leviathan::ThreadSafeGeneric< MutexType >
DLLEXPORT ThreadSafeGeneric ()
 
DLLEXPORT ~ThreadSafeGeneric ()
 
FORCE_INLINE void VerifyLock (RecursiveLock &guard) const
 
FORCE_INLINE void VerifyLock (Lock &lockit) const
 

Additional Inherited Members

- Public Types inherited from Leviathan::ThreadSafeGeneric< MutexType >
using LockT = typename LockTypeResolver< MutexType >::LType
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

Allows object to register for events that can be fired from anywhere.

This is recursive to allow EventCallbacks to also fire events. This makes it possible to cause a stackoverflow but makes it easier to make events that fire a different event

Definition at line 15 of file EventHandler.h.

Constructor & Destructor Documentation

◆ EventHandler()

EventHandler::EventHandler ( )

Definition at line 6 of file EventHandler.cpp.

6  {
7 
8 }

◆ ~EventHandler()

EventHandler::~EventHandler ( )

Definition at line 10 of file EventHandler.cpp.

10  {
11 
12 }

Member Function Documentation

◆ CallEvent() [1/4]

void EventHandler::CallEvent ( Event event)
Parameters
eventThe event to send. Reference count will be decremented by this

Definition at line 28 of file EventHandler.cpp.

28  {
29 
30  const auto type = event->GetType();
31 
32  GUARD_LOCK();
33 
34  for(auto iter = EventListeners.begin(); iter != EventListeners.end(); ){
35 
36  if(std::get<1>(*iter) == type){
37 
38  const auto result = std::get<0>(*iter)->OnEvent(event);
39 
40  if(result == -1){
41 
42  // Unregister requested //
43  iter = EventListeners.erase(iter);
44  continue;
45  }
46  }
47 
48  ++iter;
49  }
50 
51  event->Release();
52 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ CallEvent() [2/4]

DLLEXPORT void Leviathan::EventHandler::CallEvent ( const Event::pointer &  event)
inline

Definition at line 25 of file EventHandler.h.

26  {
27  event->AddRef();
28  CallEvent(event.get());
29  }
DLLEXPORT void CallEvent(Event *event)

◆ CallEvent() [3/4]

DLLEXPORT void Leviathan::EventHandler::CallEvent ( GenericEvent event)
Parameters
eventThe event to send. Reference count will be decremented by this

Definition at line 54 of file EventHandler.cpp.

54  {
55 
56  const auto type = event->GetType();
57 
58  GUARD_LOCK();
59 
60  // Loop generic listeners //
61  for(auto iter = GenericEventListeners.begin(); iter != GenericEventListeners.end(); ){
62 
63  if(std::get<1>(*iter) == type){
64 
65  const auto result = std::get<0>(*iter)->OnGenericEvent(event);
66 
67  if(result == -1){
68 
69  // Unregister requested //
70  iter = GenericEventListeners.erase(iter);
71  continue;
72  }
73  }
74 
75  ++iter;
76  }
77 
78  event->Release();
79 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ CallEvent() [4/4]

DLLEXPORT void Leviathan::EventHandler::CallEvent ( const GenericEvent::pointer &  event)
inline
Todo:
Swap this with the function above to make this more efficient (and switch to using this)

Definition at line 35 of file EventHandler.h.

36  {
37  event->AddRef();
38  CallEvent(event.get());
39  }
DLLEXPORT void CallEvent(Event *event)

◆ Init()

bool EventHandler::Init ( )

Definition at line 14 of file EventHandler.cpp.

14  {
15  return true;
16 }

◆ RegisterForEvent() [1/2]

bool EventHandler::RegisterForEvent ( CallableObject toregister,
EVENT_TYPE  totype 
)

Definition at line 81 of file EventHandler.cpp.

81  {
82  GUARD_LOCK();
83  EventListeners.push_back(std::make_tuple(toregister, totype));
84  return true;
85 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ RegisterForEvent() [2/2]

DLLEXPORT bool Leviathan::EventHandler::RegisterForEvent ( CallableObject toregister,
const std::string &  genericname 
)

Definition at line 87 of file EventHandler.cpp.

89 {
90  GUARD_LOCK();
91  GenericEventListeners.push_back(std::make_tuple(toregister, genericname));
92  return true;
93 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Release()

void EventHandler::Release ( )

Definition at line 18 of file EventHandler.cpp.

18  {
19  GUARD_LOCK();
20 
21 
22 
23  // Release listeners //
24  EventListeners.clear();
25  GenericEventListeners.clear();
26 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Unregister() [1/2]

void EventHandler::Unregister ( CallableObject caller,
EVENT_TYPE  type,
bool  all = false 
)

Definition at line 95 of file EventHandler.cpp.

95  {
96  GUARD_LOCK();
97 
98  // Loop and remove wanted objects //
99  for(auto iter = EventListeners.begin(); iter != EventListeners.end(); ){
100 
101  if(std::get<0>(*iter) == caller){
102 
103  // check type or if all is specified delete //
104  if(all || type == std::get<1>(*iter)){
105 
106  iter = EventListeners.erase(iter);
107  continue;
108  }
109  }
110 
111  ++iter;
112  }
113 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

◆ Unregister() [2/2]

DLLEXPORT void Leviathan::EventHandler::Unregister ( CallableObject caller,
const std::string &  genericname,
bool  all = false 
)

Definition at line 115 of file EventHandler.cpp.

118 {
119  GUARD_LOCK();
120 
121  // Loop and remove wanted objects //
122  for(auto iter = GenericEventListeners.begin(); iter != GenericEventListeners.end(); ){
123 
124  if(std::get<0>(*iter) == caller){
125 
126  // check type or if all is specified delete //
127  if(all || genericname == std::get<1>(*iter)){
128 
129  iter = GenericEventListeners.erase(iter);
130  continue;
131  }
132  }
133 
134  ++iter;
135  }
136 }
#define GUARD_LOCK()
Definition: ThreadSafe.h:111

The documentation for this class was generated from the following files: