#pragma once #include "NetObjID.hpp" #include "NetObjManager.hpp" #include "NetObjOwnerPtr.decl.hpp" #include "NetObjTemporaryPtr.decl.hpp" #include namespace NetworkingObjects { template class NetObjWeakPtr { public: NetObjWeakPtr(): objMan(nullptr), id({0, 0}) {} NetObjWeakPtr(const NetObjWeakPtr& other): objMan(other.objMan), id(other.id) {} NetObjWeakPtr(const NetObjOwnerPtr& other): objMan(other.objMan), id(other.id) {} NetObjWeakPtr(const NetObjTemporaryPtr& other): objMan(other.objMan), id(other.id) {} NetObjWeakPtr& operator=(const NetObjOwnerPtr& other) { objMan = other.objMan; id = other.id; return *this; } NetObjWeakPtr& operator=(const NetObjTemporaryPtr& other) { objMan = other.objMan; id = other.id; return *this; } NetObjWeakPtr& operator=(const NetObjWeakPtr& other) { objMan = other.objMan; id = other.id; return *this; } template NetObjWeakPtr cast() const { return NetObjWeakPtr(objMan, id); } bool expired() const { return !objMan || objMan->objectData.contains(id); } NetObjTemporaryPtr lock() const { if(!objMan) return NetObjTemporaryPtr(); auto it = objMan->objectData.find(id); if(it == objMan->objectData.end()) return NetObjTemporaryPtr(); return NetObjTemporaryPtr(objMan, id, static_cast(it->second.p)); } NetObjManager* get_obj_man() const { return objMan; } NetObjID get_net_id() const { return id; } bool operator!=(const NetObjWeakPtr& rhs) const { return objMan != rhs.objMan || id != rhs.id; } bool operator==(const NetObjWeakPtr& rhs) const { return objMan == rhs.objMan && id == rhs.id; } private: NetObjWeakPtr(NetObjManager* initObjMan, NetObjID initID): objMan(initObjMan), id(initID) {} NetObjManager* objMan; NetObjID id; }; }