From 00febf68ad8bc36546c02b0c0b926189fb3955a1 Mon Sep 17 00:00:00 2001 From: LogicParrot Date: Wed, 30 Aug 2017 00:13:25 +0300 Subject: Much safer raw pointers to entities --- src/Mobs/MobPointer.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/Mobs/MobPointer.h (limited to 'src/Mobs/MobPointer.h') diff --git a/src/Mobs/MobPointer.h b/src/Mobs/MobPointer.h new file mode 100644 index 000000000..109b0511e --- /dev/null +++ b/src/Mobs/MobPointer.h @@ -0,0 +1,34 @@ +#pragma once + +/** This class allows mobs to hold pointers to other mobs/players in a safe manner. +When calling GetPointer(), it first checks if the Monster/Player being pointed to is still valid. +If not, it returns a nullptr. The returned raw pointer must be used locally and then discarded. +it MUST NOT be preserved across ticks. +*/ + +class cPawn; +class cMobPointer +{ +public: + cMobPointer(cPawn * a_Pointer); // Constructor + cMobPointer(const cMobPointer & a_MobPointer); // Copy constructor + cMobPointer(cMobPointer && a_MobPointer); // Move constructor + cMobPointer& operator=(const cMobPointer& a_MobPointer); // Copy assignment operator + cMobPointer& operator=(cMobPointer&& a_MobPointer); // Move assignment operator + + void operator=(cPawn * a_Pointer); // set Pointer + + /** Returns the raw pointer. The returned raw pointer must + be used locally and then discarded. it MUST NOT be preserved across ticks. + Returns null if raw pointer is null. Returns null if mob is destroyed or moved worlds. + Must be called at least once per tick, even if the raw pointer is not going to be used that tick. */ + cPawn * GetPointer(); + + +private: + cPawn * m_Pointer; +} ; + + + + -- cgit v1.2.3