Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SyncedPrimitive< DTypeName > Class Template Reference

Template class for syncing basic types. More...

#include <ForwardDeclarations.h>

+ Inheritance diagram for Leviathan::SyncedPrimitive< DTypeName >:

Public Types

using CallbackPtr = void(*)(Lock &guard, SyncedPrimitive< DTypeName > *updated)
 The callback type. More...
 

Public Member Functions

 SyncedPrimitive (const std::string &uniquename, const DTypeName &initialvalue, CallbackPtr updatecallback=NULL)
 Constructs an instance with a initial value. More...
 
 ~SyncedPrimitive ()
 
void UpdateValue (const DTypeName &newvalue)
 Updates the value and notifies SyncedVariables. More...
 
DTypeName GetValue () const
 Gets the value with locking. More...
 
DTypeName * GetValueDirect ()
 Directly accesses the variable, you will need to use your own locking with complex types. More...
 
SyncedResourceoperator= (const DTypeName &value)
 Assignment operator that acts like UpdateValue. More...
 
bool operator== (const DTypeName &value)
 
bool operator!= (const DTypeName &value)
 
 operator DTypeName ()
 
- Public Member Functions inherited from Leviathan::SyncedResource
DLLEXPORT SyncedResource (const std::string &uniquename)
 Constructs a base class for synced variables that requires a unique name. More...
 
virtual DLLEXPORT ~SyncedResource ()
 
virtual DLLEXPORT void AddDataToPacket (Lock &guard, sf::Packet &packet)
 Serializes the name to a packet. More...
 
DLLEXPORT void AddDataToPacket (sf::Packet &packet)
 
virtual DLLEXPORT bool UpdateDataFromPacket (Lock &guard, sf::Packet &packet)
 Assigns data from a packet to this resource. More...
 
DLLEXPORT bool UpdateDataFromPacket (sf::Packet &packet)
 
virtual DLLEXPORT void StartSync (SyncedVariables &variablesync)
 Registers this resource with the SyncedVariables instance. More...
 
DLLEXPORT void NotifyUpdatedValue ()
 Notify that this is changed and we want a local message, too. More...
 
- Public Member Functions inherited from Leviathan::BaseNotifiableAll
 BaseNotifiableAll ()
 
 ~BaseNotifiableAll ()
 
- Public Member Functions inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
 BaseNotifiable (BaseNotifiableAll *ourptr)
 
virtual ~BaseNotifiable ()
 
void ReleaseParentHooks (Lock &guard)
 Release function which releases all hooks. More...
 
bool UnConnectFromNotifier (Lock &guard, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier, Lock &notifierlock)
 The actual implementation of UnConnectFromNotifier. More...
 
bool UnConnectFromNotifier (Lock &guard, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier)
 
FORCE_INLINE bool UnConnectFromNotifier (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *specificnotifier)
 Disconnects this from a previously connected notifier. More...
 
bool UnConnectFromNotifier (int id)
 This searches the connected notifiers and calls the above function with it's pointer. More...
 
virtual void NotifyAll (Lock &guard)
 Notifies all the parents of this object about something. More...
 
bool IsConnectedTo (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *check, Lock &guard)
 Actual implementation of this method. More...
 
FORCE_INLINE bool IsConnectedTo (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *check)
 Returns true when the specified object is already connected. More...
 
bool ConnectToNotifier (BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *owner)
 Connects this to a notifier object calling all the needed functions. More...
 
bool ConnectToNotifier (Lock &unlockable, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *owner)
 Variant for already locked objects. More...
 
void _OnUnhookNotifier (Lock &locked, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *parent, Lock &parentlock)
 Callback called by the parent, used to not to call the unhook again on the parent. More...
 
void _OnHookNotifier (Lock &locked, BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *parent, Lock &parentlock)
 Called by parent to hook, and doesn't call the parent's functions. More...
 
BaseNotifiableAllGetActualPointerToNotifiableObject ()
 Gets the internal pointer to the actual object. More...
 
virtual void OnNotified (Lock &ownlock, BaseNotifierAll *parent, Lock &parentlock)
 Called when our parent 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 OnValueUpdated (Lock &guard) override
 
virtual void UpdateCustomDataFromPacket (Lock &guard, sf::Packet &packet) override
 Should load the custom data from a packet. More...
 
virtual void SerializeCustomDataToPacket (Lock &guard, sf::Packet &packet) override
 Should be used to add custom data to packet. More...
 
- Protected Member Functions inherited from Leviathan::SyncedResource
virtual DLLEXPORT void UpdateOurNetworkValue (Lock &guard)
 Notifies our SyncedVariables of an update. More...
 
