Browse Source

Merge remote-tracking branch 'AntonPetrov83/sharedptr-implicit-conversion'

Lasse Öörni 9 years ago
parent
commit
67a7c3f2bc
2 changed files with 26 additions and 7 deletions
  1. 24 5
      Source/Urho3D/Container/Ptr.h
  2. 2 2
      Source/Urho3D/Graphics/Renderer.cpp

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

@@ -47,6 +47,13 @@ public:
         AddRef();
         AddRef();
     }
     }
 
 
+    /// Copy-construct from another shared pointer allowing implicit upcasting.
+    template <class U> SharedPtr(const SharedPtr<U>& rhs) :
+        ptr_(rhs.ptr_)
+    {
+        AddRef();
+    }
+
     /// Construct from a raw pointer.
     /// Construct from a raw pointer.
     explicit SharedPtr(T* ptr) :
     explicit SharedPtr(T* ptr) :
         ptr_(ptr)
         ptr_(ptr)
@@ -73,6 +80,19 @@ public:
         return *this;
         return *this;
     }
     }
 
 
+    /// Assign from another shared pointer allowing implicit upcasting.
+    template <class U> SharedPtr<T>& operator =(const SharedPtr<U>& rhs)
+    {
+        if (ptr_ == rhs.ptr_)
+            return *this;
+
+        ReleaseRef();
+        ptr_ = rhs.ptr_;
+        AddRef();
+
+        return *this;
+    }
+
     /// Assign from a raw pointer.
     /// Assign from a raw pointer.
     SharedPtr<T>& operator =(T* ptr)
     SharedPtr<T>& operator =(T* ptr)
     {
     {
@@ -108,13 +128,13 @@ public:
     }
     }
 
 
     /// Test for less than with another shared pointer.
     /// Test for less than with another shared pointer.
-    bool operator <(const SharedPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
+    template <class U> bool operator <(const SharedPtr<U>& rhs) const { return ptr_ < rhs.ptr_; }
 
 
     /// Test for equality with another shared pointer.
     /// Test for equality with another shared pointer.
-    bool operator ==(const SharedPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
+    template <class U> bool operator ==(const SharedPtr<U>& rhs) const { return ptr_ == rhs.ptr_; }
 
 
     /// Test for inequality with another shared pointer.
     /// Test for inequality with another shared pointer.
-    bool operator !=(const SharedPtr<T>& rhs) const { return ptr_ != rhs.ptr_; }
+    template <class U> bool operator !=(const SharedPtr<U>& rhs) const { return ptr_ != rhs.ptr_; }
 
 
     /// Convert to a raw pointer.
     /// Convert to a raw pointer.
     operator T*() const { return ptr_; }
     operator T*() const { return ptr_; }
@@ -172,8 +192,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 shared pointer of another type.
-    template <class U> SharedPtr<T>& operator =(const SharedPtr<U>& rhs);
+	template <class U> friend class SharedPtr;
 
 
     /// Add a reference to the object pointed to.
     /// Add a reference to the object pointed to.
     void AddRef()
     void AddRef()

+ 2 - 2
Source/Urho3D/Graphics/Renderer.cpp

@@ -1046,14 +1046,14 @@ Texture* Renderer::GetScreenBuffer(int width, int height, unsigned format, bool
             }
             }
 #endif
 #endif
 
 
-            newBuffer = StaticCast<Texture>(newTex2D);
+            newBuffer = newTex2D;
         }
         }
         else
         else
         {
         {
             SharedPtr<TextureCube> newTexCube(new TextureCube(context_));
             SharedPtr<TextureCube> newTexCube(new TextureCube(context_));
             newTexCube->SetSize(width, format, TEXTURE_RENDERTARGET);
             newTexCube->SetSize(width, format, TEXTURE_RENDERTARGET);
 
 
-            newBuffer = StaticCast<Texture>(newTexCube);
+            newBuffer = newTexCube;
         }
         }
 
 
         newBuffer->SetSRGB(srgb);
         newBuffer->SetSRGB(srgb);