Leviathan  0.8.0.0
Leviathan game engine
Leviathan::BaseNotifier< ParentType, ChildType > Class Template Reference

#include <BaseNotifier.h>

+ Inheritance diagram for Leviathan::BaseNotifier< ParentType, ChildType >:

Public Member Functions

 BaseNotifier (ParentType *ourptr)
 
virtual ~BaseNotifier ()
 
void ReleaseChildHooks ()
 Release function that unhooks all child objects. More...
 
void ReleaseChildHooks (Lock &guard)
 
FORCE_INLINE bool ConnectToNotifiable (BaseNotifiable< ParentType, ChildType > *child)
 Connects this to a notifiable object for holding a reference to it. More...
 
bool ConnectToNotifiable (Lock &guard, BaseNotifiable< ParentType, ChildType > *child, Lock &childlock)
 The actual implementation of ConnecToNotifiable. More...
 
virtual void NotifyAll (Lock &guard)
 Notifies the children about something. More...
 
bool UnConnectFromNotifiable (Lock &guard, BaseNotifiable< ParentType, ChildType > *unhookfrom)
 Disconnects from previously connected notifiable. More...
 
FORCE_INLINE bool UnConnectFromNotifiable (BaseNotifiable< ParentType, ChildType > *child)
 
bool UnConnectFromNotifiable (int id)
 Searches the connected notifiable objects and calls the above function with it's pointer. More...
 
bool IsConnectedTo (BaseNotifiable< ParentType, ChildType > *check, Lock &guard)
 Actual implementation of this method. More...
 
FORCE_INLINE bool IsConnectedTo (BaseNotifiable< ParentType, ChildType > *check)
 Returns true when the specified object is already connected. More...
 
void _OnUnhookNotifiable (Lock &guard, BaseNotifiable< ParentType, ChildType > *childtoremove, Lock &childlock)
 
void _OnHookNotifiable (Lock &guard, BaseNotifiable< ParentType, ChildType > *child, Lock &childlock)
 
ParentType * GetActualPointerToNotifierObject ()
 Gets the internal pointer to the actual object. More...
 
virtual void OnNotified (Lock &ownlock, ChildType *child, Lock &childlock)
 Called when one of our children notifies us about something. More...
 
- 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
 

Protected Member Functions

virtual void _OnNotifiableConnected (Lock &guard, ChildType *childadded, Lock &childlock)
 
virtual void _OnNotifiableDisconnected (Lock &guard, ChildType *childtoremove, Lock &childlock)
 

Protected Attributes

ParentType * PointerToOurNotifier
 Stores a pointer to the object that is inherited from this. More...
 
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
 Vector of other objects that this is connected to. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Detailed Description

template<class ParentType, class ChildType>
class Leviathan::BaseNotifier< ParentType, ChildType >

Definition at line 11 of file BaseNotifier.h.

Constructor & Destructor Documentation

◆ BaseNotifier()

template<class ParentType, class ChildType >
Leviathan::BaseNotifier< ParentType, ChildType >::BaseNotifier ( ParentType *  ourptr)

Definition at line 11 of file BaseNotifierImpl.h.

11  : PointerToOurNotifier(ourptr){
12 
13 }
ParentType * PointerToOurNotifier
Stores a pointer to the object that is inherited from this.
Definition: BaseNotifier.h:96

◆ ~BaseNotifier()

template<class ParentType , class ChildType >
Leviathan::BaseNotifier< ParentType, ChildType >::~BaseNotifier ( )
virtual

Definition at line 16 of file BaseNotifierImpl.h.

16  {
17  GUARD_LOCK();
18 
19  // Last chance to unhook if not already //
20  if(ConnectedChildren.size())
21  ReleaseChildHooks(guard);
22 }
void ReleaseChildHooks()
Release function that unhooks all child objects.
Definition: BaseNotifier.h:17
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Member Function Documentation

◆ _OnHookNotifiable()

template<class ParentType, class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::_OnHookNotifiable ( Lock guard,
BaseNotifiable< ParentType, ChildType > *  child,
Lock childlock 
)