SyncedResourceoperator= (const SyncedResource &other)
 
 SyncedResource (const SyncedResource &other)
 
- Protected Member Functions inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
virtual void _OnNotifierConnected (Lock &guard, BaseNotifierAll *parentadded, Lock &parentlock)
 
virtual void _OnNotifierDisconnected (Lock &guard, BaseNotifierAll *parentremoved, Lock &parentlock)
 

Protected Attributes

bool IsValid
 Little overhead but this is important to discard update requests after stopping. More...
 
CallbackPtr ValueUpdateCallback
 This is quite an important feature to store this function pointer. More...
 
DTypeName OurValue
 The primitive object owned by this. More...
 
- Protected Attributes inherited from Leviathan::SyncedResource
const std::string Name
 
- Protected Attributes inherited from Leviathan::BaseNotifiable< BaseNotifierAll, BaseNotifiableAll >
BaseNotifiableAllPointerToOurNotifiable
 Stores a pointer to the object that is inherited from this. More...
 
std::vector< BaseNotifier< BaseNotifierAll, BaseNotifiableAll > *> ConnectedToParents
 Vector of other objects that this is connected to. More...
 
- Protected Attributes inherited from Leviathan::ThreadSafeGeneric< MutexType >
MutexType ObjectsLock
 

Additional Inherited Members

- Static Public Member Functions inherited from Leviathan::SyncedResource
static DLLEXPORT std::string GetSyncedResourceNameFromPacket (sf::Packet &packet)
 Gets a name from packet leaving only the variable data there. More...
 

Detailed Description

template<class DTypeName>
class Leviathan::SyncedPrimitive< DTypeName >

Template class for syncing basic types.

Warning
This will only work with primitive types like int, float, string etc. For other use you must inherit SyncedResource and create a custom class

Definition at line 139 of file ForwardDeclarations.h.

Member Typedef Documentation

◆ CallbackPtr

template<class DTypeName >
using Leviathan::SyncedPrimitive< DTypeName >::CallbackPtr = void (*)(Lock &guard, SyncedPrimitive<DTypeName>* updated)

The callback type.

Definition at line 91 of file SyncedResource.h.

Constructor & Destructor Documentation

◆ SyncedPrimitive()

template<class DTypeName >
Leviathan::SyncedPrimitive< DTypeName >::SyncedPrimitive ( const std::string &  uniquename,
const DTypeName &  initialvalue,
CallbackPtr  updatecallback = NULL 
)
inline

Constructs an instance with a initial value.

Warning
The order of the initializer list is important since anytime after calling SyncedResource we can receive updates

Definition at line 96 of file SyncedResource.h.

97  :
98  SyncedResource(uniquename),
99  ValueUpdateCallback(updatecallback), OurValue(initialvalue)
100  {
101  // Now we are ready to be updated //
102  IsValid = true;
103  }
DLLEXPORT SyncedResource(const std::string &uniquename)
Constructs a base class for synced variables that requires a unique name.
DTypeName OurValue
The primitive object owned by this.
CallbackPtr ValueUpdateCallback
This is quite an important feature to store this function pointer.
bool IsValid
Little overhead but this is important to discard update requests after stopping.

◆ ~SyncedPrimitive()

template<class DTypeName >
Leviathan::SyncedPrimitive< DTypeName >::~SyncedPrimitive ( )
inline

Definition at line 105 of file SyncedResource.h.

105  {
106 
107  GUARD_LOCK();
108 
109  // Unhook already //
110  ReleaseParentHooks(guard);
111  // Set us as invalid after locking //
112 
113  IsValid = false;
114 
115  // Destructors will take care of the rest //
116  }
void ReleaseParentHooks(Lock &guard)
Release function which releases all hooks.
bool IsValid
Little overhead but this is important to discard update requests after stopping.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Member Function Documentation

◆ GetValue()

template<class DTypeName >
DTypeName Leviathan::SyncedPrimitive< DTypeName >::GetValue ( ) const
inline

Gets the value with locking.

Definition at line 131 of file SyncedResource.h.

131  {
132  GUARD_LOCK();
133  return OurValue;
134  }
DTypeName OurValue
The primitive object owned by this.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ GetValueDirect()

template<class DTypeName >
DTypeName* Leviathan::SyncedPrimitive< DTypeName >::GetValueDirect ( )
inline

Directly accesses the variable, you will need to use your own locking with complex types.

Warning
The returned value might be changed at any point and depending on this object's lifespan it may become invalid
See also
GetValue

Definition at line 140 of file SyncedResource.h.

140  {
141  return &OurValue;
142  }
DTypeName OurValue
The primitive object owned by this.

◆ OnValueUpdated()

