Boost logo

Boost-Commit :

From: srajko_at_[hidden]
Date: 2007-07-04 02:13:17


Author: srajko
Date: 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
New Revision: 7355
URL: http://svn.boost.org/trac/boost/changeset/7355

Log:
add main smart_ptr documentation and beginning of scoped_array quickbook translation

Added:
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/intrusive_ptr.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_array.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_ptr.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_array.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_ptr.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/weak_ptr.qbk (contents, props changed)
   sandbox/boost_docs/trunk/libs/smart_ptr/test/
   sandbox/boost_docs/trunk/libs/smart_ptr/test/smart_ptr_test.cpp (contents, props changed)
Text files modified:
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/Jamfile.v2 | 6
   sandbox/boost_docs/trunk/libs/smart_ptr/doc/smart_ptr.qbk | 193 +++++++++++++++++++++++++++++++++++++++
   2 files changed, 194 insertions(+), 5 deletions(-)

Modified: sandbox/boost_docs/trunk/libs/smart_ptr/doc/Jamfile.v2
==============================================================================
--- sandbox/boost_docs/trunk/libs/smart_ptr/doc/Jamfile.v2 (original)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/Jamfile.v2 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -14,17 +14,19 @@
         # Use graphics not text for navigation:
         <xsl:param>navig.graphics=1
         # How far down we chunk nested sections, basically all of them:
- <xsl:param>chunk.section.depth=10
+ <xsl:param>chunk.section.depth=2
         # Don't put the first section on the same page as the TOC:
         <xsl:param>chunk.first.sections=1
         # How far down sections get TOC's
         <xsl:param>toc.section.depth=10
         # Max depth in each TOC:
- <xsl:param>toc.max.depth=4
+ <xsl:param>toc.max.depth=2
         # How far down we go with TOC's
         <xsl:param>generate.section.toc.level=10
         # Logo location:
         <xsl:param>boost.logo=../boost.png
