From e32b2adfdad2cecd088a29fa6f5c5eff036618ca Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 21 Nov 2002 13:10:18 +0000 Subject: [PATCH] Debug hook support, removed self-reset, fixed #%20links. [SVN r16361] --- include/boost/scoped_array.hpp | 26 +++++++++++++++++++------- include/boost/scoped_ptr.hpp | 28 ++++++++++++++++++++++------ scoped_array.htm | 26 +++++++++++++------------- scoped_ptr.htm | 22 +++++++++++----------- smart_ptr.htm | 8 ++++---- 5 files changed, 69 insertions(+), 41 deletions(-) diff --git a/include/boost/scoped_array.hpp b/include/boost/scoped_array.hpp index 636fad4..d99653c 100644 --- a/include/boost/scoped_array.hpp +++ b/include/boost/scoped_array.hpp @@ -9,7 +9,7 @@ // This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // -// See http://www.boost.org/libs/smart_ptr/scoped_array.htm for documentation. +// http://www.boost.org/libs/smart_ptr/scoped_array.htm // #include @@ -20,6 +20,15 @@ namespace boost { +// Debug hooks + +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + +void sp_array_constructor_hook(void * p); +void sp_array_destructor_hook(void * p); + +#endif + // scoped_array extends scoped_ptr to arrays. Deletion of the array pointed to // is guaranteed, either on destruction of the scoped_array or via an explicit // reset(). Use shared_array or std::vector if your needs are more complex. @@ -41,20 +50,23 @@ public: explicit scoped_array(T * p = 0) : ptr(p) // never throws { +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + boost::sp_array_constructor_hook(ptr); +#endif } ~scoped_array() // never throws { - checked_array_delete(ptr); +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + boost::sp_array_destructor_hook(ptr); +#endif + boost::checked_array_delete(ptr); } void reset(T * p = 0) // never throws { - if (ptr != p) - { - checked_array_delete(ptr); - ptr = p; - } + BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors + this_type(p).swap(*this); } T & operator[](std::ptrdiff_t i) const // never throws diff --git a/include/boost/scoped_ptr.hpp b/include/boost/scoped_ptr.hpp index 1c2a520..03b34ed 100644 --- a/include/boost/scoped_ptr.hpp +++ b/include/boost/scoped_ptr.hpp @@ -9,7 +9,7 @@ // This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // -// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation. +// http://www.boost.org/libs/smart_ptr/scoped_ptr.htm // #include @@ -22,6 +22,15 @@ namespace boost { +// Debug hooks + +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + +void sp_scalar_constructor_hook(void * p); +void sp_scalar_destructor_hook(void * p); + +#endif + // scoped_ptr mimics a built-in pointer except that it guarantees deletion // of the object pointed to, either on destruction of the scoped_ptr or via // an explicit reset(). scoped_ptr is a simple solution for simple needs; @@ -44,27 +53,34 @@ public: explicit scoped_ptr(T * p = 0): ptr(p) // never throws { +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + boost::sp_scalar_constructor_hook(ptr); +#endif } #ifndef BOOST_NO_AUTO_PTR explicit scoped_ptr(std::auto_ptr p): ptr(p.release()) // never throws { +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + boost::sp_scalar_constructor_hook(ptr); +#endif } #endif ~scoped_ptr() // never throws { - checked_delete(ptr); +#if defined(BOOST_ENABLE_SP_DEBUG_HOOKS) + boost::sp_scalar_destructor_hook(ptr); +#endif + boost::checked_delete(ptr); } void reset(T * p = 0) // never throws { - if(ptr != p) - { - this_type(p).swap(*this); - } + BOOST_ASSERT(p == 0 || p != ptr); // catch self-reset errors + this_type(p).swap(*this); } T & operator*() const // never throws diff --git a/scoped_array.htm b/scoped_array.htm index a057b11..5e80f62 100644 --- a/scoped_array.htm +++ b/scoped_array.htm @@ -29,7 +29,7 @@ heavier duty but far more flexible. A boost::array is an alternative that does not use dynamic allocation.

The class template is parameterized on T, the type of the object pointed - to. T must meet the smart pointer + to. T must meet the smart pointer common requirements.

Synopsis

namespace boost {
@@ -40,7 +40,7 @@
       typedef T element_type;
 
       explicit scoped_array(T * p = 0); // never throws
-      ~scoped_array(); // never throws
+      ~scoped_array(); // never throws
 