template<class DTypeName >
virtual void Leviathan::SyncedPrimitive< DTypeName >::OnValueUpdated ( Lock guard)
inlineoverrideprotectedvirtual

Update notifications are received through this

Called from UpdateDataFromPacket

Reimplemented from Leviathan::SyncedResource.

Definition at line 165 of file SyncedResource.h.

165  {
166  // Report update //
168  ValueUpdateCallback(guard, this);
169  }
CallbackPtr ValueUpdateCallback
This is quite an important feature to store this function pointer.

◆ operator DTypeName()

template<class DTypeName >
Leviathan::SyncedPrimitive< DTypeName >::operator DTypeName ( )
inline

Definition at line 157 of file SyncedResource.h.

157  {
158  GUARD_LOCK();
159  return OurValue;
160  }
DTypeName OurValue
The primitive object owned by this.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

◆ operator!=()

template<class DTypeName >
bool Leviathan::SyncedPrimitive< DTypeName >::operator!= ( const DTypeName &  value)
inline

Definition at line 153 of file SyncedResource.h.

153  {
154  return OurValue != value;
155  }
DTypeName OurValue
The primitive object owned by this.

◆ operator=()

template<class DTypeName >
SyncedResource& Leviathan::SyncedPrimitive< DTypeName >::operator= ( const DTypeName &  value)
inline

Assignment operator that acts like UpdateValue.

Definition at line 145 of file SyncedResource.h.

145  {
146  UpdateValue(value);
147  return *this;
148  }
void UpdateValue(const DTypeName &newvalue)
Updates the value and notifies SyncedVariables.

◆ operator==()

template<class DTypeName >
bool Leviathan::SyncedPrimitive< DTypeName >::operator== ( const DTypeName &  value)
inline

Definition at line 150 of file SyncedResource.h.

150  {
151  return OurValue == value;
152  }
DTypeName OurValue
The primitive object owned by this.

◆ SerializeCustomDataToPacket()

template<class DTypeName >
virtual void Leviathan::SyncedPrimitive< DTypeName >::SerializeCustomDataToPacket ( Lock guard,
sf::Packet &  packet 
)
inlineoverrideprotectedvirtual

Should be used to add custom data to packet.

See also
UpdateCustomDataFromPacket

Implements Leviathan::SyncedResource.

Definition at line 182 of file SyncedResource.h.

182  {
183  packet << OurValue;
184  }
DTypeName OurValue
The primitive object owned by this.

◆ UpdateCustomDataFromPacket()

template<class DTypeName >
virtual void Leviathan::SyncedPrimitive< DTypeName >::UpdateCustomDataFromPacket ( Lock guard,
sf::Packet &  packet 
)
inlineoverrideprotectedvirtual

Should load the custom data from a packet.

Implements Leviathan::SyncedResource.

Definition at line 171 of file SyncedResource.h.

171  {
172  // The object is already locked at this point //
173 
174  // Try to get our variable //
175  if(!(packet >> OurValue)){
176 
177  throw InvalidArgument("resource sync primitive packet has invalid format");
178  }
179 
180  }
DTypeName OurValue
The primitive object owned by this.

◆ UpdateValue()

template<class DTypeName >
void Leviathan::SyncedPrimitive< DTypeName >::UpdateValue ( const DTypeName &  newvalue)
inline

Updates the value and notifies SyncedVariables.

Note
This does not call the callback or OnValueUpdated. They are only called when receiving updates through network

Definition at line 121 of file SyncedResource.h.

121  {
122 
123  GUARD_LOCK();
124  // Update our value //
125  OurValue = newvalue;
126 
127  UpdateOurNetworkValue(guard);
128  }
DTypeName OurValue
The primitive object owned by this.
virtual DLLEXPORT void UpdateOurNetworkValue(Lock &guard)
Notifies our SyncedVariables of an update.
#define GUARD_LOCK()
Definition: ThreadSafe.h:91

Member Data Documentation

◆ IsValid

template<class DTypeName >
bool Leviathan::SyncedPrimitive< DTypeName >::IsValid
protected

Little overhead but this is important to discard update requests after stopping.

Definition at line 189 of file SyncedResource.h.

◆ OurValue

template<class DTypeName >
DTypeName Leviathan::SyncedPrimitive< DTypeName >::OurValue
protected

The primitive object owned by this.

Definition at line 195 of file SyncedResource.h.

◆ ValueUpdateCallback

template<class DTypeName >
CallbackPtr Leviathan::SyncedPrimitive< DTypeName >::ValueUpdateCallback
protected

This is quite an important feature to store this function pointer.

Definition at line 192 of file SyncedResource.h.


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