mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-11-02 16:51:59 +01:00
Compare commits
1 Commits
boost-1.17
...
boost-1.21
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2c2b8875a |
@@ -9,6 +9,8 @@
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 21 Jan 01 Suppress some useless warnings with MSVC (David Abrahams)
|
||||
// 19 Oct 00 Make shared_ptr ctor from auto_ptr explicit. (Robert Vugts)
|
||||
// 24 Jul 00 Change throw() to // never throws. See lib guidelines
|
||||
// Exception-specification rationale. (Beman Dawes)
|
||||
// 22 Jun 00 Remove #if continuations to fix GCC 2.95.2 problem (Beman Dawes)
|
||||
@@ -74,7 +76,14 @@ template<typename T> class scoped_ptr : noncopyable {
|
||||
|
||||
void reset( T* p=0 ) { if ( ptr != p ) { delete ptr; ptr = p; } }
|
||||
T& operator*() const { return *ptr; } // never throws
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable:4284) // return type for 'identifier::operator->' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation
|
||||
#endif
|
||||
T* operator->() const { return ptr; } // never throws
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
T* get() const { return ptr; } // never throws
|
||||
#ifdef BOOST_SMART_PTR_CONVERSION
|
||||
// get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
|
||||
@@ -138,12 +147,13 @@ template<typename T> class shared_ptr {
|
||||
shared_ptr(const shared_ptr<Y>& r) : px(r.px) { // never throws
|
||||
++*(pn = r.pn);
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_AUTO_PTR
|
||||
template<typename Y>
|
||||
shared_ptr(std::auto_ptr<Y>& r) {
|
||||
explicit shared_ptr(std::auto_ptr<Y>& r) {
|
||||
pn = new long(1); // may throw
|
||||
px = r.release(); // fix: moved here to stop leak if new throws
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename Y>
|
||||
shared_ptr& operator=(const shared_ptr<Y>& r) {
|
||||
@@ -151,6 +161,7 @@ template<typename T> class shared_ptr {
|
||||
return *this;
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_AUTO_PTR
|
||||
template<typename Y>
|
||||
shared_ptr& operator=(std::auto_ptr<Y>& r) {
|
||||
// code choice driven by guarantee of "no effect if new throws"
|
||||
@@ -163,8 +174,10 @@ template<typename T> class shared_ptr {
|
||||
px = r.release(); // fix: moved here so doesn't leak if new throws
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
shared_ptr(std::auto_ptr<T>& r) {
|
||||
#ifndef BOOST_NO_AUTO_PTR
|
||||
explicit shared_ptr(std::auto_ptr<T>& r) {
|
||||
pn = new long(1); // may throw
|
||||
px = r.release(); // fix: moved here to stop leak if new throws
|
||||
}
|
||||
@@ -180,6 +193,7 @@ template<typename T> class shared_ptr {
|
||||
px = r.release(); // fix: moved here so doesn't leak if new throws
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void reset(T* p=0) {
|
||||
@@ -198,7 +212,14 @@ template<typename T> class shared_ptr {
|
||||
} // reset
|
||||
|
||||
T& operator*() const { return *px; } // never throws
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable:4284) // return type for 'identifier::operator->' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation
|
||||
#endif
|
||||
T* operator->() const { return px; } // never throws
|
||||
#ifdef BOOST_MSVC
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
T* get() const { return px; } // never throws
|
||||
#ifdef BOOST_SMART_PTR_CONVERSION
|
||||
// get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk!
|
||||
@@ -370,3 +391,4 @@ template<typename T>
|
||||
|
||||
#endif // BOOST_SMART_PTR_HPP
|
||||
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
<tr>
|
||||
<td bgcolor="#FFFFFF"><img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" width="277" height="86"></td>
|
||||
<td><a href="../../index.htm"><font face="Arial" color="#FFFFFF"><big>Home</big></font></a></td>
|
||||
<td><a href="../../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries</big></font></a></td>
|
||||
<td><a href="../../people.htm"><font face="Arial" color="#FFFFFF"><big>People</big></font></a></td>
|
||||
<td><a href="../libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries</big></font></a></td>
|
||||
<td><a href="../../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People</big></font></a></td>
|
||||
<td><a href="../../more/faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ</big></font></a></td>
|
||||
<td><a href="../../more/index.htm"><font face="Arial" color="#FFFFFF"><big>More</big></font></a></td>
|
||||
</tr>
|
||||
@@ -27,11 +27,10 @@ expressions.
|
||||
<li><a href="smart_ptr.htm">Documentation</a> (HTML).</li>
|
||||
<li>Header <a href="../../boost/smart_ptr.hpp">smart_ptr.hpp</a></li>
|
||||
<li>Test program <a href="smart_ptr_test.cpp">smart_ptr_test.cpp</a>.</li>
|
||||
<li>Download <a href="../../boost_all.zip">all of Boost</a> (ZIP format).</li>
|
||||
<li>Submitted by <a href="../../people/greg_colvin.htm">Greg Colvin</a> and <a href="../../people/beman_dawes.html">Beman
|
||||
Dawes</a>.</li>
|
||||
</ul>
|
||||
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan -->02 Aug 2000<!--webbot bot="Timestamp" endspan i-checksum="14748" -->
|
||||
<p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan -->14 Mar 2001<!--webbot bot="Timestamp" endspan i-checksum="14885" -->
|
||||
</p>
|
||||
|
||||
</body>
|
||||
|
||||
@@ -23,7 +23,7 @@ shared_array</strong> works by default for Standard Library's Associative
|
||||
Container Compare template parameter. For compilers not supporting partial
|
||||
specialization, the user must explicitly pass the less<> functor.</p>
|
||||
<p>Class<strong> shared_array</strong> cannot correctly hold a pointer to a
|
||||
single object. See <a href="shared_ptr.htm"><strong>shared_array</strong></a>
|
||||
single object. See <a href="shared_ptr.htm"><strong>shared_ptr</strong></a>
|
||||
for that usage.</p>
|
||||
<p>Class<strong> shared_array</strong> will not work correctly with cyclic data
|
||||
structures. For example, if main() holds a shared_array pointing to array A,
|
||||
|
||||
@@ -192,9 +192,34 @@ stored pointer.</p>
|
||||
<p><code>void swap( shared_ptr<T>& other ) throw()</code></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>
|
||||
<p>[To be supplied. In the meantime, see <a href="smart_ptr_test.cpp">smart_ptr_test.cpp</a>.]</p>
|
||||
<pre>// 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).
|
||||
|
||||
class Foo { ... };
|
||||
|
||||
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
|
||||
variety of circumstances. Without using a smart pointer, memory and
|
||||
exception management would be a nightmare.</p>
|
||||
<hr>
|
||||
<p>Revised December 8, 1999</p>
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->09 February, 2001<!--webbot bot="Timestamp" endspan i-checksum="40412" -->
|
||||
</p>
|
||||
<p><EFBFBD> Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use,
|
||||
modify, sell and distribute this document is granted provided this copyright
|
||||
notice appears in all copies. This document is provided "as is"
|
||||
|
||||
Reference in New Issue
Block a user