From 04bbb844aede17328df80605a05ac98e23fa6f0d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 30 Dec 2020 01:02:47 +0100 Subject: [PATCH] Allow constructing/assigning wxObjectDataPtr from compatible type Generalize copy ctor and assignment operators to allow implicit conversions from wxObjectDataPtr to wxObjectDataPtr if D is implicitly convertible to B (e.g. if B is the base class and D is a class derived from it). This makes wxObjectDataPtr<> more like standard smart pointer classes and more useful. --- include/wx/object.h | 20 ++++++++++++++++++++ interface/wx/object.h | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/wx/object.h b/include/wx/object.h index dd1e2fb927..e4baee1396 100644 --- a/include/wx/object.h +++ b/include/wx/object.h @@ -278,6 +278,15 @@ public: m_ptr->IncRef(); } + // generalized copy ctor: U must be convertible to T + template + wxObjectDataPtr(const wxObjectDataPtr &tocopy) + : m_ptr(tocopy.get()) + { + if (m_ptr) + m_ptr->IncRef(); + } + ~wxObjectDataPtr() { if (m_ptr) @@ -330,6 +339,17 @@ public: return *this; } + template + wxObjectDataPtr& operator=(const wxObjectDataPtr &tocopy) + { + if (m_ptr) + m_ptr->DecRef(); + m_ptr = tocopy.get(); + if (m_ptr) + m_ptr->IncRef(); + return *this; + } + wxObjectDataPtr& operator=(T *ptr) { if (m_ptr) diff --git a/interface/wx/object.h b/interface/wx/object.h index 16d4905e74..dc290db79c 100644 --- a/interface/wx/object.h +++ b/interface/wx/object.h @@ -585,11 +585,17 @@ public: */ wxObjectDataPtr(T* ptr = NULL); + //@{ /** This copy constructor increases the count of the reference counted object to which @a tocopy points and then this class will point to, as well. + + Using @a U different from @c T is only supported since wxWidgets 3.1.5. */ + template + wxObjectDataPtr(const wxObjectDataPtr& tocopy); wxObjectDataPtr(const wxObjectDataPtr& tocopy); + //@} /** @@ -649,7 +655,11 @@ public: //@{ /** Assignment operator. + + Using @a U different from @c T is only supported since wxWidgets 3.1.5. */ + template + wxObjectDataPtr& operator=(const wxObjectDataPtr& tocopy); wxObjectDataPtr& operator=(const wxObjectDataPtr& tocopy); wxObjectDataPtr& operator=(T* ptr); //@}