+
+ <xsl:param>boost.root=http://www.boost.org
 
         # The page style
         <xsl:param>page.style="'website'"

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/intrusive_ptr.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/intrusive_ptr.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,2 @@
+[section:intrusive_ptr intrusive_ptr]
+[endsect][/intrusive_ptr]
\ No newline at end of file

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_array.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_array.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,112 @@
+[def __reset__ [link smart_ptr.reference.scoped_array.members.reset reset]
+
+[section:scoped_array scoped_array class template]
+
+The __scoped_array__ class template stores a pointer to a dynamically allocated array. (Dynamically allocated arrays are allocated with the C++ [*new\[\]] expression.) The array pointed to is guaranteed to be deleted, either on destruction of the __scoped_array__, or via an explicit __reset__.
+
+The __scoped_array__ template is a simple solution for simple needs. It supplies a basic "resource acquisition is initialization" facility, without shared-ownership or transfer-of-ownership semantics. Both its name and enforcement of semantics (by being __noncopyable__) signal its intent to retain ownership solely within the current scope. Because it is __noncopyable__, it is safer than __shared_array__ for pointers which should not be copied.
+
+Because __scoped_array__ is so simple, in its usual implementation every operation is as fast as a built-in array pointer and it has no more space overhead that a built-in array pointer.
+
+It cannot be used in C++ standard library containers. See __shared_array__ if __scoped_array__ does not meet your needs.
+
+It cannot correctly hold a pointer to a single object. See __scoped_ptr__ for that usage.
+
+A [*std::vector] is an alternative to a __scoped_array__ that is a bit 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 __common_requirements__.
+
+[section Synopsis]
+
+ namespace boost {
+
+ template<class T> class scoped_array : __noncopyable__ {
+
+ public:
+ typedef T [@#element_type element_type];
+
+ explicit [@#ctor scoped_array](T * p = 0); // never throws
+ [@#destructor ~scoped_array](); // never throws
+
+ void [@#reset reset](T * p = 0); // never throws
+
+ T & [@#operator operator\[\]](std::ptrdiff_t i) const; // never throws
+ T * [@#get get]() const; // never throws
+
+ operator [@#conversions ['unspecified-bool-type]]() const; // never throws
+
+ void [@#swap swap](scoped_array & b); // never throws
+ };
+
+ template<class T> void [@#free-swap swap](scoped_array<T> & a, scoped_array<T> & b); // never throws
+
+ }
+
+[endsect]
+
+[section Members]
+
+[heading element_type]
+
+ typedef T element_type;
+
+Provides the type of the stored pointer.
+
+[heading constructors]
+
+ 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 __common_requirements__.
+
+[heading 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 __common_requirements__.
+
+[heading reset]
+
+ void reset(T * p = 0); // never throws
+
+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__.
+
+[heading subscripting]
+
+ T & operator\[\](std::ptrdiff_t i) const; // never throws
+
+Returns a reference to element [*i] of the array pointed to by the stored pointer. Behavior is undefined and almost certainly undesirable if the stored pointer is 0, or if [*i] is less than 0 or is greater than or equal to the number of elements in the array.
+
+[heading get]
+
+ T * get() const; // never throws
+
+Returns the stored pointer. [*T] need not be a complete type. See the smart pointer __common_requirements__.
+
+[heading conversions]
+
+ operator ['unspecified-bool-type] () const; // never throws
+
+Returns an unspecified value that, when used in boolean contexts, is equivalent to [^get() != 0].
+
+[heading 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_requirements__.
+
+[endsect]
+
+[section Free Functions]
+
+[heading swap]
+
+ template<class T> void swap(scoped_array<T> & a, scoped_array<T> & b); // never throws
+
+Equivalent to [*a.swap(b)]. Matches the interface of [*std::swap]. Provided as an aid to generic programming.
+
+Revised 09 January 2003
+
+Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. Copyright 2002-2005 Peter Dimov. Distributed under the Boost Software License, Version 1.0. See accompanying file [@../../LICENSE_1_0.txt LICENSE_1_0.txt] or copy at [@http://www.boost.org/LICENSE_1_0.txt http://www.boost.org/LICENSE_1_0.txt].
+
+[endsect]
+[endsect][/scoped_array]
\ No newline at end of file

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_ptr.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/scoped_ptr.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,2 @@
+[section:scoped_ptr scoped_ptr]
+[endsect][/scoped_ptr]
\ No newline at end of file

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_array.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_array.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,2 @@
+[section:shared_array shared_array]
+[endsect][/shared_array]
\ No newline at end of file

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_ptr.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/shared_ptr.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,2 @@
+[section:shared_ptr shared_ptr]
+[endsect][/shared_ptr]
\ No newline at end of file

Modified: sandbox/boost_docs/trunk/libs/smart_ptr/doc/smart_ptr.qbk
==============================================================================
--- sandbox/boost_docs/trunk/libs/smart_ptr/doc/smart_ptr.qbk (original)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/smart_ptr.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -9,9 +9,196 @@
     ]
     [authors [Colvin, Greg], [Dawes, Beman], [Dimov, Peter], [Adler, Darin]]
     [category memory]
- [/last-revision $Date: 2007/06/20 16:39:52 +0100 $]
 ]
 
-[section Introduction]
+[def __scoped_ptr__ [link smart_ptr.reference.scoped_ptr scoped_ptr]]
+[def __scoped_array__ [link smart_ptr.reference.scoped_array scoped_array]]
+[def __shared_ptr__ [link smart_ptr.reference.shared_ptr shared_ptr]]
+[def __shared_array__ [link smart_ptr.reference.shared_array shared_array]]
+[def __weak_ptr__ [link smart_ptr.reference.weak_ptr weak_ptr]]
+[def __intrusive_ptr__ [link smart_ptr.reference.intrusive_ptr intrusive_ptr]]
 
-[endsect]
\ No newline at end of file
+[def __common_requirements__ [link smart_ptr.common_requirements common requirements]]
+[def __smart_ptr_test__ [link smart_ptr.examples.smart_ptr_test smart_ptr_test.cpp]]
+
+[def __boost_root__ http://www.boost.org]
+
+[def __scoped_ptr_hpp__ [@http://www.boost.org/boost/scoped_ptr.hpp <boost/scoped_ptr.hpp>]]
+[def __scoped_array_hpp__ [@http://www.boost.org/boost/scoped_array.hpp <boost/scoped_array.hpp>]]
+[def __shared_ptr_hpp__ [@http://www.boost.org/boost/shared_ptr.hpp <boost/shared_ptr.hpp>]]
+[def __shared_array_hpp__ [@http://www.boost.org/boost/shared_array.hpp <boost/shared_array.hpp>]]
+[def __weak_ptr_hpp__ [@http://www.boost.org/boost/weak_ptr.hpp <boost/weak_ptr.hpp>]]
+[def __intrusive_ptr_hpp__ [@http://www.boost.org/boost/intrusive_ptr.hpp <boost/intrusive_ptr.hpp>]]
+
+[def __exception-specification__ [@http://www.boost.org/more/lib_guide.htm#Exception-specification exception-specification]]
+[def __noncopyable__ [@http://www.boost.org/doc/html/utility/utility.htm#Class_noncopyable noncopyable]]
+[def __auto_ptr__ [@http://en.wikipedia.org/wiki/Auto_ptr std::auto_ptr]]
+
+[import ../test/smart_ptr_test.cpp]
+
+[section:introduction Introduction]
+
+Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like
+built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time. Smart
+pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically
+allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners.
+
+Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the
+object when it is no longer needed.
+
+The smart pointer library provides five smart pointer class templates:
+
+[table Smart pointer class templates
+[[class template][header][description]]
+[[__scoped_ptr__] [__scoped_ptr_hpp__] [Simple sole ownership of single objects. Noncopyable.]]
+[[__scoped_array__] [__scoped_array_hpp__] [Simple sole ownership of arrays. Noncopyable.]]
+[[__shared_ptr__] [__shared_ptr_hpp__] [Object ownership shared among multiple pointers]]
+[[__shared_array__] [__shared_array_hpp__] [Array ownership shared among multiple pointers.]]
+[[__weak_ptr__] [__weak_ptr_hpp__] [Non-owning observers of an object owned by __shared_ptr__.]]
+[[__intrusive_ptr__][__intrusive_ptr_hpp__] [Shared ownership of objects with an embedded reference count.]]
+]
+
+These templates are designed to complement the __auto_ptr__ template.
+
+They are examples of the "resource acquisition is initialization" idiom described in Bjarne Stroustrup's
+"The C++ Programming Language", 3rd edition, Section 14.4, Resource Management.
+
+A test program, __smart_ptr_test__, is provided to verify correct operation.
+
+A page on compatibility with older versions of the Boost smart pointer library describes some of the changes
+since earlier versions of the smart pointer implementation.
+
+A page on smart pointer timings will be of interest to those curious about performance issues.
+
+A page on smart pointer programming techniques lists some advanced applications of __shared_ptr__ and __weak_ptr__.
+
+[endsect]
+
+[section: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` for objects of type `T` throw exceptions.
+
+`T` may be an incomplete type at the point of smart pointer declaration. Unless otherwise specified, it is required
+that `T` be a complete type at points of smart pointer instantiation. Implementations are required to diagnose
+(treat as an error) all violations of this requirement, including deletion of an incomplete type. See the
+description of the checked_delete function template.
+
+Note that __shared_ptr__ does not have this restriction, as most of its member functions do not require `T` to be a
+complete type.
+
+[heading Rationale]
+
+The requirements on `T` are carefully crafted to maximize safety yet allow handle-body (also called pimpl) and
+similar idioms. In these idioms a smart pointer may appear in translation units where `T` is an incomplete type.
+This separates interface from implementation and hides implementation from translation units which merely use the
+interface. Examples described in the documentation for specific smart pointers illustrate use of smart pointers
+in these idioms.
+
+Note that __scoped_ptr__ requires that `T` be a complete type at destruction time, but __shared_ptr__ does not.
+
+[endsect]
+
+[section:exception_safety Exception Safety]
+
+Several functions in these smart pointer classes are specified as having "no effect" or "no effect except
+such-and-such" if an exception is thrown. This means that when an exception is thrown by an object of one of these
+classes, the entire program state remains the same as it was prior to the function call 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, and that is thrown only by functions which are explicitly documented as possibly
+throwing std::bad_alloc.
+
+[endsect]
+
+[section:exception_specifications Exception-specifications]
+
+Exception-specifications are not used; see __exception-specification__ rationale.
+
+All the smart pointer templates contain member functions which can never throw exceptions, because they neither
+throw exceptions themselves nor call other functions which may throw exceptions. These members are indicated by a
+comment: `// never throws`.
+
+Functions which destroy objects of the pointed to type are prohibited from throwing exceptions by the common
+requirements.
+
+[endsect]
+
+
+[section:history_and_acknowledgements 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__. See the compatibility page for a summary of the changes.
+
+May 2001. Vladimir Prus suggested requiring a complete type on destruction. Refinement evolved in discussions
+including Dave Abrahams, Greg Colvin, Beman Dawes, Rainer Deyke, Peter Dimov, John Maddock, Vladimir Prus, Shankar
+Sai, and others.
+
+November 1999. Darin Adler provided operator ==, operator !=, and std::swap and std::less specializations for
+shared types.
+
+September 1999. Luis Coelho provided shared_ptr::swap and shared_array::swap
+
+May 1999. In April and May, 1999, Valentin Bonnard and David Abrahams made a number of suggestions resulting in
+numerous improvements.
+
+October 1998. Beman Dawes proposed reviving the original semantics under the names safe_ptr and counted_ptr,
+meeting of Per Andersson, Matt Austern, Greg Colvin, Sean Corfield, Pete Becker, Nico Josuttis, Dietmar K&#252;hl,
+Nathan Myers, Chichiang Wan and Judy Ward. During the discussion, the four new class names were finalized, it was
+decided that there was no need to exactly follow the std::auto_ptr interface, and various function signatures and
+semantics were finalized.
+
+Over the next three months, several implementations were considered for shared_ptr, and discussed on the boost.org
+mailing list. The implementation questions revolved around the reference count which must be kept, either attached
+to the pointed to object, or detached elsewhere. Each of those variants have themselves two major variants:
+
+* Direct detached: the __shared_ptr__ contains a pointer to the object, and a pointer to the count.
+* Indirect detached: the __shared_ptr__ contains a pointer to a helper object, which in turn contains a pointer to the
+object and the count.
+* Embedded attached: the count is a member of the object pointed to.
+* Placement attached: the count is attached via operator new manipulations.
+
+Each implementation technique has advantages and disadvantages. We went so far as to run various timings of the
+direct and indirect approaches, and found that at least on Intel Pentium chips there was very little measurable
+difference. Kevlin Henney provided a paper he wrote on "Counted Body Techniques." Dietmar K&#252;hl suggested an
+elegant partial template specialization technique to allow users to choose which implementation they preferred,
+and that was also experimented with.
+
+But Greg Colvin and Jerry Schwarz argued that "parameterization will discourage users", and in the end we choose
+to supply only the direct implementation.
+
+Summer, 1994. Greg Colvin proposed to the C++ Standards Committee classes named auto_ptr and counted_ptr which
+were very similar to what we now call scoped_ptr and shared_ptr. [Col-94] In one of the very few cases where the
+Library Working Group's recommendations were not followed by the full committee, counted_ptr was rejected and
+surprising transfer-of-ownership semantics were added to auto_ptr.
+
+[endsect]
+
+[section:references References]
+
+[Col-94] Gregory Colvin, Exception Safe Smart Pointers, C++ committee document 94-168/N0555, July, 1994.
+
+[E&D-94] John R. Ellis & David L. Detlefs, Safe, Efficient Garbage Collection for C++, Usenix Proceedings,
+February, 1994. This paper includes an extensive discussion of weak pointers and an extensive bibliography.
+
+[endsect]
+
+[section:reference Reference]
+
+[include scoped_ptr.qbk]
+[include scoped_array.qbk]
+[include shared_ptr.qbk]
+[include shared_array.qbk]
+[include weak_ptr.qbk]
+[include intrusive_ptr.qbk]
+
+[endsect][/reference]
+
+[section:examples Examples]
+
+[section:smart_ptr_test smart_ptr_test.cpp]
+[smart_ptr_test]
+[endsect][/smart_ptr_test]
+
+[endsect][/examples]

Added: sandbox/boost_docs/trunk/libs/smart_ptr/doc/weak_ptr.qbk
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/doc/weak_ptr.qbk 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,2 @@
+[section:weak_ptr weak_ptr]
+[endsect][/weak_ptr]
\ No newline at end of file

Added: sandbox/boost_docs/trunk/libs/smart_ptr/test/smart_ptr_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/boost_docs/trunk/libs/smart_ptr/test/smart_ptr_test.cpp 2007-07-04 02:13:16 EDT (Wed, 04 Jul 2007)
@@ -0,0 +1,307 @@
+//[ smart_ptr_test
+
+// smart pointer test program ----------------------------------------------//
+
+// Copyright Beman Dawes 1998, 1999. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/smart_ptr for documentation.
+
+// Revision History
+// 24 May 01 use Boost test library for error detection, reporting, add tests
+// for operations on incomplete types (Beman Dawes)
+// 29 Nov 99 added std::swap and associative container tests (Darin Adler)
+// 25 Sep 99 added swap tests
+// 20 Jul 99 header name changed to .hpp
+// 20 Apr 99 additional error tests added.
+
+#include <boost/config.hpp>
+
+#if defined(BOOST_MSVC)
+
+# pragma warning(disable: 4786) // identifier truncated in debug info
+# pragma warning(disable: 4710) // function not inlined
+# pragma warning(disable: 4711) // function selected for automatic inline expansion
+# pragma warning(disable: 4514) // unreferenced inline removed
+
+#if (BOOST_MSVC >= 1310)
+# pragma warning(disable: 4675) // resolved overload found with Koenig lookup
+#endif
+
+#endif
+
+#ifdef __BORLANDC__
+# pragma warn -8092 // template argument passed to 'find' is not an iterator
+#endif
+
+#include <boost/scoped_ptr.hpp>
+#include <boost/scoped_array.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/shared_array.hpp>
+
+#include <boost/detail/lightweight_test.hpp>
+
+#include <iostream>
+#include <set>
+#include <string.h>
+
+class Incomplete;
+
+Incomplete * get_ptr( boost::shared_ptr<Incomplete>& incomplete )
+{
+ return incomplete.get();
+}
+
+template<class T>
+void ck( const T* v1, T v2 ) { BOOST_TEST( *v1 == v2 ); }
+
+namespace {
+ int UDT_use_count; // independent of pointer maintained counts
+ }
+
+// user defined type -------------------------------------------------------//
+
+class UDT {
+ long value_;
+ public:
+ explicit UDT( long value=0 ) : value_(value) { ++UDT_use_count; }
+ ~UDT() {
+ --UDT_use_count;
+ std::cout << "UDT with value " << value_ << " being destroyed\n";
+ }
+ long value() const { return value_; }
+ void value( long v ) { value_ = v;; }
+ }; // UDT
+
+// tests on incomplete types -----------------------------------------------//
+
+// Certain smart pointer operations are specified to work on incomplete types,
+// and some uses depend upon this feature. These tests verify compilation
+// only - the functions aren't actually invoked.
+
+class Incomplete;
+
+Incomplete * check_incomplete( boost::scoped_ptr<Incomplete>& incomplete )
+{
+ return incomplete.get();
+}
+
+Incomplete * check_incomplete( boost::shared_ptr<Incomplete>& incomplete,
+ boost::shared_ptr<Incomplete>& i2 )
+{
+ incomplete.swap(i2);
+ std::cout << incomplete.use_count() << ' ' << incomplete.unique() << '\n';
+ return incomplete.get();
+}
+
+// This isn't a very systematic test; it just hits some of the basics.
+
+void test()
+{
+ BOOST_TEST( UDT_use_count == 0 ); // reality check
+
+ // test scoped_ptr with a built-in type
+ long * lp = new long;
+ boost::scoped_ptr<long> sp ( lp );
+ BOOST_TEST( sp.get() == lp );
+ BOOST_TEST( lp == sp.get() );
+ BOOST_TEST( &*sp == lp );
+
+ *sp = 1234568901L;
+ BOOST_TEST( *sp == 1234568901L );
+ BOOST_TEST( *lp == 1234568901L );
+ ck( static_cast<long*>(sp.get()), 1234568901L );
+ ck( lp, *sp );
+
+ sp.reset();
+ BOOST_TEST( sp.get() == 0 );
+
+ // test scoped_ptr with a user defined type
+ boost::scoped_ptr<UDT> udt_sp ( new UDT( 999888777 ) );
+ BOOST_TEST( udt_sp->value() == 999888777 );
+ udt_sp.reset();
+ udt_sp.reset( new UDT( 111222333 ) );
+ BOOST_TEST( udt_sp->value() == 111222333 );
+ udt_sp.reset( new UDT( 333222111 ) );
+ BOOST_TEST( udt_sp->value() == 333222111 );
+
+ // test scoped_array with a build-in type
+ char * sap = new char [ 100 ];
+ boost::scoped_array<char> sa ( sap );
+ BOOST_TEST( sa.get() == sap );
+ BOOST_TEST( sap == sa.get() );
+
+ strcpy( sa.get(), "Hot Dog with mustard and relish" );
+ BOOST_TEST( strcmp( sa.get(), "Hot Dog with mustard and relish" ) == 0 );
+ BOOST_TEST( strcmp( sap, "Hot Dog with mustard and relish" ) == 0 );
+
+ BOOST_TEST( sa[0] == 'H' );
+ BOOST_TEST( sa[30] == 'h' );
+
+ sa[0] = 'N';
+ sa[4] = 'd';
+ BOOST_TEST( strcmp( sap, "Not dog with mustard and relish" ) == 0 );
+
+ sa.reset();
+ BOOST_TEST( sa.get() == 0 );
+
+ // test shared_ptr with a built-in type
+ int * ip = new int;
+ boost::shared_ptr<int> cp ( ip );
+ BOOST_TEST( ip == cp.get() );
+ BOOST_TEST( cp.use_count() == 1 );
+
+ *cp = 54321;
+ BOOST_TEST( *cp == 54321 );
+ BOOST_TEST( *ip == 54321 );
+ ck( static_cast<int*>(cp.get()), 54321 );
+ ck( static_cast<int*>(ip), *cp );
+
+ boost::shared_ptr<int> cp2 ( cp );
+ BOOST_TEST( ip == cp2.get() );
+ BOOST_TEST( cp.use_count() == 2 );
+ BOOST_TEST( cp2.use_count() == 2 );
+
+ BOOST_TEST( *cp == 54321 );
+ BOOST_TEST( *cp2 == 54321 );
+ ck( static_cast<int*>(cp2.get()), 54321 );
+ ck( static_cast<int*>(ip), *cp2 );
+
+ boost::shared_ptr<int> cp3 ( cp );
+ BOOST_TEST( cp.use_count() == 3 );
+ BOOST_TEST( cp2.use_count() == 3 );
+ BOOST_TEST( cp3.use_count() == 3 );
+ cp.reset();
+ BOOST_TEST( cp2.use_count() == 2 );
+ BOOST_TEST( cp3.use_count() == 2 );
+ cp.reset( new int );
+ *cp = 98765;
+ BOOST_TEST( *cp == 98765 );
+ *cp3 = 87654;
+ BOOST_TEST( *cp3 == 87654 );
+ BOOST_TEST( *cp2 == 87654 );
+ cp.swap( cp3 );
+ BOOST_TEST( *cp == 87654 );
+ BOOST_TEST( *cp2 == 87654 );
+ BOOST_TEST( *cp3 == 98765 );
+ cp.swap( cp3 );
+ BOOST_TEST( *cp == 98765 );
+ BOOST_TEST( *cp2 == 87654 );
+ BOOST_TEST( *cp3 == 87654 );
+ cp2 = cp2;
+ BOOST_TEST( cp2.use_count() == 2 );
+ BOOST_TEST( *cp2 == 87654 );
+ cp = cp2;
+ BOOST_TEST( cp2.use_count() == 3 );
+ BOOST_TEST( *cp2 == 87654 );
+ BOOST_TEST( cp.use_count() == 3 );
+ BOOST_TEST( *cp == 87654 );
+
+ boost::shared_ptr<int> cp4;
+ swap( cp2, cp4 );
+ BOOST_TEST( cp4.use_count() == 3 );
+ BOOST_TEST( *cp4 == 87654 );
+ BOOST_TEST( cp2.get() == 0 );
+
+ std::set< boost::shared_ptr<int> > scp;
+ scp.insert(cp4);
+ BOOST_TEST( scp.find(cp4) != scp.end() );
+ BOOST_TEST( scp.find(cp4) == scp.find( boost::shared_ptr<int>(cp4) ) );
+
+ // test shared_array with a built-in type
+ char * cap = new char [ 100 ];
+ boost::shared_array<char> ca ( cap );
+ BOOST_TEST( ca.get() == cap );
+ BOOST_TEST( cap == ca.get() );
+ BOOST_TEST( &ca[0] == cap );
+
+ strcpy( ca.get(), "Hot Dog with mustard and relish" );
+ BOOST_TEST( strcmp( ca.get(), "Hot Dog with mustard and relish" ) == 0 );
+ BOOST_TEST( strcmp( cap, "Hot Dog with mustard and relish" ) == 0 );
+
+ BOOST_TEST( ca[0] == 'H' );
+ BOOST_TEST( ca[30] == 'h' );
+
+ boost::shared_array<char> ca2 ( ca );
+ boost::shared_array<char> ca3 ( ca2 );
+
+ ca[0] = 'N';
+ ca[4] = 'd';
+ BOOST_TEST( strcmp( ca.get(), "Not dog with mustard and relish" ) == 0 );
+ BOOST_TEST( strcmp( ca2.get(), "Not dog with mustard and relish" ) == 0 );
+ BOOST_TEST( strcmp( ca3.get(), "Not dog with mustard and relish" ) == 0 );
+ BOOST_TEST( ca.use_count() == 3 );
+ BOOST_TEST( ca2.use_count() == 3 );
+ BOOST_TEST( ca3.use_count() == 3 );
+ ca2.reset();
+ BOOST_TEST( ca.use_count() == 2 );
+ BOOST_TEST( ca3.use_count() == 2 );
+ BOOST_TEST( ca2.use_count() == 1 );
+
+ ca.reset();
+ BOOST_TEST( ca.get() == 0 );
+
+ boost::shared_array<char> ca4;
+ swap( ca3, ca4 );
+ BOOST_TEST( ca4.use_count() == 1 );
+ BOOST_TEST( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 );
+ BOOST_TEST( ca3.get() == 0 );
+
+ std::set< boost::shared_array<char> > sca;
+ sca.insert(ca4);
+ BOOST_TEST( sca.find(ca4) != sca.end() );
+ BOOST_TEST( sca.find(ca4) == sca.find( boost::shared_array<char>(ca4) ) );
+
+ // test shared_array with user defined type
+ boost::shared_array<UDT> udta ( new UDT[3] );
+
+ udta[0].value( 111 );
+ udta[1].value( 222 );
+ udta[2].value( 333 );
+ boost::shared_array<UDT> udta2 ( udta );
+
+ BOOST_TEST( udta[0].value() == 111 );
+ BOOST_TEST( udta[1].value() == 222 );
+ BOOST_TEST( udta[2].value() == 333 );
+ BOOST_TEST( udta2[0].value() == 111 );
+ BOOST_TEST( udta2[1].value() == 222 );
+ BOOST_TEST( udta2[2].value() == 333 );
+ udta2.reset();
+ BOOST_TEST( udta2.get() == 0 );
+ BOOST_TEST( udta.use_count() == 1 );
+ BOOST_TEST( udta2.use_count() == 1 );
+
+ BOOST_TEST( UDT_use_count == 4 ); // reality check
+
+ // test shared_ptr with a user defined type
+ UDT * up = new UDT;
+ boost::shared_ptr<UDT> sup ( up );
+ BOOST_TEST( up == sup.get() );
+ BOOST_TEST( sup.use_count() == 1 );
+
+ sup->value( 54321 ) ;
+ BOOST_TEST( sup->value() == 54321 );
+ BOOST_TEST( up->value() == 54321 );
+
+ boost::shared_ptr<UDT> sup2;
+ sup2 = sup;
+ BOOST_TEST( sup2->value() == 54321 );
+ BOOST_TEST( sup.use_count() == 2 );
+ BOOST_TEST( sup2.use_count() == 2 );
+ sup2 = sup2;
+ BOOST_TEST( sup2->value() == 54321 );
+ BOOST_TEST( sup.use_count() == 2 );
+ BOOST_TEST( sup2.use_count() == 2 );
+
+ std::cout << "OK\n";
+
+ new char[12345]; // deliberate memory leak to verify leaks detected
+}
+
+int main()
+{
+ test();
+ return boost::report_errors();
+}
+//]
\ No newline at end of file


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk