mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-01 21:54:28 +02:00
Fix doc errors, add shared_ptr_example program
[SVN r10165]
This commit is contained in:
@@ -106,7 +106,7 @@ output:</p>
|
|||||||
Buckle my shoe</pre>
|
Buckle my shoe</pre>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<h2>Handle/Body Idiom</h2>
|
<h2>Handle/Body Idiom</h2>
|
||||||
<p>One common usage of <b>shared_pointer</b> is to implement a handle/body
|
<p>One common usage of <b>scoped_ptr</b> is to implement a handle/body
|
||||||
structure which avoids exposing the body (implementation) in the header file:</p>
|
structure which avoids exposing the body (implementation) in the header file:</p>
|
||||||
<pre>class handle
|
<pre>class handle
|
||||||
{
|
{
|
||||||
@@ -123,7 +123,7 @@ body</code> is not visible at the time scoped_ptr<> deletes it. See ISO
|
|||||||
5.3.5/5. Note that some compilers will issue a warning even though the
|
5.3.5/5. Note that some compilers will issue a warning even though the
|
||||||
above code is well defined.</p>
|
above code is well defined.</p>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B %Y" startspan -->10 May 2001<!--webbot bot="Timestamp" endspan i-checksum="15101" --></p>
|
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B %Y" startspan -->21 May 2001<!--webbot bot="Timestamp" endspan i-checksum="15104" --></p>
|
||||||
<p><EFBFBD> Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use,
|
<p><EFBFBD> Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use,
|
||||||
modify, sell and distribute this document is granted provided this copyright
|
modify, sell and distribute this document is granted provided this copyright
|
||||||
notice appears in all copies. This document is provided "as is"
|
notice appears in all copies. This document is provided "as is"
|
||||||
|
@@ -192,33 +192,17 @@ stored pointer.</p>
|
|||||||
<p><code>void swap( shared_ptr<T>& other ) throw()</code></p>
|
<p><code>void swap( shared_ptr<T>& other ) throw()</code></p>
|
||||||
<p>Swaps the two smart pointers, as if by std::swap.</p>
|
<p>Swaps the two smart pointers, as if by std::swap.</p>
|
||||||
<h2>Class <a name="shared_ptr_example">shared_ptr example</a></h2>
|
<h2>Class <a name="shared_ptr_example">shared_ptr example</a></h2>
|
||||||
<pre>// The application will produce a series of
|
<p>See <a href="shared_ptr_example.cpp"> shared_ptr_example.cpp</a> for a complete example program.</p>
|
||||||
// objects of type Foo which later must be
|
<p>This program builds a std::vector and std::set of FooPtr's.</p>
|
||||||
// accessed both by occurrence (std::vector)
|
<p>Note that after the two containers have been populated, some of the FooPtr objects
|
||||||
// and by ordering relationship (std::set).
|
will have use_count()==1 rather than use_count()==2, since foo_set is a std::set
|
||||||
|
rather than a std::multiset. Furthermore, use_count() may be even higher
|
||||||
class Foo { ... };
|
at various times while push_back() and insert() container operations are performed.
|
||||||
|
|
||||||
typedef boost::shared_ptr<Foo> FooPtr;
|
|
||||||
|
|
||||||
std::vector<FooPtr> foo_vector;
|
|
||||||
std::set<FooPtr> foo_set; // NOT multiset!
|
|
||||||
|
|
||||||
...
|
|
||||||
{ // creation loop
|
|
||||||
FooPtr foo_ptr ( new Foo( ... ) );
|
|
||||||
foo_vector.push_back( foo_ptr );
|
|
||||||
foo_set.insert( foo_ptr );
|
|
||||||
}</pre>
|
|
||||||
<p>Note that at the termination of the creation loop, some of the FooPtr objects
|
|
||||||
may have use_count()==1 rather than use_count()==2, since foo_set is a std::set
|
|
||||||
rather than a std::multiset. Furthermore, use_count() will be even higher
|
|
||||||
at various times inside the loop, as container operations are performed.
|
|
||||||
More complicated yet, the container operations may throw exceptions under a
|
More complicated yet, the container operations may throw exceptions under a
|
||||||
variety of circumstances. Without using a smart pointer, memory and
|
variety of circumstances. Without using a smart pointer, memory and
|
||||||
exception management would be a nightmare.</p>
|
exception management would be a nightmare.</p>
|
||||||
<hr>
|
<hr>
|
||||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->10 February, 2001<!--webbot bot="Timestamp" endspan i-checksum="40395" -->
|
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->21 May, 2001<!--webbot bot="Timestamp" endspan i-checksum="13958" -->
|
||||||
</p>
|
</p>
|
||||||
<p><EFBFBD> Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use,
|
<p><EFBFBD> Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use,
|
||||||
modify, sell and distribute this document is granted provided this copyright
|
modify, sell and distribute this document is granted provided this copyright
|
||||||
|
96
shared_ptr_example.cpp
Normal file
96
shared_ptr_example.cpp
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
// Boost shared_ptr_example.cpp --------------------------------------------//
|
||||||
|
|
||||||
|
// (C) Copyright Beman Dawes 2001. Permission to copy,
|
||||||
|
// use, modify, sell and distribute this software is granted provided this
|
||||||
|
// copyright notice appears in all copies. 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 for most recent version including documentation.
|
||||||
|
|
||||||
|
// Revision History
|
||||||
|
// 21 May 01 Initial complete version (Beman Dawes)
|
||||||
|
|
||||||
|
// The original code for this example appeared in the shared_ptr documentation.
|
||||||
|
// Ray Gallimore pointed out that foo_set was missing a Compare template
|
||||||
|
// argument, so would not work as intended. At that point the code was
|
||||||
|
// turned into an actual .cpp file so it could be compiled and tested.
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <boost/smart_ptr.hpp>
|
||||||
|
|
||||||
|
// The application will produce a series of
|
||||||
|
// objects of type Foo which later must be
|
||||||
|
// accessed both by occurrence (std::vector)
|
||||||
|
// and by ordering relationship (std::set).
|
||||||
|
|
||||||
|
struct Foo
|
||||||
|
{
|
||||||
|
Foo( int _x ) : x(_x) {}
|
||||||
|
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
|
||||||
|
int x;
|
||||||
|
/* ... */
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef boost::shared_ptr<Foo> FooPtr;
|
||||||
|
|
||||||
|
struct FooPtrOps
|
||||||
|
{
|
||||||
|
bool operator()( const FooPtr & a, const FooPtr & b )
|
||||||
|
{ return a->x > b->x; }
|
||||||
|
void operator()( const FooPtr & a )
|
||||||
|
{ std::cout << a->x << "\n"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::vector<FooPtr> foo_vector;
|
||||||
|
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset!
|
||||||
|
|
||||||
|
FooPtr foo_ptr( new Foo( 2 ) );
|
||||||
|
foo_vector.push_back( foo_ptr );
|
||||||
|
foo_set.insert( foo_ptr );
|
||||||
|
|
||||||
|
foo_ptr.reset( new Foo( 1 ) );
|
||||||
|
foo_vector.push_back( foo_ptr );
|
||||||
|
foo_set.insert( foo_ptr );
|
||||||
|
|
||||||
|
foo_ptr.reset( new Foo( 3 ) );
|
||||||
|
foo_vector.push_back( foo_ptr );
|
||||||
|
foo_set.insert( foo_ptr );
|
||||||
|
|
||||||
|
foo_ptr.reset ( new Foo( 2 ) );
|
||||||
|
foo_vector.push_back( foo_ptr );
|
||||||
|
foo_set.insert( foo_ptr );
|
||||||
|
|
||||||
|
std::cout << "foo_vector:\n";
|
||||||
|
std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
|
||||||
|
|
||||||
|
std::cout << "\nfoo_set:\n";
|
||||||
|
std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() );
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
// Expected output:
|
||||||
|
//
|
||||||
|
// foo_vector:
|
||||||
|
// 2
|
||||||
|
// 1
|
||||||
|
// 3
|
||||||
|
// 2
|
||||||
|
//
|
||||||
|
// foo_set:
|
||||||
|
// 3
|
||||||
|
// 2
|
||||||
|
// 1
|
||||||
|
//
|
||||||
|
// Destructing a Foo with x=2
|
||||||
|
// Destructing a Foo with x=1
|
||||||
|
// Destructing a Foo with x=3
|
||||||
|
// Destructing a Foo with x=2
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user