added template wxScopedArray<> too

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58460 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2009-01-27 16:40:51 +00:00
parent e5d28ed426
commit 058f225a44
3 changed files with 190 additions and 66 deletions

View File

@@ -260,8 +260,9 @@ Major new features in this release
- wxWidgets may now use either wchar_t (UTF-16/32) or UTF-8 internally, - wxWidgets may now use either wchar_t (UTF-16/32) or UTF-8 internally,
depending on what is optimal for the target platform. depending on what is optimal for the target platform.
- New propgrid library containing wxPropertyGrid and related classes, many - New propgrid library containing wxPropertyGrid and related classes.
enhancements to wxDataViewCtrl.
- Many enhancements to wxDataViewCtrl.
- Event loops, timers and sockets can now be used in wxBase, without GUI. - Event loops, timers and sockets can now be used in wxBase, without GUI.
@@ -312,7 +313,7 @@ All:
Linnakangas). Linnakangas).
- wxVariant::Unshare allows exclusive allocation of data that must be shared, - wxVariant::Unshare allows exclusive allocation of data that must be shared,
if the wxVariantData::Clone function is implemented. if the wxVariantData::Clone function is implemented.
- Added wxWeakRef<T>, wxScopedPtr<T>, wxSharedPtr<T> class templates - Added wxWeakRef<T>, wxScopedPtr<T>, wxScopedArray<T>, wxSharedPtr<T> templates.
- Added wxVector<T> class templates - Added wxVector<T> class templates
- Added wxON_BLOCK_EXIT_SET() and wxON_BLOCK_EXIT_NULL() to wx/scopeguard.h. - Added wxON_BLOCK_EXIT_SET() and wxON_BLOCK_EXIT_NULL() to wx/scopeguard.h.
- Added wxEvtHandler::QueueEvent() replacing AddPendingEvent() and - Added wxEvtHandler::QueueEvent() replacing AddPendingEvent() and
@@ -328,6 +329,7 @@ All:
- Added wxSHUTDOWN_LOGOFF and wxSHUTDOWN_FORCE wxShutdown() flags (troelsk). - Added wxSHUTDOWN_LOGOFF and wxSHUTDOWN_FORCE wxShutdown() flags (troelsk).
- Added wxSocket::ShutdownOutput(). - Added wxSocket::ShutdownOutput().
- Handle exceptions thrown from overridden wxView::OnCreate() gracefully. - Handle exceptions thrown from overridden wxView::OnCreate() gracefully.
- Added wxPATH_RMDIR_FULL/RECURSIVE wxFileName::Rmdir() flags (Marcin Malich).
- Added wxStandardPaths::GetAppDocumentsDir(). - Added wxStandardPaths::GetAppDocumentsDir().
All (Unix): All (Unix):

View File

