From c80d4c1e207b0011db61cb2ce1cc8babe8e54582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Fri, 6 Sep 2013 17:09:16 +0000 Subject: [PATCH] Add wxWindowPtr smart pointer. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74774 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- Makefile.in | 1 + build/bakefiles/files.bkl | 1 + build/msw/wx_core.dsp | 4 ++ build/msw/wx_vc10_core.vcxproj | 3 +- build/msw/wx_vc10_core.vcxproj.filters | 3 + build/msw/wx_vc7_core.vcproj | 3 + build/msw/wx_vc8_core.vcproj | 4 ++ build/msw/wx_vc9_core.vcproj | 4 ++ include/wx/windowptr.h | 63 ++++++++++++++++++++ interface/wx/windowptr.h | 82 ++++++++++++++++++++++++++ 10 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 include/wx/windowptr.h create mode 100644 interface/wx/windowptr.h diff --git a/Makefile.in b/Makefile.in index fb4ad2ac3b..196729f64e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -4159,6 +4159,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \ wx/valnum.h \ wx/window.h \ wx/windowid.h \ + wx/windowptr.h \ wx/withimages.h \ wx/wrapsizer.h \ wx/wupdlock.h \ diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 457f8d3e99..3a032c6016 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -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/windowptr.h wx/withimages.h wx/wrapsizer.h wx/wupdlock.h diff --git a/build/msw/wx_core.dsp b/build/msw/wx_core.dsp index 0809bd9c85..0b9256399f 100644 --- a/build/msw/wx_core.dsp +++ b/build/msw/wx_core.dsp @@ -3304,6 +3304,10 @@ SOURCE=..\..\include\wx\windowid.h # End Source File # Begin Source File +SOURCE=..\..\include\wx\windowptr.h +# End Source File +# Begin Source File + SOURCE=..\..\include\wx\withimages.h # End Source File # Begin Source File diff --git a/build/msw/wx_vc10_core.vcxproj b/build/msw/wx_vc10_core.vcxproj index 14bb733b92..4cb486a44c 100644 --- a/build/msw/wx_vc10_core.vcxproj +++ b/build/msw/wx_vc10_core.vcxproj @@ -1234,6 +1234,7 @@ + @@ -1309,4 +1310,4 @@ - \ No newline at end of file + diff --git a/build/msw/wx_vc10_core.vcxproj.filters b/build/msw/wx_vc10_core.vcxproj.filters index a955d48999..1860cb5811 100644 --- a/build/msw/wx_vc10_core.vcxproj.filters +++ b/build/msw/wx_vc10_core.vcxproj.filters @@ -2524,6 +2524,9 @@ Common Headers + + Common Headers + diff --git a/build/msw/wx_vc7_core.vcproj b/build/msw/wx_vc7_core.vcproj index b8c6091d38..2ae0818ed4 100644 --- a/build/msw/wx_vc7_core.vcproj +++ b/build/msw/wx_vc7_core.vcproj @@ -2663,6 +2663,9 @@ + + diff --git a/build/msw/wx_vc8_core.vcproj b/build/msw/wx_vc8_core.vcproj index 3ec17d668a..e495176b1b 100644 --- a/build/msw/wx_vc8_core.vcproj +++ b/build/msw/wx_vc8_core.vcproj @@ -3559,6 +3559,10 @@ RelativePath="..\..\include\wx\windowid.h" > + + diff --git a/build/msw/wx_vc9_core.vcproj b/build/msw/wx_vc9_core.vcproj index 128f72bc8d..a4abc6a742 100644 --- a/build/msw/wx_vc9_core.vcproj +++ b/build/msw/wx_vc9_core.vcproj @@ -3557,6 +3557,10 @@ RelativePath="..\..\include\wx\windowid.h" > + + diff --git a/include/wx/windowptr.h b/include/wx/windowptr.h new file mode 100644 index 0000000000..bebcf6ad8f --- /dev/null +++ b/include/wx/windowptr.h @@ -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 +class wxWindowPtr : public wxSharedPtr +{ +public: + typedef T element_type; + + wxEXPLICIT wxWindowPtr(element_type* win) + : wxSharedPtr(win, wxPrivate::wxWindowDeleter()) + { + } + + wxWindowPtr() {} + wxWindowPtr(const wxWindowPtr& tocopy) : wxSharedPtr(tocopy) {} + + wxWindowPtr& operator=(const wxWindowPtr& tocopy) + { + wxSharedPtr::operator=(tocopy); + return *this; + } + + wxWindowPtr& operator=(element_type* win) + { + return operator=(wxWindowPtr(win)); + } + + void reset(T* ptr = NULL) + { + wxSharedPtr::reset(ptr, wxPrivate::wxWindowDeleter()); + } +}; + +#endif // _WX_WINDOWPTR_H_ diff --git a/interface/wx/windowptr.h b/interface/wx/windowptr.h new file mode 100644 index 0000000000..11e7b6f3d5 --- /dev/null +++ b/interface/wx/windowptr.h @@ -0,0 +1,82 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: interface/wx/windowptr.h +// Purpose: wxWindowPtr class documentation. +// Author: Vaclav Slavik +// Created: 2013-09-02 +// Copyright: (c) 2013 Vaclav Slavik +// Licence: wxWindows licence +/////////////////////////////////////////////////////////////////////////////// + +/** + A reference-counted smart pointer for holding wxWindow instances. + + This specialization of wxSharedPtr is useful for holding + wxWindow-derived objects. Unlike wxSharedPtr 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 +*/ +template +class wxWindowPtr : public wxSharedPtr +{ +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 + explicit wxWindowPtr(T* ptr, Deleter d); + + /// Copy constructor. + wxWindowPtr(const wxWindowPtr& tocopy); + + /** + Assignment operator. + + Releases any previously held pointer and creates a reference to @a ptr. + */ + wxWindowPtr& operator=(T* ptr); + + /** + Assignment operator. + + Releases any previously held pointer and creates a reference to the + same object as @a topcopy. + */ + wxWindowPtr& operator=(const wxWindowPtr& 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); +}; +