Leviathan  0.8.0.0
Leviathan game engine
BaseNotifiable.h
Go to the documentation of this file.
1 #ifndef BASENOTIFIABLE_H
2 #define BASENOTIFIABLE_H
3 #include "Define.h"
4 // ------------------------------------ //
5 #include "ThreadSafe.h"
6 #include <vector>
7 
8 namespace Leviathan{
9 
13 template<class ParentType, class ChildType>
14  class BaseNotifiable : public virtual ThreadSafe{
15 public:
16  BaseNotifiable(ChildType* ourptr);
17  virtual ~BaseNotifiable();
18 
20  void ReleaseParentHooks(Lock &guard);
21 
23  bool UnConnectFromNotifier(Lock &guard,
24  BaseNotifier<ParentType, ChildType>* specificnotifier, Lock &notifierlock);
25 
26  inline bool UnConnectFromNotifier(Lock &guard,
27  BaseNotifier<ParentType, ChildType>* specificnotifier)
28  {
29 
30  guard.unlock();
31  GUARD_LOCK_OTHER_NAME(specificnotifier, guard2);
32  guard.lock();
33 
34  return UnConnectFromNotifier(guard, specificnotifier, guard2);
35  }
36 
41  virtual void NotifyAll(Lock &guard);
42 
45  BaseNotifier<ParentType, ChildType>* specificnotifier)
46  {
47  // The parent has to be locked before this object //
48  GUARD_LOCK_OTHER_NAME(specificnotifier, guard2);
49  GUARD_LOCK();
50  return UnConnectFromNotifier(specificnotifier, guard, guard2);
51  }
52 
55 
58  GUARD_LOCK();
59  return IsConnectedTo(check, guard);
60  }
61 
63  bool UnConnectFromNotifier(int id);
64 
67 
71 
74  Lock &parentlock);
75 
78  Lock &parentlock);
79 
82 
86  virtual void OnNotified(Lock &ownlock, ParentType* parent, Lock &parentlock);
87 
88 
89 protected:
90 
91  // Callbacks for child classes to implement //
92  virtual void _OnNotifierConnected(Lock &guard, ParentType* parentadded,
93  Lock &parentlock);
94  virtual void _OnNotifierDisconnected(Lock &guard, ParentType* parentremoved,
95  Lock &parentlock);
96  // ------------------------------------ //
97 
100 
102  std::vector<BaseNotifier<ParentType, ChildType>*> ConnectedToParents;
103 
105 };
106 
107 
109 class BaseNotifiableAll : public BaseNotifiable<BaseNotifierAll, BaseNotifiableAll>{
110 public:
112  }
114  }
115 };
116 }
117 
118 // The implementations are included here to make this compile //
119 #include "BaseNotifiableImpl.h"
120 
121 #ifdef LEAK_INTO_GLOBAL
123 #endif
124 
125 
126 #endif //BASENOTIFIABLE_H
ChildType * PointerToOurNotifiable
Stores a pointer to the object that is inherited from this.
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
ChildType * GetActualPointerToNotifiableObject()
Gets the internal pointer to the actual object.
bool IsConnectedTo(BaseNotifier< ParentType, ChildType > *check, Lock &guard)
Actual implementation of this method.
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:127
#define FORCE_INLINE
Definition: Include.h:95
BaseNotifiable(ChildType *ourptr)
std::vector< BaseNotifier< ParentType, ChildType > * > ConnectedToParents
Vector of other objects that this is connected to.
bool UnConnectFromNotifier(Lock &guard, BaseNotifier< ParentType, ChildType > *specificnotifier)
FORCE_INLINE bool IsConnectedTo(BaseNotifier< ParentType, ChildType > *check)
Returns true when the specified object is already connected.
Specialized class for accepting all parent/child objects.
void _OnHookNotifier(Lock &locked, BaseNotifier< ParentType, ChildType > *parent, Lock &parentlock)
Called by parent to hook, and doesn&#39;t call the parent&#39;s functions.
virtual void NotifyAll(Lock &guard)
Notifies all the parents of this object about something.
FORCE_INLINE bool UnConnectFromNotifier(BaseNotifier< ParentType, ChildType > *specificnotifier)
Disconnects this from a previously connected notifier.
void ReleaseParentHooks(Lock &guard)
Release function which releases all hooks.
virtual void _OnNotifierDisconnected(Lock &guard, ParentType *parentremoved, Lock &parentlock)
bool ConnectToNotifier(BaseNotifier< ParentType, ChildType > *owner)
Connects this to a notifier object calling all the needed functions.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
void _OnUnhookNotifier(Lock &locked, BaseNotifier< ParentType, ChildType > *parent, Lock &parentlock)
Callback called by the parent, used to not to call the unhook again on the parent.
virtual void _OnNotifierConnected(Lock &guard, ParentType *parentadded, Lock &parentlock)
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
bool UnConnectFromNotifier(Lock &guard, BaseNotifier< ParentType, ChildType > *specificnotifier, Lock &notifierlock)
The actual implementation of UnConnectFromNotifier.
virtual void OnNotified(Lock &ownlock, ParentType *parent, Lock &parentlock)
Called when our parent notifies us about something.