Add wxWindowPtr smart pointer.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74774 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík 2013-09-06 17:09:16 +00:00
parent 4852df90e2
commit c80d4c1e20
10 changed files with 167 additions and 1 deletions

View File

@ -4159,6 +4159,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \
wx/valnum.h \ wx/valnum.h \
wx/window.h \ wx/window.h \
wx/windowid.h \ wx/windowid.h \
wx/windowptr.h \
wx/withimages.h \ wx/withimages.h \
wx/wrapsizer.h \ wx/wrapsizer.h \
wx/wupdlock.h \ wx/wupdlock.h \

View File

@ -907,6 +907,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
wx/valnum.h wx/valnum.h
wx/window.h wx/window.h
wx/windowid.h wx/windowid.h
wx/windowptr.h
wx/withimages.h wx/withimages.h
wx/wrapsizer.h wx/wrapsizer.h
wx/wupdlock.h wx/wupdlock.h

View File

@ -3304,6 +3304,10 @@ SOURCE=..\..\include\wx\windowid.h
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\include\wx\windowptr.h
# End Source File
# Begin Source File
SOURCE=..\..\include\wx\withimages.h SOURCE=..\..\include\wx\withimages.h
# End Source File # End Source File
# Begin Source File # Begin Source File

View File

@ -1234,6 +1234,7 @@
<ClInclude Include="..\..\include\wx\persist\window.h" /> <ClInclude Include="..\..\include\wx\persist\window.h" />
<ClInclude Include="..\..\include\wx\window.h" /> <ClInclude Include="..\..\include\wx\window.h" />
<ClInclude Include="..\..\include\wx\windowid.h" /> <ClInclude Include="..\..\include\wx\windowid.h" />
<ClInclude Include="..\..\include\wx\windowptr.h" />
<ClInclude Include="..\..\include\wx\withimages.h" /> <ClInclude Include="..\..\include\wx\withimages.h" />
<ClInclude Include="..\..\include\wx\wizard.h" /> <ClInclude Include="..\..\include\wx\wizard.h" />
<ClInclude Include="..\..\include\wx\wrapsizer.h" /> <ClInclude Include="..\..\include\wx\wrapsizer.h" />
@ -1309,4 +1310,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
</Project> </Project>

View File

@ -2524,6 +2524,9 @@
<ClInclude Include="..\..\include\wx\xpmhand.h"> <ClInclude Include="..\..\include\wx\xpmhand.h">
<Filter>Common Headers</Filter> <Filter>Common Headers</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\wx\windowptr.h">
<Filter>Common Headers</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="..\..\include\wx\msw\setup.h"> <CustomBuild Include="..\..\include\wx\msw\setup.h">

View File

@ -2663,6 +2663,9 @@
<File <File
RelativePath="..\..\include\wx\windowid.h"> RelativePath="..\..\include\wx\windowid.h">
</File> </File>
<File
RelativePath="..\..\include\wx\windowptr.h">
</File>
<File <File
RelativePath="..\..\include\wx\withimages.h"> RelativePath="..\..\include\wx\withimages.h">
</File> </File>

View File

@ -3559,6 +3559,10 @@
RelativePath="..\..\include\wx\windowid.h" RelativePath="..\..\include\wx\windowid.h"
> >
</File> </File>
<File
RelativePath="..\..\include\wx\windowptr.h"
>
</File>
<File <File
RelativePath="..\..\include\wx\withimages.h" RelativePath="..\..\include\wx\withimages.h"
> >

View File

@ -3557,6 +3557,10 @@
RelativePath="..\..\include\wx\windowid.h" RelativePath="..\..\include\wx\windowid.h"
> >
</File> </File>
<File
RelativePath="..\..\include\wx\windowptr.h"
>
</File>
<File <File
RelativePath="..\..\include\wx\withimages.h" RelativePath="..\..\include\wx\withimages.h"
> >

63
include/wx/windowptr.h Normal file
View File

@ -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_

82
interface/wx/windowptr.h Normal file
View File

@ -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);
};