From 247aa498499db8ef1081b047fa80d7bdb85fbd9c Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Tue, 6 May 2014 16:06:33 +0200 Subject: [PATCH] SGSharedPtr/SGWeakPtr: add some methods/operators - allow placing SGWeakPtr in sorted STL containers (eg. requiring operator<) - add reset() like for boost::shared_ptr/boost::weak_ptr - add helper to extract pointer from SGWeakPtr --- simgear/nasal/cppbind/Ghost.hxx | 6 ++++++ simgear/structure/SGSharedPtr.hxx | 25 +++++++++++++------------ simgear/structure/SGWeakPtr.hxx | 23 ++++++++++++++++++++--- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/simgear/nasal/cppbind/Ghost.hxx b/simgear/nasal/cppbind/Ghost.hxx index fa03fba8..d3484901 100644 --- a/simgear/nasal/cppbind/Ghost.hxx +++ b/simgear/nasal/cppbind/Ghost.hxx @@ -44,6 +44,12 @@ inline T* get_pointer(boost::weak_ptr const& p) return p.lock().get(); } +template +inline T* get_pointer(SGWeakPtr const& p) +{ + return p.lock().get(); +} + /** * Bindings between C++ and the Nasal scripting language */ diff --git a/simgear/structure/SGSharedPtr.hxx b/simgear/structure/SGSharedPtr.hxx index 081d5eb9..aedd8463 100644 --- a/simgear/structure/SGSharedPtr.hxx +++ b/simgear/structure/SGSharedPtr.hxx @@ -22,6 +22,7 @@ #define SGSharedPtr_HXX #include "SGReferenced.hxx" +#include /// This class is a pointer proxy doing reference counting on the object /// it is pointing to. @@ -60,16 +61,16 @@ public: SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get()) { get(_ptr); } ~SGSharedPtr(void) - { put(); } + { reset(); } SGSharedPtr& operator=(const SGSharedPtr& p) - { assign(p.get()); return *this; } + { reset(p.get()); return *this; } template SGSharedPtr& operator=(const SGSharedPtr& p) - { assign(p.get()); return *this; } + { reset(p.get()); return *this; } template SGSharedPtr& operator=(U* p) - { assign(p); return *this; } + { reset(p); return *this; } T* operator->(void) const { return _ptr; } @@ -83,6 +84,10 @@ public: { return _ptr; } T* release() { T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; } + void reset() + { if (!T::put(_ptr)) delete _ptr; _ptr = 0; } + void reset(T* p) + { SGSharedPtr(p).swap(*this); } bool isShared(void) const { return T::shared(_ptr); } @@ -93,20 +98,16 @@ public: { return _ptr != (T*)0; } void clear() - { put(); } - void swap(SGSharedPtr& sharedPtr) - { T* tmp = _ptr; _ptr = sharedPtr._ptr; sharedPtr._ptr = tmp; } + { reset(); } + void swap(SGSharedPtr& other) + { std::swap(_ptr, other._ptr); } private: - void assign(T* p) - { get(p); put(); _ptr = p; } void assignNonRef(T* p) - { put(); _ptr = p; } + { reset(); _ptr = p; } void get(const T* p) const { T::get(p); } - void put(void) - { if (!T::put(_ptr)) delete _ptr; _ptr = 0; } // The reference itself. T* _ptr; diff --git a/simgear/structure/SGWeakPtr.hxx b/simgear/structure/SGWeakPtr.hxx index 78cfab4a..9dd2650a 100644 --- a/simgear/structure/SGWeakPtr.hxx +++ b/simgear/structure/SGWeakPtr.hxx @@ -23,6 +23,8 @@ template class SGWeakPtr { public: + typedef T element_type; + SGWeakPtr(void) { } SGWeakPtr(const SGWeakPtr& p) : mWeakData(p.mWeakData) @@ -31,7 +33,7 @@ public: { assign(ptr); } template SGWeakPtr(const SGSharedPtr& p) - { SGSharedPtr sharedPtr = p; assign(sharedPtr.get()); } + { assign(p.get()); } template SGWeakPtr(const SGWeakPtr& p) { SGSharedPtr sharedPtr = p.lock(); assign(sharedPtr.get()); } @@ -40,13 +42,23 @@ public: template SGWeakPtr& operator=(const SGSharedPtr& p) - { SGSharedPtr sharedPtr = p; assign(sharedPtr.get()); return *this; } + { assign(p.get()); return *this; } template SGWeakPtr& operator=(const SGWeakPtr& p) { SGSharedPtr sharedPtr = p.lock(); assign(sharedPtr.get()); return *this; } SGWeakPtr& operator=(const SGWeakPtr& p) { mWeakData = p.mWeakData; return *this; } + template + bool operator==(const SGWeakPtr& rhs) const + { return mWeakData == rhs.mWeakData; } + template + bool operator!=(const SGWeakPtr& rhs) const + { return mWeakData != rhs.mWeakData; } + template + bool operator<(const SGWeakPtr& rhs) const + { return mWeakData < rhs.mWeakData; } + SGSharedPtr lock(void) const { if (!mWeakData) @@ -56,8 +68,13 @@ public: return sharedPtr; } + bool expired() const + { return !mWeakData || mWeakData->mRefcount == 0; } + + void reset() + { mWeakData.reset(); } void clear() - { mWeakData = 0; } + { mWeakData.reset(); } void swap(SGWeakPtr& weakPtr) { mWeakData.swap(weakPtr.mWeakData); } -- 2.39.5