Leviathan  0.8.0.0
Leviathan game engine
BaseNotifierImpl.h
Go to the documentation of this file.
1 
4 // ------------------------------------ //
5 #pragma once
6 
7 #include "BaseNotifier.h"
8 #include "BaseNotifiable.h"
9 // ------------------------------------ //
10 template<class ParentType, class ChildType>
11 Leviathan::BaseNotifier<ParentType, ChildType>::BaseNotifier(ParentType* ourptr) : PointerToOurNotifier(ourptr){
12 
13 }
14 
15 template<class ParentType, class ChildType>
17  GUARD_LOCK();
18 
19  // Last chance to unhook if not already //
20  if(ConnectedChildren.size())
21  ReleaseChildHooks(guard);
22 }
23 // ------------------------------------ //
24 template<class ParentType, class ChildType>
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 }
45 // ------------------------------------ //
46 template<class ParentType, class ChildType>
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 }
62 
63 template<class ParentType, class ChildType>
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 }
89 
90 template<class ParentType, class ChildType>
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 }
111 // ------------------------------------ //
112 template<class ParentType, class ChildType>
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 }
128 // ------------------------------------ //
129 template<class ParentType, class ChildType>
131  BaseNotifiable<ParentType, ChildType>* child, Lock &childlock)
132 {
133 
134  // Add the object to the list of objects //
135  ConnectedChildren.push_back(child);
136  _OnNotifiableConnected(guard, child->GetActualPointerToNotifiableObject(), childlock);
137 }
138 
139 template<class ParentType, class ChildType>
141  BaseNotifiable<ParentType, ChildType>* childtoremove, Lock &childlock)
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 }
158 // ------------------------------------ //
159 template<class ParentType, class ChildType>
161  return PointerToOurNotifier;
162 }
163 // ------------------------------------ //
164 template<class ParentType, class ChildType>
166  Lock &guard, ChildType* childtoremove, Lock &childlock)
167 {
168 
169 }
170 
171 template<class ParentType, class ChildType>
173  Lock &guard, ChildType* childadded, Lock &childlock)
174 {
175 
176 }
177 // ------------------------------------ //
178 template<class ParentType, class ChildType>
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 }
190 
191 template<class ParentType, class ChildType>
193  ChildType* child, Lock &childlock)
194 {
195 
196 }
197 
198 
199 
bool UnConnectFromNotifiable(Lock &guard, BaseNotifiable< ParentType, ChildType > *unhookfrom)
Disconnects from previously connected notifiable.
bool IsConnectedTo(BaseNotifiable< ParentType, ChildType > *check, Lock &guard)
Actual implementation of this method.
ChildType * GetActualPointerToNotifiableObject()
Gets the internal pointer to the actual object.
virtual void _OnNotifiableDisconnected(Lock &guard, ChildType *childtoremove, Lock &childlock)
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:127
void _OnHookNotifiable(Lock &guard, BaseNotifiable< ParentType, ChildType > *child, Lock &childlock)
virtual void OnNotified(Lock &ownlock, ChildType *child, Lock &childlock)
Called when one of our children notifies us about something.
ParentType * GetActualPointerToNotifierObject()
Gets the internal pointer to the actual object.
virtual void _OnNotifiableConnected(Lock &guard, ChildType *childadded, Lock &childlock)
void _OnHookNotifier(Lock &locked, BaseNotifier< ParentType, ChildType > *parent, Lock &parentlock)
Called by parent to hook, and doesn't call the parent's functions.
void ReleaseChildHooks()
Release function that unhooks all child objects.
Definition: BaseNotifier.h:17
virtual void NotifyAll(Lock &guard)
Notifies the children about something.
void _OnUnhookNotifiable(Lock &guard, BaseNotifiable< ParentType, ChildType > *childtoremove, Lock &childlock)
FORCE_INLINE bool ConnectToNotifiable(BaseNotifiable< ParentType, ChildType > *child)
Connects this to a notifiable object for holding a reference to it.
Definition: BaseNotifier.h:26
BaseNotifier(ParentType *ourptr)
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18