Definition at line 130 of file BaseNotifierImpl.h.

132 {
133 
134  // Add the object to the list of objects //
135  ConnectedChildren.push_back(child);
136  _OnNotifiableConnected(guard, child->GetActualPointerToNotifiableObject(), childlock);
137 }
virtual void _OnNotifiableConnected(Lock &guard, ChildType *childadded, Lock &childlock)
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ _OnNotifiableConnected()

template<class ParentType , class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::_OnNotifiableConnected ( Lock guard,
ChildType *  childadded,
Lock childlock 
)
protectedvirtual

Definition at line 172 of file BaseNotifierImpl.h.

174 {
175 
176 }

◆ _OnNotifiableDisconnected()

template<class ParentType , class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::_OnNotifiableDisconnected ( Lock guard,
ChildType *  childtoremove,
Lock childlock 
)
protectedvirtual

Definition at line 165 of file BaseNotifierImpl.h.

167 {
168 
169 }

◆ _OnUnhookNotifiable()

template<class ParentType, class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::_OnUnhookNotifiable ( Lock guard,
BaseNotifiable< ParentType, ChildType > *  childtoremove,
Lock childlock 
)

Definition at line 140 of file BaseNotifierImpl.h.

142 {
143 
144  // Remove from list //
145  auto end = ConnectedChildren.end();
146  for(auto iter = ConnectedChildren.begin(); iter != end; ++iter){
147 
148  if(*iter == childtoremove){
149 
150  _OnNotifiableDisconnected(guard, (*iter)->GetActualPointerToNotifiableObject(),
151  childlock);
152 
153  ConnectedChildren.erase(iter);
154  return;
155  }
156  }
157 }
virtual void _OnNotifiableDisconnected(Lock &guard, ChildType *childtoremove, Lock &childlock)
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ ConnectToNotifiable() [1/2]

template<class ParentType, class ChildType>
FORCE_INLINE bool Leviathan::BaseNotifier< ParentType, ChildType >::ConnectToNotifiable ( BaseNotifiable< ParentType, ChildType > *  child)
inline

Connects this to a notifiable object for holding a reference to it.

Definition at line 26 of file BaseNotifier.h.

28  {
29  GUARD_LOCK();
30  GUARD_LOCK_OTHER_NAME(child, guard2);
31  return ConnectToNotifiable(guard, child, guard2);
32  }
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:95
FORCE_INLINE bool ConnectToNotifiable(BaseNotifiable< ParentType, ChildType > *child)
Connects this to a notifiable object for holding a reference to it.
Definition: BaseNotifier.h:26
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ConnectToNotifiable() [2/2]

template<class ParentType, class ChildType>
bool Leviathan::BaseNotifier< ParentType, ChildType >::ConnectToNotifiable ( Lock guard,
BaseNotifiable< ParentType, ChildType > *  child,
Lock childlock 
)

The actual implementation of ConnecToNotifiable.

Definition at line 91 of file BaseNotifierImpl.h.

93 {
94  // Check is it already connected //
95  if(IsConnectedTo(child, guard)){
96 
97  return false;
98  }
99 
100  // Call hook on the child //
101  child->_OnHookNotifier(childlock, this, guard);
102 
103  // Add to list //
104  ConnectedChildren.push_back(child);
105 
106  // Finally call the callback //
107  _OnNotifiableConnected(guard, child->GetActualPointerToNotifiableObject(), childlock);
108 
109  return true;
110 }
bool IsConnectedTo(BaseNotifiable< ParentType, ChildType > *check, Lock &guard)
Actual implementation of this method.
virtual void _OnNotifiableConnected(Lock &guard, ChildType *childadded, Lock &childlock)
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ GetActualPointerToNotifierObject()

template<class ParentType , class ChildType >
ParentType * Leviathan::BaseNotifier< ParentType, ChildType >::GetActualPointerToNotifierObject ( )

Gets the internal pointer to the actual object.

Definition at line 160 of file BaseNotifierImpl.h.

