From 058f225a44d83d42ba9d773efc705badbf0e5e3c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 27 Jan 2009 16:40:51 +0000 Subject: [PATCH] added template wxScopedArray<> too git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58460 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 8 +- include/wx/ptr_scpd.h | 163 +++++++++++++++++++++++++--------------- interface/wx/ptr_scpd.h | 85 ++++++++++++++++++++- 3 files changed, 190 insertions(+), 66 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index d5184eecd2..240cf9af50 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -260,8 +260,9 @@ Major new features in this release - wxWidgets may now use either wchar_t (UTF-16/32) or UTF-8 internally, depending on what is optimal for the target platform. -- New propgrid library containing wxPropertyGrid and related classes, many - enhancements to wxDataViewCtrl. +- New propgrid library containing wxPropertyGrid and related classes. + +- Many enhancements to wxDataViewCtrl. - Event loops, timers and sockets can now be used in wxBase, without GUI. @@ -312,7 +313,7 @@ All: Linnakangas). - wxVariant::Unshare allows exclusive allocation of data that must be shared, if the wxVariantData::Clone function is implemented. -- Added wxWeakRef, wxScopedPtr, wxSharedPtr class templates +- Added wxWeakRef, wxScopedPtr, wxScopedArray, wxSharedPtr templates. - Added wxVector class templates - Added wxON_BLOCK_EXIT_SET() and wxON_BLOCK_EXIT_NULL() to wx/scopeguard.h. - Added wxEvtHandler::QueueEvent() replacing AddPendingEvent() and @@ -328,6 +329,7 @@ All: - Added wxSHUTDOWN_LOGOFF and wxSHUTDOWN_FORCE wxShutdown() flags (troelsk). - Added wxSocket::ShutdownOutput(). - Handle exceptions thrown from overridden wxView::OnCreate() gracefully. +- Added wxPATH_RMDIR_FULL/RECURSIVE wxFileName::Rmdir() flags (Marcin Malich). - Added wxStandardPaths::GetAppDocumentsDir(). All (Unix): diff --git a/include/wx/ptr_scpd.h b/include/wx/ptr_scpd.h index 2ae40220c9..ffbb01105d 100644 --- a/include/wx/ptr_scpd.h +++ b/include/wx/ptr_scpd.h @@ -2,10 +2,11 @@ // Name: wx/ptr_scpd.h // Purpose: scoped smart pointer class // Author: Jesse Lovelace -// Modified by: +// Modified by: Vadim Zeitlin to add template wxScopedArray // Created: 06/01/02 // RCS-ID: $Id$ // Copyright: (c) Jesse Lovelace and original Boost authors (see below) +// (c) 2009 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -31,30 +32,19 @@ #include "wx/defs.h" // ---------------------------------------------------------------------------- -// wxScopedPtr: A scoped pointer +// wxScopedPtr: A scoped pointer // ---------------------------------------------------------------------------- -template +template class wxScopedPtr -{ -private: - T * m_ptr; - - wxScopedPtr(wxScopedPtr const &); - wxScopedPtr & operator=(wxScopedPtr const &); - -public: +{ +public: typedef T element_type; - - wxEXPLICIT wxScopedPtr(T * ptr = NULL) - : m_ptr(ptr) { } - - ~wxScopedPtr() - { - if (m_ptr) - delete m_ptr; - } - + + wxEXPLICIT wxScopedPtr(T * ptr = NULL) : m_ptr(ptr) { } + + ~wxScopedPtr() { delete m_ptr; } + // test for pointer validity: defining conversion to unspecified_bool_type // and not more obvious bool to avoid implicit conversions to integer types typedef T *(wxScopedPtr::*unspecified_bool_type)() const; @@ -63,45 +53,98 @@ public: return m_ptr ? &wxScopedPtr::get : NULL; } - void reset(T * ptr = NULL) - { - if (m_ptr != ptr) - { - delete m_ptr; - m_ptr = ptr; - } - } - - T *release() - { - T *ptr = m_ptr; - m_ptr = NULL; - return ptr; - } - - T & operator*() const - { - wxASSERT(m_ptr != NULL); - return *m_ptr; - } - - T * operator->() const - { - wxASSERT(m_ptr != NULL); - return m_ptr; - } - - T * get() const - { - return m_ptr; - } - - void swap(wxScopedPtr & ot) - { - T * tmp = ot.m_ptr; - ot.m_ptr = m_ptr; - m_ptr = tmp; - } + void reset(T * ptr = NULL) + { + if ( ptr != m_ptr ) + { + delete m_ptr; + m_ptr = ptr; + } + } + + T *release() + { + T *ptr = m_ptr; + m_ptr = NULL; + return ptr; + } + + T & operator*() const + { + wxASSERT(m_ptr != NULL); + return *m_ptr; + } + + T * operator->() const + { + wxASSERT(m_ptr != NULL); + return m_ptr; + } + + T * get() const + { + return m_ptr; + } + + void swap(wxScopedPtr& other) + { + T * const tmp = other.m_ptr; + other.m_ptr = m_ptr; + m_ptr = tmp; + } + +private: + T * m_ptr; + + DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T) +}; + +// ---------------------------------------------------------------------------- +// wxScopedArray: A scoped array +// ---------------------------------------------------------------------------- + +template +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::*unspecified_bool_type)() const; + operator unspecified_bool_type() const + { + return m_array ? &wxScopedArray::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) }; // ---------------------------------------------------------------------------- diff --git a/interface/wx/ptr_scpd.h b/interface/wx/ptr_scpd.h index 2434a6dced..b551c81cb0 100644 --- a/interface/wx/ptr_scpd.h +++ b/interface/wx/ptr_scpd.h @@ -291,8 +291,11 @@ public: /** A scoped pointer template class. + 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} @category{smartpointers} @@ -303,12 +306,15 @@ class wxScopedPtr { public: /** - Constructor. + Constructor takes ownership of the pointer. + + @param ptr + Pointer allocated with @c new or @NULL. */ wxScopedPtr(T* ptr = NULL); /** - Destructor. + Destructor deletes the pointer. */ ~wxScopedPtr(); @@ -319,7 +325,7 @@ public: /** 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 a @NULL pointer it will return @false. @@ -362,3 +368,76 @@ public: void swap(wxScopedPtr& 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 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; + } +}; -- 2.45.2