@@ -2,10 +2,11 @@
// Name: wx/ptr_scpd.h // Name: wx/ptr_scpd.h
// Purpose: scoped smart pointer class // Purpose: scoped smart pointer class
// Author: Jesse Lovelace <jllovela@eos.ncsu.edu> // Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
// Modified by: // Modified by: Vadim Zeitlin to add template wxScopedArray
// Created: 06/01/02 // Created: 06/01/02
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) Jesse Lovelace and original Boost authors (see below) // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
// (c) 2009 Vadim Zeitlin
// Licence: wxWindows licence // Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@@ -37,23 +38,12 @@
template <class T> template <class T>
class wxScopedPtr class wxScopedPtr
{ {
private:
T * m_ptr;
wxScopedPtr(wxScopedPtr const &);
wxScopedPtr & operator=(wxScopedPtr const &);
public: public:
typedef T element_type; typedef T element_type;
wxEXPLICIT wxScopedPtr(T * ptr = NULL) wxEXPLICIT wxScopedPtr(T * ptr = NULL) : m_ptr(ptr) { }
: m_ptr(ptr) { }
~wxScopedPtr() ~wxScopedPtr() { delete m_ptr; }
{
if (m_ptr)
delete m_ptr;
}
// test for pointer validity: defining conversion to unspecified_bool_type // test for pointer validity: defining conversion to unspecified_bool_type
// and not more obvious bool to avoid implicit conversions to integer types // and not more obvious bool to avoid implicit conversions to integer types
@@ -65,7 +55,7 @@ public:
void reset(T * ptr = NULL) void reset(T * ptr = NULL)
{ {
if (m_ptr != ptr) if ( ptr != m_ptr )
{ {
delete m_ptr; delete m_ptr;
m_ptr = ptr; m_ptr = ptr;
@@ -96,12 +86,65 @@ public:
return m_ptr; return m_ptr;
} }
void swap(wxScopedPtr & ot) void swap(wxScopedPtr& other)
{ {
T * tmp = ot.m_ptr; T * const tmp = other.m_ptr;
ot.m_ptr = m_ptr; other.m_ptr = m_ptr;
m_ptr = tmp; m_ptr = tmp;
} }
private:
T * m_ptr;
DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T)
};
// ----------------------------------------------------------------------------
// wxScopedArray: A scoped array
// ----------------------------------------------------------------------------
template <class T>
class wxScopedArray
{
public:
typedef T element_type;
wxEXPLICIT wxScopedArray(T * array = NULL) : m_array(array) { }
~wxScopedArray() { delete [] m_array; }
// test for pointer validity: defining conversion to unspecified_bool_type
// and not more obvious bool to avoid implicit conversions to integer types
typedef T *(wxScopedArray<T>::*unspecified_bool_type)() const;
operator unspecified_bool_type() const
{
return m_array ? &wxScopedArray<T>::get : NULL;
}
void reset(T *array = NULL)
{
if ( array != m_array )
{
delete m_array;
m_array = array;
}
}
T& operator[](size_t n) const { return m_array[n]; }
T *get() const { return m_array; }
void swap(wxScopedArray &other)
{
T * const tmp = other.m_array;
other.m_array = m_array;
m_array = tmp;
}
private:
T *m_array;
DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray, T)
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -291,8 +291,11 @@ public:
/** /**
A scoped pointer template class. A scoped pointer template class.
It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros". It is the template version of the old-style @ref wxScopedPtr "scoped pointer macros".
Notice that objects of this class intentionally cannot be copied.
@library{wxbase} @library{wxbase}
@category{smartpointers} @category{smartpointers}
@@ -303,12 +306,15 @@ class wxScopedPtr<T>
{ {
public: public:
/** /**
Constructor. Constructor takes ownership of the pointer.
@param ptr
Pointer allocated with @c new or @NULL.
*/ */
wxScopedPtr(T* ptr = NULL); wxScopedPtr(T* ptr = NULL);
/** /**
Destructor. Destructor deletes the pointer.
*/ */
~wxScopedPtr(); ~wxScopedPtr();
@@ -319,7 +325,7 @@ public:
/** /**
Conversion to a boolean expression (in a variant which is not Conversion to a boolean expression (in a variant which is not
convertable to anything but a boolean expression). convertible to anything but a boolean expression).
If this class contains a valid pointer it will return @true, if it contains If this class contains a valid pointer it will return @true, if it contains
a @NULL pointer it will return @false. a @NULL pointer it will return @false.
@@ -362,3 +368,76 @@ public:
void swap(wxScopedPtr<T>& ot); void swap(wxScopedPtr<T>& ot);
}; };
/**
A scoped array template class.
This class is similar to boost scoped_array class:
http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/scoped_array.htm
Notice that objects of this class intentionally cannot be copied.
@library{wxbase}
@category{smartpointers}
*/
template <class T>
class wxScopedArray
{
public:
/// The type of the array elements.
typedef T element_type;
/**
Constructor takes ownership of the given array.
If @a array is @NULL, reset() must presumably be called later.
@param array
An array allocated using @c new[] or @NULL.
*/
explicit wxScopedArray(T * array = NULL);
/// Destructor destroy the array.
~wxScopedArray();
/**
Conversion to a boolean expression (in a variant which is not
convertible to anything but a boolean expression).
If this class contains a valid array it will return @true, if it contains
a @NULL pointer it will return @false.
*/
operator unspecified_bool_type() const;
/**
Change the array pointer stored.
The previously stored array is deleted.
@param array
An array allocated using @c new[] or @NULL.
*/
void reset(T *array = NULL);
/**
Return the n-th element of the array.
Must not be called if the array has no valid pointer.
*/
T& operator[](size_t n) const;
/**
Return the array pointer.
The returned pointer may be @NULL. It must not be deleted by the
caller, call @c reset(NULL) instead.
*/
T *get() const { return m_array; }
/// Swaps the contents of this array with another one.
void swap(wxScopedArray &other)
{
T * const tmp = other.m_array;
other.m_array = m_array;
m_array = tmp;
}
};