From 4931e449989f74b9518d15bc69521fdbefb313e7 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 2 Sep 2016 12:34:02 -0700 Subject: Implement "weak pointer" behavior on Topology Path canonicalization hash table. --- node/SharedPtr.hpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'node/SharedPtr.hpp') diff --git a/node/SharedPtr.hpp b/node/SharedPtr.hpp index 3ff5ed18..1dd3b43d 100644 --- a/node/SharedPtr.hpp +++ b/node/SharedPtr.hpp @@ -119,15 +119,39 @@ public: inline T *ptr() const throw() { return _ptr; } /** - * Set this pointer to null + * Set this pointer to NULL */ inline void zero() { if (_ptr) { if (--_ptr->__refCount <= 0) delete _ptr; + _ptr = (T *)0; + } + } + + /** + * Set this pointer to NULL if this is the only pointer holding the object + * + * @return True if object was deleted and SharedPtr is now NULL (or was already NULL) + */ + inline bool reclaimIfWeak() + { + if (_ptr) { + if (++_ptr->__refCount <= 2) { + if (--_ptr->__refCount <= 1) { + delete _ptr; + _ptr = (T *)0; + return true; + } else { + return false; + } + } else { + return false; + } + } else { + return true; } - _ptr = (T *)0; } inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); } -- cgit v1.2.3