       void reset(T * p = 0); // never throws
 
@@ -62,23 +62,23 @@
 		
explicit scoped_array(T * p = 0); // never throws

Constructs a scoped_array, storing a copy of p, which must have been allocated via a C++ new[] expression or be 0. T is not - required be a complete type. See the smart pointer + required be a complete type. See the smart pointer common requirements.

-

destructor

+

destructor

~scoped_array(); // never throws

Deletes the array pointed to by the stored pointer. Note that delete[] on a pointer with a value of 0 is harmless. The guarantee that this does not throw exceptions depends on the requirement that the deleted array's objects' - destructors do not throw exceptions. See the smart pointer + destructors do not throw exceptions. See the smart pointer common requirements.

reset

void reset(T * p = 0); // never throws
-

If p is not equal to the stored pointer, deletes the array pointed to by the - stored pointer and then stores a copy of p, which must have been allocated via - a C++ new[] expression or be 0. The guarantee that this does not throw - exceptions depends on the requirement that the deleted array's objects' - destructors do not throw exceptions. See the smart pointer - common requirements.

+

+ Deletes the array pointed to by the stored pointer and then stores a copy of p, + which must have been allocated via a C++ new[] expression or be 0. The + guarantee that this does not throw exceptions depends on the requirement that + the deleted array's objects' destructors do not throw exceptions. See the smart + pointer common requirements.

subscripting

T & operator[](std::ptrdiff_t i) const; // never throws

Returns a reference to element i of the array pointed to by the stored @@ -88,11 +88,11 @@

get

T * get() const; // never throws

Returns the stored pointer. T need not be a complete type. See the smart - pointer common requirements.

+ pointer common requirements.

swap

void swap(scoped_array & b); // never throws

Exchanges the contents of the two smart pointers. T need not be a - complete type. See the smart pointer common + complete type. See the smart pointer common requirements.

Free Functions

swap

diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 53815c7..148cd42 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -29,7 +29,7 @@ allocated array. See scoped_array for that usage.

The class template is parameterized on T, the type of the object pointed - to. T must meet the smart pointer + to. T must meet the smart pointer common requirements.

Synopsis

namespace boost {
@@ -62,7 +62,7 @@
 		
explicit scoped_ptr(T * p = 0); // never throws

Constructs a scoped_ptr, storing a copy of p, which must have been allocated via a C++ new expression or be 0. T is not required be - a complete type. See the smart pointer common + a complete type. See the smart pointer common requirements.

destructor

~scoped_ptr(); // never throws
@@ -71,15 +71,15 @@

The guarantee that this does not throw exceptions depends on the requirement that the deleted object's destructor does not throw exceptions. See the smart - pointer common requirements.

+ pointer common requirements.

reset

void reset(T * p = 0); // never throws
-

If p is not equal to the stored pointer, deletes the object pointed to by the - stored pointer and then stores a copy of p, which must have been allocated via - a C++ new expression or be 0. The guarantee that this does not throw - exceptions depends on the requirement that the deleted object's destructor does - not throw exceptions. See the smart pointer - common requirements.

+

+ Deletes the object pointed to by the stored pointer and then stores a copy of + p, which must have been allocated via a C++ new expression or be 0. The + guarantee that this does not throw exceptions depends on the requirement that + the deleted object's destructor does not throw exceptions. See the smart + pointer common requirements.

indirection

T & operator*() const; // never throws

Returns a reference to the object pointed to by the stored pointer. Behavior is @@ -89,11 +89,11 @@

get

T * get() const; // never throws

Returns the stored pointer. T need not be a complete type. See the smart - pointer common requirements.

+ pointer common requirements.

swap

void swap(scoped_ptr & b); // never throws

Exchanges the contents of the two smart pointers. T need not be a - complete type. See the smart pointer common + complete type. See the smart pointer common requirements.

Free Functions

swap

diff --git a/smart_ptr.htm b/smart_ptr.htm index 9e5357a..ac8a96f 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -56,7 +56,7 @@ versions of the smart pointer implementation.

A page on smart pointer timings will be of interest to those curious about performance issues.

-

Common Requirements

+

Common Requirements

These smart pointer class templates have a template parameter, T, which specifies the type of the object pointed to by the smart pointer. The behavior of the smart pointer templates is undefined if the destructor or operator delete @@ -87,7 +87,7 @@ which resulted in the exception being thrown. This amounts to a guarantee that there are no detectable side effects. Other functions never throw exceptions. The only exception ever thrown by functions which do throw (assuming T meets - the common requirements) is std::bad_alloc, + the common requirements) is std::bad_alloc, and that is thrown only by functions which are explicitly documented as possibly throwing std::bad_alloc.

Exception-specifications

@@ -99,7 +99,7 @@ // never throws.

Functions which destroy objects of the pointed to type are prohibited from - throwing exceptions by the common requirements.

+ throwing exceptions by the common requirements.

History and Acknowledgements

January 2002. Peter Dimov reworked all four classes, adding features, fixing bugs, and splitting them into four separate headers, and added weak_ptr. @@ -156,7 +156,7 @@ users", and in the end we choose to supply only the direct implementation.


Revised +--> 4 February 2002

Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler.