|
|
@@ -250,6 +250,14 @@ public:
|
|
|
AddRef();
|
|
|
}
|
|
|
|
|
|
+ /// Copy-construct from another weak pointer allowing implicit upcasting.
|
|
|
+ template <class U> WeakPtr(const WeakPtr<U>& rhs) :
|
|
|
+ ptr_(rhs.ptr_),
|
|
|
+ refCount_(rhs.refCount_)
|
|
|
+ {
|
|
|
+ AddRef();
|
|
|
+ }
|
|
|
+
|
|
|
/// Construct from a shared pointer.
|
|
|
WeakPtr(const SharedPtr<T>& rhs) :
|
|
|
ptr_(rhs.Get()),
|
|
|
@@ -300,6 +308,20 @@ public:
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
+ /// Assign from another weak pointer allowing implicit upcasting.
|
|
|
+ template <class U> WeakPtr<T>& operator =(const WeakPtr<U>& rhs)
|
|
|
+ {
|
|
|
+ if (ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_)
|
|
|
+ return *this;
|
|
|
+
|
|
|
+ ReleaseRef();
|
|
|
+ ptr_ = rhs.ptr_;
|
|
|
+ refCount_ = rhs.refCount_;
|
|
|
+ AddRef();
|
|
|
+
|
|
|
+ return *this;
|
|
|
+ }
|
|
|
+
|
|
|
/// Assign from a raw pointer.
|
|
|
WeakPtr<T>& operator =(T* ptr)
|
|
|
{
|
|
|
@@ -359,13 +381,13 @@ public:
|
|
|
}
|
|
|
|
|
|
/// Test for equality with another weak pointer.
|
|
|
- bool operator ==(const WeakPtr<T>& rhs) const { return ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_; }
|
|
|
+ template <class U> bool operator ==(const WeakPtr<U>& rhs) const { return ptr_ == rhs.ptr_ && refCount_ == rhs.refCount_; }
|
|
|
|
|
|
/// Test for inequality with another weak pointer.
|
|
|
- bool operator !=(const WeakPtr<T>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
|
|
|
+ template <class U> bool operator !=(const WeakPtr<U>& rhs) const { return ptr_ != rhs.ptr_ || refCount_ != rhs.refCount_; }
|
|
|
|
|
|
/// Test for less than with another weak pointer.
|
|
|
- bool operator <(const WeakPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
|
|
|
+ template <class U> bool operator <(const WeakPtr<U>& rhs) const { return ptr_ < rhs.ptr_; }
|
|
|
|
|
|
/// Convert to a raw pointer, null if the object is expired.
|
|
|
operator T*() const { return Get(); }
|
|
|
@@ -425,8 +447,7 @@ public:
|
|
|
unsigned ToHash() const { return (unsigned)((size_t)ptr_ / sizeof(T)); }
|
|
|
|
|
|
private:
|
|
|
- /// Prevent direct assignment from a weak pointer of different type.
|
|
|
- template <class U> WeakPtr<T>& operator =(const WeakPtr<U>& rhs);
|
|
|
+ template <class U> friend class WeakPtr;
|
|
|
|
|
|
/// Add a weak reference to the object pointed to.
|
|
|
void AddRef()
|