Browse Source

Similar implicit conversion for WeakPtr.

Lasse Öörni 9 years ago
parent
commit
9fa87d54b6
1 changed files with 26 additions and 5 deletions
  1. 26 5
      Source/Urho3D/Container/Ptr.h

+ 26 - 5
Source/Urho3D/Container/Ptr.h

@@ -250,6 +250,14 @@ public:
         AddRef();
         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.
     /// Construct from a shared pointer.
     WeakPtr(const SharedPtr<T>& rhs) :
     WeakPtr(const SharedPtr<T>& rhs) :
         ptr_(rhs.Get()),
         ptr_(rhs.Get()),
@@ -300,6 +308,20 @@ public:
         return *this;
         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.
     /// Assign from a raw pointer.
     WeakPtr<T>& operator =(T* ptr)
     WeakPtr<T>& operator =(T* ptr)
     {
     {
@@ -359,13 +381,13 @@ public:
     }
     }
 
 
     /// Test for equality with another weak pointer.
     /// 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.
     /// 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.
     /// 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.
     /// Convert to a raw pointer, null if the object is expired.
     operator T*() const { return Get(); }
     operator T*() const { return Get(); }
@@ -425,8 +447,7 @@ public:
     unsigned ToHash() const { return (unsigned)((size_t)ptr_ / sizeof(T)); }
     unsigned ToHash() const { return (unsigned)((size_t)ptr_ / sizeof(T)); }
 
 
 private:
 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.
     /// Add a weak reference to the object pointed to.
     void AddRef()
     void AddRef()