160  {
161  return PointerToOurNotifier;
162 }
ParentType * PointerToOurNotifier
Stores a pointer to the object that is inherited from this.
Definition: BaseNotifier.h:96

◆ IsConnectedTo() [1/2]

template<class ParentType, class ChildType>
bool Leviathan::BaseNotifier< ParentType, ChildType >::IsConnectedTo ( BaseNotifiable< ParentType, ChildType > *  check,
Lock guard 
)

Actual implementation of this method.

Definition at line 113 of file BaseNotifierImpl.h.

115 {
116  VerifyLock(guard);
117 
118  auto end = ConnectedChildren.end();
119  for(auto iter = ConnectedChildren.begin(); iter != end; ++iter){
120 
121  if(*iter == check)
122  return true;
123  }
124 
125  // Didn't find a match //
126  return false;
127 }
FORCE_INLINE void VerifyLock(RecursiveLock &guard) const
Definition: ThreadSafe.h:112
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ IsConnectedTo() [2/2]

template<class ParentType, class ChildType>
FORCE_INLINE bool Leviathan::BaseNotifier< ParentType, ChildType >::IsConnectedTo ( BaseNotifiable< ParentType, ChildType > *  check)
inline

Returns true when the specified object is already connected.

Definition at line 64 of file BaseNotifier.h.

64  {
65  GUARD_LOCK();
66  return IsConnectedTo(check, guard);
67  }
bool IsConnectedTo(BaseNotifiable< ParentType, ChildType > *check, Lock &guard)
Actual implementation of this method.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ NotifyAll()

template<class ParentType , class ChildType >
void Leviathan::BaseNotifier< ParentType, ChildType >::NotifyAll ( Lock guard)
virtual

Notifies the children about something.

This will call the BaseNotifiable::OnNotified on all the child objects

Parameters
guardThe lock for this object

Definition at line 179 of file BaseNotifierImpl.h.

179  {
180  // Notify all the children //
181  auto* actualptr = GetActualPointerToNotifierObject();
182 
183  auto end = ConnectedChildren.end();
184  for(auto iter = ConnectedChildren.begin(); iter != end; ++iter){
185 
186  GUARD_LOCK_OTHER_NAME((*iter), guard2);
187  (*iter)->OnNotified(guard2, actualptr, guard);
188  }
189 }
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:95
ParentType * GetActualPointerToNotifierObject()
Gets the internal pointer to the actual object.
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ OnNotified()

template<class ParentType , class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::OnNotified ( Lock ownlock,
ChildType *  child,
Lock childlock 
)
virtual

Called when one of our children notifies us about something.

Note
Both the child and this object has been locked when this is called
Warning
Do not directly call this if you don't know what you are doing!

Reimplemented in Leviathan::ScriptNotifier.

Definition at line 192 of file BaseNotifierImpl.h.

194 {
195 
196 }

◆ ReleaseChildHooks() [1/2]

template<class ParentType, class ChildType>
void Leviathan::BaseNotifier< ParentType, ChildType >::ReleaseChildHooks ( )
inline

Release function that unhooks all child objects.

Definition at line 17 of file BaseNotifier.h.

17  {
18 
19  GUARD_LOCK();
20  ReleaseChildHooks(guard);
21  }
void ReleaseChildHooks()
Release function that unhooks all child objects.
Definition: BaseNotifier.h:17
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ ReleaseChildHooks() [2/2]

template<class ParentType , class ChildType >
void Leviathan::BaseNotifier< ParentType, ChildType >::ReleaseChildHooks ( Lock guard)

Definition at line 25 of file BaseNotifierImpl.h.

25  {
26 
27  // Go through all and unhook them //
28  while(ConnectedChildren.size()){
29  // Get the iterator //
30  auto iter = ConnectedChildren.begin();
31 
32  auto tmpobj = (*iter);
33 
34  GUARD_LOCK_OTHER_NAME((*iter), guard2);
35 
36  // Call unhook on the child //
37  tmpobj->_OnUnhookNotifier(guard2, this, guard);
38 
39  // Remove it //
40  _OnNotifiableDisconnected(guard, tmpobj->GetActualPointerToNotifiableObject(), guard2);
41 
42  ConnectedChildren.erase(iter);
43  }
44 }
virtual void _OnNotifiableDisconnected(Lock &guard, ChildType *childtoremove, Lock &childlock)
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:95
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ UnConnectFromNotifiable() [1/3]

