]> git.saurik.com Git - wxWidgets.git/commitdiff
Add wxWindowPtr smart pointer.
authorVáclav Slavík <vslavik@fastmail.fm>
Fri, 6 Sep 2013 17:09:16 +0000 (17:09 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Fri, 6 Sep 2013 17:09:16 +0000 (17:09 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74774 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

Makefile.in
build/bakefiles/files.bkl
build/msw/wx_core.dsp
build/msw/wx_vc10_core.vcxproj
build/msw/wx_vc10_core.vcxproj.filters
build/msw/wx_vc7_core.vcproj
build/msw/wx_vc8_core.vcproj
build/msw/wx_vc9_core.vcproj
include/wx/windowptr.h [new file with mode: 0644]
interface/wx/windowptr.h [new file with mode: 0644]

index fb4ad2ac3b4ba88943b2818664d2f368b5ef4ff1..196729f64ed48f8270ac53c0b56aad4ea8b191f4 100644 (file)
@@ -4159,6 +4159,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS =  \
        wx/valnum.h \
        wx/window.h \
        wx/windowid.h \
        wx/valnum.h \
        wx/window.h \
        wx/windowid.h \
+       wx/windowptr.h \
        wx/withimages.h \
        wx/wrapsizer.h \
        wx/wupdlock.h \
        wx/withimages.h \
        wx/wrapsizer.h \
        wx/wupdlock.h \
index 457f8d3e99d5a8b2fdae940bddc67c8fdd31f776..3a032c6016f06d3dc78871fb24be8ed6d2cd85e4 100644 (file)
@@ -907,6 +907,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
     wx/valnum.h
     wx/window.h
     wx/windowid.h
     wx/valnum.h
     wx/window.h
     wx/windowid.h
+    wx/windowptr.h
     wx/withimages.h
     wx/wrapsizer.h
     wx/wupdlock.h
     wx/withimages.h
     wx/wrapsizer.h
     wx/wupdlock.h
index 0809bd9c85c7e8611154c2121cf5ee4c73c8c9b1..0b9256399fe2e1b3378abd908f0e122cc1574188 100644 (file)
@@ -3304,6 +3304,10 @@ SOURCE=..\..\include\wx\windowid.h
 # End Source File\r
 # Begin Source File\r
 \r
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=..\..\include\wx\windowptr.h\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=..\..\include\wx\withimages.h\r
 # End Source File\r
 # Begin Source File\r
 SOURCE=..\..\include\wx\withimages.h\r
 # End Source File\r
 # Begin Source File\r
index 14bb733b92e929269cfc2e4c47e54f43feb2255f..4cb486a44cee85ebc03f855386b2752e1af460c7 100644 (file)
     <ClInclude Include="..\..\include\wx\persist\window.h" />\r
     <ClInclude Include="..\..\include\wx\window.h" />\r
     <ClInclude Include="..\..\include\wx\windowid.h" />\r
     <ClInclude Include="..\..\include\wx\persist\window.h" />\r
     <ClInclude Include="..\..\include\wx\window.h" />\r
     <ClInclude Include="..\..\include\wx\windowid.h" />\r
+    <ClInclude Include="..\..\include\wx\windowptr.h" />\r
     <ClInclude Include="..\..\include\wx\withimages.h" />\r
     <ClInclude Include="..\..\include\wx\wizard.h" />\r
     <ClInclude Include="..\..\include\wx\wrapsizer.h" />\r
     <ClInclude Include="..\..\include\wx\withimages.h" />\r
     <ClInclude Include="..\..\include\wx\wizard.h" />\r
     <ClInclude Include="..\..\include\wx\wrapsizer.h" />\r
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
   <ImportGroup Label="ExtensionTargets">\r
   </ImportGroup>\r
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
   <ImportGroup Label="ExtensionTargets">\r
   </ImportGroup>\r
-</Project>
\ No newline at end of file
+</Project>
index a955d48999cf6105135e5af7371bf612d785638a..1860cb5811a56183a63b3939aee72f3301f9ae0a 100644 (file)
     <ClInclude Include="..\..\include\wx\xpmhand.h">\r
       <Filter>Common Headers</Filter>\r
     </ClInclude>\r
     <ClInclude Include="..\..\include\wx\xpmhand.h">\r
       <Filter>Common Headers</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="..\..\include\wx\windowptr.h">\r
+      <Filter>Common Headers</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <CustomBuild Include="..\..\include\wx\msw\setup.h">\r
   </ItemGroup>\r
   <ItemGroup>\r
     <CustomBuild Include="..\..\include\wx\msw\setup.h">\r
index b8c6091d380e3ccd64370ac88a56601fbe1e7d62..2ae0818ed458c9a9117d64cc8d54d96b464ec0be 100644 (file)
                        <File\r
                                RelativePath="..\..\include\wx\windowid.h">\r
                        </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\windowid.h">\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\windowptr.h">\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h">\r
                        </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h">\r
                        </File>\r
index 3ec17d668a6af79710a459787f4927c7e7b6e614..e495176b1b54d9e62a318f78ff6d30dff79b7910 100644 (file)
                                RelativePath="..\..\include\wx\windowid.h"\r
                                >\r
                        </File>\r
                                RelativePath="..\..\include\wx\windowid.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\windowptr.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h"\r
                                >\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h"\r
                                >\r
index 128f72bc8dbeb6a22d2c53c659f47ef1365b5bc9..a4abc6a74274029193575543d9ba84694b99f0ec 100644 (file)
                                RelativePath="..\..\include\wx\windowid.h"\r
                                >\r
                        </File>\r
                                RelativePath="..\..\include\wx\windowid.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\windowptr.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h"\r
                                >\r
                        <File\r
                                RelativePath="..\..\include\wx\withimages.h"\r
                                >\r
diff --git a/include/wx/windowptr.h b/include/wx/windowptr.h
new file mode 100644 (file)
index 0000000..bebcf6a
--- /dev/null
@@ -0,0 +1,63 @@
+/////////////////////////////////////////////////////////////////////////////
+// Name:        wx/windowptr.h
+// Purpose:     smart pointer for holding wxWindow instances
+// Author:      Vaclav Slavik
+// Created:     2013-09-01
+// Copyright:   (c) 2013 Vaclav Slavik
+// Licence:     wxWindows licence
+/////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_WINDOWPTR_H_
+#define _WX_WINDOWPTR_H_
+
+#include "wx/sharedptr.h"
+
+// ----------------------------------------------------------------------------
+// wxWindowPtr: A smart pointer with correct wxWindow destruction.
+// ----------------------------------------------------------------------------
+
+namespace wxPrivate
+{
+
+struct wxWindowDeleter
+{
+    void operator()(wxWindow *win)
+    {
+        win->Destroy();
+    }
+};
+
+} // namespace wxPrivate
+
+template<typename T>
+class wxWindowPtr : public wxSharedPtr<T>
+{
+public:
+    typedef T element_type;
+
+    wxEXPLICIT wxWindowPtr(element_type* win)
+        : wxSharedPtr<T>(win, wxPrivate::wxWindowDeleter())
+    {
+    }
+
+    wxWindowPtr() {}
+    wxWindowPtr(const wxWindowPtr& tocopy) : wxSharedPtr<T>(tocopy) {}
+
+    wxWindowPtr& operator=(const wxWindowPtr& tocopy)
+    {
+        wxSharedPtr<T>::operator=(tocopy);
+        return *this;
+    }
+
+    wxWindowPtr& operator=(element_type* win)
+    {
+        return operator=(wxWindowPtr(win));
+    }
+
+    void reset(T* ptr = NULL)
+    {
+        wxSharedPtr<T>::reset(ptr, wxPrivate::wxWindowDeleter());
+    }
+};
+
+#endif // _WX_WINDOWPTR_H_
diff --git a/interface/wx/windowptr.h b/interface/wx/windowptr.h
new file mode 100644 (file)
index 0000000..11e7b6f
--- /dev/null
@@ -0,0 +1,82 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        interface/wx/windowptr.h
+// Purpose:     wxWindowPtr<T> class documentation.
+// Author:      Vaclav Slavik
+// Created:     2013-09-02
+// Copyright:   (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+    A reference-counted smart pointer for holding wxWindow instances.
+
+    This specialization of wxSharedPtr<T> is useful for holding
+    wxWindow-derived objects. Unlike wxSharedPtr<T> or @c std::shared_ptr<>, it
+    doesn't use the delete operator to destroy the value when reference count
+    drops to zero, but calls wxWindow::Destroy() to safely destroy the window.
+
+    The template parameter T must be wxWindow or a class derived from it.
+
+    @library{wxcore}
+    @category{smartpointers}
+
+    @since 3.0
+
+    @see wxSharedPtr<T>
+*/
+template<typename T>
+class wxWindowPtr<T> : public wxSharedPtr<T>
+{
+public:
+    /// Default constructor.
+    wxWindowPtr();
+
+    /**
+        Constructor.
+
+        Creates shared pointer from the raw pointer @a ptr and takes ownership
+        of it.
+    */
+    explicit wxWindowPtr(T* ptr);
+
+    /**
+        Constructor.
+
+        Creates shared pointer from the raw pointer @a ptr and deleter @a d
+        and takes ownership of it.
+
+        @param ptr  The raw pointer.
+        @param d    Deleter - a functor that is called instead of delete to
+                    free the @a ptr raw pointer when its reference count drops to
+                    zero.
+
+    */
+    template<typename Deleter>
+    explicit wxWindowPtr(T* ptr, Deleter d);
+
+    /// Copy constructor.
+    wxWindowPtr(const wxWindowPtr<T>& tocopy);
+
+    /**
+        Assignment operator.
+
+        Releases any previously held pointer and creates a reference to @a ptr.
+    */
+    wxWindowPtr<T>& operator=(T* ptr);
+
+    /**
+        Assignment operator.
+
+        Releases any previously held pointer and creates a reference to the
+        same object as @a topcopy.
+    */
+    wxWindowPtr<T>& operator=(const wxWindowPtr<T>& tocopy);
+
+    /**
+        Reset pointer to @a ptr.
+
+        If the reference count of the previously owned pointer was 1 it will be deleted.
+    */
+    void reset(T* ptr = NULL);
+};
+