Leviathan  0.8.0.0
Leviathan game engine
Leviathan::CallableObject Class Referenceabstract

#include <CallableObject.h>

+ Inheritance diagram for Leviathan::CallableObject:

Public Member Functions

DLLEXPORT CallableObject ()
 
virtual DLLEXPORT ~CallableObject ()
 
virtual DLLEXPORT int OnEvent (Event *event)=0
 
virtual DLLEXPORT int OnGenericEvent (GenericEvent *event)=0
 

Static Public Member Functions

static DLLEXPORT EVENT_TYPE ResolveStringToType (const std::string &type)
 
static DLLEXPORT EVENT_TYPE GetCommonEventType (const std::string &type)
 
static DLLEXPORT std::string GetListenerNameFromType (EVENT_TYPE type)
 

Protected Member Functions

void UnRegisterAllEvents ()
 
void UnRegister (EVENT_TYPE from, bool all=false)
 
void UnRegister (const std::string &genericname, bool all=false)
 
void RegisterForEvent (EVENT_TYPE toregister)
 
void RegisterForEvent (const std::string &genericname)
 

Detailed Description

Todo:
Rewrite this to be cleaner with the event return codes

Definition at line 11 of file CallableObject.h.

Constructor & Destructor Documentation

◆ CallableObject()

CallableObject::CallableObject ( )

Definition at line 9 of file CallableObject.cpp.

9 {}

◆ ~CallableObject()

CallableObject::~CallableObject ( )
virtual

Definition at line 10 of file CallableObject.cpp.

11 {
12  if(HasRegisteredForSomeEvent) {
13 
14  // For easier debugging this
15  // DEBUG_BREAK;
16  LOG_FATAL("Callable object derived class hasn't called UnRegisterAllEvents");
17  }
18 }
#define LOG_FATAL(x)
Definition: Define.h:92

Member Function Documentation

◆ GetCommonEventType()

DLLEXPORT EVENT_TYPE CallableObject::GetCommonEventType ( const std::string &  type)
static
Returns
event type from string if it is one of the common events that different objects handle themselves

not a global event like in ResolveStringToType

Definition at line 63 of file CallableObject.cpp.

64 {
65  // lookup map and return //
66  auto iter = EventListenerCommonNameToEventMap.find(type);
67 
68  if(iter != EventListenerCommonNameToEventMap.end()) {
69 
70  return iter->second;
71  }
72 
73  return EVENT_TYPE_ERROR;
74 }
static const std::map< std::string, EVENT_TYPE > EventListenerCommonNameToEventMap
Definition: Event.h:76

◆ GetListenerNameFromType()

DLLEXPORT std::string CallableObject::GetListenerNameFromType ( EVENT_TYPE  type)
static

reverse of above and returns for example from EVENT_TYPE_SHOW "OnShow"

Todo:
Have a bi-directional map to speed up lookups

Definition at line 76 of file CallableObject.cpp.

77 {
78  for(auto iter = EventListenerNameToEventMap.begin();
79  iter != EventListenerNameToEventMap.end(); ++iter) {
80  // when the type matches return the string associated with that value//
81  if(iter->second == type)
82  return iter->first;
83  }
84 
85  // Also common //
86  for(auto iter = EventListenerCommonNameToEventMap.begin();
87  iter != EventListenerCommonNameToEventMap.end(); ++iter) {
88  // when the type matches return the string associated with that value//
89  if(iter->second == type)
90  return iter->first;
91  }
92 
93  return "";
94 }
static const std::map< std::string, EVENT_TYPE > EventListenerCommonNameToEventMap
Definition: Event.h:76
static const std::map< std::string, EVENT_TYPE > EventListenerNameToEventMap
Definition: Event.h:70

◆ OnEvent()

virtual DLLEXPORT int Leviathan::CallableObject::OnEvent ( Event event)
pure virtual

◆ OnGenericEvent()

virtual DLLEXPORT int Leviathan::CallableObject::OnGenericEvent ( GenericEvent event)
pure virtual

◆ RegisterForEvent() [1/2]

void CallableObject::RegisterForEvent ( EVENT_TYPE  toregister)
protected

Definition at line 30 of file CallableObject.cpp.

31 {
32  Engine::Get()->GetEventHandler()->RegisterForEvent(this, toregister);
33  HasRegisteredForSomeEvent = true;
34 }
EventHandler * GetEventHandler()
Definition: Engine.h:189
DLLEXPORT bool RegisterForEvent(CallableObject *toregister, EVENT_TYPE totype)
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ RegisterForEvent() [2/2]

void CallableObject::RegisterForEvent ( const std::string &  genericname)
protected

Definition at line 36 of file CallableObject.cpp.

37 {
38  Engine::Get()->GetEventHandler()->RegisterForEvent(this, genericname);
39  HasRegisteredForSomeEvent = true;
40 }
EventHandler * GetEventHandler()
Definition: Engine.h:189
DLLEXPORT bool RegisterForEvent(CallableObject *toregister, EVENT_TYPE totype)
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ ResolveStringToType()

DLLEXPORT EVENT_TYPE CallableObject::ResolveStringToType ( const std::string &  type)
static
Returns
event type from wstring (invalid event type is returned if matches none)
Note
if invalid type is returned type should be registered as generic event

Definition at line 50 of file CallableObject.cpp.

51 {
52  // lookup map and return //
53  auto iter = EventListenerNameToEventMap.find(type);
54 
55  if(iter != EventListenerNameToEventMap.end()) {
56 
57  return iter->second;
58  }
59 
60  return EVENT_TYPE_ERROR;
61 }
static const std::map< std::string, EVENT_TYPE > EventListenerNameToEventMap
Definition: Event.h:70

◆ UnRegister() [1/2]

void CallableObject::UnRegister ( EVENT_TYPE  from,
bool  all = false 
)
protected

Definition at line 20 of file CallableObject.cpp.

21 {
22  Engine::Get()->GetEventHandler()->Unregister(this, from, all);
23 }
DLLEXPORT void Unregister(CallableObject *caller, EVENT_TYPE type, bool all=false)
EventHandler * GetEventHandler()
Definition: Engine.h:189
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ UnRegister() [2/2]

void CallableObject::UnRegister ( const std::string &  genericname,
bool  all = false 
)
protected

Definition at line 25 of file CallableObject.cpp.

26 {
27  Engine::Get()->GetEventHandler()->Unregister(this, genericname, all);
28 }
DLLEXPORT void Unregister(CallableObject *caller, EVENT_TYPE type, bool all=false)
EventHandler * GetEventHandler()
Definition: Engine.h:189
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

◆ UnRegisterAllEvents()

void CallableObject::UnRegisterAllEvents ( )
protected
Warning
Unregister all has to be called the first thing in any derived class constructor otherwise this class' destructor will assert
Note
This object shouldn't be locked while calling this to avoid deadlocking when unregistering while an event is being processed

Definition at line 42 of file CallableObject.cpp.

43 {
45  Engine::Get()->GetEventHandler()->Unregister(this, "", true);
46 
47  HasRegisteredForSomeEvent = false;
48 }
DLLEXPORT void Unregister(CallableObject *caller, EVENT_TYPE type, bool all=false)
EventHandler * GetEventHandler()
Definition: Engine.h:189
static DLLEXPORT Engine * Get()
Definition: Engine.cpp:85

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