template<class ParentType, class ChildType>
bool Leviathan::BaseNotifier< ParentType, ChildType >::UnConnectFromNotifiable ( Lock guard,
BaseNotifiable< ParentType, ChildType > *  unhookfrom 
)

Disconnects from previously connected notifiable.

Definition at line 64 of file BaseNotifierImpl.h.

66 {
67  VerifyLock(guard);
68  GUARD_LOCK_OTHER_NAME(unhookfrom, guard2);
69 
70  // Remove from the list and call functions //
71  auto end = ConnectedChildren.end();
72  for(auto iter = ConnectedChildren.begin(); iter != end; ++iter){
73 
74  if(*iter == unhookfrom){
75  // Call unhook on the child //
76 
77  (*iter)->_OnUnhookNotifier(guard2, this, guard);
78 
79  // Remove it //
80  _OnNotifiableDisconnected(guard, (*iter)->GetActualPointerToNotifiableObject(),
81  guard2);
82 
83  ConnectedChildren.erase(iter);
84  return true;
85  }
86  }
87  return false;
88 }
FORCE_INLINE void VerifyLock(RecursiveLock &guard) const
Definition: ThreadSafe.h:112
virtual void _OnNotifiableDisconnected(Lock &guard, ChildType *childtoremove, Lock &childlock)
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:95
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99

◆ UnConnectFromNotifiable() [2/3]

template<class ParentType, class ChildType>
FORCE_INLINE bool Leviathan::BaseNotifier< ParentType, ChildType >::UnConnectFromNotifiable ( BaseNotifiable< ParentType, ChildType > *  child)
inline

Definition at line 50 of file BaseNotifier.h.

52  {
53  GUARD_LOCK();
54  return UnConnectFromNotifiable(guard, child);
55  }
bool UnConnectFromNotifiable(Lock &guard, BaseNotifiable< ParentType, ChildType > *unhookfrom)
Disconnects from previously connected notifiable.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ UnConnectFromNotifiable() [3/3]

template<class ParentType, class ChildType>
bool Leviathan::BaseNotifier< ParentType, ChildType >::UnConnectFromNotifiable ( int  id)

Searches the connected notifiable objects and calls the above function with it's pointer.

Definition at line 47 of file BaseNotifierImpl.h.

47  {
48  GUARD_LOCK();
49 
50  // Find child matching the provided id //
51  auto end = ConnectedChildren.end();
52  for(auto iter = ConnectedChildren.begin(); iter != end; ++iter){
53 
54  if((*iter)->GetID() == id){
55  // Remove it //
56  // We don't need to lock the child since the called function will do that //
57  return UnConnectFromNotifiable(iter, guard);
58  }
59  }
60  return false;
61 }
bool UnConnectFromNotifiable(Lock &guard, BaseNotifiable< ParentType, ChildType > *unhookfrom)
Disconnects from previously connected notifiable.
std::vector< BaseNotifiable< ParentType, ChildType > * > ConnectedChildren
Vector of other objects that this is connected to.
Definition: BaseNotifier.h:99
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Member Data Documentation

◆ ConnectedChildren

template<class ParentType, class ChildType>
std::vector<BaseNotifiable<ParentType, ChildType>*> Leviathan::BaseNotifier< ParentType, ChildType >::ConnectedChildren
protected

Vector of other objects that this is connected to.

Definition at line 99 of file BaseNotifier.h.

◆ PointerToOurNotifier

template<class ParentType, class ChildType>
ParentType* Leviathan::BaseNotifier< ParentType, ChildType >::PointerToOurNotifier
protected

Stores a pointer to the object that is inherited from this.

Definition at line 96 of file BaseNotifier.h.


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