From 468c41041b3d7ec0394cea52e3c24b7ba90743f7 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 7 Jul 2000 16:04:40 +0000 Subject: [PATCH 01/53] This commit was generated by cvs2svn to compensate for changes in r4, which included commits to RCS files with non-trunk default branches. [SVN r7621] --- include/boost/smart_ptr.hpp | 369 ++++++++++++++++++++++++++++++++++++ smart_ptr_test.cpp | 263 +++++++++++++++++++++++++ 2 files changed, 632 insertions(+) create mode 100644 include/boost/smart_ptr.hpp create mode 100644 smart_ptr_test.cpp diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp new file mode 100644 index 0000000..e233104 --- /dev/null +++ b/include/boost/smart_ptr.hpp @@ -0,0 +1,369 @@ +// Boost smart_ptr.hpp header file -----------------------------------------// + +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. 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 +// 22 Jun 00 Remove #if continuations to fix GCC 2.95.2 problem (Beman Dawes) +// 1 Feb 00 Additional shared_ptr BOOST_NO_MEMBER_TEMPLATES workarounds +// (Dave Abrahams) +// 31 Dec 99 Condition tightened for no member template friend workaround +// (Dave Abrahams) +// 30 Dec 99 Moved BOOST_NMEMBER_TEMPLATES compatibility code to config.hpp +// (Dave Abrahams) +// 30 Nov 99 added operator ==, operator !=, and std::swap and std::less +// specializations for shared types (Darin Adler) +// 11 Oct 99 replaced op[](int) with op[](std::size_t) (Ed Brey, Valentin +// Bonnard), added shared_ptr workaround for no member template +// friends (Matthew Langston) +// 25 Sep 99 added shared_ptr::swap and shared_array::swap (Luis Coelho). +// 20 Jul 99 changed name to smart_ptr.hpp, #include , +// #include and use boost::noncopyable +// 17 May 99 remove scoped_array and shared_array operator*() as +// unnecessary (Beman Dawes) +// 14 May 99 reorder code so no effects when bad_alloc thrown (Abrahams/Dawes) +// 13 May 99 remove certain throw() specifiers to avoid generated try/catch +// code cost (Beman Dawes) +// 11 May 99 get() added, conversion to T* placed in macro guard (Valentin +// Bonnard, Dave Abrahams, and others argued for elimination +// of the automatic conversion) +// 28 Apr 99 #include fix (Valentin Bonnard) +// 28 Apr 99 rename transfer() to share() for clarity (Dave Abrahams) +// 28 Apr 99 remove unsafe shared_array template conversions(Valentin Bonnard) +// 28 Apr 99 p(r) changed to p(r.px) for clarity (Dave Abrahams) +// 21 Apr 99 reset() self assignment fix (Valentin Bonnard) +// 21 Apr 99 dispose() provided to improve clarity (Valentin Bonnard) +// 27 Apr 99 leak when new throws fixes (Dave Abrahams) +// 21 Oct 98 initial Version (Greg Colvin/Beman Dawes) + +#ifndef BOOST_SMART_PTR_HPP +#define BOOST_SMART_PTR_HPP + +#include // for broken compiler workarounds +#include // for std::size_t +#include // for std::auto_ptr +#include // for std::swap +#include // for boost::noncopyable +#include // for std::less + +namespace boost { + +// scoped_ptr --------------------------------------------------------------// + +// 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; +// see shared_ptr (below) or std::auto_ptr if your needs are more complex. + +template class scoped_ptr : noncopyable { + + T* ptr; + + public: + typedef T element_type; + + explicit scoped_ptr( T* p=0 ) throw() : ptr(p) {} + ~scoped_ptr() { delete ptr; } + + void reset( T* p=0 ) { if ( ptr != p ) { delete ptr; ptr = p; } } + T& operator*() const throw() { return *ptr; } + T* operator->() const throw() { return ptr; } + T* get() const throw() { return ptr; } +#ifdef BOOST_SMART_PTR_CONVERSION + // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! + operator T*() const throw() { return ptr; } +#endif + }; // scoped_ptr + +// scoped_array ------------------------------------------------------------// + +// 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(). See shared_array or std::vector if your needs are more complex. + +template class scoped_array : noncopyable { + + T* ptr; + + public: + typedef T element_type; + + explicit scoped_array( T* p=0 ) throw() : ptr(p) {} + ~scoped_array() { delete [] ptr; } + + void reset( T* p=0 ) { if ( ptr != p ) {delete [] ptr; ptr=p;} } + + T* get() const throw() { return ptr; } +#ifdef BOOST_SMART_PTR_CONVERSION + // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! + operator T*() const throw() { return ptr; } +#else + T& operator[](std::size_t i) const throw() { return ptr[i]; } +#endif + }; // scoped_array + +// shared_ptr --------------------------------------------------------------// + +// An enhanced relative of scoped_ptr with reference counted copy semantics. +// The object pointed to is deleted when the last shared_ptr pointing to it +// is destroyed or reset. + +template class shared_ptr { + public: + typedef T element_type; + + explicit shared_ptr(T* p =0) : px(p) { + try { pn = new long(1); } // fix: prevent leak if new throws + catch (...) { delete p; throw; } + } + + shared_ptr(const shared_ptr& r) throw() : px(r.px) { ++*(pn = r.pn); } + + ~shared_ptr() { dispose(); } + + shared_ptr& operator=(const shared_ptr& r) { + share(r.px,r.pn); + return *this; + } + +#if !defined( BOOST_NO_MEMBER_TEMPLATES ) + template + shared_ptr(const shared_ptr& r) throw() : px(r.px) { + ++*(pn = r.pn); + } + + template + shared_ptr(std::auto_ptr& r) { + pn = new long(1); // may throw + px = r.release(); // fix: moved here to stop leak if new throws + } + + template + shared_ptr& operator=(const shared_ptr& r) { + share(r.px,r.pn); + return *this; + } + + template + shared_ptr& operator=(std::auto_ptr& r) { + // code choice driven by guarantee of "no effect if new throws" + if (*pn == 1) { delete px; } + else { // allocate new reference counter + long * tmp = new long(1); // may throw + --*pn; // only decrement once danger of new throwing is past + pn = tmp; + } // allocate new reference counter + px = r.release(); // fix: moved here so doesn't leak if new throws + return *this; + } +#else + shared_ptr(std::auto_ptr& r) { + pn = new long(1); // may throw + px = r.release(); // fix: moved here to stop leak if new throws + } + + shared_ptr& operator=(std::auto_ptr& r) { + // code choice driven by guarantee of "no effect if new throws" + if (*pn == 1) { delete px; } + else { // allocate new reference counter + long * tmp = new long(1); // may throw + --*pn; // only decrement once danger of new throwing is past + pn = tmp; + } // allocate new reference counter + px = r.release(); // fix: moved here so doesn't leak if new throws + return *this; + } +#endif + + void reset(T* p=0) { + if ( px == p ) return; // fix: self-assignment safe + if (--*pn == 0) { delete px; } + else { // allocate new reference counter + try { pn = new long; } // fix: prevent leak if new throws + catch (...) { + ++*pn; // undo effect of --*pn above to meet effects guarantee + delete p; + throw; + } // catch + } // allocate new reference counter + *pn = 1; + px = p; + } // reset + + T& operator*() const throw() { return *px; } + T* operator->() const throw() { return px; } + T* get() const throw() { return px; } + #ifdef BOOST_SMART_PTR_CONVERSION + // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! + operator T*() const throw() { return px; } + #endif + + long use_count() const throw(){ return *pn; } + bool unique() const throw() { return *pn == 1; } + + void swap(shared_ptr& other) throw() + { std::swap(px,other.px); std::swap(pn,other.pn); } + +// Tasteless as this may seem, making all members public allows member templates +// to work in the absence of member template friends. (Matthew Langston) +// Don't split this line into two; that causes problems for some GCC 2.95.2 builds +#if defined(BOOST_NO_MEMBER_TEMPLATES) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) + private: +#endif + + T* px; // contained pointer + long* pn; // ptr to reference counter + +// Don't split this line into two; that causes problems for some GCC 2.95.2 builds +#if !defined( BOOST_NO_MEMBER_TEMPLATES ) && !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) + template friend class shared_ptr; +#endif + + void dispose() { if (--*pn == 0) { delete px; delete pn; } } + + void share(T* rpx, long* rpn) { + if (pn != rpn) { + dispose(); + px = rpx; + ++*(pn = rpn); + } + } // share +}; // shared_ptr + +template + inline bool operator==(const shared_ptr& a, const shared_ptr& b) + { return a.get() == b.get(); } + +template + inline bool operator!=(const shared_ptr& a, const shared_ptr& b) + { return a.get() != b.get(); } + +// shared_array ------------------------------------------------------------// + +// shared_array extends shared_ptr to arrays. +// The array pointed to is deleted when the last shared_array pointing to it +// is destroyed or reset. + +template class shared_array { + public: + typedef T element_type; + + explicit shared_array(T* p =0) : px(p) { + try { pn = new long(1); } // fix: prevent leak if new throws + catch (...) { delete [] p; throw; } + } + + shared_array(const shared_array& r) throw() : px(r.px) { ++*(pn = r.pn); } + + ~shared_array() { dispose(); } + + shared_array& operator=(const shared_array& r) { + if (pn != r.pn) { + dispose(); + px = r.px; + ++*(pn = r.pn); + } + return *this; + } // operator= + + void reset(T* p=0) { + if ( px == p ) return; // fix: self-assignment safe + if (--*pn == 0) { delete [] px; } + else { // allocate new reference counter + try { pn = new long; } // fix: prevent leak if new throws + catch (...) { + ++*pn; // undo effect of --*pn above to meet effects guarantee + delete [] p; + throw; + } // catch + } // allocate new reference counter + *pn = 1; + px = p; + } // reset + + T* get() const throw() { return px; } + #ifdef BOOST_SMART_PTR_CONVERSION + // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! + operator T*() const throw() { return px; } + #else + T& operator[](std::size_t i) const throw() { return px[i]; } + #endif + + long use_count() const throw() { return *pn; } + bool unique() const throw() { return *pn == 1; } + + void swap(shared_array& other) throw() + { std::swap(px,other.px); std::swap(pn,other.pn); } + + private: + + T* px; // contained pointer + long* pn; // ptr to reference counter + + void dispose() { if (--*pn == 0) { delete [] px; delete pn; } } + +}; // shared_array + +template + inline bool operator==(const shared_array& a, const shared_array& b) + { return a.get() == b.get(); } + +template + inline bool operator!=(const shared_array& a, const shared_array& b) + { return a.get() != b.get(); } + +} // namespace boost + +// specializations for things in namespace std -----------------------------// + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + +namespace std { + +// Specialize std::swap to use the fast, non-throwing swap that's provided +// as a member function instead of using the default algorithm which creates +// a temporary and uses assignment. + +template + inline void swap(boost::shared_ptr& a, boost::shared_ptr& b) + { a.swap(b); } + +template + inline void swap(boost::shared_array& a, boost::shared_array& b) + { a.swap(b); } + +// Specialize std::less so we can use shared pointers and arrays as keys in +// associative collections. + +// It's still a controversial question whether this is better than supplying +// a full range of comparison operators (<, >, <=, >=). + +template + struct less< boost::shared_ptr > + : binary_function, boost::shared_ptr, bool> + { + bool operator()(const boost::shared_ptr& a, + const boost::shared_ptr& b) const + { return less()(a.get(),b.get()); } + }; + +template + struct less< boost::shared_array > + : binary_function, boost::shared_array, bool> + { + bool operator()(const boost::shared_array& a, + const boost::shared_array& b) const + { return less()(a.get(),b.get()); } + }; + +} // namespace std + +#endif // ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + +#endif // BOOST_SMART_PTR_HPP + diff --git a/smart_ptr_test.cpp b/smart_ptr_test.cpp new file mode 100644 index 0000000..cd4d61b --- /dev/null +++ b/smart_ptr_test.cpp @@ -0,0 +1,263 @@ +// smart pointer test program ----------------------------------------------// + +// (C) Copyright Beman Dawes 1998, 1999. 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. + +// Revision History +// 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 +#include +#include +#include +#include + +#ifdef NDEBUG +#error This test program makes no sense if NDEBUG is defined +#endif + +using namespace std; +using boost::scoped_ptr; +using boost::scoped_array; +using boost::shared_ptr; +using boost::shared_array; + +template +void ck( const T* v1, T v2 ) { assert( *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; + cout << "UDT with value " << value_ << " being destroyed" << endl; + } + long value() const { return value_; } + void value( long v ) { value_ = v;; } + }; // UDT + +// main --------------------------------------------------------------------// + +// This isn't a very systematic test; it just hits some of the basics. + +int main() { + + assert( UDT_use_count == 0 ); // reality check + + // test scoped_ptr with a built-in type + long * lp = new long; + scoped_ptr sp ( lp ); + assert( sp.get() == lp ); + assert( lp == sp.get() ); + assert( &*sp == lp ); + + *sp = 1234568901L; + assert( *sp == 1234568901L ); + assert( *lp == 1234568901L ); + ck( static_cast(sp.get()), 1234568901L ); + ck( lp, *sp ); + + sp.reset(); + assert( sp.get() == 0 ); + + // test scoped_ptr with a user defined type + scoped_ptr udt_sp ( new UDT( 999888777 ) ); + assert( udt_sp->value() == 999888777 ); + udt_sp.reset(); + udt_sp.reset( new UDT( 111222333 ) ); + assert( udt_sp->value() == 111222333 ); + udt_sp.reset( new UDT( 333222111 ) ); + assert( udt_sp->value() == 333222111 ); + + // test scoped_array with a build-in type + char * sap = new char [ 100 ]; + scoped_array sa ( sap ); + assert( sa.get() == sap ); + assert( sap == sa.get() ); + + strcpy( sa.get(), "Hot Dog with mustard and relish" ); + assert( strcmp( sa.get(), "Hot Dog with mustard and relish" ) == 0 ); + assert( strcmp( sap, "Hot Dog with mustard and relish" ) == 0 ); + + assert( sa[0] == 'H' ); + assert( sa[30] == 'h' ); + + sa[0] = 'N'; + sa[4] = 'd'; + assert( strcmp( sap, "Not dog with mustard and relish" ) == 0 ); + + sa.reset(); + assert( sa.get() == 0 ); + + // test shared_ptr with a built-in type + int * ip = new int; + shared_ptr cp ( ip ); + assert( ip == cp.get() ); + assert( cp.use_count() == 1 ); + + *cp = 54321; + assert( *cp == 54321 ); + assert( *ip == 54321 ); + ck( static_cast(cp.get()), 54321 ); + ck( static_cast(ip), *cp ); + + shared_ptr cp2 ( cp ); + assert( ip == cp2.get() ); + assert( cp.use_count() == 2 ); + assert( cp2.use_count() == 2 ); + + assert( *cp == 54321 ); + assert( *cp2 == 54321 ); + ck( static_cast(cp2.get()), 54321 ); + ck( static_cast(ip), *cp2 ); + + shared_ptr cp3 ( cp ); + assert( cp.use_count() == 3 ); + assert( cp2.use_count() == 3 ); + assert( cp3.use_count() == 3 ); + cp.reset(); + assert( cp2.use_count() == 2 ); + assert( cp3.use_count() == 2 ); + assert( cp.use_count() == 1 ); + cp.reset( new int ); + *cp = 98765; + assert( *cp == 98765 ); + *cp3 = 87654; + assert( *cp3 == 87654 ); + assert( *cp2 == 87654 ); + cp.swap( cp3 ); + assert( *cp == 87654 ); + assert( *cp2 == 87654 ); + assert( *cp3 == 98765 ); + cp.swap( cp3 ); + assert( *cp == 98765 ); + assert( *cp2 == 87654 ); + assert( *cp3 == 87654 ); + cp2 = cp2; + assert( cp2.use_count() == 2 ); + assert( *cp2 == 87654 ); + cp = cp2; + assert( cp2.use_count() == 3 ); + assert( *cp2 == 87654 ); + assert( cp.use_count() == 3 ); + assert( *cp == 87654 ); + + shared_ptr cp4; + swap( cp2, cp4 ); + assert( cp4.use_count() == 3 ); + assert( *cp4 == 87654 ); + assert( cp2.get() == 0 ); + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + set< shared_ptr > scp; + scp.insert(cp4); + assert( scp.find(cp4) != scp.end() ); + assert( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); +#endif + + // test shared_array with a built-in type + char * cap = new char [ 100 ]; + shared_array ca ( cap ); + assert( ca.get() == cap ); + assert( cap == ca.get() ); + assert( &ca[0] == cap ); + + strcpy( ca.get(), "Hot Dog with mustard and relish" ); + assert( strcmp( ca.get(), "Hot Dog with mustard and relish" ) == 0 ); + assert( strcmp( cap, "Hot Dog with mustard and relish" ) == 0 ); + + assert( ca[0] == 'H' ); + assert( ca[30] == 'h' ); + + shared_array ca2 ( ca ); + shared_array ca3 ( ca2 ); + + ca[0] = 'N'; + ca[4] = 'd'; + assert( strcmp( ca.get(), "Not dog with mustard and relish" ) == 0 ); + assert( strcmp( ca2.get(), "Not dog with mustard and relish" ) == 0 ); + assert( strcmp( ca3.get(), "Not dog with mustard and relish" ) == 0 ); + assert( ca.use_count() == 3 ); + assert( ca2.use_count() == 3 ); + assert( ca3.use_count() == 3 ); + ca2.reset(); + assert( ca.use_count() == 2 ); + assert( ca3.use_count() == 2 ); + assert( ca2.use_count() == 1 ); + + ca.reset(); + assert( ca.get() == 0 ); + + shared_array ca4; + swap( ca3, ca4 ); + assert( ca4.use_count() == 1 ); + assert( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 ); + assert( ca3.get() == 0 ); + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + set< shared_array > sca; + sca.insert(ca4); + assert( sca.find(ca4) != sca.end() ); + assert( sca.find(ca4) == sca.find( shared_array(ca4) ) ); +#endif + + // test shared_array with user defined type + shared_array udta ( new UDT[3] ); + + udta[0].value( 111 ); + udta[1].value( 222 ); + udta[2].value( 333 ); + shared_array udta2 ( udta ); + + assert( udta[0].value() == 111 ); + assert( udta[1].value() == 222 ); + assert( udta[2].value() == 333 ); + assert( udta2[0].value() == 111 ); + assert( udta2[1].value() == 222 ); + assert( udta2[2].value() == 333 ); + udta2.reset(); + assert( udta2.get() == 0 ); + assert( udta.use_count() == 1 ); + assert( udta2.use_count() == 1 ); + + assert( UDT_use_count == 4 ); // reality check + + // test shared_ptr with a user defined type + UDT * up = new UDT; + shared_ptr sup ( up ); + assert( up == sup.get() ); + assert( sup.use_count() == 1 ); + + sup->value( 54321 ) ; + assert( sup->value() == 54321 ); + assert( up->value() == 54321 ); + + shared_ptr sup2; + sup2 = sup; + assert( sup2->value() == 54321 ); + assert( sup.use_count() == 2 ); + assert( sup2.use_count() == 2 ); + sup2 = sup2; + assert( sup2->value() == 54321 ); + assert( sup.use_count() == 2 ); + assert( sup2.use_count() == 2 ); + + cout << "OK" << endl; + + new char[12345]; // deliberate memory leak to verify leaks detected + + return 0; + } // main + From 90b7ec19d115aa2a27a8d3c3fdbe620fbb002bd4 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 24 Jul 2000 16:21:10 +0000 Subject: [PATCH 02/53] Changed throw() to // never throws. [SVN r7628] --- include/boost/smart_ptr.hpp | 53 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index e233104..353e472 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,8 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 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) // 1 Feb 00 Additional shared_ptr BOOST_NO_MEMBER_TEMPLATES workarounds // (Dave Abrahams) @@ -67,16 +69,16 @@ template class scoped_ptr : noncopyable { public: typedef T element_type; - explicit scoped_ptr( T* p=0 ) throw() : ptr(p) {} + explicit scoped_ptr( T* p=0 ) : ptr(p) {} // never throws ~scoped_ptr() { delete ptr; } void reset( T* p=0 ) { if ( ptr != p ) { delete ptr; ptr = p; } } - T& operator*() const throw() { return *ptr; } - T* operator->() const throw() { return ptr; } - T* get() const throw() { return ptr; } + T& operator*() const { return *ptr; } // never throws + T* operator->() const { return ptr; } // never throws + T* get() const { return ptr; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const throw() { return ptr; } + operator T*() const { return ptr; } // never throws #endif }; // scoped_ptr @@ -93,17 +95,17 @@ template class scoped_array : noncopyable { public: typedef T element_type; - explicit scoped_array( T* p=0 ) throw() : ptr(p) {} + explicit scoped_array( T* p=0 ) : ptr(p) {} // never throws ~scoped_array() { delete [] ptr; } void reset( T* p=0 ) { if ( ptr != p ) {delete [] ptr; ptr=p;} } - T* get() const throw() { return ptr; } + T* get() const { return ptr; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const throw() { return ptr; } + operator T*() const { return ptr; } // never throws #else - T& operator[](std::size_t i) const throw() { return ptr[i]; } + T& operator[](std::size_t i) const { return ptr[i]; } // never throws #endif }; // scoped_array @@ -122,7 +124,7 @@ template class shared_ptr { catch (...) { delete p; throw; } } - shared_ptr(const shared_ptr& r) throw() : px(r.px) { ++*(pn = r.pn); } + shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); } // never throws ~shared_ptr() { dispose(); } @@ -133,7 +135,7 @@ template class shared_ptr { #if !defined( BOOST_NO_MEMBER_TEMPLATES ) template - shared_ptr(const shared_ptr& r) throw() : px(r.px) { + shared_ptr(const shared_ptr& r) : px(r.px) { // never throws ++*(pn = r.pn); } @@ -195,18 +197,18 @@ template class shared_ptr { px = p; } // reset - T& operator*() const throw() { return *px; } - T* operator->() const throw() { return px; } - T* get() const throw() { return px; } + T& operator*() const { return *px; } // never throws + T* operator->() const { return px; } // never throws + T* get() const { return px; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const throw() { return px; } + operator T*() const { return px; } // never throws #endif - long use_count() const throw(){ return *pn; } - bool unique() const throw() { return *pn == 1; } + long use_count() const { return *pn; } // never throws + bool unique() const { return *pn == 1; } // never throws - void swap(shared_ptr& other) throw() + void swap(shared_ptr& other) // never throws { std::swap(px,other.px); std::swap(pn,other.pn); } // Tasteless as this may seem, making all members public allows member templates @@ -258,7 +260,8 @@ template class shared_array { catch (...) { delete [] p; throw; } } - shared_array(const shared_array& r) throw() : px(r.px) { ++*(pn = r.pn); } + shared_array(const shared_array& r) : px(r.px) // never throws + { ++*(pn = r.pn); } ~shared_array() { dispose(); } @@ -286,18 +289,18 @@ template class shared_array { px = p; } // reset - T* get() const throw() { return px; } + T* get() const { return px; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const throw() { return px; } + operator T*() const { return px; } // never throws #else - T& operator[](std::size_t i) const throw() { return px[i]; } + T& operator[](std::size_t i) const { return px[i]; } // never throws #endif - long use_count() const throw() { return *pn; } - bool unique() const throw() { return *pn == 1; } + long use_count() const { return *pn; } // never throws + bool unique() const { return *pn == 1; } // never throws - void swap(shared_array& other) throw() + void swap(shared_array& other) // never throws { std::swap(px,other.px); std::swap(pn,other.pn); } private: From ed7e13da9f2d7ea2878e0bac3627d7271ab52b07 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 27 Jul 2000 14:18:23 +0000 Subject: [PATCH 03/53] Initial commit [SVN r7638] --- gccspeed.gif | Bin 0 -> 6603 bytes msvcspeed.gif | Bin 0 -> 6169 bytes smarttest.zip | Bin 0 -> 11513 bytes 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 gccspeed.gif create mode 100644 msvcspeed.gif create mode 100644 smarttest.zip diff --git a/gccspeed.gif b/gccspeed.gif new file mode 100644 index 0000000000000000000000000000000000000000..d78c06bbf8b6b72380950b597c32ada51e5186b6 GIT binary patch literal 6603 zcmZ?wbh9u|G+;_*{LTOZ#taN;3=A_F7|t*-{AXZbFlI0|W=Jz;m}$&##+czhSlT#^ zAuWwzW*WnpG=~3R)y6X!(q=NuoXK!zCc}TQX5%vqX=fN_o?$q1hT%WhEaU$SY5y5! z{%1JzpW#2)Xk!NBGzR0D48~^|jQ@jeGB!3&Gd7-SY<$Mp_&?ZU<22*6G~=0R#%I!u z|AXCOJkvOBrt!>~#%E?4{|7tE_>6Jd8RMB}jL)1g{ttGg@qgpA|Hd=_8=v`a{2%OS zV~~^6W`Z1*_Mah*0c1~F8px!yGa$7fFBzwSz|6F?Giho6!9FyenU*#)ZRX6hGc(iv zgFS3~1|&W6Oxl?T=@jGmb`^Gw>z|KPAQo(ZyP=FFLAX3qQ%4oc%QAd6?7nR({S%>Uq!HU1BB z$ISmT&-|bH9~{ueph!3~6BNv6{xh6m0EOI{G*H-_IRo-NC>o4G9s-$l2INLitQdo= zJu?$z@R|SMNHRVH^3u#RAn+d?XU6|QKAidg%$fgZ{)3~?7!=$8XM*DM|9^)644^3b zp9YGA|7SqK42oc5P{@H?c?J|1p!hZh`S1TskPrU<2S>m08Bk!&JOeWNKR7iQ{|ANL z%>N*p{(}<-C@s<*EB<5w+o=O0L3zS~<3GcCP8p943l27O2y4Zh*s$<$yMV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g2nL2op+Nbtk`W~Be+t;HD#mgO^=fE XE|U^`I^D$cj~KekmiOo=FjxZs3WTu? literal 0 HcmV?d00001 diff --git a/msvcspeed.gif b/msvcspeed.gif new file mode 100644 index 0000000000000000000000000000000000000000..56295ee9a506c65a672bf105bb1a0f357377f579 GIT binary patch literal 6169 zcmZ?wbh9u|G+;_*{LTOZ#taN;3=A_F7|t*-{AXZbFlI0|W=Jz;m}$&##+czhSlT#^ zAuWwzW*WnpG=~3R)y6X!(q=NuoXK!zCc}TQX5%vqX=fN_o?$q1hT%WhEaU$SY5y5! z{%1JzpW#2)Xk!NBGzR0D48~^|jQ@jeGB!3&Gd7-SY<$Mp_&?ZU<22*6G~=0R#%I!u z|AXCOJkvOBrt!>~#%E?4{|7tE_>6Jd8RMB}jL)1g{ttGg@qgpA|Hd=_8=v`a{2%OS zV~~^6W`Z1*_Mah*0c1~F8px!yGa$7fFBzwSz|6F?Giho6!9FyenU*#)ZRX6hGc(iv zgFS3~1|&W6Oxl?T=@jGmb`^Gw>z|KPAQo(ZyP=FFLAX3qQ%4oc%QAd6?7nR({S%>Uq!HU1BB z$ISmT&-|bH9~{ueph!3~6BNv6{xh6m0EOI{G*H-_IRo-NC>o4G9s-$l2INLitQdo= zJu?$z@R|SMNHRVH^3u#RAn+d?XU6|QKAidg%$fgZ{)3~?7!=$8XM*DM|9^)644^3b zp9YGA|7SqK42oc5P{@H?c?J|1p!hZh`S1TskPrU<2S>m08Bk!&JOeWNKR7iQ{|ANL z%>N*p{(}<-C@s<*EB<5w+o=O0L3zS~<3GcCP8p943l27O2y4Zh*s$<$yMVIS9FL8Q zj&@5JXWcomaq;nf1?Mgq&rM4_7&HR;LNq5lHJz>*yp6@-LaOg9?ZR6rCxxG%nQxr@ z>Pw`+vkP-P;^lmIP1Xol>ba0DS8Hol;BvLiS$9uuU44Ch!r?Ai?`>;tZqB&8D)#iY zwYRqyJU%tod;7Y(yDL7wx_f&2`uqDE9{y5UwK>iEaHFtx+?gF4lh*V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9r24I_QKp4<2%)pR_H1~k46J#~| z$S*>tsTczT>Nqg6UTlLS2)%yNScXcFwPS1XAhhpLW?(2rZvO;$v$BDt*%{awRx&a$ ISZRZJ0Kbrpg8%>k literal 0 HcmV?d00001 From d3347b6d0832eec766fb70731e9aeb07f31450b0 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 27 Jul 2000 14:27:00 +0000 Subject: [PATCH 04/53] Initial HTML commit [SVN r7640] --- index.htm | 37 ++++ scoped_array.htm | 90 ++++++++ scoped_ptr.htm | 128 +++++++++++ shared_array.htm | 180 ++++++++++++++++ shared_ptr.htm | 206 ++++++++++++++++++ smart_ptr.htm | 138 ++++++++++++ smarttests.htm | 540 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1319 insertions(+) create mode 100644 index.htm create mode 100644 scoped_array.htm create mode 100644 scoped_ptr.htm create mode 100644 shared_array.htm create mode 100644 shared_ptr.htm create mode 100644 smart_ptr.htm create mode 100644 smarttests.htm diff --git a/index.htm b/index.htm new file mode 100644 index 0000000..04643fa --- /dev/null +++ b/index.htm @@ -0,0 +1,37 @@ + + + + +Boost Smart Pointer Library + + + + + + + + + + + + + + +
c++boost.gif (8819 bytes)Home Libraries People FAQ More
+ +

Smart pointer library

+ +

The header smart_ptr.h provides four smart pointer classes.  Smart pointers ease +the management of memory dynamically allocated with C++ new expressions. + +

+ +

Revised July 23, 1999

+ + diff --git a/scoped_array.htm b/scoped_array.htm new file mode 100644 index 0000000..3bf65cd --- /dev/null +++ b/scoped_array.htm @@ -0,0 +1,90 @@ + + + +scoped_array + + + + + + +

c++boost.gif (8819 bytes)Class +scoped_array

+

Class scoped_array 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 scoped_array::reset().

+

Class scoped_array is a simple solution for simple +needs.  It cannot be used in C++ Standard Library containers.  See shared_array +if scoped_array does not meet your needs.

+

Class scoped_array cannot correctly hold a pointer to a +single object.  See scoped_ptr +for that usage.

+

Because scoped_array is so simple, in its usual +implementation every operation is as fast as a built-in array pointer and has no +more space overhead that a built-in array pointer.

+

A heavier duty alternative to a scoped_array is a scoped_ptr +to a C++ Standard Library vector.

+

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

+

Class scoped_array Synopsis

+
#include <boost/smart_ptr.hpp>
+namespace boost {
+
+template<typename T> class scoped_array : noncopyable {
+
+ public:
+   typedef T element_type;
+
+   explicit scoped_array( T* p=0 );  // never throws
+   ~scoped_array();
+
+   void reset( T* p=0 );
+
+   T& operator[](std::size_t i) const;  // never throws
+   T* get() const;  // never throws
+   };
+}
+

Class scoped_array Members

+

scoped_array element_type

+
typedef T element_type;
+

Provides the type of the stored pointer.

+

scoped_array 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.

+

scoped_array destructor

+
~scoped_array();
+

Deletes the array pointed to by the stored pointer.  Note that in C++ delete[] +on a pointer with a value of 0 is harmless.

+

Does not throw exceptions.

+

scoped_array reset

+
void reset( T* p=0 )();
+

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.

+

Does not throw exceptions.

+

scoped_array operator[]

+

T& operator[](std::size_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 get()==0, +or if i is less than 0 or is greater or equal to the number of elements +in the array.

+

scoped_array get

+
T* get() const;  // never throws
+

Returns the stored pointer.

+

Class scoped_array example

+

[To be supplied. In the meantime, see smart_ptr_test.cpp.]

+
+

Revised  December 8, 1999

+

© 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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/scoped_ptr.htm b/scoped_ptr.htm new file mode 100644 index 0000000..3430481 --- /dev/null +++ b/scoped_ptr.htm @@ -0,0 +1,128 @@ + + + +scoped_ptr + + + + + + +

c++boost.gif (8819 bytes)Class +scoped_ptr

+

Class scoped_ptr stores a pointer to a dynamically allocated +object. (Dynamically allocated objects are allocated with the C++ new +expression.)   The object pointed to is guaranteed to be deleted, +either on destruction of the scoped_ptr, or via an explicit scoped_ptr::reset().  +See example.

+

Class scoped_ptr is a simple solution for simple +needs.  It cannot be used in C++ Standard Library containers.  See shared_ptr +or std::auto_ptr if scoped_ptr does not meet your needs.

+

Class scoped_ptr cannot correctly hold a pointer to a +dynamically allocated array.  See scoped_array +for that usage.

+

Because scoped_ptr is so simple, in its usual implementation +every operation is as fast as a built-in pointer and has no more space overhead +that a built-in pointer.

+

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

+

Class scoped_ptr Synopsis

+
#include <boost/smart_ptr.hpp>
+namespace boost {
+
+template<typename T> class scoped_ptr : noncopyable {
+
+ public:
+   typedef T element_type;
+
+   explicit scoped_ptr( T* p=0 );  // never throws
+   ~scoped_ptr();
+
+   void reset( T* p=0 );
+
+   T& operator*() const;  // never throws
+   T* operator->() const;  // never throws
+   T* get() const;  // never throws
+   };
+}
+

Class scoped_ptr Members

+

scoped_ptr element_type

+
typedef T element_type;
+

Provides the type of the stored pointer.

+

scoped_ptr constructors

+
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..

+

scoped_ptr destructor

+
~scoped_ptr();
+

Deletes the object pointed to by the stored pointer.  Note that in C++, delete +on a pointer with a value of 0 is harmless.

+

Does not throw exceptions.

+

scoped_ptr reset

+
void reset( T* p=0 );
+

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.

+

Does not throw exceptions.

+

scoped_ptr operator*

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

Returns a reference to the object pointed to by the stored pointer.

+

scoped_ptr operator-> and get

+
T* operator->() const;  // never throws
+T* get() const;  // never throws
+

Both return the stored pointer.

+

Class scoped_ptr examples

+
#include <iostream>
+#include <boost/smart_ptr.h>
+
+struct Shoe { ~Shoe(){ std::cout << "Buckle my shoe" << std::endl; } };
+
+class MyClass {
+    boost::scoped_ptr<int> ptr;
+  public:
+    MyClass() : ptr(new int) { *ptr = 0; }
+    int add_one() { return ++*ptr; }
+    };
+
+void main() {
+    boost::scoped_ptr<Shoe> x(new Shoe);
+    MyClass my_instance;
+    std::cout << my_instance.add_one() << std::endl;
+    std::cout << my_instance.add_one() << std::endl;
+    }
+

The example program produces the beginning of a child's nursery rhyme as +output:

+
+
1
+2
+Buckle my shoe
+
+

Handle/Body Idiom

+

One common usage of shared_pointer is to implement a handle/body +structure which avoids exposing the body (implementation) in the header file:

+
class handle
+{
+public:    // simple forwarding functions to the body class
+    void f();
+    void g(int);
+private:
+    friend class body;  //incomplete class hides implementation
+    boost::scoped_ptr<body> imp;
+};
+

This code requires that class body have a trivial destructor to +avoid undefined behavior.  This is because the definition of class +body 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 +above code is well defined.

+
+

Revised 24 July 2000

+

© 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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/shared_array.htm b/shared_array.htm new file mode 100644 index 0000000..eebafdc --- /dev/null +++ b/shared_array.htm @@ -0,0 +1,180 @@ + + + +shared_array + + + + + + +

c++boost.gif (8819 bytes)Class +shared_array

+

Class shared_array 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 shared_array, on shared_array::operator=(), +or via an explicit shared_array::reset().  See example.

+

Class shared_array meets the CopyConstuctible +and Assignable requirements of the C++ Standard Library, and so +can be used in C++ Standard Library containers.  A specialization of std:: +less< > for  boost::shared_ptr<Y> is supplied so that  +shared_array 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.

+

Class shared_array cannot correctly hold a pointer to a +single object.  See shared_array +for that usage.

+

Class shared_array will not work correctly with cyclic data +structures. For example, if main() holds a shared_array pointing to array A, +which directly or indirectly holds a shared_array pointing back to array A, then +array A's use_count() will be 2, and destruction of the main() shared_array will +leave array A dangling with a use_count() of 1.

+

A heavier duty alternative to a shared_array is a shared_ptr +to a C++ Standard Library vector.

+

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

+

Class shared_array Synopsis

+
#include <boost/smart_ptr.hpp>
+namespace boost {
+
+template<typename T> class shared_array {
+
+ public:
+   typedef T element_type;
+
+   explicit shared_array( T* p=0 );
+   shared_array( const shared_array& );  // never throws   
+   ~shared_array();
+
+   shared_array& operator=( const shared_array& );  // never throws  
+
+   void reset( T* p=0 );
+
+   T& operator[](std::size_t i) const;  // never throws
+   T* get() const;  // never throws
+
+   long use_count() const;  // never throws
+   bool unique() const;  // never throws
+
+   void swap( shared_array<T>& other ) throw()
+   };
+
+template<typename T>
+  inline bool operator==(const shared_array<T>& a, const shared_array<T>& b)
+    { return a.get() == b.get(); }
+
+template<typename T>
+  inline bool operator!=(const shared_array<T>& a, const shared_array<T>& b)
+    { return a.get() != b.get(); }
+}
+
namespace std {
+
+template<typename T>
+  inline void swap(boost::shared_array<T>& a, boost::shared_array<T>& b)
+    { a.swap(b); }
+
+template<typename T>
+  struct less< boost::shared_array<T> >
+    : binary_function<boost::shared_array<T>, boost::shared_array<T>, bool>
+  {
+    bool operator()(const boost::shared_array<T>& a,
+        const boost::shared_array<T>& b) const
+      { return less<T*>()(a.get(),b.get()); }
+  };
+
+} // namespace std 
+

Specialization of std::swap uses the fast, non-throwing swap that's provided +as a member function instead of using the default algorithm which creates a +temporary and uses assignment.
+
+Specialization of std::less allows use of shared arrays as keys in C++ +Standard Library associative collections.
+
+The std::less specializations use std::less<T*> to perform the +comparison.  This insures that pointers are handled correctly, since the +standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 8).
+
+It's still a controversial question whether supplying only std::less is better +than supplying a full range of comparison operators (<, >, <=, >=).

+

The current implementation does not supply the specializations if the macro +name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

+

Class shared_array Members

+

shared_array element_type

+
typedef T element_type;
+

Provides the type of the stored pointer.

+

shared_array constructors

+
explicit shared_array( T* p=0 );
+

Constructs a shared_array, storing a copy of p, +which must have been allocated via a C++ new[] expression or be 0. +Afterwards, use_count() is 1 (even if p==0; see ~shared_array).

+

The only exception which may be thrown is std::bad_alloc.  If +an exception is thrown,  delete[] p is called.

+
shared_array( const shared_array& r);  // never throws
+

Constructs a shared_array, as if by storing a copy of the +pointer stored in r. Afterwards, use_count() +for all copies is 1 more than the initial r.use_count().

+

shared_array destructor

+
~shared_array();
+

If use_count() == 1, deletes the array pointed to by the +stored pointer. Otherwise, use_count() for any remaining +copies is decremented by 1. Note that in C++ delete[] on a pointer with +a value of 0 is harmless.

+

Does not throw exceptions.

+

shared_array operator=

+
shared_array& operator=( const shared_array& r);  // never throws
+

First, if use_count() == 1, deletes the array pointed to by +the stored pointer. Otherwise, use_count() for any +remaining copies is decremented by 1. Note that in C++ delete[] on a +pointer with a value of 0 is harmless.

+

Then replaces the contents of this, as if by storing a copy +of the pointer stored in r. Afterwards, use_count() +for all copies is 1 more than the initial r.use_count()

+

shared_array reset

+
void reset( T* p=0 );
+

First, if use_count() == 1, deletes the array pointed to by +the stored pointer. Otherwise, use_count() for any +remaining copies is decremented by 1. Note that in C++  delete[] +on a pointer with a value of 0 is harmless.

+

Then replaces the contents of this, as if by storing a copy +of p, which must have been allocated via a C++ new[] +expression or be 0. Afterwards, use_count() is 1 (even if p==0; +see ~shared_array).

+

The only exception which may be thrown is std::bad_alloc.  If +an exception is thrown,  delete[] p is called.

+

shared_array operator[]

+

T& operator[](std::size_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 get()==0, +or if i is less than 0 or is greater or equal to the number of elements +in the array.

+

shared_array get

+
T* get() const;  // never throws
+

Returns the stored pointer.

+

shared_array use_count

+

long use_count() const; // never throws

+

Returns the number of shared_arrays sharing ownership of the +stored pointer.

+

shared_array unique

+

bool unique() const; // never throws

+

Returns use_count() == 1.

+

shared_array swap

+

void swap( shared_array<T>& other ) throw()

+

Swaps the two smart pointers, as if by std::swap.

+

Class shared_array example

+

[To be supplied. In the meantime, see smart_ptr_test.cpp.]

+
+

Revised December 8, 1999

+

© 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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/shared_ptr.htm b/shared_ptr.htm new file mode 100644 index 0000000..3fdca13 --- /dev/null +++ b/shared_ptr.htm @@ -0,0 +1,206 @@ + + + +shared_ptr + + + + + + +

c++boost.gif (8819 bytes)Class +shared_ptr

+

Class shared_ptr stores a pointer to a dynamically allocated +object. (Dynamically allocated objects are allocated with the C++ new +expression.)   The object pointed to is guaranteed to be deleted when +the last shared_ptr pointing to it is deleted or reset.  +See example.

+

Class shared_ptr meets the CopyConstuctible +and Assignable requirements of the C++ Standard Library, and so +can be used in C++ Standard Library containers.  A specialization of std:: +less< > for  boost::shared_ptr<Y> is supplied so that  +shared_ptr 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.

+

Class shared_ptr cannot correctly hold a pointer to a +dynamically allocated array.  See shared_array +for that usage.

+

Class shared_ptr will not work correctly with cyclic data +structures. For example, if main() holds a shared_ptr to object A, which +directly or indirectly holds a shared_ptr back to object A, then object A's +use_count() will be 2, and destruction of the main() shared_ptr will leave +object A dangling with a use_count() of 1.

+

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

+

Class shared_ptr Synopsis

+
#include <boost/smart_ptr.hpp>
+namespace boost {
+
+template<typename T> class shared_ptr {
+
+ public:
+   typedef T element_type;
+
+   explicit shared_ptr( T* p=0 );
+   ~shared_ptr();
+
+   shared_ptr( const shared_ptr& );   
+   template<typename Y>
+      shared_ptr(const shared_ptr<Y>& r);  // never throws
+   template<typename Y>
+      shared_ptr(std::auto_ptr<Y>& r);
+
+   shared_ptr& operator=( const shared_ptr& );  // never throws  
+   template<typename Y>
+      shared_ptr& operator=(const shared_ptr<Y>& r);  // never throws
+   template<typename Y>
+      shared_ptr& operator=(std::auto_ptr<Y>& r);
+
+   void reset( T* p=0 );
+
+   T& operator*() const;  // never throws
+   T* operator->() const;  // never throws
+   T* get() const;  // never throws
+
+   long use_count() const;  // never throws
+   bool unique() const;  // never throws
+
+   void swap( shared_ptr<T>& other ) throw()
+   };
+
+template<typename T, typename U>
+  inline bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b)
+    { return a.get() == b.get(); }
+
+template<typename T, typename U>
+  inline bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b)
+    { return a.get() != b.get(); }
+}
+
namespace std {
+
+template<typename T>
+  inline void swap(boost::shared_ptr<T>& a, boost::shared_ptr<T>& b)
+    { a.swap(b); }
+
+template<typename T>
+  struct less< boost::shared_ptr<T> >
+    : binary_function<boost::shared_ptr<T>, boost::shared_ptr<T>, bool>
+  {
+    bool operator()(const boost::shared_ptr<T>& a,
+        const boost::shared_ptr<T>& b) const
+      { return less<T*>()(a.get(),b.get()); }
+  };
+
+} // namespace std 
+

Specialization of std::swap uses the fast, non-throwing swap that's provided +as a member function instead of using the default algorithm which creates a +temporary and uses assignment.
+
+Specialization of std::less allows use of shared pointers as keys in C++ +Standard Library associative collections.
+
+The std::less specializations use std::less<T*> to perform the +comparison.  This insures that pointers are handled correctly, since the +standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 8).
+
+It's still a controversial question whether supplying only std::less is better +than supplying a full range of comparison operators (<, >, <=, >=).

+

The current implementation does not supply the specializations if the macro +name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

+

The current implementation does not supply the member template functions if +the macro name BOOST_NO_MEMBER_TEMPLATES is defined.

+

Class shared_ptr Members

+

shared_ptr element_type

+
typedef T element_type;
+

Provides the type of the stored pointer.

+

shared_ptr constructors

+
explicit shared_ptr( T* p=0 );
+

Constructs a shared_ptr, storing a copy of p, which +must have been allocated via a C++ new expression or be 0. Afterwards, use_count() +is 1 (even if p==0; see ~shared_ptr).

+

The only exception which may be thrown by this constructor is std::bad_alloc.   +If an exception is thrown,  delete p is called.

+
shared_ptr( const shared_ptr& r);  // never throws   
+template<typename Y>
+   shared_ptr(const shared_ptr<Y>& r);  // never throws
+template<typename Y>
+   shared_ptr(std::auto_ptr<Y>& r);
+

Constructs a shared_ptr, as if by storing a copy of the +pointer stored in r. Afterwards, use_count() +for all copies is 1 more than the initial r.use_count(), or 1 +in the auto_ptr case. In the auto_ptr case, r.release() +is called.

+

The only exception which may be thrown by the constructor from auto_ptr +is std::bad_alloc.   If an exception is thrown, that +constructor has no effect.

+

shared_ptr destructor

+
~shared_ptr();
+

If use_count() == 1, deletes the object pointed to by the +stored pointer. Otherwise, use_count() for any remaining +copies is decremented by 1. Note that in C++  delete on a pointer +with a value of 0 is harmless.

+

Does not throw exceptions.

+

shared_ptr operator=

+
shared_ptr& operator=( const shared_ptr& r);  
+template<typename Y>
+   shared_ptr& operator=(const shared_ptr<Y>& r);
+template<typename Y>
+   shared_ptr& operator=(std::auto_ptr<Y>& r);
+

First, if use_count() == 1, deletes the object pointed to by +the stored pointer. Otherwise, use_count() for any +remaining copies is decremented by 1. Note that in C++  delete on +a pointer with a value of 0 is harmless.

+

Then replaces the contents of this, as if by storing a copy +of the pointer stored in r. Afterwards, use_count() +for all copies is 1 more than the initial r.use_count(), or 1 +in the auto_ptr case. In the auto_ptr case, r.release() +is called.

+

The first two forms of operator= above do not throw exceptions.

+

The only exception which may be thrown by the auto_ptr form +is std::bad_alloc.   If an exception is thrown, the function +has no effect.

+

shared_ptr reset

+
void reset( T* p=0 );
+

First, if use_count() == 1, deletes the object pointed to by +the stored pointer. Otherwise, use_count() for any +remaining copies is decremented by 1. 

+

Then replaces the contents of this, as if by storing a copy +of p, which must have been allocated via a C++ new +expression or be 0. Afterwards, use_count() is 1 (even if p==0; +see ~shared_ptr). Note that in C++  delete +on a pointer with a value of 0 is harmless.

+

The only exception which may be thrown is std::bad_alloc.  If +an exception is thrown,  delete p is called.

+

shared_ptr operator*

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

Returns a reference to the object pointed to by the stored pointer.

+

shared_ptr operator-> and get

+
T* operator->() const;  // never throws
+T* get() const;  // never throws
+

Both return the stored pointer.

+

shared_ptr use_count

+

long use_count() const; // never throws

+

Returns the number of shared_ptrs sharing ownership of the +stored pointer.

+

shared_ptr unique

+

bool unique() const; // never throws

+

Returns use_count() == 1.

+

shared_ptr swap

+

void swap( shared_ptr<T>& other ) throw()

+

Swaps the two smart pointers, as if by std::swap.

+

Class shared_ptr example

+

[To be supplied. In the meantime, see smart_ptr_test.cpp.]

+
+

Revised December 8, 1999

+

© 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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/smart_ptr.htm b/smart_ptr.htm new file mode 100644 index 0000000..ab95fc8 --- /dev/null +++ b/smart_ptr.htm @@ -0,0 +1,138 @@ + + + + + + +Smart Pointer Classes + + + + +

c++boost.gif (8819 bytes)Smart +Pointers

+

Smart pointers are classes 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 header boost/smart_ptr.hpp +provides four smart pointer template classes:

+
+ + + + + + + + + + + + + + + + + +
+

scoped_ptr

Simple sole ownership of single objects.
scoped_arraySimple sole ownership of arrays.
shared_ptrObject ownership shared among multiple pointers
shared_arrayArray ownership shared among multiple pointers.
+
+

These classes are designed to complement the C++ Standard Library auto_ptr +class.

+

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.cpp) is +provided to verify correct operation.

+

A page on Smart Pointer Timings will be of +interest to those curious about performance issues.

+

Common requirements

+

These smart pointer classes have a template parameter, T, which +specifies the type of the object pointed to by the smart pointer.  The +behavior of all four classes is undefined if the destructor or operator delete +for objects of type T throws exceptions.

+

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.

+

Exception-specifications

+

Exception-specifications are not used; see exception-specification +rationale.

+

All four classes 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.

+

History and acknowledgements

+

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.  See the +revision history in smart_ptr.hpp +for the specific changes made as a result of their constructive criticism.

+

Oct, 1998.  In 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.  +The committee document was 94-168/N0555, Exception Safe Smart Pointers.  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.

+

Beman Dawes proposed reviving the original semantics under the names safe_ptr +and counted_ptr at an October, 1998, meeting of Per Andersson, +Matt Austern, Greg Colvin, Sean Corfield, Pete Becker, Nico Josuttis, Dietmar +Kühl, Nathan Myers, Chichiang Wan and Judy Ward.  During the discussion, +the four 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ü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.

+

See the Revision History section of the header for further contributors.

+
+

Revised  24 Jul 2000

+

© 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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

+ + + + diff --git a/smarttests.htm b/smarttests.htm new file mode 100644 index 0000000..dd976e4 --- /dev/null +++ b/smarttests.htm @@ -0,0 +1,540 @@ + + +boost: smart pointer tests + + + + +

c++boost.gif (8819 bytes)Smart +Pointers Timings

+ +

In late January 2000, Mark Borgerding put forward a suggestion to boost for + a new design of smart pointer whereby an intrusive doubly linked list is used + to join together all instances of smart pointers sharing a given raw pointer. + This allowed avoidance of the costly heap allocation of a reference count that + occurred in the initial construction of the then current version of boost::shared_ptr. + Of course, nothing is for free and the benefit here was gained at the expense + of increased size and more costly copy operations. A debate ensued on the boost + mailing list and the tests which this page describes were performed to provide + a guide for current and future investigations into smart pointer implementation + strategies.

+

Thanks are due to Dave Abrahams, + Gavin Collings, Greg Colvin and + Beman Dawes + for test code and trial implementations, the final version of which can be found + in .zip format here.

+

Description

+

Two tests were run: the first aimed to obtain timings for two basic individual + operations:

+
    +
  1. Initial construction from raw pointer.
  2. +
  3. An amortized copy operation consisting of half an assignment and half a + copy construction - designed to reflect average usage.
  4. +
+

The second attempted to gain more insight into normal usage by timing the fill + and sort algorithms for vectors and lists filled with the various smart pointers.

+

Five smart pointer implementation strategies were tested:

+
    +
  1. Counted pointer using a heap allocated reference count, this is referred + to as simple counted.
  2. +
  3. Counted pointer using a special purpose allocator for the reference count + - special counted.
  4. +
  5. Counted pointer using an intrusive reference count - intrusive.
  6. +
  7. Linked pointer as described above - linked.
  8. +
  9. Cyclic pointer, a counted implementation using a std::deque for allocation + with provision for weak pointers and garbage collection of cycles of pointers + - cyclic.
  10. +
+

on two compilers:

+
    +
  1. MSVC 6.0 service pack 3, using default release optimization mode (/O2 - + optimized for speed, no inlining of functions defined outside a class body + unless specified as inline).
  2. +
  3. gcc 2.95.2 using full optimization (-O3 -DNDEBUG).
  4. +
+

Additionally, generated pointer sizes (taking into account struct alignment) + were compared, as were generated code sizes for MSVC mainly by manual inspection + of generated assembly code - a necessity due to function inlining.

+

All tests were run on a PII-200 running Windows NT version 4.0

+

 

+

Operation Timing Test Results

+

The following graphs show the overall time in nanoseconds to acquire a pointer + (default construction) perform n amortized copy operations on it and finally + release it. The initial allocation time for the contained pointer is not included, + although the time for it's deallocation is. The contained pointer pointed to + a trivial class, but for the inclusion of an intrusive reference count for the + benefit of the intrusive counted shared pointer. A dumb pointer (i.e. a smart + pointer that simply acquires and releases its contained pointer with no extra + overhead) and a raw pointer were also included for comparison.

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
   
   
   
  
   
+

 

+

Fitting straight lines to the above plots gives the following figures for initialization + and amortized copy operation for the two compilers (times in nanoseconds, errors + at two standard deviations) : -

+

 

+

MSVC

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
initialization
+
copy operation
+
simple counted
+
3000 +/- 170104 +/- 31
+
special counted
+
1330 +/- 5085 +/- 9
+
intrusive
+
1000 +/- 2071 +/- 3
linked970 +/- 60136 +/- 10
cyclic1290 +/- 70112 +/- 12
dumb1020 +/- 2010 +/- 4
+
raw
+
1038 +/- 3010 +/- 5
+

 

+

GCC

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
initialization
+
copy operation
+
simple counted
+
4620 +/- 150301 +/- 28
+
special counted
+
1990 +/- 40264 +/- 7
+
intrusive
+
1590 +/- 70181 +/- 12
linked1470 +/- 140345 +/- 26
cyclic2180 +/- 100330 +/- 18
dumb1590 +/- 7074 +/- 12
+
raw
+
1430 +/- 6027 +/- 11
+

Note that the above times include a certain amount of loop overhead etc. for + each operation. An estimate of the pure smart pointer operation time 'overhead' + can be obtained by subtracting the dumb or raw figure from the smart pointer + time of interest.

+

Detail

+

The test involved iterating a loop which creates raw pointers. These were then + shared among a varying number (set size) of smart pointers. A range of set sizes + was used and then a line fitted to get a linear relation with number of initializations + and copy-operations. A spreadsheet was used for the line fit, and to produce + the performance graphs above.

+

 

+

Container Test Results

+

To gain some insight in to operation within real life programs, this test was + devised. Smart pointers were used to fill standard containers which were then + sorted.

+

In this case, the contained pointer pointed to a class which initializes a + private data member to a random value in its default constructor. This value + is used subsequently for the sort comparison test. The class also contains an + intrusive reference count for the benefit of the intrusive counted pointer.

+

All times are in seconds for 300,000 contained pointers.

+

GCC

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 vectorlist
+
+
+
fill
+
sortfillsort
+
simple counted
+
46.542.4447.093.22
+
special counted
+
14.022.837.283.21
+
intrusive
+
12.151.917.993.08
linked12.462.328.143.27
cyclic22.603.191.633.18
+
raw
+
11.810.2427.510.77
+

 

+

MSVC

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 vectorlist
+
+
+
fill
+
sortfillsort
+
simple counted
+
1.832.371.864.85
+
special counted
+
1.042.351.384.58
+
intrusive
+
1.041.841.164.29
linked1.082.001.214.33
cyclic1.382.841.474.73
+
raw
+
0.670.281.241.81
+

 

+

Code Size

+

The following code sizes were determined by inspection of generated code for + MSVC only. Sizes are given in the form N / M / I where:

+
    +
  • N is the instruction count of the operation
  • +
  • M is the size of the code in bytes
  • +
  • I determines whether generated code was inlined or not I = inline, O = "outline"
  • +
+

 

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
ptr()
+
ptr(p)ptr(ptr)op=() +
~ptr()
+
+
simple counted
+
38/110/O38/110/O9/23/I22/57/I17/40/I
+
special counted
+
50/141/O50/141/O9/23/I23/64/I13/38/I
+
intrusive
+
1/2/I3/6/I3/6/I6/11/I6/11/I
+
linked
+
5/19/I5/15/I10/30/I27/59/I14/38/I
+

During the code inspection, a couple of minor points were noticed: -

+
    +
  • Function inlining was critical to performance.
  • +
  • For MSVC, at least, a "delete 0" caused execution of 11 assembly + instructions, including a function call. So in cases where performance is + at an absolute premium it can be worth inserting the extra manual test.
  • +
+

 

+

Data Size

+

The following smart pointer sizes were obtained in bytes

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
MSVC
+
+
GCC
+
+
simple counted
+
+
8
+
+
8
+
+
special counted
+
+
8
+
+
12
+
+
intrusive
+
+
4
+
+
4
+
+
linked
+
+
12
+
+
12
+
+
cyclic
+
+
8
+
+
8
+
+

 

+

Summary

+

The timing results mainly speak for themselves: clearly an intrusive pointer + outperforms all others and a simple heap based counted pointer has poor performance + relative to other implementations. The selection of an optimal non-intrusive + smart pointer implementation is more application dependent, however. Where small + numbers of copies are expected, it is likely that the linked implementation + will be favoured. Conversely, for larger numbers of copies a counted pointer + with some type of special purpose allocator looks like a win. Other factors + to bear in mind are: -

+
    +
  • Deterministic individual, as opposed to amortized, operation time. This + weighs against any implementation depending on an allocator.
  • +
  • Multithreaded synchronization. This weighs against an implementation which + spreads its information as in the case of linked pointer.
  • +
+
+

Revised 21 Feb 2000 +

+

© Copyright Gavin Collings 2000. 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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

+ + From 00c5642eb46d5d2bb06fdd0596b5b5ddb20d5f2b Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 3 Aug 2000 15:26:16 +0000 Subject: [PATCH 05/53] 1.17.0 release candidate runup [SVN r7683] --- index.htm | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/index.htm b/index.htm index 04643fa..4a15b3e 100644 --- a/index.htm +++ b/index.htm @@ -4,34 +4,36 @@ Boost Smart Pointer Library + - - - - - - + + + + + +
c++boost.gif (8819 bytes)Home Libraries People FAQ More c++boost.gif (8819 bytes)HomeLibrariesPeopleFAQMore
-

Smart pointer library

- -

The header smart_ptr.h provides four smart pointer classes.  Smart pointers ease -the management of memory dynamically allocated with C++ new expressions. - +

The header smart_ptr.hpp provides four smart pointer classes.  Smart +pointers ease the management of memory dynamically allocated with C++ new +expressions.

+

Revised 02 Aug 2000 +

-

Revised July 23, 1999

+ From 1412e40490deb3feb9c8603e0a64d466e2309f6f Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 19 Oct 2000 21:16:46 +0000 Subject: [PATCH 06/53] Make shared_ptr ctor from auto_ptr explicit. (Robert Vugts) [SVN r8012] --- include/boost/smart_ptr.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 353e472..686df8d 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,7 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 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) @@ -140,7 +141,7 @@ template class shared_ptr { } template - shared_ptr(std::auto_ptr& r) { + explicit shared_ptr(std::auto_ptr& r) { pn = new long(1); // may throw px = r.release(); // fix: moved here to stop leak if new throws } @@ -164,7 +165,7 @@ template class shared_ptr { return *this; } #else - shared_ptr(std::auto_ptr& r) { + explicit shared_ptr(std::auto_ptr& r) { pn = new long(1); // may throw px = r.release(); // fix: moved here to stop leak if new throws } From db43d160b4a58d1295f95d38e72f2ddf7462ab5c Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 10 Nov 2000 15:39:05 +0000 Subject: [PATCH 07/53] libraries.htm and people.htm moved to sub-directories to make root directory cleaner. [SVN r8166] --- index.htm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.htm b/index.htm index 4a15b3e..da4e16f 100644 --- a/index.htm +++ b/index.htm @@ -13,8 +13,8 @@ c++boost.gif (8819 bytes) Home - Libraries - People + Libraries + People FAQ More @@ -31,7 +31,7 @@ expressions.
  • Submitted by Greg Colvin and Beman Dawes.
  • -

    Revised 02 Aug 2000 +

    Revised 10 Nov 2000

    From 4e832788bfae6fdbfc1aac88c37b6dd47103c2ea Mon Sep 17 00:00:00 2001 From: John Maddock Date: Thu, 16 Nov 2000 11:17:22 +0000 Subject: [PATCH 08/53] Fix for egcs 1.1.1 problems with std::auto_ptr [SVN r8225] --- include/boost/smart_ptr.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 686df8d..59e42a7 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -139,12 +139,13 @@ template class shared_ptr { shared_ptr(const shared_ptr& r) : px(r.px) { // never throws ++*(pn = r.pn); } - +#ifndef BOOST_NO_AUTO_PTR template explicit shared_ptr(std::auto_ptr& r) { pn = new long(1); // may throw px = r.release(); // fix: moved here to stop leak if new throws - } + } +#endif template shared_ptr& operator=(const shared_ptr& r) { @@ -152,6 +153,7 @@ template class shared_ptr { return *this; } +#ifndef BOOST_NO_AUTO_PTR template shared_ptr& operator=(std::auto_ptr& r) { // code choice driven by guarantee of "no effect if new throws" @@ -164,7 +166,9 @@ template class shared_ptr { px = r.release(); // fix: moved here so doesn't leak if new throws return *this; } +#endif #else +#ifndef BOOST_NO_AUTO_PTR explicit shared_ptr(std::auto_ptr& r) { pn = new long(1); // may throw px = r.release(); // fix: moved here to stop leak if new throws @@ -181,6 +185,7 @@ template 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) { @@ -371,3 +376,4 @@ template #endif // BOOST_SMART_PTR_HPP + From 26fe4c40788dd94f5bf09da8b64748a6c9fe3fdc Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Wed, 6 Dec 2000 14:46:44 +0000 Subject: [PATCH 09/53] Fix typo shared_array should have read shared_ptr in one place (Ed Brey) [SVN r8392] --- shared_array.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared_array.htm b/shared_array.htm index eebafdc..89d9339 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -23,7 +23,7 @@ shared_array 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.

    Class shared_array cannot correctly hold a pointer to a -single object.  See shared_array +single object.  See shared_ptr for that usage.

    Class shared_array will not work correctly with cyclic data structures. For example, if main() holds a shared_array pointing to array A, From cb1b1b7cc09032c588e1842be3e7f813d1a9f2f0 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 22 Jan 2001 04:53:38 +0000 Subject: [PATCH 10/53] Suppress some useless warnings with MSVC [SVN r8704] --- include/boost/smart_ptr.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 59e42a7..162b458 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,7 @@ // 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) @@ -75,7 +76,14 @@ template 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! @@ -204,7 +212,14 @@ template 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! From 16902b1f4ffb71eb3249a3fe8181e89c79ef18cc Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 9 Feb 2001 14:39:43 +0000 Subject: [PATCH 11/53] Add example [SVN r9054] --- shared_ptr.htm | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index 3fdca13..0d489ad 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -192,7 +192,31 @@ stored pointer.

    void swap( shared_ptr<T>& other ) throw()

    Swaps the two smart pointers, as if by std::swap.

    Class shared_ptr example

    -

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]

    +
    //  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 );
    +}
    +

    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.


    Revised December 8, 1999

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, From e57d3f4bc1e77ea51289141f7bccfb2df97cd835 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sat, 10 Feb 2001 12:47:02 +0000 Subject: [PATCH 12/53] Fix revision date [SVN r9069] --- shared_ptr.htm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index 0d489ad..b478dc9 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -218,7 +218,8 @@ 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.


    -

    Revised December 8, 1999

    +

    Revised 09 February, 2001 +

    © 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" From 6a12efb77bf64e79c232b37057129a603356b327 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Wed, 14 Mar 2001 15:11:55 +0000 Subject: [PATCH 13/53] 1.21.1 run up, including new download instructions and fix broken hyperlinks [SVN r9557] --- index.htm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.htm b/index.htm index da4e16f..9c8e78a 100644 --- a/index.htm +++ b/index.htm @@ -27,11 +27,10 @@ expressions.

  • Documentation (HTML).
  • Header smart_ptr.hpp
  • Test program smart_ptr_test.cpp.
  • -
  • Download all of Boost (ZIP format).
  • Submitted by Greg Colvin and Beman Dawes.
  • -

    Revised 10 Nov 2000 +

    Revised 14 Mar 2001

    From 060ea4a573e1b8841148e3d9cd441a7ccfd2e8ca Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sun, 18 Mar 2001 22:25:51 +0000 Subject: [PATCH 14/53] Add a couple of comments to cut down on FAQ's [SVN r9581] --- include/boost/smart_ptr.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 162b458..e549f91 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -250,7 +250,7 @@ template class shared_ptr { void dispose() { if (--*pn == 0) { delete px; delete pn; } } void share(T* rpx, long* rpn) { - if (pn != rpn) { + if (pn != rpn) { // assert ((pn==rpn) == (px==rpx)) dispose(); px = rpx; ++*(pn = rpn); @@ -287,7 +287,7 @@ template class shared_array { ~shared_array() { dispose(); } shared_array& operator=(const shared_array& r) { - if (pn != r.pn) { + if (pn != r.pn) { //assert ((pn==r.pn) == (px==r.px)) dispose(); px = r.px; ++*(pn = r.pn); From 2d342f0ddfbe852290b666b812f3acecabd87837 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 19 Mar 2001 12:34:12 +0000 Subject: [PATCH 15/53] Comment corrected (thanks to Joe Gottman) [SVN r9582] --- include/boost/smart_ptr.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index e549f91..1decce7 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -250,7 +250,7 @@ template class shared_ptr { void dispose() { if (--*pn == 0) { delete px; delete pn; } } void share(T* rpx, long* rpn) { - if (pn != rpn) { // assert ((pn==rpn) == (px==rpx)) + if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0 dispose(); px = rpx; ++*(pn = rpn); @@ -287,7 +287,7 @@ template class shared_array { ~shared_array() { dispose(); } shared_array& operator=(const shared_array& r) { - if (pn != r.pn) { //assert ((pn==r.pn) == (px==r.px)) + if (pn != r.pn) { // Q: why not px != r.px? A: fails when both px == 0 dispose(); px = r.px; ++*(pn = r.pn); From 55a377b44668366ba02612929f7f3dd743748e7b Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Wed, 25 Apr 2001 00:24:50 +0000 Subject: [PATCH 16/53] Change all eGroups references to YahooGroups [SVN r9979] --- scoped_ptr.htm | 2 +- shared_ptr.htm | 2 +- smart_ptr.htm | 2 +- smarttests.htm | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 3430481..0e47606 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -116,7 +116,7 @@ body 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 above code is well defined.


    -

    Revised 24 July 2000

    +

    Revised 27 July 2000

    © 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" diff --git a/shared_ptr.htm b/shared_ptr.htm index b478dc9..affff12 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -218,7 +218,7 @@ 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.


    -

    Revised 09 February, 2001 +

    Revised 10 February, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/smart_ptr.htm b/smart_ptr.htm index ab95fc8..dc5db1f 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -125,7 +125,7 @@ implementation.

    See the Revision History section of the header for further contributors.


    Revised  24 Jul 200027 Jul 2000

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/smarttests.htm b/smarttests.htm index dd976e4..f78b618 100644 --- a/smarttests.htm +++ b/smarttests.htm @@ -530,7 +530,7 @@ Pointers Timings spreads its information as in the case of linked pointer.


    -

    Revised 21 Feb 2000 +

    Revised 27 Jul 2000

    © Copyright Gavin Collings 2000. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all From 3b183163c9ab8b5995cd7c3e832b0c79b57dfca9 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 10 May 2001 16:00:49 +0000 Subject: [PATCH 17/53] Clarify rationale for noncopyability [SVN r10085] --- scoped_array.htm | 17 +++++++++++------ scoped_ptr.htm | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/scoped_array.htm b/scoped_array.htm index 3bf65cd..e317e21 100644 --- a/scoped_array.htm +++ b/scoped_array.htm @@ -15,16 +15,21 @@ allocated array. (Dynamically allocated arrays are allocated with the C++ ne expression.)   The array pointed to is guaranteed to be deleted, either on destruction of the scoped_array, or via an explicit scoped_array::reset().

    Class scoped_array is a simple solution for simple -needs.  It cannot be used in C++ Standard Library containers.  See shared_array +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.  By +being 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.

    Class scoped_array cannot correctly hold a pointer to a single object.  See scoped_ptr for that usage.

    -

    Because scoped_array is so simple, in its usual -implementation every operation is as fast as a built-in array pointer and has no -more space overhead that a built-in array pointer.

    -

    A heavier duty alternative to a scoped_array is a scoped_ptr -to a C++ Standard Library vector.

    +

    A C++ Standard Library vector is a heavier duty alternative to a scoped_array.

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

    diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 0e47606..27512d1 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -16,14 +16,21 @@ expression.)   The object pointed to is guaranteed to be deleted, either on destruction of the scoped_ptr, or via an explicit scoped_ptr::reset().  See example.

    Class scoped_ptr is a simple solution for simple -needs.  It cannot be used in C++ Standard Library containers.  See shared_ptr +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.  By +being noncopyable, it is +safer than shared_ptr or std::auto_ptr for pointers which should not be +copied.

    +

    Because scoped_ptr is so simple, in its usual implementation +every operation is as fast as for a built-in pointer and it has no more space overhead +that a built-in pointer.

    +

    Class scoped_ptr cannot be used in C++ Standard Library containers.  See shared_ptr or std::auto_ptr if scoped_ptr does not meet your needs.

    Class scoped_ptr cannot correctly hold a pointer to a dynamically allocated array.  See scoped_array for that usage.

    -

    Because scoped_ptr is so simple, in its usual implementation -every operation is as fast as a built-in pointer and has no more space overhead -that a built-in pointer.

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

    @@ -116,7 +123,7 @@ body 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 above code is well defined.


    -

    Revised 27 July 2000

    +

    Revised 10 May 2001

    © 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" From 8f23f07740e3e76ff71b83d1f14fe9727ed0849c Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 21 May 2001 14:56:51 +0000 Subject: [PATCH 18/53] Fix transitive dependency bug [SVN r10164] --- include/boost/smart_ptr.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 1decce7..2111032 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,8 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 21 May 01 operator= fails if operand transitively owned by *this, as in a +// linked list (report by Ken Johnson, fix by Beman Dawes) // 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 @@ -251,9 +253,11 @@ template class shared_ptr { void share(T* rpx, long* rpn) { if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0 + ++*rpn; // done before dispose() in case rpn transitively + // dependent on *this (bug reported by Ken Johnson) dispose(); px = rpx; - ++*(pn = rpn); + pn = rpn; } } // share }; // shared_ptr @@ -288,9 +292,11 @@ template class shared_array { shared_array& operator=(const shared_array& r) { if (pn != r.pn) { // Q: why not px != r.px? A: fails when both px == 0 + ++*r.pn; // done before dispose() in case r.pn transitively + // dependent on *this (bug reported by Ken Johnson) dispose(); px = r.px; - ++*(pn = r.pn); + pn = r.pn; } return *this; } // operator= From ac8d0f55053b04d8b0a0632cd4c8b624141abb24 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 21 May 2001 14:58:07 +0000 Subject: [PATCH 19/53] Fix doc errors, add shared_ptr_example program [SVN r10165] --- scoped_ptr.htm | 4 +- shared_ptr.htm | 30 +++---------- shared_ptr_example.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 shared_ptr_example.cpp diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 27512d1..277b38e 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -106,7 +106,7 @@ output:

    Buckle my shoe

    Handle/Body Idiom

    -

    One common usage of shared_pointer is to implement a handle/body +

    One common usage of scoped_ptr is to implement a handle/body structure which avoids exposing the body (implementation) in the header file:

    class handle
     {
    @@ -123,7 +123,7 @@ body 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
     above code is well defined.


    -

    Revised 10 May 2001

    +

    Revised 21 May 2001

    © 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" diff --git a/shared_ptr.htm b/shared_ptr.htm index affff12..4dd2b87 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -192,33 +192,17 @@ stored pointer.

    void swap( shared_ptr<T>& other ) throw()

    Swaps the two smart pointers, as if by std::swap.

    Class shared_ptr example

    -
    //  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 );
    -}
    -

    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.  +

    See shared_ptr_example.cpp for a complete example program.

    +

    This program builds a std::vector and std::set of FooPtr's.

    +

    Note that after the two containers have been populated, some of the FooPtr objects +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 +at various times while push_back() and insert() 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.


    -

    Revised 10 February, 2001 +

    Revised 21 May, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/shared_ptr_example.cpp b/shared_ptr_example.cpp new file mode 100644 index 0000000..988efbd --- /dev/null +++ b/shared_ptr_example.cpp @@ -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 +#include +#include +#include +#include + +// 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 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 foo_vector; + std::set 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; +} + From a90a157ea60dba8286401e4c300ef3182f9cd0a0 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Tue, 22 May 2001 18:58:21 +0000 Subject: [PATCH 20/53] Smart pointer and utility changes related to adding checked_delere and checked_array_delete [SVN r10189] --- include/boost/smart_ptr.hpp | 34 +++++++++++++++--------------- scoped_array.htm | 10 +++++++++ scoped_ptr.htm | 41 +++++++++++++++++++++---------------- scoped_ptr_example.cpp | 16 +++++++++++++++ scoped_ptr_example.hpp | 21 +++++++++++++++++++ scoped_ptr_example_test.cpp | 10 +++++++++ shared_array.htm | 20 ++++++++++++++++++ shared_ptr.htm | 27 ++++++++++++++++++++++-- smart_ptr.htm | 29 +++++++++++++++++++++----- 9 files changed, 167 insertions(+), 41 deletions(-) create mode 100644 scoped_ptr_example.cpp create mode 100644 scoped_ptr_example.hpp create mode 100644 scoped_ptr_example_test.cpp diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 2111032..3dea3f9 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,7 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 21 May 01 Require complete type on delete (suggested by Vladimir Prus) // 21 May 01 operator= fails if operand transitively owned by *this, as in a // linked list (report by Ken Johnson, fix by Beman Dawes) // 21 Jan 01 Suppress some useless warnings with MSVC (David Abrahams) @@ -54,8 +55,9 @@ #include // for std::size_t #include // for std::auto_ptr #include // for std::swap -#include // for boost::noncopyable +#include // for boost::noncopyable, checked_delete, checked_array_delete #include // for std::less +#include // for BOOST_STATIC_ASSERT namespace boost { @@ -74,9 +76,8 @@ template class scoped_ptr : noncopyable { typedef T element_type; explicit scoped_ptr( T* p=0 ) : ptr(p) {} // never throws - ~scoped_ptr() { delete ptr; } - - void reset( T* p=0 ) { if ( ptr != p ) { delete ptr; ptr = p; } } + ~scoped_ptr() { checked_delete(ptr); } + void reset( T* p=0 ) { if ( ptr != p ) { checked_delete(ptr); ptr = p; } } T& operator*() const { return *ptr; } // never throws #ifdef BOOST_MSVC # pragma warning(push) @@ -107,9 +108,10 @@ template class scoped_array : noncopyable { typedef T element_type; explicit scoped_array( T* p=0 ) : ptr(p) {} // never throws - ~scoped_array() { delete [] ptr; } + ~scoped_array() { checked_array_delete(ptr); } - void reset( T* p=0 ) { if ( ptr != p ) {delete [] ptr; ptr=p;} } + void reset( T* p=0 ) { if ( ptr != p ) + {checked_array_delete(ptr); ptr=p;} } T* get() const { return ptr; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION @@ -132,7 +134,7 @@ template class shared_ptr { explicit shared_ptr(T* p =0) : px(p) { try { pn = new long(1); } // fix: prevent leak if new throws - catch (...) { delete p; throw; } + catch (...) { checked_delete(p); throw; } } shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); } // never throws @@ -167,7 +169,7 @@ template class shared_ptr { template shared_ptr& operator=(std::auto_ptr& r) { // code choice driven by guarantee of "no effect if new throws" - if (*pn == 1) { delete px; } + if (*pn == 1) { checked_delete(px); } else { // allocate new reference counter long * tmp = new long(1); // may throw --*pn; // only decrement once danger of new throwing is past @@ -186,7 +188,7 @@ template class shared_ptr { shared_ptr& operator=(std::auto_ptr& r) { // code choice driven by guarantee of "no effect if new throws" - if (*pn == 1) { delete px; } + if (*pn == 1) { checked_delete(px); } else { // allocate new reference counter long * tmp = new long(1); // may throw --*pn; // only decrement once danger of new throwing is past @@ -200,12 +202,12 @@ template class shared_ptr { void reset(T* p=0) { if ( px == p ) return; // fix: self-assignment safe - if (--*pn == 0) { delete px; } + if (--*pn == 0) { checked_delete(px); } else { // allocate new reference counter try { pn = new long; } // fix: prevent leak if new throws catch (...) { ++*pn; // undo effect of --*pn above to meet effects guarantee - delete p; + checked_delete(p); throw; } // catch } // allocate new reference counter @@ -249,7 +251,7 @@ template class shared_ptr { template friend class shared_ptr; #endif - void dispose() { if (--*pn == 0) { delete px; delete pn; } } + void dispose() { if (--*pn == 0) { checked_delete(px); delete pn; } } void share(T* rpx, long* rpn) { if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0 @@ -282,7 +284,7 @@ template class shared_array { explicit shared_array(T* p =0) : px(p) { try { pn = new long(1); } // fix: prevent leak if new throws - catch (...) { delete [] p; throw; } + catch (...) { checked_array_delete(p); throw; } } shared_array(const shared_array& r) : px(r.px) // never throws @@ -303,12 +305,12 @@ template class shared_array { void reset(T* p=0) { if ( px == p ) return; // fix: self-assignment safe - if (--*pn == 0) { delete [] px; } + if (--*pn == 0) { checked_array_delete(px); } else { // allocate new reference counter try { pn = new long; } // fix: prevent leak if new throws catch (...) { ++*pn; // undo effect of --*pn above to meet effects guarantee - delete [] p; + checked_array_delete(p); throw; } // catch } // allocate new reference counter @@ -335,7 +337,7 @@ template class shared_array { T* px; // contained pointer long* pn; // ptr to reference counter - void dispose() { if (--*pn == 0) { delete [] px; delete pn; } } + void dispose() { if (--*pn == 0) { checked_array_delete(px); delete pn; } } }; // shared_array diff --git a/scoped_array.htm b/scoped_array.htm index e317e21..f146ee2 100644 --- a/scoped_array.htm +++ b/scoped_array.htm @@ -59,19 +59,27 @@ template<typename T> class scoped_array : Common Requirements.

    scoped_array destructor

    ~scoped_array();
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Deletes the array pointed to by the stored pointer.  Note that in C++ delete[] on a pointer with a value of 0 is harmless.

    Does not throw exceptions.

    scoped_array reset

    void reset( T* p=0 )();
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    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.

    Does not throw exceptions.

    scoped_array operator[]

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

    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Returns a reference to element i of the array pointed to by the stored pointer.

    Behavior is undefined (and almost certainly undesirable) if get()==0, @@ -79,6 +87,8 @@ or if i is less than 0 or is greater or equal to the number of elements in the array.

    scoped_array get

    T* get() const;  // never throws
    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns the stored pointer.

    Class scoped_array example

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]

    diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 277b38e..4945084 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -59,25 +59,38 @@ template<typename T> class scoped_ptr : scoped_ptr constructors
    explicit scoped_ptr( T* p=0 );  // never throws
    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Constructs a scoped_ptr, storing a copy of p, which must -have been allocated via a C++ new expression or be 0..

    +have been allocated via a C++ new expression or be 0.

    scoped_ptr destructor

    ~scoped_ptr();
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Deletes the object pointed to by the stored pointer.  Note that in C++, delete on a pointer with a value of 0 is harmless.

    Does not throw exceptions.

    scoped_ptr reset

    void reset( T* p=0 );
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    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.

    Does not throw exceptions.

    scoped_ptr operator*

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

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Returns a reference to the object pointed to by the stored pointer.

    scoped_ptr operator-> and get

    T* operator->() const;  // never throws
     T* get() const;  // never throws
    +

    T is required be a complete type at point of instantiation of +operator->().  See Common +Requirements.

    +

    T is not required be a complete type at point of instantiation of +get().  See Common Requirements.

    Both return the stored pointer.

    Class scoped_ptr examples

    #include <iostream>
    @@ -106,24 +119,16 @@ output:

    Buckle my shoe

    Handle/Body Idiom

    -

    One common usage of scoped_ptr is to implement a handle/body -structure which avoids exposing the body (implementation) in the header file:

    -
    class handle
    -{
    -public:    // simple forwarding functions to the body class
    -    void f();
    -    void g(int);
    -private:
    -    friend class body;  //incomplete class hides implementation
    -    boost::scoped_ptr<body> imp;
    -};
    -

    This code requires that class body have a trivial destructor to -avoid undefined behavior.  This is because the definition of class -body 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 -above code is well defined.

    +

    One common usage of scoped_ptr is to implement a handle/body idiom which avoids exposing the body (implementation) in the header +file.

    +

    The scoped_ptr_example_test.cpp +sample program includes a header file, scoped_ptr_example.hpp, +which uses a scoped_ptr<> to an incomplete type to hide the +implementation.   The +instantiation of member functions which require a complete type occurs in the scoped_ptr_example.cpp +implementation file. 


    -

    Revised 21 May 2001

    +

    Revised 22 May 2001

    © 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" diff --git a/scoped_ptr_example.cpp b/scoped_ptr_example.cpp new file mode 100644 index 0000000..3e2e511 --- /dev/null +++ b/scoped_ptr_example.cpp @@ -0,0 +1,16 @@ +// Boost scoped_ptr_example implementation file -----------------------------// + +#include "scoped_ptr_example.hpp" +#include + +class example::implementation +{ + public: + ~implementation() { std::cout << "destroying implementation\n"; } +}; + +example::example() : _imp( new implementation ) {} + +void example::do_something() { std::cout << "did something\n"; } + +example::~example() {} diff --git a/scoped_ptr_example.hpp b/scoped_ptr_example.hpp new file mode 100644 index 0000000..97c8bff --- /dev/null +++ b/scoped_ptr_example.hpp @@ -0,0 +1,21 @@ +// Boost scoped_ptr_example header file ------------------------------------// + +#include + +// The point of this example is to prove that even though +// example::implementation is an incomplete type in translation units using +// this header, scoped_ptr< implementation > is still valid because the type +// is complete where it counts - in the inplementation translation unit where +// destruction is actually instantiated. + +class example : boost::noncopyable +{ + public: + example(); + ~example(); + void do_something(); + private: + class implementation; + boost::scoped_ptr< implementation > _imp; // hide implementation details +}; + diff --git a/scoped_ptr_example_test.cpp b/scoped_ptr_example_test.cpp new file mode 100644 index 0000000..3629ec8 --- /dev/null +++ b/scoped_ptr_example_test.cpp @@ -0,0 +1,10 @@ +// Boost scoped_ptr_example_test main program -------------------------------// + +#include "scoped_ptr_example.hpp" + +int main() +{ + example my_example; + my_example.do_something(); + return 0; +} \ No newline at end of file diff --git a/shared_array.htm b/shared_array.htm index 89d9339..24842d1 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -108,17 +108,23 @@ name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

    Provides the type of the stored pointer.

    shared_array constructors

    explicit shared_array( T* p=0 );
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Constructs a shared_array, storing a copy of p, which must have been allocated via a C++ new[] expression or be 0. Afterwards, use_count() is 1 (even if p==0; see ~shared_array).

    The only exception which may be thrown is std::bad_alloc.  If an exception is thrown,  delete[] p is called.

    shared_array( const shared_array& r);  // never throws
    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Constructs a shared_array, as if by storing a copy of the pointer stored in r. Afterwards, use_count() for all copies is 1 more than the initial r.use_count().

    shared_array destructor

    ~shared_array();
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    If use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++ delete[] on a pointer with @@ -126,6 +132,8 @@ a value of 0 is harmless.

    Does not throw exceptions.

    shared_array operator=

    shared_array& operator=( const shared_array& r);  // never throws
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    First, if use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++ delete[] on a @@ -135,6 +143,8 @@ of the pointer stored in r. Afterwards, use_count()r.use_count()

    shared_array reset

    void reset( T* p=0 );
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    First, if use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete[] @@ -147,6 +157,8 @@ see ~shared_array).

    an exception is thrown,  delete[] p is called.

    shared_array operator[]

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

    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Returns a reference to element i of the array pointed to by the stored pointer.

    Behavior is undefined (and almost certainly undesirable) if get()==0, @@ -154,16 +166,24 @@ or if i is less than 0 or is greater or equal to the number of elements in the array.

    shared_array get

    T* get() const;  // never throws
    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns the stored pointer.

    shared_array use_count

    long use_count() const; // never throws

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns the number of shared_arrays sharing ownership of the stored pointer.

    shared_array unique

    bool unique() const; // never throws

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns use_count() == 1.

    shared_array swap

    void swap( shared_array<T>& other ) throw()

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Swaps the two smart pointers, as if by std::swap.

    Class shared_array example

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]

    diff --git a/shared_ptr.htm b/shared_ptr.htm index 4dd2b87..78ed702 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -118,6 +118,8 @@ the macro name BOOST_NO_MEMBER_TEMPLATES is defined.

    Provides the type of the stored pointer.

    shared_ptr constructors

    explicit shared_ptr( T* p=0 );
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Constructs a shared_ptr, storing a copy of p, which must have been allocated via a C++ new expression or be 0. Afterwards, use_count() is 1 (even if p==0; see ~shared_ptr).

    @@ -128,6 +130,8 @@ template<typename Y> shared_ptr(const shared_ptr<Y>& r); // never throws template<typename Y> shared_ptr(std::auto_ptr<Y>& r);
    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Constructs a shared_ptr, as if by storing a copy of the pointer stored in r. Afterwards, use_count() for all copies is 1 more than the initial r.use_count(), or 1 @@ -138,6 +142,8 @@ is std::bad_alloc.   If an exception is thrown, that constructor has no effect.

    shared_ptr destructor

    ~shared_ptr();
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    If use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete on a pointer @@ -149,6 +155,8 @@ template<typename Y> shared_ptr& operator=(const shared_ptr<Y>& r); template<typename Y> shared_ptr& operator=(std::auto_ptr<Y>& r); +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    First, if use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete on @@ -164,6 +172,8 @@ is std::bad_alloc.   If an exception is thrown, the function has no effect.

    shared_ptr reset

    void reset( T* p=0 );
    +

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    First, if use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. 

    @@ -176,33 +186,46 @@ on a pointer with a value of 0 is harmless.

    an exception is thrown,  delete p is called.

    shared_ptr operator*

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

    T is required be a complete type at point of instantiation.  See Common +Requirements.

    Returns a reference to the object pointed to by the stored pointer.

    shared_ptr operator-> and get

    T* operator->() const;  // never throws
     T* get() const;  // never throws
    +

    T is required be a complete type at point of instantiation of +operator->().  See Common +Requirements.

    +

    T is not required be a complete type at point of instantiation of +get().  See Common Requirements.

    Both return the stored pointer.

    shared_ptr use_count

    long use_count() const; // never throws

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns the number of shared_ptrs sharing ownership of the stored pointer.

    shared_ptr unique

    bool unique() const; // never throws

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Returns use_count() == 1.

    shared_ptr swap

    void swap( shared_ptr<T>& other ) throw()

    +

    T is not required be a complete type at point of instantiation.  +See Common Requirements.

    Swaps the two smart pointers, as if by std::swap.

    Class shared_ptr example

    See shared_ptr_example.cpp for a complete example program.

    This program builds a std::vector and std::set of FooPtr's.

    Note that after the two containers have been populated, some of the FooPtr objects 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 +rather than a std::multiset, and thus does not contain duplicate entries.  Furthermore, use_count() may be even higher at various times while push_back() and insert() 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.


    -

    Revised 21 May, 2001 +

    Revised 22 May, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/smart_ptr.htm b/smart_ptr.htm index dc5db1f..b7bcf04 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -27,11 +27,11 @@ provides four smart pointer template classes:

    scoped_ptr - Simple sole ownership of single objects. + Simple sole ownership of single objects. Noncopyable. scoped_array - Simple sole ownership of arrays. + Simple sole ownership of arrays. Noncopyable. shared_ptr @@ -53,10 +53,25 @@ provided to verify correct operation.

    A page on Smart Pointer Timings will be of interest to those curious about performance issues.

    Common requirements

    -

    These smart pointer classes have a template parameter, T, which +

    These smart pointer classes have a template parameter, T, which specifies the type of the object pointed to by the smart pointer.  The behavior of all four classes is undefined if the destructor or operator delete -for objects of type T throws exceptions.

    +for objects of type T throws 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 point of instantiation of all member functions.

    +

    Rationale

    +

    The requirements on T are carefully crafted to ensure safety +yet allow handle-body (aka 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.

    +

    Example

    +

    The scoped_ptr_example_test.cpp +sample program includes a header file, scoped_ptr_example.hpp, +which uses a scoped_ptr<> to an incomplete type.   The +instantiation of member functions which require a complete type occurs in the scoped_ptr_example.cpp +implementation file. 

    Exception safety

    Several functions in these smart pointer classes are specified as having "no effect" or "no effect except such-and-such" if an @@ -78,6 +93,10 @@ never throws.

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

    History and acknowledgements

    +

    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

    @@ -125,7 +144,7 @@ implementation.

    See the Revision History section of the header for further contributors.


    Revised  27 Jul 200022 May 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright From 7c09884eacafeb8c83b4ec2b2a7d98764dd5d86a Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Wed, 23 May 2001 20:14:15 +0000 Subject: [PATCH 21/53] Move MS VC++ pragmas to workaround compiler crash reported by several people with SP4 and SP5. [SVN r10204] --- include/boost/smart_ptr.hpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 3dea3f9..0ec807b 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,7 +9,8 @@ // See http://www.boost.org for most recent version including documentation. // Revision History -// 21 May 01 Require complete type on delete (suggested by Vladimir Prus) +// 21 May 01 Require complete type where incomplete type is unsafe. +// (suggested by Vladimir Prus) // 21 May 01 operator= fails if operand transitively owned by *this, as in a // linked list (report by Ken Johnson, fix by Beman Dawes) // 21 Jan 01 Suppress some useless warnings with MSVC (David Abrahams) @@ -59,6 +60,11 @@ #include // for std::less #include // for BOOST_STATIC_ASSERT +#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash +# 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 + namespace boost { // scoped_ptr --------------------------------------------------------------// @@ -80,13 +86,7 @@ template class scoped_ptr : noncopyable { void reset( T* p=0 ) { if ( ptr != p ) { checked_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! @@ -216,14 +216,7 @@ template 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! @@ -397,6 +390,10 @@ template #endif // ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +#ifdef BOOST_MSVC +# pragma warning(pop) +#endif + #endif // BOOST_SMART_PTR_HPP From 94287044ba41125fe689417a65a35a1d03859e5a Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 24 May 2001 01:32:07 +0000 Subject: [PATCH 22/53] Oops! Fix boo boo from prior fix [SVN r10206] --- include/boost/smart_ptr.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 0ec807b..442598b 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -85,7 +85,6 @@ template class scoped_ptr : noncopyable { ~scoped_ptr() { checked_delete(ptr); } void reset( T* p=0 ) { if ( ptr != p ) { checked_delete(ptr); ptr = p; } } T& operator*() const { return *ptr; } // never throws -#ifdef BOOST_MSVC T* operator->() const { return ptr; } // never throws T* get() const { return ptr; } // never throws #ifdef BOOST_SMART_PTR_CONVERSION From c17921c417413ac11041b04f7ed23a5d435ce09a Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 24 May 2001 18:42:25 +0000 Subject: [PATCH 23/53] Documentation and example program improvements [SVN r10220] --- scoped_array.htm | 17 ++-- scoped_ptr.htm | 55 ++++++----- shared_array.htm | 29 ++---- shared_ptr.htm | 49 +++++----- smart_ptr.htm | 22 ++--- smart_ptr_test.cpp | 228 +++++++++++++++++++++++++-------------------- 6 files changed, 214 insertions(+), 186 deletions(-) diff --git a/scoped_array.htm b/scoped_array.htm index f146ee2..518b4cb 100644 --- a/scoped_array.htm +++ b/scoped_array.htm @@ -34,7 +34,7 @@ for that usage.

    pointed to.   T must meet the smart pointer common requirements.

    Class scoped_array Synopsis

    -
    #include <boost/smart_ptr.hpp>
    +
    #include <boost/smart_ptr.hpp>
     namespace boost {
     
     template<typename T> class scoped_array : noncopyable {
    @@ -59,27 +59,21 @@ template<typename T> class scoped_array : Common Requirements.

    scoped_array destructor

    ~scoped_array();
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Deletes the array pointed to by the stored pointer.  Note that in C++ delete[] on a pointer with a value of 0 is harmless.

    Does not throw exceptions.

    scoped_array reset

    void reset( T* p=0 )();
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    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.

    Does not throw exceptions.

    scoped_array operator[]

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

    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Returns a reference to element i of the array pointed to by the stored pointer.

    Behavior is undefined (and almost certainly undesirable) if get()==0, @@ -87,13 +81,16 @@ or if i is less than 0 or is greater or equal to the number of elements in the array.

    scoped_array get

    T* get() const;  // never throws
    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns the stored pointer.

    Class scoped_array example

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]


    -

    Revised  December 8, 1999

    +

    Revised  24 May, 2001 +

    © 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" diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 4945084..652d1bb 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -19,13 +19,16 @@ See example.

    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.  By -being noncopyable, it is -safer than shared_ptr or std::auto_ptr for pointers which should not be +signal its intent to retain ownership solely within the current scope.  +Because it is noncopyable, it is +safer than shared_ptr or std::auto_ptr for pointers which should not be copied.

    Because scoped_ptr is so simple, in its usual implementation every operation is as fast as for a built-in pointer and it has no more space overhead -that a built-in pointer.

    +that a built-in pointer.  (Because of the "complete type" +requirement for delete and reset members, they may have one additional function +call overhead in certain idioms.  See Handle/Body +Idiom.)   

    Class scoped_ptr cannot be used in C++ Standard Library containers.  See shared_ptr or std::auto_ptr if scoped_ptr does not meet your needs.

    Class scoped_ptr cannot correctly hold a pointer to a @@ -35,7 +38,7 @@ for that usage.

    pointed to.   T must meet the smart pointer common requirements.

    Class scoped_ptr Synopsis

    -
    #include <boost/smart_ptr.hpp>
    +
    #include <boost/smart_ptr.hpp>
     namespace boost {
     
     template<typename T> class scoped_ptr : noncopyable {
    @@ -59,38 +62,28 @@ template<typename T> class scoped_ptr : scoped_ptr constructors
     
    explicit scoped_ptr( T* p=0 );  // never throws
    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Constructs a scoped_ptr, storing a copy of p, which must have been allocated via a C++ new expression or be 0.

    scoped_ptr destructor

    ~scoped_ptr();
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Deletes the object pointed to by the stored pointer.  Note that in C++, delete on a pointer with a value of 0 is harmless.

    Does not throw exceptions.

    scoped_ptr reset

    void reset( T* p=0 );
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    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.

    Does not throw exceptions.

    scoped_ptr operator*

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

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Returns a reference to the object pointed to by the stored pointer.

    scoped_ptr operator-> and get

    T* operator->() const;  // never throws
     T* get() const;  // never throws
    -

    T is required be a complete type at point of instantiation of -operator->().  See Common -Requirements.

    -

    T is not required be a complete type at point of instantiation of -get().  See Common Requirements.

    +

    T is not required by get() be a complete type.  See Common Requirements.

    Both return the stored pointer.

    Class scoped_ptr examples

    #include <iostream>
    @@ -118,17 +111,37 @@ output:

    2 Buckle my shoe
    -

    Handle/Body Idiom

    -

    One common usage of scoped_ptr is to implement a handle/body idiom which avoids exposing the body (implementation) in the header +

    Rationale

    +

    The primary reason to use scoped_ptr rather than auto_ptr is to let readers +of your code know that you intend "resource acquisition is initialization" to be applied only for the current scope, and have no intent to transfer +ownership.

    +

    A secondary reason to use scoped_ptr is to prevent a later maintenance programmer from adding a function that actually transfers +ownership by returning the auto_ptr (because the maintenance programmer saw +auto_ptr, and assumed ownership could safely be transferred.) 

    +

    Think of bool vs int. We all know that under the covers bool is usually +just an int. Indeed, some argued against including bool in the +C++ standard because of that. But by coding bool rather than int, you tell your readers +what your intent is. Same with scoped_ptr - you are signaling intent.

    +

    It has been suggested that boost::scoped_ptr<T> is equivalent to +std::auto_ptr<T> const.  Ed Brey pointed out, however, that +reset() will not work on a std::auto_ptr<T> const.

    +

    Handle/Body Idiom

    +

    One common usage of scoped_ptr is to implement a handle/body (also +called pimpl) idiom which avoids exposing the body (implementation) in the header file.

    The scoped_ptr_example_test.cpp sample program includes a header file, scoped_ptr_example.hpp, which uses a scoped_ptr<> to an incomplete type to hide the implementation.   The instantiation of member functions which require a complete type occurs in the scoped_ptr_example.cpp -implementation file. 

    +implementation file.

    +

    FAQ

    +

    Q. Why doesn't scoped_ptr have a release() member?
    +A. Because the whole point of scoped_ptr is to signal intent not +to transfer ownership.  Use std::auto_ptr if ownership transfer is +required.


    -

    Revised 22 May 2001

    +

    Revised 24 May 2001

    © 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" diff --git a/shared_array.htm b/shared_array.htm index 24842d1..6de8704 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -30,13 +30,13 @@ structures. For example, if main() holds a shared_array pointing to array A, which directly or indirectly holds a shared_array pointing back to array A, then array A's use_count() will be 2, and destruction of the main() shared_array will leave array A dangling with a use_count() of 1.

    -

    A heavier duty alternative to a shared_array is a shared_ptr -to a C++ Standard Library vector.

    +

    A C++ Standard Library vector is a +heavier duty alternative to a shared_array.

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

    Class shared_array Synopsis

    -
    #include <boost/smart_ptr.hpp>
    +
    #include <boost/smart_ptr.hpp>
     namespace boost {
     
     template<typename T> class shared_array {
    @@ -108,23 +108,17 @@ name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

    Provides the type of the stored pointer.

    shared_array constructors

    explicit shared_array( T* p=0 );
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Constructs a shared_array, storing a copy of p, which must have been allocated via a C++ new[] expression or be 0. Afterwards, use_count() is 1 (even if p==0; see ~shared_array).

    The only exception which may be thrown is std::bad_alloc.  If an exception is thrown,  delete[] p is called.

    shared_array( const shared_array& r);  // never throws
    -

    T is not required be a complete type at point of instantiation.  -See Common Requirements.

    Constructs a shared_array, as if by storing a copy of the pointer stored in r. Afterwards, use_count() for all copies is 1 more than the initial r.use_count().

    shared_array destructor

    ~shared_array();
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    If use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++ delete[] on a pointer with @@ -132,8 +126,6 @@ a value of 0 is harmless.

    Does not throw exceptions.

    shared_array operator=

    shared_array& operator=( const shared_array& r);  // never throws
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    First, if use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++ delete[] on a @@ -143,8 +135,6 @@ of the pointer stored in r. Afterwards, use_count()r.use_count()

    shared_array reset

    void reset( T* p=0 );
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    First, if use_count() == 1, deletes the array pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete[] @@ -157,8 +147,6 @@ see ~shared_array).

    an exception is thrown,  delete[] p is called.

    shared_array operator[]

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

    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Returns a reference to element i of the array pointed to by the stored pointer.

    Behavior is undefined (and almost certainly undesirable) if get()==0, @@ -166,29 +154,30 @@ or if i is less than 0 or is greater or equal to the number of elements in the array.

    shared_array get

    T* get() const;  // never throws
    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns the stored pointer.

    shared_array use_count

    long use_count() const; // never throws

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns the number of shared_arrays sharing ownership of the stored pointer.

    shared_array unique

    bool unique() const; // never throws

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns use_count() == 1.

    shared_array swap

    void swap( shared_array<T>& other ) throw()

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Swaps the two smart pointers, as if by std::swap.

    Class shared_array example

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]


    -

    Revised December 8, 1999

    +

    Revised 24 May, 2001 +

    © 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" diff --git a/shared_ptr.htm b/shared_ptr.htm index 78ed702..cdf2b93 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -34,7 +34,7 @@ object A dangling with a use_count() of 1.

    pointed to.   T must meet the smart pointer Common requirements.

    Class shared_ptr Synopsis

    -
    #include <boost/smart_ptr.hpp>
    +
    #include <boost/smart_ptr.hpp>
     namespace boost {
     
     template<typename T> class shared_ptr {
    @@ -118,8 +118,6 @@ the macro name BOOST_NO_MEMBER_TEMPLATES is defined.

    Provides the type of the stored pointer.

    shared_ptr constructors

    explicit shared_ptr( T* p=0 );
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Constructs a shared_ptr, storing a copy of p, which must have been allocated via a C++ new expression or be 0. Afterwards, use_count() is 1 (even if p==0; see ~shared_ptr).

    @@ -130,8 +128,6 @@ template<typename Y> shared_ptr(const shared_ptr<Y>& r); // never throws template<typename Y> shared_ptr(std::auto_ptr<Y>& r);
    -

    T is not required be a complete type at point of instantiation.  -See Common Requirements.

    Constructs a shared_ptr, as if by storing a copy of the pointer stored in r. Afterwards, use_count() for all copies is 1 more than the initial r.use_count(), or 1 @@ -142,8 +138,6 @@ is std::bad_alloc.   If an exception is thrown, that constructor has no effect.

    shared_ptr destructor

    ~shared_ptr();
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    If use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete on a pointer @@ -155,8 +149,6 @@ template<typename Y> shared_ptr& operator=(const shared_ptr<Y>& r); template<typename Y> shared_ptr& operator=(std::auto_ptr<Y>& r);

    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    First, if use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. Note that in C++  delete on @@ -172,8 +164,6 @@ is std::bad_alloc.   If an exception is thrown, the function has no effect.

    shared_ptr reset

    void reset( T* p=0 );
    -

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    First, if use_count() == 1, deletes the object pointed to by the stored pointer. Otherwise, use_count() for any remaining copies is decremented by 1. 

    @@ -186,32 +176,26 @@ on a pointer with a value of 0 is harmless.

    an exception is thrown,  delete p is called.

    shared_ptr operator*

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

    T is required be a complete type at point of instantiation.  See Common -Requirements.

    Returns a reference to the object pointed to by the stored pointer.

    shared_ptr operator-> and get

    T* operator->() const;  // never throws
     T* get() const;  // never throws
    -

    T is required be a complete type at point of instantiation of -operator->().  See Common -Requirements.

    -

    T is not required be a complete type at point of instantiation of -get().  See Common Requirements.

    +

    T is not required by get() to be a complete type .  See Common Requirements.

    Both return the stored pointer.

    shared_ptr use_count

    long use_count() const; // never throws

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns the number of shared_ptrs sharing ownership of the stored pointer.

    shared_ptr unique

    bool unique() const; // never throws

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Returns use_count() == 1.

    shared_ptr swap

    void swap( shared_ptr<T>& other ) throw()

    -

    T is not required be a complete type at point of instantiation.  +

    T is not required be a complete type.  See Common Requirements.

    Swaps the two smart pointers, as if by std::swap.

    Class shared_ptr example

    @@ -224,8 +208,29 @@ at various times while push_back() and insert() container operations are perform 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.

    +

    Handle/Body Idiom

    +

    One common usage of shared_ptr is to implement a handle/body (also +called pimpl) idiom which avoids exposing the body (implementation) in the header +file.

    +

    The shared_ptr_example2_test.cpp +sample program includes a header file, shared_ptr_example2.hpp, +which uses a shared_ptr<> to an incomplete type to hide the +implementation.   The +instantiation of member functions which require a complete type occurs in the shared_ptr_example2.cpp +implementation file.

    +

    FAQ

    +

    Q. Why doesn't shared_ptr have template parameters supplying +traits or policies to allow extensive user customization?
    +A. Parameterization discourages users.  Shared_ptr is +carefully crafted to meet common needs without extensive parameterization. +Someday a highly configurable smart pointer may be invented that is also very +easy to use and very hard to misuse.  Until then, shared_ptr is the +smart pointer of choice for a wide range of applications.

    +

    Q. Why don't shared_ptr (and the other Boost smart pointers) +supply an automatic conversion to T*?
    +A. Automatic conversion is believed to be too error prone.


    -

    Revised 22 May, 2001 +

    Revised 24 May, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/smart_ptr.htm b/smart_ptr.htm index b7bcf04..07538e4 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -56,22 +56,20 @@ interest to those curious about performance issues.

    These smart pointer classes have a template parameter, T, which specifies the type of the object pointed to by the smart pointer.  The behavior of all four classes is undefined if the destructor or operator delete -for objects of type T throws exceptions.

    +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 point of instantiation of all member functions.

    +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 checked_delete().

    Rationale

    -

    The requirements on T are carefully crafted to ensure safety -yet allow handle-body (aka pimpl) and similar idioms.  In these idioms a +

    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.

    -

    Example

    -

    The scoped_ptr_example_test.cpp -sample program includes a header file, scoped_ptr_example.hpp, -which uses a scoped_ptr<> to an incomplete type.   The -instantiation of member functions which require a complete type occurs in the scoped_ptr_example.cpp -implementation file. 

    +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.

    Exception safety

    Several functions in these smart pointer classes are specified as having "no effect" or "no effect except such-and-such" if an @@ -144,7 +142,7 @@ implementation.

    See the Revision History section of the header for further contributors.


    Revised  22 May 200124 May 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright diff --git a/smart_ptr_test.cpp b/smart_ptr_test.cpp index cd4d61b..62c97ca 100644 --- a/smart_ptr_test.cpp +++ b/smart_ptr_test.cpp @@ -6,20 +6,26 @@ // implied warranty, and with no claim as to its suitability for any purpose. // 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. +#define BOOST_INCLUDE_MAIN +#include #include -#include #include #include #include -#ifdef NDEBUG -#error This test program makes no sense if NDEBUG is defined -#endif +class Incomplete; + +Incomplete * get_ptr( boost::shared_ptr& incomplete ) +{ + return incomplete.get(); +} using namespace std; using boost::scoped_ptr; @@ -28,7 +34,7 @@ using boost::shared_ptr; using boost::shared_array; template -void ck( const T* v1, T v2 ) { assert( *v1 == v2 ); } +void ck( const T* v1, T v2 ) { BOOST_TEST( *v1 == v2 ); } namespace { int UDT_use_count; // independent of pointer maintained counts @@ -48,169 +54,189 @@ class UDT { 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( scoped_ptr& incomplete ) +{ + return incomplete.get(); +} + +Incomplete * check_incomplete( shared_ptr& incomplete, + shared_ptr& i2 ) +{ + incomplete.swap(i2); + cout << incomplete.use_count() << " " << incomplete.unique() << endl; + return incomplete.get(); +} // main --------------------------------------------------------------------// // This isn't a very systematic test; it just hits some of the basics. -int main() { +int test_main( int, char ** ) { - assert( UDT_use_count == 0 ); // reality check + BOOST_TEST( UDT_use_count == 0 ); // reality check // test scoped_ptr with a built-in type long * lp = new long; scoped_ptr sp ( lp ); - assert( sp.get() == lp ); - assert( lp == sp.get() ); - assert( &*sp == lp ); + BOOST_TEST( sp.get() == lp ); + BOOST_TEST( lp == sp.get() ); + BOOST_TEST( &*sp == lp ); *sp = 1234568901L; - assert( *sp == 1234568901L ); - assert( *lp == 1234568901L ); + BOOST_TEST( *sp == 1234568901L ); + BOOST_TEST( *lp == 1234568901L ); ck( static_cast(sp.get()), 1234568901L ); ck( lp, *sp ); sp.reset(); - assert( sp.get() == 0 ); + BOOST_TEST( sp.get() == 0 ); // test scoped_ptr with a user defined type scoped_ptr udt_sp ( new UDT( 999888777 ) ); - assert( udt_sp->value() == 999888777 ); + BOOST_TEST( udt_sp->value() == 999888777 ); udt_sp.reset(); udt_sp.reset( new UDT( 111222333 ) ); - assert( udt_sp->value() == 111222333 ); + BOOST_TEST( udt_sp->value() == 111222333 ); udt_sp.reset( new UDT( 333222111 ) ); - assert( udt_sp->value() == 333222111 ); + BOOST_TEST( udt_sp->value() == 333222111 ); // test scoped_array with a build-in type char * sap = new char [ 100 ]; scoped_array sa ( sap ); - assert( sa.get() == sap ); - assert( sap == sa.get() ); + BOOST_TEST( sa.get() == sap ); + BOOST_TEST( sap == sa.get() ); strcpy( sa.get(), "Hot Dog with mustard and relish" ); - assert( strcmp( sa.get(), "Hot Dog with mustard and relish" ) == 0 ); - assert( strcmp( sap, "Hot Dog with mustard and relish" ) == 0 ); + BOOST_TEST( strcmp( sa.get(), "Hot Dog with mustard and relish" ) == 0 ); + BOOST_TEST( strcmp( sap, "Hot Dog with mustard and relish" ) == 0 ); - assert( sa[0] == 'H' ); - assert( sa[30] == 'h' ); + BOOST_TEST( sa[0] == 'H' ); + BOOST_TEST( sa[30] == 'h' ); sa[0] = 'N'; sa[4] = 'd'; - assert( strcmp( sap, "Not dog with mustard and relish" ) == 0 ); + BOOST_TEST( strcmp( sap, "Not dog with mustard and relish" ) == 0 ); sa.reset(); - assert( sa.get() == 0 ); + BOOST_TEST( sa.get() == 0 ); // test shared_ptr with a built-in type int * ip = new int; shared_ptr cp ( ip ); - assert( ip == cp.get() ); - assert( cp.use_count() == 1 ); + BOOST_TEST( ip == cp.get() ); + BOOST_TEST( cp.use_count() == 1 ); *cp = 54321; - assert( *cp == 54321 ); - assert( *ip == 54321 ); + BOOST_TEST( *cp == 54321 ); + BOOST_TEST( *ip == 54321 ); ck( static_cast(cp.get()), 54321 ); ck( static_cast(ip), *cp ); shared_ptr cp2 ( cp ); - assert( ip == cp2.get() ); - assert( cp.use_count() == 2 ); - assert( cp2.use_count() == 2 ); + BOOST_TEST( ip == cp2.get() ); + BOOST_TEST( cp.use_count() == 2 ); + BOOST_TEST( cp2.use_count() == 2 ); - assert( *cp == 54321 ); - assert( *cp2 == 54321 ); + BOOST_TEST( *cp == 54321 ); + BOOST_TEST( *cp2 == 54321 ); ck( static_cast(cp2.get()), 54321 ); ck( static_cast(ip), *cp2 ); shared_ptr cp3 ( cp ); - assert( cp.use_count() == 3 ); - assert( cp2.use_count() == 3 ); - assert( cp3.use_count() == 3 ); + BOOST_TEST( cp.use_count() == 3 ); + BOOST_TEST( cp2.use_count() == 3 ); + BOOST_TEST( cp3.use_count() == 3 ); cp.reset(); - assert( cp2.use_count() == 2 ); - assert( cp3.use_count() == 2 ); - assert( cp.use_count() == 1 ); + BOOST_TEST( cp2.use_count() == 2 ); + BOOST_TEST( cp3.use_count() == 2 ); + BOOST_TEST( cp.use_count() == 1 ); cp.reset( new int ); *cp = 98765; - assert( *cp == 98765 ); + BOOST_TEST( *cp == 98765 ); *cp3 = 87654; - assert( *cp3 == 87654 ); - assert( *cp2 == 87654 ); + BOOST_TEST( *cp3 == 87654 ); + BOOST_TEST( *cp2 == 87654 ); cp.swap( cp3 ); - assert( *cp == 87654 ); - assert( *cp2 == 87654 ); - assert( *cp3 == 98765 ); + BOOST_TEST( *cp == 87654 ); + BOOST_TEST( *cp2 == 87654 ); + BOOST_TEST( *cp3 == 98765 ); cp.swap( cp3 ); - assert( *cp == 98765 ); - assert( *cp2 == 87654 ); - assert( *cp3 == 87654 ); + BOOST_TEST( *cp == 98765 ); + BOOST_TEST( *cp2 == 87654 ); + BOOST_TEST( *cp3 == 87654 ); cp2 = cp2; - assert( cp2.use_count() == 2 ); - assert( *cp2 == 87654 ); + BOOST_TEST( cp2.use_count() == 2 ); + BOOST_TEST( *cp2 == 87654 ); cp = cp2; - assert( cp2.use_count() == 3 ); - assert( *cp2 == 87654 ); - assert( cp.use_count() == 3 ); - assert( *cp == 87654 ); + BOOST_TEST( cp2.use_count() == 3 ); + BOOST_TEST( *cp2 == 87654 ); + BOOST_TEST( cp.use_count() == 3 ); + BOOST_TEST( *cp == 87654 ); shared_ptr cp4; swap( cp2, cp4 ); - assert( cp4.use_count() == 3 ); - assert( *cp4 == 87654 ); - assert( cp2.get() == 0 ); + BOOST_TEST( cp4.use_count() == 3 ); + BOOST_TEST( *cp4 == 87654 ); + BOOST_TEST( cp2.get() == 0 ); #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION set< shared_ptr > scp; scp.insert(cp4); - assert( scp.find(cp4) != scp.end() ); - assert( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); + BOOST_TEST( scp.find(cp4) != scp.end() ); + BOOST_TEST( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); #endif // test shared_array with a built-in type char * cap = new char [ 100 ]; shared_array ca ( cap ); - assert( ca.get() == cap ); - assert( cap == ca.get() ); - assert( &ca[0] == 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" ); - assert( strcmp( ca.get(), "Hot Dog with mustard and relish" ) == 0 ); - assert( strcmp( cap, "Hot Dog with mustard and relish" ) == 0 ); + BOOST_TEST( strcmp( ca.get(), "Hot Dog with mustard and relish" ) == 0 ); + BOOST_TEST( strcmp( cap, "Hot Dog with mustard and relish" ) == 0 ); - assert( ca[0] == 'H' ); - assert( ca[30] == 'h' ); + BOOST_TEST( ca[0] == 'H' ); + BOOST_TEST( ca[30] == 'h' ); shared_array ca2 ( ca ); shared_array ca3 ( ca2 ); ca[0] = 'N'; ca[4] = 'd'; - assert( strcmp( ca.get(), "Not dog with mustard and relish" ) == 0 ); - assert( strcmp( ca2.get(), "Not dog with mustard and relish" ) == 0 ); - assert( strcmp( ca3.get(), "Not dog with mustard and relish" ) == 0 ); - assert( ca.use_count() == 3 ); - assert( ca2.use_count() == 3 ); - assert( ca3.use_count() == 3 ); + 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(); - assert( ca.use_count() == 2 ); - assert( ca3.use_count() == 2 ); - assert( ca2.use_count() == 1 ); + BOOST_TEST( ca.use_count() == 2 ); + BOOST_TEST( ca3.use_count() == 2 ); + BOOST_TEST( ca2.use_count() == 1 ); ca.reset(); - assert( ca.get() == 0 ); + BOOST_TEST( ca.get() == 0 ); shared_array ca4; swap( ca3, ca4 ); - assert( ca4.use_count() == 1 ); - assert( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 ); - assert( ca3.get() == 0 ); + BOOST_TEST( ca4.use_count() == 1 ); + BOOST_TEST( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 ); + BOOST_TEST( ca3.get() == 0 ); #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION set< shared_array > sca; sca.insert(ca4); - assert( sca.find(ca4) != sca.end() ); - assert( sca.find(ca4) == sca.find( shared_array(ca4) ) ); + BOOST_TEST( sca.find(ca4) != sca.end() ); + BOOST_TEST( sca.find(ca4) == sca.find( shared_array(ca4) ) ); #endif // test shared_array with user defined type @@ -221,38 +247,38 @@ int main() { udta[2].value( 333 ); shared_array udta2 ( udta ); - assert( udta[0].value() == 111 ); - assert( udta[1].value() == 222 ); - assert( udta[2].value() == 333 ); - assert( udta2[0].value() == 111 ); - assert( udta2[1].value() == 222 ); - assert( udta2[2].value() == 333 ); + 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(); - assert( udta2.get() == 0 ); - assert( udta.use_count() == 1 ); - assert( udta2.use_count() == 1 ); + BOOST_TEST( udta2.get() == 0 ); + BOOST_TEST( udta.use_count() == 1 ); + BOOST_TEST( udta2.use_count() == 1 ); - assert( UDT_use_count == 4 ); // reality check + BOOST_TEST( UDT_use_count == 4 ); // reality check // test shared_ptr with a user defined type UDT * up = new UDT; shared_ptr sup ( up ); - assert( up == sup.get() ); - assert( sup.use_count() == 1 ); + BOOST_TEST( up == sup.get() ); + BOOST_TEST( sup.use_count() == 1 ); sup->value( 54321 ) ; - assert( sup->value() == 54321 ); - assert( up->value() == 54321 ); + BOOST_TEST( sup->value() == 54321 ); + BOOST_TEST( up->value() == 54321 ); shared_ptr sup2; sup2 = sup; - assert( sup2->value() == 54321 ); - assert( sup.use_count() == 2 ); - assert( sup2.use_count() == 2 ); + BOOST_TEST( sup2->value() == 54321 ); + BOOST_TEST( sup.use_count() == 2 ); + BOOST_TEST( sup2.use_count() == 2 ); sup2 = sup2; - assert( sup2->value() == 54321 ); - assert( sup.use_count() == 2 ); - assert( sup2.use_count() == 2 ); + BOOST_TEST( sup2->value() == 54321 ); + BOOST_TEST( sup.use_count() == 2 ); + BOOST_TEST( sup2.use_count() == 2 ); cout << "OK" << endl; From c41b060618df4e3e51075b4f9690fd721d31cef6 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 24 May 2001 18:43:24 +0000 Subject: [PATCH 24/53] Initial checkin [SVN r10221] --- shared_ptr_example2.cpp | 21 +++++++++++++++++++++ shared_ptr_example2.hpp | 27 +++++++++++++++++++++++++++ shared_ptr_example2_test.cpp | 15 +++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 shared_ptr_example2.cpp create mode 100644 shared_ptr_example2.hpp create mode 100644 shared_ptr_example2_test.cpp diff --git a/shared_ptr_example2.cpp b/shared_ptr_example2.cpp new file mode 100644 index 0000000..eddc806 --- /dev/null +++ b/shared_ptr_example2.cpp @@ -0,0 +1,21 @@ +// Boost shared_ptr_example2 implementation file -----------------------------// + +#include "shared_ptr_example2.hpp" +#include + +class example::implementation +{ + public: + ~implementation() { std::cout << "destroying implementation\n"; } +}; + +example::example() : _imp( new implementation ) {} +example::example( const example & s ) : _imp( s._imp ) {} + +example & example::operator=( const example & s ) + { _imp = s._imp; return *this; } + +void example::do_something() + { std::cout << "use_count() is " << _imp.use_count() << "\n"; } + +example::~example() {} diff --git a/shared_ptr_example2.hpp b/shared_ptr_example2.hpp new file mode 100644 index 0000000..fea810b --- /dev/null +++ b/shared_ptr_example2.hpp @@ -0,0 +1,27 @@ +// Boost shared_ptr_example2 header file -----------------------------------// + +#include + +// This example demonstrates the handle/body idiom (also called pimpl and +// several other names). It separates the interface (in this header file) +// from the implementation (in shared_ptr_example2.cpp). + +// Note that even though example::implementation is an incomplete type in +// some translation units using this header, shared_ptr< implementation > +// is still valid because the type is complete where it counts - in the +// shared_ptr_example2.cpp translation unit where functions requiring a +// complete type are actually instantiated. + +class example +{ + public: + example(); + ~example(); + example( const example & ); + example & operator=( const example & ); + void do_something(); + private: + class implementation; + boost::shared_ptr< implementation > _imp; // hide implementation details +}; + diff --git a/shared_ptr_example2_test.cpp b/shared_ptr_example2_test.cpp new file mode 100644 index 0000000..1e9886b --- /dev/null +++ b/shared_ptr_example2_test.cpp @@ -0,0 +1,15 @@ +// Boost shared_ptr_example2_test main program ------------------------------// + +#include "shared_ptr_example2.hpp" + +int main() +{ + example a; + a.do_something(); + example b(a); + b.do_something(); + example c; + c = a; + c.do_something(); + return 0; +} \ No newline at end of file From 5fbc553611676fb063334774f134511cec1d1725 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 6 Jul 2001 13:23:07 +0000 Subject: [PATCH 25/53] Reorder shared_ptr code so VC++ 6 member templates work, allowing polymorphic pointers to now work with that compiler (Gary Powell) [SVN r10548] --- include/boost/smart_ptr.hpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index 442598b..d9c0aa5 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -9,6 +9,8 @@ // See http://www.boost.org for most recent version including documentation. // Revision History +// 6 Jul 01 Reorder shared_ptr code so VC++ 6 member templates work, allowing +// polymorphic pointers to now work with that compiler (Gary Powell) // 21 May 01 Require complete type where incomplete type is unsafe. // (suggested by Vladimir Prus) // 21 May 01 operator= fails if operand transitively owned by *this, as in a @@ -136,16 +138,9 @@ template class shared_ptr { catch (...) { checked_delete(p); throw; } } - shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); } // never throws - ~shared_ptr() { dispose(); } - shared_ptr& operator=(const shared_ptr& r) { - share(r.px,r.pn); - return *this; - } - -#if !defined( BOOST_NO_MEMBER_TEMPLATES ) +#if !defined( BOOST_NO_MEMBER_TEMPLATES ) || defined (BOOST_MSVC6_MEMBER_TEMPLATES) template shared_ptr(const shared_ptr& r) : px(r.px) { // never throws ++*(pn = r.pn); @@ -199,6 +194,15 @@ template class shared_ptr { #endif #endif + // The assignment operator and the copy constructor must come after + // the templated versions for MSVC6 to work. (Gary Powell) + shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); } // never throws + + shared_ptr& operator=(const shared_ptr& r) { + share(r.px,r.pn); + return *this; + } + void reset(T* p=0) { if ( px == p ) return; // fix: self-assignment safe if (--*pn == 0) { checked_delete(px); } @@ -231,7 +235,7 @@ template class shared_ptr { // Tasteless as this may seem, making all members public allows member templates // to work in the absence of member template friends. (Matthew Langston) // Don't split this line into two; that causes problems for some GCC 2.95.2 builds -#if defined(BOOST_NO_MEMBER_TEMPLATES) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) +#if ( defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) ) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) private: #endif From 65c3f2dc859669732b03f0e93f7c660876642440 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Thu, 12 Jul 2001 19:51:53 +0000 Subject: [PATCH 26/53] Add table of contents, improve the FAQ [SVN r10599] --- shared_ptr.htm | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index cdf2b93..33db24b 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -10,6 +10,14 @@

    c++boost.gif (8819 bytes)Class shared_ptr

    +

    Introduction
    +Synopsis

    +Members
    +Example
    +Handle/Body Idiom
    +Frequently Asked Questions
    +Smart Pointer Timings

    +

    Introduction

    Class shared_ptr stores a pointer to a dynamically allocated object. (Dynamically allocated objects are allocated with the C++ new expression.)   The object pointed to is guaranteed to be deleted when @@ -33,7 +41,7 @@ object A dangling with a use_count() of 1.

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

    -

    Class shared_ptr Synopsis

    +

    Class shared_ptr Synopsis

    #include <boost/smart_ptr.hpp>
     namespace boost {
     
    @@ -112,7 +120,7 @@ than supplying a full range of comparison operators (<, >, <=, >=).<
     name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

    The current implementation does not supply the member template functions if the macro name BOOST_NO_MEMBER_TEMPLATES is defined.

    -

    Class shared_ptr Members

    +

    Class shared_ptr Members

    shared_ptr element_type

    typedef T element_type;

    Provides the type of the stored pointer.

    @@ -218,19 +226,25 @@ which uses a shared_ptr<> to an incomplete type to hide the implementation.   The instantiation of member functions which require a complete type occurs in the shared_ptr_example2.cpp implementation file.

    -

    FAQ

    +

    Frequently Asked Questions

    Q. Why doesn't shared_ptr have template parameters supplying traits or policies to allow extensive user customization?
    A. Parameterization discourages users.  Shared_ptr is carefully crafted to meet common needs without extensive parameterization. Someday a highly configurable smart pointer may be invented that is also very easy to use and very hard to misuse.  Until then, shared_ptr is the -smart pointer of choice for a wide range of applications.

    +smart pointer of choice for a wide range of applications.  (Those +interested in policy based smart pointers should read Modern +C++ Design by Andrei Alexandrescu.)

    +

    Q. Why doesn't shared_ptr use a linked list implementation?
    +A. A linked list implementation does not offer enough advantages to +offset the added cost of an extra pointer.  See timings +page.

    Q. Why don't shared_ptr (and the other Boost smart pointers) supply an automatic conversion to T*?
    A. Automatic conversion is believed to be too error prone.


    -

    Revised 24 May, 2001 +

    Revised 12 July, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright From aea7d0c9c8ffe5763148bc30ea1ed7ed984505a3 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 13 Jul 2001 14:07:08 +0000 Subject: [PATCH 27/53] Add FAQ why use_count()? [SVN r10604] --- shared_ptr.htm | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index 33db24b..3f6c7df 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -227,24 +227,28 @@ implementation.   The instantiation of member functions which require a complete type occurs in the shared_ptr_example2.cpp implementation file.

    Frequently Asked Questions

    -

    Q. Why doesn't shared_ptr have template parameters supplying +

    Q. Why doesn't shared_ptr have template parameters supplying traits or policies to allow extensive user customization?
    -A. Parameterization discourages users.  Shared_ptr is +A. Parameterization discourages users.  Shared_ptr is carefully crafted to meet common needs without extensive parameterization. Someday a highly configurable smart pointer may be invented that is also very easy to use and very hard to misuse.  Until then, shared_ptr is the smart pointer of choice for a wide range of applications.  (Those interested in policy based smart pointers should read Modern C++ Design by Andrei Alexandrescu.)

    -

    Q. Why doesn't shared_ptr use a linked list implementation?
    -A. A linked list implementation does not offer enough advantages to +

    Q. Why doesn't shared_ptr use a linked list implementation?
    +A. A linked list implementation does not offer enough advantages to offset the added cost of an extra pointer.  See timings page.

    -

    Q. Why don't shared_ptr (and the other Boost smart pointers) +

    Q. Why don't shared_ptr (and the other Boost smart pointers) supply an automatic conversion to T*?
    -A. Automatic conversion is believed to be too error prone.

    +A. Automatic conversion is believed to be too error prone.

    +

    Q. Why does shared_ptr supply use_count()?
    +A. As an aid to writing test cases and debugging displays. One of the +progenitors had use_count(), and it was useful in tracking down bugs in a +complex project that turned out to have cyclic-dependencies.


    -

    Revised 12 July, 2001 +

    Revised 13 July, 2001

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright From c5846378ab2789f82510bbc80ac5a1a8dacddb6e Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 13 Jul 2001 16:32:34 +0000 Subject: [PATCH 28/53] More FAQ entries added. [SVN r10605] --- shared_ptr.htm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/shared_ptr.htm b/shared_ptr.htm index 3f6c7df..c9fe1e6 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -247,6 +247,14 @@ supply an automatic conversion to T*?
    A. As an aid to writing test cases and debugging displays. One of the progenitors had use_count(), and it was useful in tracking down bugs in a complex project that turned out to have cyclic-dependencies.

    +

    Q. Why doesn't shared_ptr specify complexity requirements?
    +A. Because complexity limit implementors and complicate the specification without apparent benefit to +shared_ptr users. For example, error-checking implementations might become non-conforming if they +had to meet stringent complexity requirements.

    +

    Q. Why doesn't shared_ptr provide (your pet feature here)?
    +A. Because (your pet feature here) would mandate a reference counted (or a link-list, or ...) implementation. This is not the intent. +[Provided by Peter Dimov]
    +


    Revised 13 July, 2001

    From a93dfc1837480fab88c646b7d4f5d403ba26188a Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sun, 19 Aug 2001 15:08:33 +0000 Subject: [PATCH 29/53] Fix broken hyperlink [SVN r10896] --- smarttests.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smarttests.htm b/smarttests.htm index f78b618..ad0fbb1 100644 --- a/smarttests.htm +++ b/smarttests.htm @@ -530,7 +530,7 @@ Pointers Timings spreads its information as in the case of linked pointer.
    -

    Revised 27 Jul 2000 +

    Revised 17 Aug 2001

    © Copyright Gavin Collings 2000. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all From 1a9b1dd123abc92a0ba534f8e457bd94045a3f74 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 1 Oct 2001 15:54:23 +0000 Subject: [PATCH 30/53] 1.25.0 Final runup [SVN r11315] --- smarttests.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smarttests.htm b/smarttests.htm index ad0fbb1..dcd8fda 100644 --- a/smarttests.htm +++ b/smarttests.htm @@ -530,7 +530,7 @@ Pointers Timings spreads its information as in the case of linked pointer.


    -

    Revised 17 Aug 2001 +

    Revised 19 Aug 2001

    © Copyright Gavin Collings 2000. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all From 11ec5153783df16d216450fbdc8edd8bd0862d7f Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 11 Jan 2002 16:15:09 +0000 Subject: [PATCH 31/53] Fix broken link [SVN r12285] --- shared_ptr.htm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index c9fe1e6..2108b43 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -2,21 +2,22 @@ shared_ptr - + -

    c++boost.gif (8819 bytes)Class +

    +c++boost.gif (8819 bytes)Class shared_ptr

    Introduction
    Synopsis

    Members
    Example
    Handle/Body Idiom
    -Frequently Asked Questions
    -Smart Pointer Timings

    +Frequently Asked Questions
    +Smart Pointer Timings

    Introduction

    Class shared_ptr stores a pointer to a dynamically allocated object. (Dynamically allocated objects are allocated with the C++ new @@ -256,7 +257,7 @@ had to meet stringent complexity requirements.

    [Provided by Peter Dimov]


    -

    Revised 13 July, 2001 +

    Revised 11 January, 2002

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright @@ -266,4 +267,4 @@ any purpose.

    - + \ No newline at end of file From 4f964ce6ada8f1585828f1320511acc9892b94fa Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 11 Jan 2002 20:20:07 +0000 Subject: [PATCH 32/53] Add FAQ: why no release() [SVN r12286] --- shared_ptr.htm | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/shared_ptr.htm b/shared_ptr.htm index 2108b43..4913f65 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -252,9 +252,23 @@ complex project that turned out to have cyclic-dependencies.

    A. Because complexity limit implementors and complicate the specification without apparent benefit to shared_ptr users. For example, error-checking implementations might become non-conforming if they had to meet stringent complexity requirements.

    +

    Q. Why doesn't shared_ptr provide a release() function?
    +A. shared_ptr cannot give away ownership unless it's unique() +because the other copy will still destroy the object.

    +

    Consider:

    +
    +
    shared_ptr<int> a(new int);
    +shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2
    +
    +int * p = a.release();
    +
    +// Who owns p now? b will still call delete on it in its destructor.
    +
    +

    [Provided by Peter Dimov]

    Q. Why doesn't shared_ptr provide (your pet feature here)?
    A. Because (your pet feature here) would mandate a reference counted (or a link-list, or ...) implementation. This is not the intent. -[Provided by Peter Dimov]
    +[Provided by Peter Dimov]

    +



    Revised 11 January, 2002 From b104e9ae7843821f98efdbbe1ae0ea903240fb58 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 17 Jan 2002 12:46:45 +0000 Subject: [PATCH 33/53] smart_ptr.hpp less<> fixed, partial specialization enabled on Sun 5.3 [SVN r12334] --- include/boost/smart_ptr.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index d9c0aa5..dbcb787 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -377,7 +377,7 @@ template { bool operator()(const boost::shared_ptr& a, const boost::shared_ptr& b) const - { return less()(a.get(),b.get()); } + { return std::less()(a.get(),b.get()); } }; template @@ -386,7 +386,7 @@ template { bool operator()(const boost::shared_array& a, const boost::shared_array& b) const - { return less()(a.get(),b.get()); } + { return std::less()(a.get(),b.get()); } }; } // namespace std From f255439ece0687eb61b8bcae576faf9b2521912f Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sat, 19 Jan 2002 15:54:28 +0000 Subject: [PATCH 34/53] add eof newline [SVN r12358] --- scoped_ptr_example_test.cpp | 2 +- shared_ptr_example2_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scoped_ptr_example_test.cpp b/scoped_ptr_example_test.cpp index 3629ec8..1b77af2 100644 --- a/scoped_ptr_example_test.cpp +++ b/scoped_ptr_example_test.cpp @@ -7,4 +7,4 @@ int main() example my_example; my_example.do_something(); return 0; -} \ No newline at end of file +} diff --git a/shared_ptr_example2_test.cpp b/shared_ptr_example2_test.cpp index 1e9886b..3a2cf53 100644 --- a/shared_ptr_example2_test.cpp +++ b/shared_ptr_example2_test.cpp @@ -12,4 +12,4 @@ int main() c = a; c.do_something(); return 0; -} \ No newline at end of file +} From 09c14760632986e4068a059411cae9c89cb84f46 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 22 Jan 2002 13:38:52 +0000 Subject: [PATCH 35/53] Smart pointer enhancements, initial commit [SVN r12439] --- include/boost/detail/atomic_count.hpp | 122 +++++++++ include/boost/detail/atomic_count_linux.hpp | 64 +++++ .../boost/detail/atomic_count_pthreads.hpp | 99 +++++++ include/boost/detail/atomic_count_win32.hpp | 68 +++++ include/boost/detail/shared_array_nmt.hpp | 135 ++++++++++ include/boost/detail/shared_count.hpp | 250 ++++++++++++++++++ include/boost/detail/shared_ptr_nmt.hpp | 165 ++++++++++++ include/boost/scoped_array.hpp | 87 ++++++ include/boost/scoped_ptr.hpp | 90 +++++++ include/boost/shared_array.hpp | 136 ++++++++++ include/boost/shared_ptr.hpp | 246 +++++++++++++++++ include/boost/weak_ptr.hpp | 175 ++++++++++++ 12 files changed, 1637 insertions(+) create mode 100644 include/boost/detail/atomic_count.hpp create mode 100644 include/boost/detail/atomic_count_linux.hpp create mode 100644 include/boost/detail/atomic_count_pthreads.hpp create mode 100644 include/boost/detail/atomic_count_win32.hpp create mode 100644 include/boost/detail/shared_array_nmt.hpp create mode 100644 include/boost/detail/shared_count.hpp create mode 100644 include/boost/detail/shared_ptr_nmt.hpp create mode 100644 include/boost/scoped_array.hpp create mode 100644 include/boost/scoped_ptr.hpp create mode 100644 include/boost/shared_array.hpp create mode 100644 include/boost/shared_ptr.hpp create mode 100644 include/boost/weak_ptr.hpp diff --git a/include/boost/detail/atomic_count.hpp b/include/boost/detail/atomic_count.hpp new file mode 100644 index 0000000..0a22e64 --- /dev/null +++ b/include/boost/detail/atomic_count.hpp @@ -0,0 +1,122 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED + +#if _MSC_VER >= 1020 +#pragma once +#endif + +// +// boost/detail/atomic_count.hpp - thread/SMP safe reference counter +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// +// 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. +// +// typedef boost::detail::atomic_count; +// +// atomic_count a(n); +// +// (n is convertible to long) +// +// Effects: Constructs an atomic_count with an initial value of n +// +// a; +// +// Returns: (long) the current value of a +// +// ++a; +// +// Effects: Atomically increments the value of a +// Returns: nothing +// +// --a; +// +// Effects: Atomically decrements the value of a +// Returns: (long) zero if the new value of a is zero, +// unspecified non-zero value otherwise (usually the new value) +// +// Important note: when --a returns zero, it must act as a +// read memory barrier (RMB); i.e. the calling thread must +// have a synchronized view of the memory +// +// On Intel IA-32 (x86) memory is always synchronized, so this +// is not a problem. +// +// On many architectures the atomic instructions already act as +// a memory barrier. +// +// This property is necessary for proper reference counting, since +// a thread can update the contents of a shared object, then +// release its reference, and another thread may immediately +// release the last reference causing object destruction. +// +// The destructor needs to have a synchronized view of the +// object to perform proper cleanup. +// +// Original example by Alexander Terekhov: +// +// Given: +// +// - a mutable shared object OBJ; +// - two threads THREAD1 and THREAD2 each holding +// a private smart_ptr object pointing to that OBJ. +// +// t1: THREAD1 updates OBJ (thread-safe via some synchronization) +// and a few cycles later (after "unlock") destroys smart_ptr; +// +// t2: THREAD2 destroys smart_ptr WITHOUT doing any synchronization +// with respect to shared mutable object OBJ; OBJ destructors +// are called driven by smart_ptr interface... +// + + +#include + +#ifndef BOOST_HAS_THREADS + +namespace boost +{ + +namespace detail +{ + +typedef long atomic_count; + +} + +} + +#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) + +#include "atomic_count_win32.hpp" + +#elif defined(linux) || defined(__linux) || defined(__linux__) + +#include "atomic_count_linux.hpp" + +#elif defined(BOOST_HAS_PTHREADS) + +#include "atomic_count_pthreads.hpp" + +#else + +// #warning Unrecognized platform, detail::atomic_count will not be thread safe + +namespace boost +{ + +namespace detail +{ + +typedef long atomic_counter; + +} + +} + +#endif + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_HPP_INCLUDED diff --git a/include/boost/detail/atomic_count_linux.hpp b/include/boost/detail/atomic_count_linux.hpp new file mode 100644 index 0000000..8fd5346 --- /dev/null +++ b/include/boost/detail/atomic_count_linux.hpp @@ -0,0 +1,64 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_LINUX_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_LINUX_HPP_INCLUDED + +// +// boost/detail/atomic_count_linux.hpp +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// +// 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. +// + +// +// On Linux, atomic.h is usually located in /usr/include/asm +// + +#include + +namespace boost +{ + +namespace detail +{ + +class atomic_count +{ +public: + + explicit atomic_count(long v) + { + atomic_t init = ATOMIC_INIT(v); + value_ = init; + } + + void operator++() + { + atomic_inc(&value_); + } + + long operator--() + { + return !atomic_dec_and_test(&value_); + } + + operator long() const + { + return atomic_read(&value_); + } + +private: + + atomic_count(atomic_count const &); + atomic_count & operator=(atomic_count const &); + + atomic_t value_; +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_LINUX_HPP_INCLUDED diff --git a/include/boost/detail/atomic_count_pthreads.hpp b/include/boost/detail/atomic_count_pthreads.hpp new file mode 100644 index 0000000..eb1dd97 --- /dev/null +++ b/include/boost/detail/atomic_count_pthreads.hpp @@ -0,0 +1,99 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED + +// +// boost/detail/atomic_count_pthreads.hpp +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// +// 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. +// + +#include + +// +// The generic pthread_mutex-based implementation sometimes leads to +// inefficiencies. Example: a class with two atomic_count members +// can get away with a single mutex. +// +// Define a macro so that users can detect the situation and optimize. +// + +#define BOOST_ATOMIC_COUNT_USES_PTHREADS + +namespace boost +{ + +namespace detail +{ + +class atomic_count +{ +private: + + class scoped_lock + { + public: + + scoped_lock(pthread_mutex_t & m): m_(m) + { + pthread_mutex_lock(&m_); + } + + ~scoped_lock() + { + pthread_mutex_unlock(&m_); + } + + private: + + pthread_mutex_t & m_; + }; + +public: + + explicit atomic_count(long v): value_(v) + { + pthread_mutex_init(&mutex_, 0); + } + + ~atomic_count() + { + pthread_mutex_destroy(&mutex_); + } + + void operator++() + { + scoped_lock lock(mutex_); + ++value_; + } + + long operator--() + { + scoped_lock lock(mutex_); + return --value_; + } + + operator long() const + { + scoped_lock lock(mutex_); + return value_; + } + +private: + + atomic_count(atomic_count const &); + atomic_count & operator=(atomic_count const &); + + mutable pthread_mutex_t mutex_; + long value_; +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_PTHREADS_HPP_INCLUDED diff --git a/include/boost/detail/atomic_count_win32.hpp b/include/boost/detail/atomic_count_win32.hpp new file mode 100644 index 0000000..163a26d --- /dev/null +++ b/include/boost/detail/atomic_count_win32.hpp @@ -0,0 +1,68 @@ +#ifndef BOOST_DETAIL_ATOMIC_COUNT_WIN32_HPP_INCLUDED +#define BOOST_DETAIL_ATOMIC_COUNT_WIN32_HPP_INCLUDED + +#if _MSC_VER >= 1020 +#pragma once +#endif + +// +// boost/detail/atomic_count_win32.hpp +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// +// 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. +// + +namespace boost +{ + +namespace detail +{ + +// Avoid #including + +namespace win32 +{ +extern "C" __declspec(dllimport) long __stdcall InterlockedIncrement(long volatile *); +extern "C" __declspec(dllimport) long __stdcall InterlockedDecrement(long volatile *); +} + +class atomic_count +{ +public: + + explicit atomic_count(long v): value_(v) + { + } + + long operator++() + { + return win32::InterlockedIncrement(&value_); + } + + long operator--() + { + return win32::InterlockedDecrement(&value_); + } + + operator long() const + { + return value_; + } + +private: + + atomic_count(atomic_count const &); + atomic_count & operator=(atomic_count const &); + + volatile long value_; +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_ATOMIC_COUNT_WIN32_HPP_INCLUDED diff --git a/include/boost/detail/shared_array_nmt.hpp b/include/boost/detail/shared_array_nmt.hpp new file mode 100644 index 0000000..e5137a3 --- /dev/null +++ b/include/boost/detail/shared_array_nmt.hpp @@ -0,0 +1,135 @@ +#ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED +#define BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED + +// +// detail/shared_array_nmt.hpp - shared_array.hpp without member templates +// +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. +// + +#include +#include +#include + +#include // for std::ptrdiff_t +#include // for std::swap +#include // for std::less + +namespace boost +{ + +template class shared_array +{ +private: + + typedef detail::atomic_count count_type; + +public: + + typedef T element_type; + + explicit shared_array(T * p = 0): px(p) + { + try // prevent leak if new throws + { + pn = new count_type(1); + } + catch(...) + { + checked_array_delete(p); + throw; + } + } + + ~shared_array() + { + if(--*pn == 0) + { + checked_array_delete(px); + delete pn; + } + } + + shared_array(shared_array const & r) : px(r.px) // never throws + { + pn = r.pn; + ++*pn; + } + + shared_array & operator=(shared_array const & r) + { + shared_array(r).swap(*this); + return *this; + } + + void reset(T * p = 0) + { + shared_array(p).swap(*this); + } + + T * get() const // never throws + { + return px; + } + + T & operator[](std::ptrdiff_t i) const // never throws + { + BOOST_ASSERT(px != 0); + BOOST_ASSERT(i >= 0); + return px[i]; + } + + long use_count() const // never throws + { + return *pn; + } + + bool unique() const // never throws + { + return *pn == 1; + } + + void swap(shared_array & other) // never throws + { + std::swap(px, other.px); + std::swap(pn, other.pn); + } + +private: + + T * px; // contained pointer + count_type * pn; // ptr to reference counter + +}; // shared_array + +template inline bool operator==(shared_array const & a, shared_array const & b) +{ + return a.get() == b.get(); +} + +template inline bool operator!=(shared_array const & a, shared_array const & b) +{ + return a.get() != b.get(); +} + +template inline bool operator<(shared_array const & a, shared_array const & b) +{ + return std::less()(a.get(), b.get()); +} + +template void swap(shared_array & a, shared_array & b) +{ + a.swap(b); +} + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_SHARED_ARRAY_NMT_HPP_INCLUDED diff --git a/include/boost/detail/shared_count.hpp b/include/boost/detail/shared_count.hpp new file mode 100644 index 0000000..46ceaf5 --- /dev/null +++ b/include/boost/detail/shared_count.hpp @@ -0,0 +1,250 @@ +#ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED +#define BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED + +#if _MSC_VER >= 1020 +#pragma once +#endif + +// +// detail/shared_count.hpp +// +// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. +// +// 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. +// + +#include + +namespace boost +{ + +namespace detail +{ + +class counted_base +{ +public: + + typedef atomic_count count_type; + + explicit counted_base(long initial_use_count, long initial_weak_count): + use_count_(initial_use_count), weak_count_(initial_weak_count), self_deleter_(&self_delete) + { + } + + virtual ~counted_base() + { + } + + virtual void dispose() + { + } + + void add_ref() + { + ++use_count_; + ++weak_count_; + } + + void release() + { + if(--use_count_ == 0) + { + dispose(); + } + + if(--weak_count_ == 0) + { + // not a direct 'delete this', because the inlined + // release() may use a different heap manager + self_deleter_(this); + } + } + + void weak_add_ref() + { + ++weak_count_; + } + + void weak_release() + { + if(--weak_count_ == 0) + { + self_deleter_(this); + } + } + + long use_count() const + { + return use_count_; + } + +private: + + counted_base(counted_base const &); + counted_base & operator= (counted_base const &); + + static void self_delete(counted_base * p) + { + delete p; + } + + count_type use_count_; + count_type weak_count_; + + void (*self_deleter_) (counted_base *); +}; + +template class counted_base_impl: public counted_base +{ +private: + + P ptr; // copy constructor must not throw + D del; // copy constructor must not throw + + counted_base_impl(counted_base_impl const &); + counted_base_impl & operator= (counted_base_impl const &); + +public: + + counted_base_impl(P p, D d, long initial_use_count, long initial_weak_count): + counted_base(initial_use_count, initial_weak_count), ptr(p), del(d) + { + } + + virtual void dispose() + { + del(ptr); + } +}; + + +class shared_count +{ +private: + + counted_base * pi_; + + friend class weak_count; + +public: + + template shared_count(P p, D d): pi_(0) + { + try + { + pi_ = new counted_base_impl(p, d, 1, 1); + } + catch(...) + { + d(p); // delete p + throw; + } + } + + ~shared_count() // nothrow + { + pi_->release(); + } + + shared_count(shared_count const & r): pi_(r.pi_) // nothrow + { + pi_->add_ref(); + } + + shared_count & operator= (shared_count const & r) // nothrow + { + counted_base * tmp = r.pi_; + tmp->add_ref(); + pi_->release(); + pi_ = tmp; + + return *this; + } + + void swap(shared_count & r) // nothrow + { + counted_base * tmp = r.pi_; + r.pi_ = pi_; + pi_ = tmp; + } + + long use_count() const // nothrow + { + return pi_->use_count(); + } + + bool unique() const // nothrow + { + return pi_->use_count() == 1; + } +}; + +class weak_count +{ +private: + + counted_base * pi_; + +public: + + weak_count(): pi_(new counted_base(0, 1)) // can throw + { + } + + weak_count(shared_count const & r): pi_(r.pi_) // nothrow + { + pi_->weak_add_ref(); + } + + weak_count(weak_count const & r): pi_(r.pi_) // nothrow + { + pi_->weak_add_ref(); + } + + ~weak_count() // nothrow + { + pi_->weak_release(); + } + + weak_count & operator= (shared_count const & r) // nothrow + { + counted_base * tmp = r.pi_; + tmp->weak_add_ref(); + pi_->weak_release(); + pi_ = tmp; + + return *this; + } + + weak_count & operator= (weak_count const & r) // nothrow + { + counted_base * tmp = r.pi_; + tmp->weak_add_ref(); + pi_->weak_release(); + pi_ = tmp; + + return *this; + } + + void swap(weak_count & r) // nothrow + { + counted_base * tmp = r.pi_; + r.pi_ = pi_; + pi_ = tmp; + } + + long use_count() const // nothrow + { + return pi_->use_count(); + } +}; + +} // namespace detail + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_SHARED_COUNT_HPP_INCLUDED diff --git a/include/boost/detail/shared_ptr_nmt.hpp b/include/boost/detail/shared_ptr_nmt.hpp new file mode 100644 index 0000000..8b268a2 --- /dev/null +++ b/include/boost/detail/shared_ptr_nmt.hpp @@ -0,0 +1,165 @@ +#ifndef BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED +#define BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED + +// +// detail/shared_ptr_nmt.hpp - shared_ptr.hpp without member templates +// +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. +// + +#include +#include +#include + +#ifndef BOOST_NO_AUTO_PTR +#include // for std::auto_ptr +#endif + +#include // for std::swap +#include // for std::less + +namespace boost +{ + +template class shared_ptr +{ +private: + + typedef detail::atomic_count count_type; + +public: + + typedef T element_type; + + explicit shared_ptr(T * p = 0): px(p) + { + try // prevent leak if new throws + { + pn = new count_type(1); + } + catch(...) + { + checked_delete(p); + throw; + } + } + + ~shared_ptr() + { + if(--*pn == 0) + { + checked_delete(px); + delete pn; + } + } + + shared_ptr(shared_ptr const & r): px(r.px) // never throws + { + pn = r.pn; + ++*pn; + } + + shared_ptr & operator=(shared_ptr const & r) + { + shared_ptr(r).swap(*this); + return *this; + } + +#ifndef BOOST_NO_AUTO_PTR + + explicit shared_ptr(std::auto_ptr & r) + { + pn = new count_type(1); // may throw + px = r.release(); // fix: moved here to stop leak if new throws + } + + shared_ptr & operator=(std::auto_ptr & r) + { + shared_ptr(r).swap(*this); + return *this; + } + +#endif + + void reset(T * p = 0) + { + shared_ptr(p).swap(*this); + } + + T & operator*() const // never throws + { + BOOST_ASSERT(px != 0); + return *px; + } + + T * operator->() const // never throws + { + BOOST_ASSERT(px != 0); + return px; + } + + T * get() const // never throws + { + return px; + } + + long use_count() const // never throws + { + return *pn; + } + + bool unique() const // never throws + { + return *pn == 1; + } + + void swap(shared_ptr & other) // never throws + { + std::swap(px, other.px); + std::swap(pn, other.pn); + } + +private: + + T * px; // contained pointer + count_type * pn; // ptr to reference counter +}; + +template inline bool operator==(shared_ptr const & a, shared_ptr const & b) +{ + return a.get() == b.get(); +} + +template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) +{ + return a.get() != b.get(); +} + +template inline bool operator<(shared_ptr const & a, shared_ptr const & b) +{ + return std::less()(a.get(), b.get()); +} + +template void swap(shared_ptr & a, shared_ptr & b) +{ + a.swap(b); +} + +// get_pointer() enables boost::mem_fn to recognize shared_ptr + +template inline T * get_pointer(shared_ptr const & p) +{ + return p.get(); +} + +} // namespace boost + +#endif // #ifndef BOOST_DETAIL_SHARED_PTR_NMT_HPP_INCLUDED diff --git a/include/boost/scoped_array.hpp b/include/boost/scoped_array.hpp new file mode 100644 index 0000000..3fcce8d --- /dev/null +++ b/include/boost/scoped_array.hpp @@ -0,0 +1,87 @@ +#ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED +#define BOOST_SCOPED_ARRAY_HPP_INCLUDED + +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. + +#include +#include // in case ptrdiff_t not in std +#include // for std::ptrdiff_t + +namespace boost +{ + +// 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. + +template class scoped_array // noncopyable +{ +private: + + T* ptr; + + scoped_array(scoped_array const &); + scoped_array & operator=(scoped_array const &); + +public: + + typedef T element_type; + + explicit scoped_array( T* p = 0 ): ptr(p) // never throws + { + } + + ~scoped_array() + { + typedef char type_must_be_complete[sizeof(T)]; + delete [] ptr; + } + + void reset( T* p = 0 ) + { + typedef char type_must_be_complete[sizeof(T)]; + + if ( ptr != p ) + { + delete [] ptr; + ptr = p; + } + } + + T& operator[](std::ptrdiff_t i) const // never throws + { + BOOST_ASSERT(ptr != 0); + BOOST_ASSERT(i >= 0); + return ptr[i]; + } + + T* get() const // never throws + { + return ptr; + } + + void swap(scoped_array & rhs) + { + T * tmp = rhs.ptr; + rhs.ptr = ptr; + ptr = tmp; + } + +}; + +template inline void swap(scoped_array & a, scoped_array & b) +{ + a.swap(b); +} + +} // namespace boost + +#endif // #ifndef BOOST_SCOPED_ARRAY_HPP_INCLUDED diff --git a/include/boost/scoped_ptr.hpp b/include/boost/scoped_ptr.hpp new file mode 100644 index 0000000..b920a72 --- /dev/null +++ b/include/boost/scoped_ptr.hpp @@ -0,0 +1,90 @@ +#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED +#define BOOST_SCOPED_PTR_HPP_INCLUDED + +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. + +#include + +namespace boost +{ + +// 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; +// use shared_ptr or std::auto_ptr if your needs are more complex. + +template class scoped_ptr // noncopyable +{ +private: + + T* ptr; + + scoped_ptr(scoped_ptr const &); + scoped_ptr & operator=(scoped_ptr const &); + +public: + + typedef T element_type; + + explicit scoped_ptr( T* p = 0 ): ptr(p) // never throws + { + } + + ~scoped_ptr() + { + typedef char type_must_be_complete[sizeof(T)]; + delete ptr; + } + + void reset( T* p = 0 ) + { + typedef char type_must_be_complete[sizeof(T)]; + + if ( ptr != p ) + { + delete ptr; + ptr = p; + } + } + + T& operator*() const // never throws + { + BOOST_ASSERT(ptr != 0); + return *ptr; + } + + T* operator->() const // never throws + { + BOOST_ASSERT(ptr != 0); + return ptr; + } + + T* get() const // never throws + { + return ptr; + } + + void swap(scoped_ptr & rhs) + { + T * tmp = rhs.ptr; + rhs.ptr = ptr; + ptr = tmp; + } +}; + +template inline void swap(scoped_ptr & a, scoped_ptr & b) +{ + a.swap(b); +} + +} // namespace boost + +#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED diff --git a/include/boost/shared_array.hpp b/include/boost/shared_array.hpp new file mode 100644 index 0000000..85741b2 --- /dev/null +++ b/include/boost/shared_array.hpp @@ -0,0 +1,136 @@ +#ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED +#define BOOST_SHARED_ARRAY_HPP_INCLUDED + +// +// shared_array.hpp +// +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. +// + +#include // for broken compiler workarounds + +#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) +#include +#else + +#include +#include + +#include + +#include // for std::ptrdiff_t +#include // for std::swap +#include // for std::less + +namespace boost +{ + +// +// shared_array +// +// shared_array extends shared_ptr to arrays. +// The array pointed to is deleted when the last shared_array pointing to it +// is destroyed or reset. +// + +template class shared_array +{ +private: + + // Borland 5.5.1 specific workarounds + typedef checked_array_deleter deleter; + typedef shared_array this_type; + +public: + + typedef T element_type; + + explicit shared_array(T * p = 0): px(p), pn(p, deleter()) + { + } + + // + // Requirements: D's copy constructor must not throw + // + // shared_array will release p by calling d(p) + // + + template shared_array(T * p, D d): px(p), pn(p, d) + { + } + +// generated copy constructor, assignment, destructor are fine + + void reset(T * p = 0) + { + this_type(p).swap(*this); + } + + T & operator[] (std::ptrdiff_t i) const // never throws + { + BOOST_ASSERT(px != 0); + BOOST_ASSERT(i >= 0); + return px[i]; + } + + T * get() const // never throws + { + return px; + } + + long use_count() const // never throws + { + return pn.use_count(); + } + + bool unique() const // never throws + { + return pn.unique(); + } + + void swap(shared_array & other) // never throws + { + std::swap(px, other.px); + pn.swap(other.pn); + } + +private: + + T * px; // contained pointer + detail::shared_count pn; // reference counter + +}; // shared_array + +template inline bool operator==(shared_array const & a, shared_array const & b) +{ + return a.get() == b.get(); +} + +template inline bool operator!=(shared_array const & a, shared_array const & b) +{ + return a.get() != b.get(); +} + +template inline bool operator<(shared_array const & a, shared_array const & b) +{ + return std::less()(a.get(), b.get()); +} + +template void swap(shared_array & a, shared_array & b) +{ + a.swap(b); +} + +} // namespace boost + +#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC_MEMBER_TEMPLATES) + +#endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp new file mode 100644 index 0000000..996a1b5 --- /dev/null +++ b/include/boost/shared_ptr.hpp @@ -0,0 +1,246 @@ +#ifndef BOOST_SHARED_PTR_HPP_INCLUDED +#define BOOST_SHARED_PTR_HPP_INCLUDED + +// +// shared_ptr.hpp +// +// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. +// + +#include // for broken compiler workarounds + +#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) +#include +#else + +#include +#include + +#include + +#include // for std::auto_ptr +#include // for std::swap +#include // for std::less + +#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash +# pragma warning(push) +# pragma warning(disable:4284) // odd return type for operator-> +#endif + +namespace boost +{ + +namespace detail +{ + +struct static_cast_tag {}; +struct dynamic_cast_tag {}; + +template struct shared_ptr_traits +{ + typedef T & reference; +}; + +template<> struct shared_ptr_traits +{ + typedef void reference; +}; + +} // namespace detail + + +// +// shared_ptr +// +// An enhanced relative of scoped_ptr with reference counted copy semantics. +// The object pointed to is deleted when the last shared_ptr pointing to it +// is destroyed or reset. +// + +template class weak_ptr; + +template class shared_ptr +{ +private: + + // Borland 5.5.1 specific workarounds + typedef checked_deleter deleter; + typedef shared_ptr this_type; + +public: + + typedef T element_type; + + explicit shared_ptr(T * p = 0): px(p), pn(p, deleter()) + { + } + + // + // Requirements: D's copy constructor must not throw + // + // shared_ptr will release p by calling d(p) + // + + template shared_ptr(T * p, D d): px(p), pn(p, d) + { + } + +// generated copy constructor, assignment, destructor are fine + + template + shared_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws + { + } + + template + shared_ptr(shared_ptr const & r, detail::static_cast_tag): px(static_cast(r.px)), pn(r.pn) + { + } + + template + shared_ptr(shared_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) + { + if(px == 0) // need to allocate new counter -- the cast failed + { + pn = detail::shared_count(static_cast(0), deleter()); + } + } + +#ifndef BOOST_NO_AUTO_PTR + + template + explicit shared_ptr(std::auto_ptr & r): px(r.get()), pn(r.release(), checked_deleter()) + { + } + +#endif + + template + shared_ptr & operator=(shared_ptr const & r) // nothrow? + { + px = r.px; + pn = r.pn; // shared_count::op= doesn't throw + return *this; + } + +#ifndef BOOST_NO_AUTO_PTR + + template + shared_ptr & operator=(std::auto_ptr & r) + { + this_type(r).swap(*this); + return *this; + } + +#endif + + void reset(T * p = 0) + { + this_type(p).swap(*this); + } + + typename detail::shared_ptr_traits::reference operator* () const // never throws + { + BOOST_ASSERT(px != 0); + return *px; + } + + T * operator-> () const // never throws + { + BOOST_ASSERT(px != 0); + return px; + } + + T * get() const // never throws + { + return px; + } + + long use_count() const // never throws + { + return pn.use_count(); + } + + bool unique() const // never throws + { + return pn.unique(); + } + + void swap(shared_ptr & other) // never throws + { + std::swap(px, other.px); + pn.swap(other.pn); + } + +// Tasteless as this may seem, making all members public allows member templates +// to work in the absence of member template friends. (Matthew Langston) + +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS + +private: + + template friend class shared_ptr; + template friend class weak_ptr; + + +#endif + + T * px; // contained pointer + detail::shared_count pn; // reference counter + +}; // shared_ptr + +template inline bool operator==(shared_ptr const & a, shared_ptr const & b) +{ + return a.get() == b.get(); +} + +template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) +{ + return a.get() != b.get(); +} + +template inline bool operator<(shared_ptr const & a, shared_ptr const & b) +{ + return std::less()(a.get(), b.get()); +} + +template shared_ptr shared_static_cast(shared_ptr const & r) +{ + return shared_ptr(r, detail::static_cast_tag()); +} + +template shared_ptr shared_dynamic_cast(shared_ptr const & r) +{ + return shared_ptr(r, detail::dynamic_cast_tag()); +} + +template void swap(shared_ptr & a, shared_ptr & b) +{ + a.swap(b); +} + +// get_pointer() enables boost::mem_fn to recognize shared_ptr + +template inline T * get_pointer(shared_ptr const & p) +{ + return p.get(); +} + +} // namespace boost + +#ifdef BOOST_MSVC +# pragma warning(pop) +#endif + +#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC_MEMBER_TEMPLATES) + +#endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp new file mode 100644 index 0000000..87231c3 --- /dev/null +++ b/include/boost/weak_ptr.hpp @@ -0,0 +1,175 @@ +#ifndef BOOST_WEAK_PTR_HPP_INCLUDED +#define BOOST_WEAK_PTR_HPP_INCLUDED + +// +// weak_ptr.hpp +// +// Copyright (c) 2001, 2002 Peter Dimov +// +// 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. +// + +#include + +#include // for broken compiler workarounds +#include + +#include + +#include // for std::swap +#include // for std::less + +#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash +# pragma warning(push) +# pragma warning(disable:4284) // odd return type for operator-> +#endif + +namespace boost +{ + +template class weak_ptr +{ +private: + + // Borland 5.5.1 specific workarounds + typedef weak_ptr this_type; + +public: + + typedef T element_type; + + weak_ptr(): px(0), pn() + { + } + +// generated copy constructor, assignment, destructor are fine + + template + weak_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws + { + } + + template + weak_ptr(weak_ptr const & r, detail::static_cast_tag): px(static_cast(r.px)), pn(r.pn) + { + } + + template + weak_ptr(weak_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) + { + if(px == 0) // need to allocate new counter -- the cast failed + { + pn = detail::weak_count(); + } + } + + template + weak_ptr & operator=(weak_ptr const & r) // nothrow? + { + px = r.px; + pn = r.pn; + return *this; + } + + void reset() + { + this_type().swap(*this); + } + + long use_count() const // never throws + { + return pn.use_count(); + } + + T * get() const // never throws + { + return use_count() == 0? 0: px; + } + + typename detail::shared_ptr_traits::reference operator* () const // never throws + { + T * p = get(); + + BOOST_ASSERT(p != 0); + return *p; + } + + T * operator-> () const // never throws + { + T * p = get(); + + BOOST_ASSERT(p != 0); + return p; + } + + void swap(weak_ptr & other) // never throws + { + std::swap(px, other.px); + pn.swap(other.pn); + } + +// Tasteless as this may seem, making all members public allows member templates +// to work in the absence of member template friends. (Matthew Langston) + +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS + +private: + + template friend class weak_ptr; + +#endif + + T * px; // contained pointer + detail::weak_count pn; // reference counter + +}; // weak_ptr + +template inline bool operator==(weak_ptr const & a, weak_ptr const & b) +{ + return a.get() == b.get(); +} + +template inline bool operator!=(weak_ptr const & a, weak_ptr const & b) +{ + return a.get() != b.get(); +} + +template inline bool operator<(weak_ptr const & a, weak_ptr const & b) +{ + return std::less()(a.get(), b.get()); +} + +template weak_ptr shared_static_cast(weak_ptr const & r) +{ + return weak_ptr(r, detail::static_cast_tag()); +} + +template weak_ptr shared_dynamic_cast(weak_ptr const & r) +{ + return weak_ptr(r, detail::dynamic_cast_tag()); +} + +template void swap(weak_ptr & a, weak_ptr & b) +{ + a.swap(b); +} + +// get_pointer() enables boost::mem_fn to recognize weak_ptr + +template inline T * get_pointer(weak_ptr const & p) +{ + return p.get(); +} + +} // namespace boost + +#ifdef BOOST_MSVC +# pragma warning(pop) +#endif + +#endif // #ifndef BOOST_WEAK_PTR_HPP_INCLUDED From 5d564a2f01f0caf9e11261b988dbef1abaf91392 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Thu, 24 Jan 2002 19:16:12 +0000 Subject: [PATCH 36/53] Add a self-assignment assert to reset(). Also add an overload of reset to allow resetting to a new pointer with a new deletion function. [SVN r12491] --- include/boost/detail/shared_array_nmt.hpp | 1 + include/boost/detail/shared_ptr_nmt.hpp | 1 + include/boost/shared_array.hpp | 1 + include/boost/shared_ptr.hpp | 8 +++++++- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/boost/detail/shared_array_nmt.hpp b/include/boost/detail/shared_array_nmt.hpp index e5137a3..570af13 100644 --- a/include/boost/detail/shared_array_nmt.hpp +++ b/include/boost/detail/shared_array_nmt.hpp @@ -72,6 +72,7 @@ public: void reset(T * p = 0) { + BOOST_ASSERT(p == 0 || p != px); shared_array(p).swap(*this); } diff --git a/include/boost/detail/shared_ptr_nmt.hpp b/include/boost/detail/shared_ptr_nmt.hpp index 8b268a2..5364e77 100644 --- a/include/boost/detail/shared_ptr_nmt.hpp +++ b/include/boost/detail/shared_ptr_nmt.hpp @@ -91,6 +91,7 @@ public: void reset(T * p = 0) { + BOOST_ASSERT(p == 0 || p != px); shared_ptr(p).swap(*this); } diff --git a/include/boost/shared_array.hpp b/include/boost/shared_array.hpp index 85741b2..5098dee 100644 --- a/include/boost/shared_array.hpp +++ b/include/boost/shared_array.hpp @@ -71,6 +71,7 @@ public: void reset(T * p = 0) { + BOOST_ASSERT(p == 0 || p != px); this_type(p).swap(*this); } diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index 996a1b5..db75b9e 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -33,7 +33,7 @@ #ifdef BOOST_MSVC // moved here to work around VC++ compiler crash # pragma warning(push) # pragma warning(disable:4284) // odd return type for operator-> -#endif +#endif namespace boost { @@ -144,9 +144,15 @@ public: void reset(T * p = 0) { + BOOST_ASSERT(p == 0 || p != px); this_type(p).swap(*this); } + template void reset(T * p, D d) + { + this_type(p, d).swap(*this); + } + typename detail::shared_ptr_traits::reference operator* () const // never throws { BOOST_ASSERT(px != 0); From 58c5711b47153d50a914c5e86090517b9c35e83f Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 25 Jan 2002 13:54:30 +0000 Subject: [PATCH 37/53] Added tests for the new smart pointers. [SVN r12500] --- shared_ptr_test.cpp | 157 +++++++++++++++++++++++ smart_ptr_test2.cpp | 295 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 452 insertions(+) create mode 100644 shared_ptr_test.cpp create mode 100644 smart_ptr_test2.cpp diff --git a/shared_ptr_test.cpp b/shared_ptr_test.cpp new file mode 100644 index 0000000..3dd5e98 --- /dev/null +++ b/shared_ptr_test.cpp @@ -0,0 +1,157 @@ +#if defined(_MSC_VER) && !defined(__ICL) +#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 +#endif + +// +// shared_ptr_test.cpp - a test for shared_ptr.hpp and weak_ptr.hpp +// +// Copyright (c) 2001 Peter Dimov and Multi Media Ltd. +// +// 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. +// + +#define BOOST_INCLUDE_MAIN +#include + +#include +#include + +bool boost_error(char const *, char const *, char const *, long) +{ + return true; +} + +namespace +{ + int cnt = 0; +} + +struct X +{ + X() + { + ++cnt; + std::cout << "X(" << this << ")::X()\n"; + } + + virtual ~X() + { + --cnt; + std::cout << "X(" << this << ")::~X()\n"; + } + +private: + + X(X const &); + X & operator= (X const &); +}; + +struct Y: public X +{ + Y() + { + ++cnt; + std::cout << "Y(" << this << ")::Y()\n"; + } + + ~Y() + { + --cnt; + std::cout << "Y(" << this << ")::~Y()\n"; + } + +private: + + Y(Y const &); + Y & operator= (Y const &); +}; + +int * get_object() +{ + ++cnt; + std::cout << "get_object()\n"; + return &cnt; +} + +void release_object(int * p) +{ + BOOST_TEST(p == &cnt); + --cnt; + std::cout << "release_object()\n"; +} + +int test_main(int, char * []) +{ + using namespace boost; + + { + shared_ptr p(new Y); + shared_ptr p2(new X); + + shared_ptr p3 = shared_dynamic_cast(p); + shared_ptr p4 = shared_dynamic_cast(p2); + + BOOST_TEST(p.use_count() == 2); + BOOST_TEST(p2.use_count() == 1); + BOOST_TEST(p3.use_count() == 2); + BOOST_TEST(p4.use_count() == 1); + + shared_ptr p5(p); + + std::cout << "--\n"; + + p.reset(); + p2.reset(); + p3.reset(); + p4.reset(); + + std::cout << "--\n"; + + BOOST_TEST(p5.use_count() == 1); + + weak_ptr wp1; + + BOOST_TEST(wp1.use_count() == 0); + BOOST_TEST(wp1.get() == 0); + + weak_ptr wp2 = shared_static_cast(p5); + + BOOST_TEST(wp2.use_count() == 1); + BOOST_TEST(wp2.get() != 0); + + weak_ptr wp3 = shared_dynamic_cast(wp2); + + BOOST_TEST(wp3.use_count() == 1); + BOOST_TEST(wp3.get() != 0); + BOOST_TEST(wp2 == wp3); + + wp1 = wp2; + + BOOST_TEST(wp1.use_count() == 1); + BOOST_TEST(wp1.get() != 0); + BOOST_TEST(wp1 == wp2); + + p5.reset(); + + BOOST_TEST(wp1.use_count() == 0); + BOOST_TEST(wp1.get() == 0); + + BOOST_TEST(wp2.use_count() == 0); + BOOST_TEST(wp2.get() == 0); + + BOOST_TEST(wp3.use_count() == 0); + BOOST_TEST(wp3.get() == 0); + + shared_ptr p6(get_object(), release_object); + } + + BOOST_TEST(cnt == 0); + + return 0; +} diff --git a/smart_ptr_test2.cpp b/smart_ptr_test2.cpp new file mode 100644 index 0000000..4ea305e --- /dev/null +++ b/smart_ptr_test2.cpp @@ -0,0 +1,295 @@ +// smart pointer test program ----------------------------------------------// + +// (C) Copyright Beman Dawes 1998, 1999. 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. + +// 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 +#include +#include +#include + +#define BOOST_INCLUDE_MAIN +#include + +#include +#include +#include + +bool boost_error(char const *, char const *, char const *, long) +{ + return true; // fail with assert() +} + +class Incomplete; + +Incomplete * get_ptr( boost::shared_ptr& incomplete ) +{ + return incomplete.get(); +} + +using namespace std; +using boost::scoped_ptr; +using boost::scoped_array; +using boost::shared_ptr; +using boost::shared_array; + +template +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; + cout << "UDT with value " << value_ << " being destroyed" << endl; + } + 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( scoped_ptr& incomplete ) +{ + return incomplete.get(); +} + +Incomplete * check_incomplete( shared_ptr& incomplete, + shared_ptr& i2 ) +{ + incomplete.swap(i2); + cout << incomplete.use_count() << " " << incomplete.unique() << endl; + return incomplete.get(); +} +// main --------------------------------------------------------------------// + +// This isn't a very systematic test; it just hits some of the basics. + +int test_main( int, char ** ) { + + BOOST_TEST( UDT_use_count == 0 ); // reality check + + // test scoped_ptr with a built-in type + long * lp = new long; + scoped_ptr 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(sp.get()), 1234568901L ); + ck( lp, *sp ); + + sp.reset(); + BOOST_TEST( sp.get() == 0 ); + + // test scoped_ptr with a user defined type + scoped_ptr 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 ]; + scoped_array 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; + shared_ptr 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(cp.get()), 54321 ); + ck( static_cast(ip), *cp ); + + shared_ptr 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(cp2.get()), 54321 ); + ck( static_cast(ip), *cp2 ); + + shared_ptr 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 ); + BOOST_TEST( cp.use_count() == 1 ); + 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 ); + + shared_ptr cp4; + swap( cp2, cp4 ); + BOOST_TEST( cp4.use_count() == 3 ); + BOOST_TEST( *cp4 == 87654 ); + BOOST_TEST( cp2.get() == 0 ); + + set< shared_ptr > scp; + scp.insert(cp4); + BOOST_TEST( scp.find(cp4) != scp.end() ); + BOOST_TEST( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); + + // test shared_array with a built-in type + char * cap = new char [ 100 ]; + shared_array 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' ); + + shared_array ca2 ( ca ); + shared_array 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 ); + + shared_array 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 ); + + set< shared_array > sca; + sca.insert(ca4); + BOOST_TEST( sca.find(ca4) != sca.end() ); + BOOST_TEST( sca.find(ca4) == sca.find( shared_array(ca4) ) ); + + // test shared_array with user defined type + shared_array udta ( new UDT[3] ); + + udta[0].value( 111 ); + udta[1].value( 222 ); + udta[2].value( 333 ); + shared_array 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; + shared_ptr 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 ); + + shared_ptr 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 ); + + cout << "OK" << endl; + + new char[12345]; // deliberate memory leak to verify leaks detected + + return 0; + } // main + From a8efe20862a99d8c59953dcd49ca87bc574aae4c Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 25 Jan 2002 16:10:26 +0000 Subject: [PATCH 38/53] Comments added. [SVN r12510] --- include/boost/detail/shared_count.hpp | 31 ++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/include/boost/detail/shared_count.hpp b/include/boost/detail/shared_count.hpp index 46ceaf5..cf8f8ee 100644 --- a/include/boost/detail/shared_count.hpp +++ b/include/boost/detail/shared_count.hpp @@ -30,26 +30,37 @@ public: typedef atomic_count count_type; + // pre: initial_use_count <= initial_weak_count + explicit counted_base(long initial_use_count, long initial_weak_count): use_count_(initial_use_count), weak_count_(initial_weak_count), self_deleter_(&self_delete) { } - virtual ~counted_base() + virtual ~counted_base() // nothrow { } - virtual void dispose() + // dispose() is called when use_count_ drops to zero, to release + // the resources managed by *this. + // + // counted_base doesn't manage any resources except itself, and + // the default implementation is a no-op. + // + // dispose() is not pure virtual since weak_ptr instantiates a + // counted_base in its default constructor. + + virtual void dispose() // nothrow { } - void add_ref() + void add_ref() // nothrow { ++use_count_; ++weak_count_; } - void release() + void release() // nothrow { if(--use_count_ == 0) { @@ -64,12 +75,12 @@ public: } } - void weak_add_ref() + void weak_add_ref() // nothrow { ++weak_count_; } - void weak_release() + void weak_release() // nothrow { if(--weak_count_ == 0) { @@ -77,7 +88,7 @@ public: } } - long use_count() const + long use_count() const // nothrow { return use_count_; } @@ -92,6 +103,8 @@ private: delete p; } + // inv: use_count_ <= weak_count_ + count_type use_count_; count_type weak_count_; @@ -110,12 +123,14 @@ private: public: + // pre: initial_use_count <= initial_weak_count, d(p) must not throw + counted_base_impl(P p, D d, long initial_use_count, long initial_weak_count): counted_base(initial_use_count, initial_weak_count), ptr(p), del(d) { } - virtual void dispose() + virtual void dispose() // nothrow { del(ptr); } From 4fdc84f29eeb7b19e1cb731d55f6ab29cb458350 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Thu, 31 Jan 2002 06:52:58 +0000 Subject: [PATCH 39/53] Make the non-threads version work. [SVN r12597] --- include/boost/detail/atomic_count.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/detail/atomic_count.hpp b/include/boost/detail/atomic_count.hpp index 0a22e64..0f33ac4 100644 --- a/include/boost/detail/atomic_count.hpp +++ b/include/boost/detail/atomic_count.hpp @@ -111,7 +111,7 @@ namespace boost namespace detail { -typedef long atomic_counter; +typedef long atomic_count; } From 39c10f739d8a809a1d86b3e0bc8174fd00040b0d Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 1 Feb 2002 18:40:35 +0000 Subject: [PATCH 40/53] added an auto_ptr & constructor to shared_count [SVN r12623] --- include/boost/detail/shared_count.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/boost/detail/shared_count.hpp b/include/boost/detail/shared_count.hpp index cf8f8ee..8756294 100644 --- a/include/boost/detail/shared_count.hpp +++ b/include/boost/detail/shared_count.hpp @@ -16,6 +16,13 @@ // warranty, and with no claim as to its suitability for any purpose. // +#include + +#ifndef BOOST_NO_AUTO_PTR +# include +#endif + +#include #include namespace boost @@ -160,6 +167,18 @@ public: } } +#ifndef BOOST_NO_AUTO_PTR + + // auto_ptr is special cased to provide the strong guarantee + + template + explicit shared_count(std::auto_ptr & r): pi_(new counted_base_impl< Y *, checked_deleter >(r.get(), checked_deleter(), 1, 1)) + { + r.release(); + } + +#endif + ~shared_count() // nothrow { pi_->release(); From 8b5b780c2c4f6251370d3cac8cc721ae5b8b18d5 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Fri, 1 Feb 2002 19:34:31 +0000 Subject: [PATCH 41/53] better weak_ptr coverage [SVN r12624] --- shared_ptr_test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/shared_ptr_test.cpp b/shared_ptr_test.cpp index 3dd5e98..7387cc8 100644 --- a/shared_ptr_test.cpp +++ b/shared_ptr_test.cpp @@ -131,6 +131,11 @@ int test_main(int, char * []) BOOST_TEST(wp3.get() != 0); BOOST_TEST(wp2 == wp3); + weak_ptr wp4(wp3); + + wp1 = p2; + wp1 = p4; + wp1 = wp3; wp1 = wp2; BOOST_TEST(wp1.use_count() == 1); From b224270cc0d70243af4455b62867c781c6d3e72f Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 2 Feb 2002 16:18:04 +0000 Subject: [PATCH 42/53] auto_ptr& constructor now has no effects when exception is thrown [SVN r12643] --- include/boost/shared_ptr.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index db75b9e..eb4d662 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -117,7 +117,7 @@ public: #ifndef BOOST_NO_AUTO_PTR template - explicit shared_ptr(std::auto_ptr & r): px(r.get()), pn(r.release(), checked_deleter()) + explicit shared_ptr(std::auto_ptr & r): px(r.get()), pn(r) { } From d3c76575f945d225d690d1134005a4594aa7dccd Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 2 Feb 2002 16:19:45 +0000 Subject: [PATCH 43/53] templated copy constructor added. [SVN r12644] --- include/boost/weak_ptr.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp index 87231c3..d73c5f3 100644 --- a/include/boost/weak_ptr.hpp +++ b/include/boost/weak_ptr.hpp @@ -49,6 +49,11 @@ public: // generated copy constructor, assignment, destructor are fine + template + weak_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // never throws + { + } + template weak_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws { From 1a7cd887e4ee658f763d88590a2a05b9ad76ac7b Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Sat, 2 Feb 2002 18:36:12 +0000 Subject: [PATCH 44/53] New smart pointer documentation. Related clean-up of the smart pointer library. Changing includes to include the new individual smart pointer headers. Replacing old smart pointer library with an include of the new smart pointer headers. Simplify ifdefs that involve the member templates macros now that BOOST_MSVC6_MEMBER_TEMPLATES is also guaranteed to bet set for platforms that have full member templates. [SVN r12647] --- compatibility.htm | 105 +++++++ include/boost/scoped_array.hpp | 20 +- include/boost/scoped_ptr.hpp | 26 +- include/boost/shared_array.hpp | 27 +- include/boost/shared_ptr.hpp | 52 ++-- include/boost/smart_ptr.hpp | 405 +------------------------- include/boost/weak_ptr.hpp | 36 +-- index.htm | 31 +- scoped_array.htm | 191 ++++++++----- scoped_ptr.htm | 262 ++++++++++------- scoped_ptr_example.hpp | 2 +- shared_array.htm | 351 +++++++++++++---------- shared_ptr.htm | 506 +++++++++++++++++++-------------- shared_ptr_example.cpp | 2 +- shared_ptr_example2.cpp | 2 - shared_ptr_example2.hpp | 3 +- smart_ptr.htm | 196 ++++++++----- smart_ptr_test.cpp | 22 +- smart_ptr_test2.cpp | 295 ------------------- smarttests.htm | 25 +- weak_ptr.htm | 233 +++++++++++++++ 21 files changed, 1367 insertions(+), 1425 deletions(-) create mode 100644 compatibility.htm delete mode 100644 smart_ptr_test2.cpp create mode 100644 weak_ptr.htm diff --git a/compatibility.htm b/compatibility.htm new file mode 100644 index 0000000..65acfd7 --- /dev/null +++ b/compatibility.htm @@ -0,0 +1,105 @@ + + + + + + +Smart Pointer Changes + + + + +

    c++boost.gif (8819 bytes)Smart +Pointer Changes

    + +

    The February 2002 change to the Boost smart pointers introduced a number +of changes. Since the previous version of the smart pointers was in use for +a long time, it's useful to have a detailed list of what changed from a library +user's point of view.

    + +

    Note that for compilers that don't support member templates well enough, +a separate implementation is used that lacks many of the new features and is +more like the old version.

    + +

    Features Requiring Code Changes to Take Advantage

    + +
      + +
    • The smart pointer class templates now each have their own header file. +For compatibility, the +<boost/smart_ptr.hpp> +header now includes the headers for the four classic smart pointer class templates.
    • + +
    • The weak_ptr template was added.
    • + +
    • The new shared_ptr and shared_array relax the requirement that the pointed-to object's +destructor must be visible when instantiating the shared_ptr destructor. +This makes it easier to have shared_ptr members in classes without explicit destructors.
    • + +
    • A custom deallocator can be passed in when creating a shared_ptr or shared_array.
    • + +
    • shared_static_cast and shared_dynamic_cast function templates are +provided which work for shared_ptr and weak_ptr as static_cast and +dynamic_cast do for pointers.
    • + +
    • The self-assignment misfeature has been removed from shared_ptr::reset, +although it is still present in scoped_ptr, and in std::auto_ptr. +Calling reset with a pointer to the object that's already owned by the +shared_ptr results in undefined behavior +(an assertion, or eventually a double-delete if assertions are off).
    • + +
    • The BOOST_SMART_PTR_CONVERSION feature has been removed.
    • + +
    • shared_ptr<void> is now allowed.
    • + +
    + +

    Features That Improve Robustness

    + +
      + +
    • The manipulation of use counts is now thread safe on Windows, Linux, and platforms +that support pthreads. See the +<boost/detail/atomic_count.hpp> +file for details
    • + +
    • The new shared_ptr will always delete the object using the pointer it was originally constructed with. +This prevents subtle problems that could happen if the last shared_ptr was a pointer to a sub-object +of a class that did not have a virtual destructor.
    • + +
    + +

    Implementation Details

    + +
      + +
    • Some bugs in the assignment operator implementations and in reset +have been fixed by using the "copy and swap" idiom.
    • + +
    • Assertions have been added to check preconditions of various functions; +however, since these use the new +<boost/assert.hpp> +header, the assertions are disabled by default.
    • + +
    • The partial specialization of std::less has been replaced by operator< +overloads which accomplish the same thing without relying on undefined behavior.
    • + +
    • The incorrect overload of std::swap has been replaced by boost::swap, which +has many of the same advantages for generic programming but does not violate the C++ standard.
    • + +
    + +
    + +

    Revised 1 February 2002

    + +

    Copyright 2002 Darin Adler. +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" +without express or implied warranty, and with no claim as to its suitability for +any purpose.

    + + + + diff --git a/include/boost/scoped_array.hpp b/include/boost/scoped_array.hpp index 3fcce8d..74ecebe 100644 --- a/include/boost/scoped_array.hpp +++ b/include/boost/scoped_array.hpp @@ -35,49 +35,49 @@ public: typedef T element_type; - explicit scoped_array( T* p = 0 ): ptr(p) // never throws + explicit scoped_array(T * p = 0) : ptr(p) // never throws { } - ~scoped_array() + ~scoped_array() // never throws { typedef char type_must_be_complete[sizeof(T)]; delete [] ptr; } - void reset( T* p = 0 ) + void reset(T * p = 0) // never throws { typedef char type_must_be_complete[sizeof(T)]; - if ( ptr != p ) + if (ptr != p) { delete [] ptr; ptr = p; } } - T& operator[](std::ptrdiff_t i) const // never throws + T& operator[](std::ptrdiff_t i) const // never throws { BOOST_ASSERT(ptr != 0); BOOST_ASSERT(i >= 0); return ptr[i]; } - T* get() const // never throws + T* get() const // never throws { return ptr; } - void swap(scoped_array & rhs) + void swap(scoped_array & b) // never throws { - T * tmp = rhs.ptr; - rhs.ptr = ptr; + T * tmp = b.ptr; + b.ptr = ptr; ptr = tmp; } }; -template inline void swap(scoped_array & a, scoped_array & b) +template inline void swap(scoped_array & a, scoped_array & b) // never throws { a.swap(b); } diff --git a/include/boost/scoped_ptr.hpp b/include/boost/scoped_ptr.hpp index b920a72..6648f09 100644 --- a/include/boost/scoped_ptr.hpp +++ b/include/boost/scoped_ptr.hpp @@ -18,7 +18,7 @@ namespace boost // 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; +// an explicit reset(). scoped_ptr is a simple solution for simple needs; // use shared_ptr or std::auto_ptr if your needs are more complex. template class scoped_ptr // noncopyable @@ -34,57 +34,57 @@ public: typedef T element_type; - explicit scoped_ptr( T* p = 0 ): ptr(p) // never throws + explicit scoped_ptr(T * p = 0): ptr(p) // never throws { } - ~scoped_ptr() + ~scoped_ptr() // never throws { typedef char type_must_be_complete[sizeof(T)]; delete ptr; } - void reset( T* p = 0 ) + void reset(T * p = 0) // never throws { typedef char type_must_be_complete[sizeof(T)]; - if ( ptr != p ) + if (ptr != p) { delete ptr; ptr = p; } } - T& operator*() const // never throws + T & operator*() const // never throws { BOOST_ASSERT(ptr != 0); return *ptr; } - T* operator->() const // never throws + T * operator->() const // never throws { BOOST_ASSERT(ptr != 0); return ptr; } - T* get() const // never throws + T * get() const // never throws { return ptr; } - void swap(scoped_ptr & rhs) + void swap(scoped_ptr & b) // never throws { - T * tmp = rhs.ptr; - rhs.ptr = ptr; + T * tmp = b.ptr; + b.ptr = ptr; ptr = tmp; } }; -template inline void swap(scoped_ptr & a, scoped_ptr & b) +template inline void swap(scoped_ptr & a, scoped_ptr & b) // never throws { a.swap(b); } } // namespace boost -#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED +#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED diff --git a/include/boost/shared_array.hpp b/include/boost/shared_array.hpp index 5098dee..4d400dc 100644 --- a/include/boost/shared_array.hpp +++ b/include/boost/shared_array.hpp @@ -17,7 +17,7 @@ #include // for broken compiler workarounds -#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) +#ifndef BOOST_MSVC6_MEMBER_TEMPLATES #include #else @@ -75,6 +75,11 @@ public: this_type(p).swap(*this); } + template void reset(T * p = 0, D d) + { + this_type(p, d).swap(*this); + } + T & operator[] (std::ptrdiff_t i) const // never throws { BOOST_ASSERT(px != 0); @@ -87,16 +92,16 @@ public: return px; } - long use_count() const // never throws - { - return pn.use_count(); - } - bool unique() const // never throws { return pn.unique(); } + long use_count() const // never throws + { + return pn.use_count(); + } + void swap(shared_array & other) // never throws { std::swap(px, other.px); @@ -110,28 +115,28 @@ private: }; // shared_array -template inline bool operator==(shared_array const & a, shared_array const & b) +template inline bool operator==(shared_array const & a, shared_array const & b) // never throws { return a.get() == b.get(); } -template inline bool operator!=(shared_array const & a, shared_array const & b) +template inline bool operator!=(shared_array const & a, shared_array const & b) // never throws { return a.get() != b.get(); } -template inline bool operator<(shared_array const & a, shared_array const & b) +template inline bool operator<(shared_array const & a, shared_array const & b) // never throws { return std::less()(a.get(), b.get()); } -template void swap(shared_array & a, shared_array & b) +template void swap(shared_array & a, shared_array & b) // never throws { a.swap(b); } } // namespace boost -#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC_MEMBER_TEMPLATES) +#endif // #ifndef BOOST_MSVC6_MEMBER_TEMPLATES #endif // #ifndef BOOST_SHARED_ARRAY_HPP_INCLUDED diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index eb4d662..d601bab 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -17,7 +17,7 @@ #include // for broken compiler workarounds -#if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) +#ifndef BOOST_MSVC6_MEMBER_TEMPLATES #include #else @@ -44,7 +44,7 @@ namespace detail struct static_cast_tag {}; struct dynamic_cast_tag {}; -template struct shared_ptr_traits +template struct shared_ptr_traits { typedef T & reference; }; @@ -108,10 +108,8 @@ public: template shared_ptr(shared_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { - if(px == 0) // need to allocate new counter -- the cast failed - { + if (px == 0) // need to allocate new counter -- the cast failed pn = detail::shared_count(static_cast(0), deleter()); - } } #ifndef BOOST_NO_AUTO_PTR @@ -124,7 +122,7 @@ public: #endif template - shared_ptr & operator=(shared_ptr const & r) // nothrow? + shared_ptr & operator=(shared_ptr const & r) // never throws { px = r.px; pn = r.pn; // shared_count::op= doesn't throw @@ -170,16 +168,16 @@ public: return px; } - long use_count() const // never throws - { - return pn.use_count(); - } - bool unique() const // never throws { return pn.unique(); } + long use_count() const // never throws + { + return pn.use_count(); + } + void swap(shared_ptr & other) // never throws { std::swap(px, other.px); @@ -204,39 +202,39 @@ private: }; // shared_ptr -template inline bool operator==(shared_ptr const & a, shared_ptr const & b) +template inline bool operator==(shared_ptr const & a, shared_ptr const & b) { return a.get() == b.get(); } -template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) +template inline bool operator!=(shared_ptr const & a, shared_ptr const & b) { return a.get() != b.get(); } -template inline bool operator<(shared_ptr const & a, shared_ptr const & b) +template inline bool operator<(shared_ptr const & a, shared_ptr const & b) { return std::less()(a.get(), b.get()); } -template shared_ptr shared_static_cast(shared_ptr const & r) -{ - return shared_ptr(r, detail::static_cast_tag()); -} - -template shared_ptr shared_dynamic_cast(shared_ptr const & r) -{ - return shared_ptr(r, detail::dynamic_cast_tag()); -} - -template void swap(shared_ptr & a, shared_ptr & b) +template void swap(shared_ptr & a, shared_ptr & b) { a.swap(b); } +template shared_ptr shared_static_cast(shared_ptr const & r) +{ + return shared_ptr(r, detail::static_cast_tag()); +} + +template shared_ptr shared_dynamic_cast(shared_ptr const & r) +{ + return shared_ptr(r, detail::dynamic_cast_tag()); +} + // get_pointer() enables boost::mem_fn to recognize shared_ptr -template inline T * get_pointer(shared_ptr const & p) +template inline T * get_pointer(shared_ptr const & p) { return p.get(); } @@ -247,6 +245,6 @@ template inline T * get_pointer(shared_ptr const & p) # pragma warning(pop) #endif -#endif // #if defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC_MEMBER_TEMPLATES) +#endif // #ifndef BOOST_MSVC6_MEMBER_TEMPLATES #endif // #ifndef BOOST_SHARED_PTR_HPP_INCLUDED diff --git a/include/boost/smart_ptr.hpp b/include/boost/smart_ptr.hpp index dbcb787..da28e87 100644 --- a/include/boost/smart_ptr.hpp +++ b/include/boost/smart_ptr.hpp @@ -1,402 +1,9 @@ // Boost smart_ptr.hpp header file -----------------------------------------// -// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. 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 -// 6 Jul 01 Reorder shared_ptr code so VC++ 6 member templates work, allowing -// polymorphic pointers to now work with that compiler (Gary Powell) -// 21 May 01 Require complete type where incomplete type is unsafe. -// (suggested by Vladimir Prus) -// 21 May 01 operator= fails if operand transitively owned by *this, as in a -// linked list (report by Ken Johnson, fix by Beman Dawes) -// 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) -// 1 Feb 00 Additional shared_ptr BOOST_NO_MEMBER_TEMPLATES workarounds -// (Dave Abrahams) -// 31 Dec 99 Condition tightened for no member template friend workaround -// (Dave Abrahams) -// 30 Dec 99 Moved BOOST_NMEMBER_TEMPLATES compatibility code to config.hpp -// (Dave Abrahams) -// 30 Nov 99 added operator ==, operator !=, and std::swap and std::less -// specializations for shared types (Darin Adler) -// 11 Oct 99 replaced op[](int) with op[](std::size_t) (Ed Brey, Valentin -// Bonnard), added shared_ptr workaround for no member template -// friends (Matthew Langston) -// 25 Sep 99 added shared_ptr::swap and shared_array::swap (Luis Coelho). -// 20 Jul 99 changed name to smart_ptr.hpp, #include , -// #include and use boost::noncopyable -// 17 May 99 remove scoped_array and shared_array operator*() as -// unnecessary (Beman Dawes) -// 14 May 99 reorder code so no effects when bad_alloc thrown (Abrahams/Dawes) -// 13 May 99 remove certain throw() specifiers to avoid generated try/catch -// code cost (Beman Dawes) -// 11 May 99 get() added, conversion to T* placed in macro guard (Valentin -// Bonnard, Dave Abrahams, and others argued for elimination -// of the automatic conversion) -// 28 Apr 99 #include fix (Valentin Bonnard) -// 28 Apr 99 rename transfer() to share() for clarity (Dave Abrahams) -// 28 Apr 99 remove unsafe shared_array template conversions(Valentin Bonnard) -// 28 Apr 99 p(r) changed to p(r.px) for clarity (Dave Abrahams) -// 21 Apr 99 reset() self assignment fix (Valentin Bonnard) -// 21 Apr 99 dispose() provided to improve clarity (Valentin Bonnard) -// 27 Apr 99 leak when new throws fixes (Dave Abrahams) -// 21 Oct 98 initial Version (Greg Colvin/Beman Dawes) - -#ifndef BOOST_SMART_PTR_HPP -#define BOOST_SMART_PTR_HPP - -#include // for broken compiler workarounds -#include // for std::size_t -#include // for std::auto_ptr -#include // for std::swap -#include // for boost::noncopyable, checked_delete, checked_array_delete -#include // for std::less -#include // for BOOST_STATIC_ASSERT - -#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash -# 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 - -namespace boost { - -// scoped_ptr --------------------------------------------------------------// - -// 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; -// see shared_ptr (below) or std::auto_ptr if your needs are more complex. - -template class scoped_ptr : noncopyable { - - T* ptr; - - public: - typedef T element_type; - - explicit scoped_ptr( T* p=0 ) : ptr(p) {} // never throws - ~scoped_ptr() { checked_delete(ptr); } - void reset( T* p=0 ) { if ( ptr != p ) { checked_delete(ptr); ptr = p; } } - T& operator*() const { return *ptr; } // never throws - T* operator->() const { return ptr; } // never throws - T* get() const { return ptr; } // never throws -#ifdef BOOST_SMART_PTR_CONVERSION - // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const { return ptr; } // never throws -#endif - }; // scoped_ptr - -// scoped_array ------------------------------------------------------------// - -// 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(). See shared_array or std::vector if your needs are more complex. - -template class scoped_array : noncopyable { - - T* ptr; - - public: - typedef T element_type; - - explicit scoped_array( T* p=0 ) : ptr(p) {} // never throws - ~scoped_array() { checked_array_delete(ptr); } - - void reset( T* p=0 ) { if ( ptr != p ) - {checked_array_delete(ptr); ptr=p;} } - - T* get() const { return ptr; } // never throws -#ifdef BOOST_SMART_PTR_CONVERSION - // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const { return ptr; } // never throws -#else - T& operator[](std::size_t i) const { return ptr[i]; } // never throws -#endif - }; // scoped_array - -// shared_ptr --------------------------------------------------------------// - -// An enhanced relative of scoped_ptr with reference counted copy semantics. -// The object pointed to is deleted when the last shared_ptr pointing to it -// is destroyed or reset. - -template class shared_ptr { - public: - typedef T element_type; - - explicit shared_ptr(T* p =0) : px(p) { - try { pn = new long(1); } // fix: prevent leak if new throws - catch (...) { checked_delete(p); throw; } - } - - ~shared_ptr() { dispose(); } - -#if !defined( BOOST_NO_MEMBER_TEMPLATES ) || defined (BOOST_MSVC6_MEMBER_TEMPLATES) - template - shared_ptr(const shared_ptr& r) : px(r.px) { // never throws - ++*(pn = r.pn); - } -#ifndef BOOST_NO_AUTO_PTR - template - explicit shared_ptr(std::auto_ptr& r) { - pn = new long(1); // may throw - px = r.release(); // fix: moved here to stop leak if new throws - } -#endif - - template - shared_ptr& operator=(const shared_ptr& r) { - share(r.px,r.pn); - return *this; - } - -#ifndef BOOST_NO_AUTO_PTR - template - shared_ptr& operator=(std::auto_ptr& r) { - // code choice driven by guarantee of "no effect if new throws" - if (*pn == 1) { checked_delete(px); } - else { // allocate new reference counter - long * tmp = new long(1); // may throw - --*pn; // only decrement once danger of new throwing is past - pn = tmp; - } // allocate new reference counter - px = r.release(); // fix: moved here so doesn't leak if new throws - return *this; - } -#endif -#else -#ifndef BOOST_NO_AUTO_PTR - explicit shared_ptr(std::auto_ptr& r) { - pn = new long(1); // may throw - px = r.release(); // fix: moved here to stop leak if new throws - } - - shared_ptr& operator=(std::auto_ptr& r) { - // code choice driven by guarantee of "no effect if new throws" - if (*pn == 1) { checked_delete(px); } - else { // allocate new reference counter - long * tmp = new long(1); // may throw - --*pn; // only decrement once danger of new throwing is past - pn = tmp; - } // allocate new reference counter - px = r.release(); // fix: moved here so doesn't leak if new throws - return *this; - } -#endif -#endif - - // The assignment operator and the copy constructor must come after - // the templated versions for MSVC6 to work. (Gary Powell) - shared_ptr(const shared_ptr& r) : px(r.px) { ++*(pn = r.pn); } // never throws - - shared_ptr& operator=(const shared_ptr& r) { - share(r.px,r.pn); - return *this; - } - - void reset(T* p=0) { - if ( px == p ) return; // fix: self-assignment safe - if (--*pn == 0) { checked_delete(px); } - else { // allocate new reference counter - try { pn = new long; } // fix: prevent leak if new throws - catch (...) { - ++*pn; // undo effect of --*pn above to meet effects guarantee - checked_delete(p); - throw; - } // catch - } // allocate new reference counter - *pn = 1; - px = p; - } // reset - - T& operator*() const { return *px; } // never throws - T* operator->() const { return px; } // never throws - T* get() const { return px; } // never throws - #ifdef BOOST_SMART_PTR_CONVERSION - // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const { return px; } // never throws - #endif - - long use_count() const { return *pn; } // never throws - bool unique() const { return *pn == 1; } // never throws - - void swap(shared_ptr& other) // never throws - { std::swap(px,other.px); std::swap(pn,other.pn); } - -// Tasteless as this may seem, making all members public allows member templates -// to work in the absence of member template friends. (Matthew Langston) -// Don't split this line into two; that causes problems for some GCC 2.95.2 builds -#if ( defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(BOOST_MSVC6_MEMBER_TEMPLATES) ) || !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) - private: -#endif - - T* px; // contained pointer - long* pn; // ptr to reference counter - -// Don't split this line into two; that causes problems for some GCC 2.95.2 builds -#if !defined( BOOST_NO_MEMBER_TEMPLATES ) && !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) - template friend class shared_ptr; -#endif - - void dispose() { if (--*pn == 0) { checked_delete(px); delete pn; } } - - void share(T* rpx, long* rpn) { - if (pn != rpn) { // Q: why not px != rpx? A: fails when both == 0 - ++*rpn; // done before dispose() in case rpn transitively - // dependent on *this (bug reported by Ken Johnson) - dispose(); - px = rpx; - pn = rpn; - } - } // share -}; // shared_ptr - -template - inline bool operator==(const shared_ptr& a, const shared_ptr& b) - { return a.get() == b.get(); } - -template - inline bool operator!=(const shared_ptr& a, const shared_ptr& b) - { return a.get() != b.get(); } - -// shared_array ------------------------------------------------------------// - -// shared_array extends shared_ptr to arrays. -// The array pointed to is deleted when the last shared_array pointing to it -// is destroyed or reset. - -template class shared_array { - public: - typedef T element_type; - - explicit shared_array(T* p =0) : px(p) { - try { pn = new long(1); } // fix: prevent leak if new throws - catch (...) { checked_array_delete(p); throw; } - } - - shared_array(const shared_array& r) : px(r.px) // never throws - { ++*(pn = r.pn); } - - ~shared_array() { dispose(); } - - shared_array& operator=(const shared_array& r) { - if (pn != r.pn) { // Q: why not px != r.px? A: fails when both px == 0 - ++*r.pn; // done before dispose() in case r.pn transitively - // dependent on *this (bug reported by Ken Johnson) - dispose(); - px = r.px; - pn = r.pn; - } - return *this; - } // operator= - - void reset(T* p=0) { - if ( px == p ) return; // fix: self-assignment safe - if (--*pn == 0) { checked_array_delete(px); } - else { // allocate new reference counter - try { pn = new long; } // fix: prevent leak if new throws - catch (...) { - ++*pn; // undo effect of --*pn above to meet effects guarantee - checked_array_delete(p); - throw; - } // catch - } // allocate new reference counter - *pn = 1; - px = p; - } // reset - - T* get() const { return px; } // never throws - #ifdef BOOST_SMART_PTR_CONVERSION - // get() is safer! Define BOOST_SMART_PTR_CONVERSION at your own risk! - operator T*() const { return px; } // never throws - #else - T& operator[](std::size_t i) const { return px[i]; } // never throws - #endif - - long use_count() const { return *pn; } // never throws - bool unique() const { return *pn == 1; } // never throws - - void swap(shared_array& other) // never throws - { std::swap(px,other.px); std::swap(pn,other.pn); } - - private: - - T* px; // contained pointer - long* pn; // ptr to reference counter - - void dispose() { if (--*pn == 0) { checked_array_delete(px); delete pn; } } - -}; // shared_array - -template - inline bool operator==(const shared_array& a, const shared_array& b) - { return a.get() == b.get(); } - -template - inline bool operator!=(const shared_array& a, const shared_array& b) - { return a.get() != b.get(); } - -} // namespace boost - -// specializations for things in namespace std -----------------------------// - -#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION - -namespace std { - -// Specialize std::swap to use the fast, non-throwing swap that's provided -// as a member function instead of using the default algorithm which creates -// a temporary and uses assignment. - -template - inline void swap(boost::shared_ptr& a, boost::shared_ptr& b) - { a.swap(b); } - -template - inline void swap(boost::shared_array& a, boost::shared_array& b) - { a.swap(b); } - -// Specialize std::less so we can use shared pointers and arrays as keys in -// associative collections. - -// It's still a controversial question whether this is better than supplying -// a full range of comparison operators (<, >, <=, >=). - -template - struct less< boost::shared_ptr > - : binary_function, boost::shared_ptr, bool> - { - bool operator()(const boost::shared_ptr& a, - const boost::shared_ptr& b) const - { return std::less()(a.get(),b.get()); } - }; - -template - struct less< boost::shared_array > - : binary_function, boost::shared_array, bool> - { - bool operator()(const boost::shared_array& a, - const boost::shared_array& b) const - { return std::less()(a.get(),b.get()); } - }; - -} // namespace std - -#endif // ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION - -#ifdef BOOST_MSVC -# pragma warning(pop) -#endif - -#endif // BOOST_SMART_PTR_HPP - +// For compatibility, this header includes the header for the four "classic" +// smart pointer class templates. +#include +#include +#include +#include diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp index d73c5f3..087ab12 100644 --- a/include/boost/weak_ptr.hpp +++ b/include/boost/weak_ptr.hpp @@ -67,14 +67,20 @@ public: template weak_ptr(weak_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { - if(px == 0) // need to allocate new counter -- the cast failed - { + if (px == 0) // need to allocate new counter -- the cast failed pn = detail::weak_count(); - } } template - weak_ptr & operator=(weak_ptr const & r) // nothrow? + weak_ptr & operator=(weak_ptr const & r) // never throws + { + px = r.px; + pn = r.pn; + return *this; + } + + template + weak_ptr & operator=(shared_ptr const & r) // never throws { px = r.px; pn = r.pn; @@ -86,11 +92,6 @@ public: this_type().swap(*this); } - long use_count() const // never throws - { - return pn.use_count(); - } - T * get() const // never throws { return use_count() == 0? 0: px; @@ -99,7 +100,6 @@ public: typename detail::shared_ptr_traits::reference operator* () const // never throws { T * p = get(); - BOOST_ASSERT(p != 0); return *p; } @@ -107,11 +107,15 @@ public: T * operator-> () const // never throws { T * p = get(); - BOOST_ASSERT(p != 0); return p; } + long use_count() const // never throws + { + return pn.use_count(); + } + void swap(weak_ptr & other) // never throws { std::swap(px, other.px); @@ -149,6 +153,11 @@ template inline bool operator<(weak_ptr const & a, weak_ptr const return std::less()(a.get(), b.get()); } +template void swap(weak_ptr & a, weak_ptr & b) +{ + a.swap(b); +} + template weak_ptr shared_static_cast(weak_ptr const & r) { return weak_ptr(r, detail::static_cast_tag()); @@ -159,11 +168,6 @@ template weak_ptr shared_dynamic_cast(weak_ptr const & r return weak_ptr(r, detail::dynamic_cast_tag()); } -template void swap(weak_ptr & a, weak_ptr & b) -{ - a.swap(b); -} - // get_pointer() enables boost::mem_fn to recognize weak_ptr template inline T * get_pointer(weak_ptr const & p) diff --git a/index.htm b/index.htm index 9c8e78a..676c78c 100644 --- a/index.htm +++ b/index.htm @@ -1,10 +1,10 @@ + + Boost Smart Pointer Library - - @@ -19,19 +19,28 @@ More -

    Smart pointer library

    -

    The header smart_ptr.hpp provides four smart pointer classes.  Smart -pointers ease the management of memory dynamically allocated with C++ new -expressions. +

    Smart Pointer Library

    +

    The smart pointer library includes five smart pointer class templates. Smart +pointers ease the management of memory dynamically allocated with C++ new +expressions. In addition, scoped_ptr can ease the management of memory +dynamically allocated in other ways.

    -

    Revised 14 Mar 2001 -

    + +

    Revised 1 February 2002.

    diff --git a/scoped_array.htm b/scoped_array.htm index 518b4cb..4df98a9 100644 --- a/scoped_array.htm +++ b/scoped_array.htm @@ -1,101 +1,144 @@ + + + scoped_array - - -

    c++boost.gif (8819 bytes)Class -scoped_array

    -

    Class scoped_array 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 scoped_array::reset().

    -

    Class scoped_array is a simple solution for simple -needs. It supplies a basic "resource acquisition is +

    c++boost.gif (8819 bytes)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.  By -being noncopyable, it is +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 + +

    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.

    -

    Class scoped_array cannot correctly hold a pointer to a -single object.  See scoped_ptr + +

    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 C++ Standard Library vector is a heavier duty alternative to a scoped_array.

    -

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

    -

    Class scoped_array Synopsis

    -
    #include <boost/smart_ptr.hpp>
    -namespace boost {
     
    -template<typename T> class scoped_array : noncopyable {
    +

    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.

    - public: - typedef T element_type; +

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

    - explicit scoped_array( T* p=0 ); // never throws - ~scoped_array(); +

    Synopsis

    - void reset( T* p=0 ); +
    namespace boost {
    +
    +  template<typename T> class scoped_array : noncopyable {
    +
    +    public:
    +      typedef T element_type;
    +
    +      explicit scoped_array(T * p = 0); // never throws
    +      ~scoped_array(); // never throws
    +
    +      void reset(T * p = 0); // never throws
    +
    +      T & operator[](std::size_t i) const; // never throws
    +      T * get() const; // never throws
    +     
    +      void swap(scoped_array & b); // never throws
    +  };
    +
    +  template<typename T> void swap(scoped_array<T> & a, scoped_array<T> & b); // never throws
     
    -   T& operator[](std::size_t i) const;  // never throws
    -   T* get() const;  // never throws
    -   };
     }
    -

    Class scoped_array Members

    -

    scoped_array element_type

    + +

    Members

    + +

    +element_type

    typedef T element_type;

    Provides the type of the stored pointer.

    -

    scoped_array 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 Common Requirements.

    -

    scoped_array destructor

    -
    ~scoped_array();
    -

    Deletes the array pointed to by the stored pointer.  Note that in C++ delete[] -on a pointer with a value of 0 is harmless.

    -

    Does not throw exceptions.

    -

    scoped_array reset

    -
    void reset( T* p=0 )();
    + +

    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.

    + +

    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.

    + +

    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.

    -

    Does not throw exceptions.

    -

    scoped_array operator[]

    -

    T& operator[](std::size_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 get()==0, -or if i is less than 0 or is greater or equal to the number of elements +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::size_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.

    -

    scoped_array get

    -
    T* get() const;  // never throws
    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns the stored pointer.

    -

    Class scoped_array example

    -

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]

    + +

    get

    +
    T * get() const; // never throws
    +

    Returns the stored pointer. +T need not be a complete type. +See the smart 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 requirements.

    + +

    Free Functions

    + +

    swap

    +
    template<typename 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  24 May, 2001 -

    -

    © 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" -without express or implied warranty, and with no claim as to its suitability for -any purpose.

    + +

    Revised 1 February 2002

    + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

    diff --git a/scoped_ptr.htm b/scoped_ptr.htm index 652d1bb..ec0057d 100644 --- a/scoped_ptr.htm +++ b/scoped_ptr.htm @@ -1,152 +1,216 @@ + + + scoped_ptr - - -

    c++boost.gif (8819 bytes)Class -scoped_ptr

    -

    Class scoped_ptr stores a pointer to a dynamically allocated -object. (Dynamically allocated objects are allocated with the C++ new -expression.)   The object pointed to is guaranteed to be deleted, -either on destruction of the scoped_ptr, or via an explicit scoped_ptr::reset().  -See example.

    -

    Class scoped_ptr is a simple solution for simple -needs.  It supplies a basic "resource acquisition is +

    c++boost.gif (8819 bytes)scoped_ptr class template

    + +

    The scoped_ptr class template stores a pointer to a dynamically allocated +object. (Dynamically allocated objects are allocated with the C++ new +expression.) The object pointed to is guaranteed to be deleted, +either on destruction of the scoped_ptr, or via an explicit reset. +See the example.

    + +

    The scoped_ptr 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.  +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_ptr or std::auto_ptr for pointers which should not be +safer than shared_ptr or std::auto_ptr for pointers which should not be copied.

    -

    Because scoped_ptr is so simple, in its usual implementation + +

    Because scoped_ptr is simple, in its usual implementation every operation is as fast as for a built-in pointer and it has no more space overhead -that a built-in pointer.  (Because of the "complete type" -requirement for delete and reset members, they may have one additional function -call overhead in certain idioms.  See Handle/Body -Idiom.)   

    -

    Class scoped_ptr cannot be used in C++ Standard Library containers.  See shared_ptr -or std::auto_ptr if scoped_ptr does not meet your needs.

    -

    Class scoped_ptr cannot correctly hold a pointer to a -dynamically allocated array.  See scoped_array +that a built-in pointer.

    + +

    It cannot be used in C++ Standard Library containers. +See shared_ptr +or std::auto_ptr if scoped_ptr does not meet your needs.

    + +

    It cannot correctly hold a pointer to a +dynamically allocated array. See scoped_array for that usage.

    -

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

    -

    Class scoped_ptr Synopsis

    -
    #include <boost/smart_ptr.hpp>
    -namespace boost {
     
    -template<typename T> class scoped_ptr : noncopyable {
    +

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

    - public: - typedef T element_type; +

    Synopsis

    - explicit scoped_ptr( T* p=0 ); // never throws - ~scoped_ptr(); +
    namespace boost {
     
    -   void reset( T* p=0 );
    +  template<typename T> class scoped_ptr : noncopyable {
    +
    +   public:
    +     typedef T element_type;
    +
    +     explicit scoped_ptr(T * p = 0); // never throws
    +     ~scoped_ptr(); // never throws
    +
    +     void reset(T * p = 0); // never throws
    +
    +     T & operator*() const; // never throws
    +     T * operator->() const; // never throws
    +     T * get() const; // never throws
    +     
    +     void swap(scoped_ptr & b); // never throws
    +  };
    +
    +  template<typename T> void swap(scoped_ptr<T> & a, scoped_ptr<T> & b); // never throws
     
    -   T& operator*() const;  // never throws
    -   T* operator->() const;  // never throws
    -   T* get() const;  // never throws
    -   };
     }
    -

    Class scoped_ptr Members

    -

    scoped_ptr element_type

    + +

    Members

    + +

    element_type

    typedef T element_type;

    Provides the type of the stored pointer.

    -

    scoped_ptr constructors

    -
    explicit scoped_ptr( T* p=0 );  // never throws
    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Constructs a scoped_ptr, storing a copy of p, which must -have been allocated via a C++ new expression or be 0.

    -

    scoped_ptr destructor

    -
    ~scoped_ptr();
    -

    Deletes the object pointed to by the stored pointer.  Note that in C++, delete -on a pointer with a value of 0 is harmless.

    -

    Does not throw exceptions.

    -

    scoped_ptr reset

    -
    void reset( T* p=0 );
    + +

    constructors

    +
    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 requirements.

    + +

    destructor

    +
    ~scoped_ptr(); // never throws
    +

    Deletes the object 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 object's destructor does 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 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.

    -

    Does not throw exceptions.

    -

    scoped_ptr operator*

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

    Returns a reference to the object pointed to by the stored pointer.

    -

    scoped_ptr operator-> and get

    -
    T* operator->() const;  // never throws
    -T* get() const;  // never throws
    -

    T is not required by get() be a complete type.  See Common Requirements.

    -

    Both return the stored pointer.

    -

    Class scoped_ptr examples

    -
    #include <iostream>
    -#include <boost/smart_ptr.h>
    +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.

    -struct Shoe { ~Shoe(){ std::cout << "Buckle my shoe" << std::endl; } }; +

    indirection

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

    Returns a reference to the object pointed to by the stored pointer. +Behavior is undefined if the stored pointer is 0.

    +
    T * operator->() const; // never throws
    +

    Returns the stored pointer. Behavior is undefined if the stored pointer is 0.

    + +

    get

    +
    T * get() const; // never throws
    +

    Returns the stored pointer. +T need not be a complete type. +See the smart 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 requirements.

    + +

    Free Functions

    + +

    swap

    +
    template<typename T> void swap(scoped_ptr<T> & a, scoped_ptr<T> & b); // never throws
    +

    Equivalent to a.swap(b). Matches the interface of std::swap. +Provided as an aid to generic programming.

    + +

    Example

    + +

    Here's an example that uses scoped_ptr.

    + +
    +
    #include <boost/scoped_ptr.hpp>
    +#include <iostream>
    +
    +struct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };
     
     class MyClass {
         boost::scoped_ptr<int> ptr;
       public:
         MyClass() : ptr(new int) { *ptr = 0; }
         int add_one() { return ++*ptr; }
    -    };
    +};
     
    -void main() {
    +void main()
    +{
         boost::scoped_ptr<Shoe> x(new Shoe);
         MyClass my_instance;
    -    std::cout << my_instance.add_one() << std::endl;
    -    std::cout << my_instance.add_one() << std::endl;
    -    }
    -

    The example program produces the beginning of a child's nursery rhyme as -output:

    + std::cout << my_instance.add_one() << '\n'; + std::cout << my_instance.add_one() << '\n'; +}
    + + +

    The example program produces the beginning of a child's nursery rhyme:

    +
    1
     2
     Buckle my shoe
    +

    Rationale

    -

    The primary reason to use scoped_ptr rather than auto_ptr is to let readers -of your code know that you intend "resource acquisition is initialization" to be applied only for the current scope, and have no intent to transfer -ownership.

    -

    A secondary reason to use scoped_ptr is to prevent a later maintenance programmer from adding a function that actually transfers -ownership by returning the auto_ptr (because the maintenance programmer saw -auto_ptr, and assumed ownership could safely be transferred.) 

    -

    Think of bool vs int. We all know that under the covers bool is usually -just an int. Indeed, some argued against including bool in the -C++ standard because of that. But by coding bool rather than int, you tell your readers -what your intent is. Same with scoped_ptr - you are signaling intent.

    -

    It has been suggested that boost::scoped_ptr<T> is equivalent to -std::auto_ptr<T> const.  Ed Brey pointed out, however, that -reset() will not work on a std::auto_ptr<T> const.

    + +

    The primary reason to use scoped_ptr rather than auto_ptr is to let readers +of your code know that you intend "resource acquisition is initialization" to be applied only +for the current scope, and have no intent to transfer ownership.

    + +

    A secondary reason to use scoped_ptr is to prevent a later maintenance programmer +from adding a function that transfers ownership by returning the auto_ptr, +because the maintenance programmer saw auto_ptr, and assumed ownership could safely +be transferred.

    + +

    Think of bool vs int. We all know that under the covers bool is usually +just an int. Indeed, some argued against including bool in the +C++ standard because of that. But by coding bool rather than int, you tell your readers +what your intent is. Same with scoped_ptr; by using it you are signaling intent.

    + +

    It has been suggested that scoped_ptr<T> is equivalent to +std::auto_ptr<T> const. Ed Brey pointed out, however, that +reset will not work on a std::auto_ptr<T> const.

    +

    Handle/Body Idiom

    +

    One common usage of scoped_ptr is to implement a handle/body (also called pimpl) idiom which avoids exposing the body (implementation) in the header file.

    +

    The scoped_ptr_example_test.cpp sample program includes a header file, scoped_ptr_example.hpp, which uses a scoped_ptr<> to an incomplete type to hide the -implementation.   The -instantiation of member functions which require a complete type occurs in the scoped_ptr_example.cpp +implementation. The +instantiation of member functions which require a complete type occurs in +the scoped_ptr_example.cpp implementation file.

    -

    FAQ

    + +

    Frequently Asked Questions

    +

    Q. Why doesn't scoped_ptr have a release() member?
    -A. Because the whole point of scoped_ptr is to signal intent not -to transfer ownership.  Use std::auto_ptr if ownership transfer is +A. Because the point of scoped_ptr is to signal intent, not +to transfer ownership. Use std::auto_ptr if ownership transfer is required.

    +
    -

    Revised 24 May 2001

    -

    © 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" -without express or implied warranty, and with no claim as to its suitability for -any purpose.

    + +

    Revised 1 February 2002

    + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

    diff --git a/scoped_ptr_example.hpp b/scoped_ptr_example.hpp index 97c8bff..ae1fb8d 100644 --- a/scoped_ptr_example.hpp +++ b/scoped_ptr_example.hpp @@ -1,6 +1,6 @@ // Boost scoped_ptr_example header file ------------------------------------// -#include +#include // The point of this example is to prove that even though // example::implementation is an incomplete type in translation units using diff --git a/shared_array.htm b/shared_array.htm index 6de8704..8d21823 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -1,188 +1,223 @@ + + + shared_array - - -

    c++boost.gif (8819 bytes)Class -shared_array

    -

    Class shared_array 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 shared_array, on shared_array::operator=(), -or via an explicit shared_array::reset().  See example.

    -

    Class shared_array meets the CopyConstuctible -and Assignable requirements of the C++ Standard Library, and so -can be used in C++ Standard Library containers.  A specialization of std:: -less< > for  boost::shared_ptr<Y> is supplied so that  -shared_array 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.

    -

    Class shared_array cannot correctly hold a pointer to a -single object.  See shared_ptr +

    c++boost.gif (8819 bytes)shared_array class template

    + +

    The shared_array class template stores a pointer to a dynamically allocated +array. (Dynamically allocated array are allocated with the C++ new[] +expression.) The object pointed to is guaranteed to be deleted when +the last shared_array pointing to it is destroyed or reset.

    + +

    Every shared_array meets the CopyConstructible +and Assignable requirements of the C++ Standard Library, and so +can be used in standard library containers. Comparison operators +are supplied so that shared_array works with +the standard library's associative containers.

    + +

    Normally, a shared_array cannot correctly hold a pointer to a +dynamically allocated array. See shared_ptr for that usage.

    -

    Class shared_array will not work correctly with cyclic data -structures. For example, if main() holds a shared_array pointing to array A, -which directly or indirectly holds a shared_array pointing back to array A, then -array A's use_count() will be 2, and destruction of the main() shared_array will -leave array A dangling with a use_count() of 1.

    -

    A C++ Standard Library vector is a -heavier duty alternative to a shared_array.

    -

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

    -

    Class shared_array Synopsis

    -
    #include <boost/smart_ptr.hpp>
    -namespace boost {
     
    -template<typename T> class shared_array {
    +

    Because the implementation uses reference counting, shared_array will not work +correctly with cyclic data structures. For example, if main() holds a shared_array +to A, which directly or indirectly holds a shared_array back to A, +A's use count will be 2. Destruction of the original shared_array +will leave A dangling with a use count of 1.

    - public: - typedef T element_type; +

    A shared_ptr to a std::vector is an alternative to a shared_array that is +a bit heavier duty but far more flexible.

    - explicit shared_array( T* p=0 ); - shared_array( const shared_array& ); // never throws - ~shared_array(); +

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

    - shared_array& operator=( const shared_array& ); // never throws +

    Synopsis

    - void reset( T* p=0 ); +
    namespace boost {
     
    -   T& operator[](std::size_t i) const;  // never throws
    -   T* get() const;  // never throws
    +  template<typename T> class shared_array {
     
    -   long use_count() const;  // never throws
    -   bool unique() const;  // never throws
    +    public:
    +      typedef T element_type;
     
    -   void swap( shared_array<T>& other ) throw()
    -   };
    +      explicit shared_array(T * p = 0);
    +      template<typename D> shared_array(T * p, D d);
    +      ~shared_array(); // never throws
     
    -template<typename T>
    -  inline bool operator==(const shared_array<T>& a, const shared_array<T>& b)
    -    { return a.get() == b.get(); }
    +      shared_array(shared_array const & r); // never throws
     
    -template<typename T>
    -  inline bool operator!=(const shared_array<T>& a, const shared_array<T>& b)
    -    { return a.get() != b.get(); }
    -}
    -
    namespace std {
    +      shared_array & operator=(shared_array const & r); // never throws  
     
    -template<typename T>
    -  inline void swap(boost::shared_array<T>& a, boost::shared_array<T>& b)
    -    { a.swap(b); }
    +      void reset(T * p = 0); // never throws
    +      template<typename D> void reset(T * p, D d); // never throws
     
    -template<typename T>
    -  struct less< boost::shared_array<T> >
    -    : binary_function<boost::shared_array<T>, boost::shared_array<T>, bool>
    -  {
    -    bool operator()(const boost::shared_array<T>& a,
    -        const boost::shared_array<T>& b) const
    -      { return less<T*>()(a.get(),b.get()); }
    +      T & operator[](std::ptrdiff_t i) const() const; // never throws
    +      T * get() const; // never throws
    +
    +      bool unique() const; // never throws
    +      long use_count() const; // never throws
    +
    +      void swap(shared_array<T> & b); // never throws
       };
     
    -} // namespace std 
    -

    Specialization of std::swap uses the fast, non-throwing swap that's provided -as a member function instead of using the default algorithm which creates a -temporary and uses assignment.
    -
    -Specialization of std::less allows use of shared arrays as keys in C++ -Standard Library associative collections.
    -
    -The std::less specializations use std::less<T*> to perform the -comparison.  This insures that pointers are handled correctly, since the -standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] -paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] -paragraph 8).
    -
    -It's still a controversial question whether supplying only std::less is better -than supplying a full range of comparison operators (<, >, <=, >=).

    -

    The current implementation does not supply the specializations if the macro -name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

    -

    Class shared_array Members

    -

    shared_array element_type

    + template<typename T> + bool operator==(shared_array<T> const & a, shared_array<T> const & b); // never throws + template<typename T> + bool operator!=(shared_array<T> const & a, shared_array<T> const & b); // never throws + template<typename T> + bool operator<(shared_array<T> const & a, shared_array<T> const & b); // never throws + + template<typename T> void swap(shared_array<T> & a, shared_array<T> & b); // never throws + +}
    + +

    Members

    + +

    element_type

    typedef T element_type;

    Provides the type of the stored pointer.

    -

    shared_array constructors

    -
    explicit shared_array( T* p=0 );
    -

    Constructs a shared_array, storing a copy of p, -which must have been allocated via a C++ new[] expression or be 0. -Afterwards, use_count() is 1 (even if p==0; see ~shared_array).

    -

    The only exception which may be thrown is std::bad_alloc.  If -an exception is thrown,  delete[] p is called.

    -
    shared_array( const shared_array& r);  // never throws
    -

    Constructs a shared_array, as if by storing a copy of the -pointer stored in r. Afterwards, use_count() -for all copies is 1 more than the initial r.use_count().

    -

    shared_array destructor

    -
    ~shared_array();
    -

    If use_count() == 1, deletes the array pointed to by the -stored pointer. Otherwise, use_count() for any remaining -copies is decremented by 1. Note that in C++ delete[] on a pointer with -a value of 0 is harmless.

    -

    Does not throw exceptions.

    -

    shared_array operator=

    -
    shared_array& operator=( const shared_array& r);  // never throws
    -

    First, if use_count() == 1, deletes the array pointed to by -the stored pointer. Otherwise, use_count() for any -remaining copies is decremented by 1. Note that in C++ delete[] on a -pointer with a value of 0 is harmless.

    -

    Then replaces the contents of this, as if by storing a copy -of the pointer stored in r. Afterwards, use_count() -for all copies is 1 more than the initial r.use_count()

    -

    shared_array reset

    -
    void reset( T* p=0 );
    -

    First, if use_count() == 1, deletes the array pointed to by -the stored pointer. Otherwise, use_count() for any -remaining copies is decremented by 1. Note that in C++  delete[] -on a pointer with a value of 0 is harmless.

    -

    Then replaces the contents of this, as if by storing a copy -of p, which must have been allocated via a C++ new[] -expression or be 0. Afterwards, use_count() is 1 (even if p==0; -see ~shared_array).

    -

    The only exception which may be thrown is std::bad_alloc.  If -an exception is thrown,  delete[] p is called.

    -

    shared_array operator[]

    -

    T& operator[](std::size_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 get()==0, -or if i is less than 0 or is greater or equal to the number of elements + +

    constructors

    + +
    explicit shared_array(T * p = 0);
    +

    Constructs a shared_array, storing a copy of p, which +must be a pointer to an array that was allocated via a C++ new[] expression or be 0. +Afterwards, the use count is 1 (even if p == 0; see ~shared_array). +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, delete[] p is called.

    + +
    template<typename D> shared_array(T * p, D d);
    +

    Constructs a shared_array, storing a copy of p and of d. +Afterwards, the use count is 1. +D's copy constructor must not throw. +When the the time comes to delete the array pointed to by p, the object +d is used in the statement d(p). Invoking the object d with +parameter p in this way must not throw. +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, d(p) is called.

    + +
    shared_array(shared_array const & r); // never throws
    +

    Constructs a shared_array, as if by storing a copy of the +pointer stored in r. Afterwards, the use count +for all copies is 1 more than the initial use count.

    + +

    destructor

    + +
    ~shared_array(); // never throws
    +

    Decrements the use count. Then, if the use count is 0, +deletes the array pointed to by the stored pointer. +Note that delete[] on a pointer with a value of 0 is harmless. +T need not be a complete type. +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.

    + +

    assignment

    + +
    shared_array & operator=(shared_array const & r); // never throws
    +

    Constructs a new shared_array as described above, +then replaces this shared_array with the new one, destroying the replaced object.

    + +

    reset

    + +
    void reset(T * p = 0);
    +

    Constructs a new shared_array as described above, +then replaces this shared_array with the new one, destroying the replaced object. +The only exception which may be thrown is std::bad_alloc. If +an exception is thrown, delete[] p is called.

    + +
    template<typename D> void reset(T * p, D d);
    +

    Constructs a new shared_array as described above, +then replaces this shared_array with the new one, destroying the replaced object. +D's copy constructor must not throw. +The only exception which may be thrown is std::bad_alloc. If +an exception is thrown, d(p) is called.

    + +

    indexing

    +
    T & operator[](std::size_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.

    -

    shared_array get

    -
    T* get() const;  // never throws
    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns the stored pointer.

    -

    shared_array use_count

    -

    long use_count() const; // never throws

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns the number of shared_arrays sharing ownership of the -stored pointer.

    -

    shared_array unique

    -

    bool unique() const; // never throws

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns use_count() == 1.

    -

    shared_array swap

    -

    void swap( shared_array<T>& other ) throw()

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Swaps the two smart pointers, as if by std::swap.

    -

    Class shared_array example

    -

    [To be supplied. In the meantime, see smart_ptr_test.cpp.]

    + +

    get

    +
    T * get() const; // never throws
    +

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

    + +

    unique

    +
    bool unique() const; // never throws
    +

    Returns true if no other shared_array is sharing ownership of +the stored pointer, false otherwise. +T need not be a complete type. +See the smart pointer +common requirements.

    + +

    use_count

    +
    long use_count() const; // never throws
    +

    Returns the number of shared_array objects sharing ownership of the +stored pointer. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    Because use_count is not necessarily efficient to implement for +implementations of shared_array that do not use an explicit reference +count, it might be removed from some future version. Thus it should +be used for debugging purposes only, and not production code.

    + +

    swap

    +
    void swap(shared_ptr & b); // never throws
    +

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

    + +

    Free Functions

    + +

    comparison

    +
    template<typename T>
    +  bool operator==(shared_array<T> const & a, shared_array<T> const & b); // never throws
    +template<typename T>
    +  bool operator!=(shared_array<T> const & a, shared_array<T> const & b); // never throws
    +template<typename T>
    +  bool operator<(shared_array<T> const & a, shared_array<T> const & b); // never throws
    +

    Compares the stored pointers of the two smart pointers. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    The operator< overload is provided to define an ordering so that shared_array +objects can be used in associative containers such as std::map. +The implementation uses std::less<T*> to perform the +comparison. This ensures that the comparison is handled correctly, since the +standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 8).

    + +

    swap

    +
    template<typename T>
    +  void swap(shared_array<T> & a, shared_array<T> & b) // never throws
    +

    Equivalent to a.swap(b). Matches the interface of std::swap. +Provided as an aid to generic programming.

    +
    -

    Revised 24 May, 2001 -

    -

    © 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" -without express or implied warranty, and with no claim as to its suitability for -any purpose.

    + +

    Revised 1 February 2002

    + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

    diff --git a/shared_ptr.htm b/shared_ptr.htm index 4913f65..fc6d0d6 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -1,284 +1,356 @@ + + + shared_ptr - - -

    -c++boost.gif (8819 bytes)Class -shared_ptr

    -

    Introduction
    -Synopsis

    +

    c++boost.gif (8819 bytes)shared_ptr class template

    + +

    Introduction
    +Synopsis
    Members
    -Example
    +Free Functions
    +Example
    Handle/Body Idiom
    Frequently Asked Questions
    Smart Pointer Timings

    +

    Introduction

    -

    Class shared_ptr stores a pointer to a dynamically allocated -object. (Dynamically allocated objects are allocated with the C++ new -expression.)   The object pointed to is guaranteed to be deleted when -the last shared_ptr pointing to it is deleted or reset.  -See example.

    -

    Class shared_ptr meets the CopyConstuctible -and Assignable requirements of the C++ Standard Library, and so -can be used in C++ Standard Library containers.  A specialization of std:: -less< > for  boost::shared_ptr<Y> is supplied so that  -shared_ptr 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.

    -

    Class shared_ptr cannot correctly hold a pointer to a -dynamically allocated array.  See shared_array + +

    The shared_ptr class template stores a pointer to a dynamically allocated +object. (Dynamically allocated objects are allocated with the C++ new +expression.) The object pointed to is guaranteed to be deleted when +the last shared_ptr pointing to it is destroyed or reset. +See the example.

    + +

    Every shared_ptr meets the CopyConstructible +and Assignable requirements of the C++ Standard Library, and so +can be used in standard library containers. Comparison operators +are supplied so that shared_ptr works with +the standard library's associative containers.

    + +

    Normally, a shared_ptr cannot correctly hold a pointer to a +dynamically allocated array. See shared_array for that usage.

    -

    Class shared_ptr will not work correctly with cyclic data -structures. For example, if main() holds a shared_ptr to object A, which -directly or indirectly holds a shared_ptr back to object A, then object A's -use_count() will be 2, and destruction of the main() shared_ptr will leave -object A dangling with a use_count() of 1.

    -

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

    -

    Class shared_ptr Synopsis

    -
    #include <boost/smart_ptr.hpp>
    -namespace boost {
     
    -template<typename T> class shared_ptr {
    +

    Because the implementation uses reference counting, shared_ptr will not work +correctly with cyclic data structures. For example, if main() holds a shared_ptr +to A, which directly or indirectly holds a shared_ptr back to A, +A's use count will be 2. Destruction of the original shared_ptr +will leave A dangling with a use count of 1.

    - public: - typedef T element_type; +

    The class template is parameterized on T, the type of the object +pointed to. T must meet the smart pointer +common requirements. +T may be void, but in that case, either an explicit delete +function must be passed in, or the pointed-to object must have a trivial destructor.

    - explicit shared_ptr( T* p=0 ); - ~shared_ptr(); +

    Synopsis

    - shared_ptr( const shared_ptr& ); - template<typename Y> - shared_ptr(const shared_ptr<Y>& r); // never throws - template<typename Y> - shared_ptr(std::auto_ptr<Y>& r); +
    namespace boost {
     
    -   shared_ptr& operator=( const shared_ptr& );  // never throws  
    -   template<typename Y>
    -      shared_ptr& operator=(const shared_ptr<Y>& r);  // never throws
    -   template<typename Y>
    -      shared_ptr& operator=(std::auto_ptr<Y>& r);
    +  template<typename T> class shared_ptr {
     
    -   void reset( T* p=0 );
    +    public:
    +      typedef T element_type;
     
    -   T& operator*() const;  // never throws
    -   T* operator->() const;  // never throws
    -   T* get() const;  // never throws
    +      explicit shared_ptr(T * p = 0);
    +      template<typename D> shared_ptr(T * p, D d);
    +      ~shared_ptr(); // never throws
     
    -   long use_count() const;  // never throws
    -   bool unique() const;  // never throws
    +      shared_ptr(shared_ptr const & r); // never throws
    +      template<typename Y> shared_ptr(shared_ptr<Y> const & r); // never throws
    +      template<typename Y> shared_ptr(std::auto_ptr<Y> & r);
     
    -   void swap( shared_ptr<T>& other ) throw()
    -   };
    +      shared_ptr & operator=(shared_ptr const & r); // never throws  
    +      template<typename Y> shared_ptr & operator=(shared_ptr<Y> const & r); // never throws
    +      template<typename Y> shared_ptr & operator=(std::auto_ptr<Y> & r);
     
    -template<typename T, typename U>
    -  inline bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b)
    -    { return a.get() == b.get(); }
    +      void reset(T * p = 0); // never throws
    +      template<typename D> void reset(T * p, D d); // never throws
     
    -template<typename T, typename U>
    -  inline bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b)
    -    { return a.get() != b.get(); }
    -}
    -
    namespace std {
    +      T & operator*() const; // never throws
    +      T * operator->() const; // never throws
    +      T * get() const; // never throws
     
    -template<typename T>
    -  inline void swap(boost::shared_ptr<T>& a, boost::shared_ptr<T>& b)
    -    { a.swap(b); }
    +      bool unique() const; // never throws
    +      long use_count() const; // never throws
     
    -template<typename T>
    -  struct less< boost::shared_ptr<T> >
    -    : binary_function<boost::shared_ptr<T>, boost::shared_ptr<T>, bool>
    -  {
    -    bool operator()(const boost::shared_ptr<T>& a,
    -        const boost::shared_ptr<T>& b) const
    -      { return less<T*>()(a.get(),b.get()); }
    +      void swap(shared_ptr<T> & b); // never throws
       };
     
    -} // namespace std 
    -

    Specialization of std::swap uses the fast, non-throwing swap that's provided -as a member function instead of using the default algorithm which creates a -temporary and uses assignment.
    -
    -Specialization of std::less allows use of shared pointers as keys in C++ -Standard Library associative collections.
    -
    -The std::less specializations use std::less<T*> to perform the -comparison.  This insures that pointers are handled correctly, since the -standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] -paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] -paragraph 8).
    -
    -It's still a controversial question whether supplying only std::less is better -than supplying a full range of comparison operators (<, >, <=, >=).

    -

    The current implementation does not supply the specializations if the macro -name BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION is defined.

    -

    The current implementation does not supply the member template functions if -the macro name BOOST_NO_MEMBER_TEMPLATES is defined.

    -

    Class shared_ptr Members

    -

    shared_ptr element_type

    + template<typename T, typename U> + bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws + template<typename T, typename U> + bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws + template<typename T, typename U> + bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws + + template<typename T> void swap(shared_ptr<T> & a, shared_ptr<T> & b); // never throws + + template<typename T, typename U> + shared_ptr<T> shared_static_cast(shared_ptr<U> const & r); // never throws + template<typename T, typename U> + shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r); + +}
    + +

    Members

    + +

    element_type

    typedef T element_type;

    Provides the type of the stored pointer.

    -

    shared_ptr constructors

    -
    explicit shared_ptr( T* p=0 );
    -

    Constructs a shared_ptr, storing a copy of p, which -must have been allocated via a C++ new expression or be 0. Afterwards, use_count() -is 1 (even if p==0; see ~shared_ptr).

    -

    The only exception which may be thrown by this constructor is std::bad_alloc.   -If an exception is thrown,  delete p is called.

    -
    shared_ptr( const shared_ptr& r);  // never throws   
    -template<typename Y>
    -   shared_ptr(const shared_ptr<Y>& r);  // never throws
    -template<typename Y>
    -   shared_ptr(std::auto_ptr<Y>& r);
    -

    Constructs a shared_ptr, as if by storing a copy of the -pointer stored in r. Afterwards, use_count() -for all copies is 1 more than the initial r.use_count(), or 1 -in the auto_ptr case. In the auto_ptr case, r.release() -is called.

    -

    The only exception which may be thrown by the constructor from auto_ptr -is std::bad_alloc.   If an exception is thrown, that -constructor has no effect.

    -

    shared_ptr destructor

    -
    ~shared_ptr();
    -

    If use_count() == 1, deletes the object pointed to by the -stored pointer. Otherwise, use_count() for any remaining -copies is decremented by 1. Note that in C++  delete on a pointer -with a value of 0 is harmless.

    -

    Does not throw exceptions.

    -

    shared_ptr operator=

    -
    shared_ptr& operator=( const shared_ptr& r);  
    -template<typename Y>
    -   shared_ptr& operator=(const shared_ptr<Y>& r);
    -template<typename Y>
    -   shared_ptr& operator=(std::auto_ptr<Y>& r);
    -

    First, if use_count() == 1, deletes the object pointed to by -the stored pointer. Otherwise, use_count() for any -remaining copies is decremented by 1. Note that in C++  delete on -a pointer with a value of 0 is harmless.

    -

    Then replaces the contents of this, as if by storing a copy -of the pointer stored in r. Afterwards, use_count() -for all copies is 1 more than the initial r.use_count(), or 1 -in the auto_ptr case. In the auto_ptr case, r.release() -is called.

    -

    The first two forms of operator= above do not throw exceptions.

    -

    The only exception which may be thrown by the auto_ptr form -is std::bad_alloc.   If an exception is thrown, the function -has no effect.

    -

    shared_ptr reset

    -
    void reset( T* p=0 );
    -

    First, if use_count() == 1, deletes the object pointed to by -the stored pointer. Otherwise, use_count() for any -remaining copies is decremented by 1. 

    -

    Then replaces the contents of this, as if by storing a copy -of p, which must have been allocated via a C++ new -expression or be 0. Afterwards, use_count() is 1 (even if p==0; -see ~shared_ptr). Note that in C++  delete -on a pointer with a value of 0 is harmless.

    -

    The only exception which may be thrown is std::bad_alloc.  If -an exception is thrown,  delete p is called.

    -

    shared_ptr operator*

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

    Returns a reference to the object pointed to by the stored pointer.

    -

    shared_ptr operator-> and get

    -
    T* operator->() const;  // never throws
    -T* get() const;  // never throws
    -

    T is not required by get() to be a complete type .  See Common Requirements.

    -

    Both return the stored pointer.

    -

    shared_ptr use_count

    -

    long use_count() const; // never throws

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns the number of shared_ptrs sharing ownership of the -stored pointer.

    -

    shared_ptr unique

    -

    bool unique() const; // never throws

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Returns use_count() == 1.

    -

    shared_ptr swap

    -

    void swap( shared_ptr<T>& other ) throw()

    -

    T is not required be a complete type.  -See Common Requirements.

    -

    Swaps the two smart pointers, as if by std::swap.

    -

    Class shared_ptr example

    -

    See shared_ptr_example.cpp for a complete example program.

    -

    This program builds a std::vector and std::set of FooPtr's.

    -

    Note that after the two containers have been populated, some of the FooPtr objects -will have use_count()==1 rather than use_count()==2, since foo_set is a std::set -rather than a std::multiset, and thus does not contain duplicate entries.  Furthermore, use_count() may be even higher -at various times while push_back() and insert() container operations are performed.  + +

    constructors

    + +
    explicit shared_ptr(T * p = 0);
    +

    Constructs a shared_ptr, storing a copy of p, which +must be a pointer to an object that was allocated via a C++ new expression or be 0. +Afterwards, the use count is 1 (even if p == 0; see ~shared_ptr). +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, delete p is called.

    + +
    template<typename D> shared_ptr(T * p, D d);
    +

    Constructs a shared_ptr, storing a copy of p and of d. +Afterwards, the use count is 1. +D's copy constructor must not throw. +When the the time comes to delete the object pointed to by p, the object +d is used in the statement d(p). Invoking the object d with +parameter p in this way must not throw. +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, d(p) is called.

    + +
    shared_ptr(shared_ptr const & r); // never throws
    +template<typename Y> shared_ptr(shared_ptr<Y> const & r); // never throws
    +template<typename Y> shared_ptr(std::auto_ptr<Y> & r);
    +

    Constructs a shared_ptr, as if by storing a copy of the +pointer stored in r. Afterwards, the use count +for all copies is 1 more than the initial use count, or 1 +in the auto_ptr case. In the auto_ptr case, r.release() +is called. +The only exception which may be thrown is std::bad_alloc, +which may be thrown during construction from auto_ptr. +If an exception is thrown, the constructor has no effect.

    + +

    destructor

    + +
    ~shared_ptr(); // never throws
    +

    Decrements the use count. Then, if the use count is 0, +deletes the object pointed to by the stored pointer. +Note that delete on a pointer with a value of 0 is harmless. +T need not be a complete type. +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.

    + +

    assignment

    + +
    shared_ptr & operator=(shared_ptr const & r); // never throws
    +template<typename Y> shared_ptr & operator=(shared_ptr<Y> const & r); // never throws
    +template<typename Y> shared_ptr & operator=(std::auto_ptr<Y> & r);
    +

    Constructs a new shared_ptr as described above, +then replaces this shared_ptr with the new one, destroying the replaced object. +The only exception which may be thrown is std::bad_alloc, +which may be thrown during assignment from auto_ptr. +If an exception is thrown, the assignment has no effect.

    + +

    reset

    + +
    void reset(T * p = 0);
    +

    Constructs a new shared_ptr as described above, +then replaces this shared_ptr with the new one, destroying the replaced object. +The only exception which may be thrown is std::bad_alloc. If +an exception is thrown, delete p is called.

    + +
    template<typename D> void reset(T * p, D d);
    +

    Constructs a new shared_ptr as described above, +then replaces this shared_ptr with the new one, destroying the replaced object. +D's copy constructor must not throw. +The only exception which may be thrown is std::bad_alloc. If +an exception is thrown, d(p) is called.

    + +

    indirection

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

    Returns a reference to the object pointed to by the stored pointer. +Behavior is undefined if the stored pointer is 0.

    +
    T * operator->() const; // never throws
    +

    Returns the stored pointer. Behavior is undefined if the stored pointer is 0.

    + +

    get

    +
    T * get() const; // never throws
    +

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

    + +

    unique

    +
    bool unique() const; // never throws
    +

    Returns true if no other shared_ptr is sharing ownership of +the stored pointer, false otherwise. +T need not be a complete type. +See the smart pointer +common requirements.

    + +

    use_count

    +
    long use_count() const; // never throws
    +

    Returns the number of shared_ptr objects sharing ownership of the +stored pointer. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    Because use_count is not necessarily efficient to implement for +implementations of shared_ptr that do not use an explicit reference +count, it might be removed from some future version. Thus it should +be used for debugging purposes only, and not production code.

    + +

    swap

    +
    void swap(shared_ptr & b); // never throws
    +

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

    + +

    Free Functions

    + +

    comparison

    +
    template<typename T, typename U>
    +  bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws
    +template<typename T, typename U>
    +  bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws
    +template<typename T, typename U>
    +  bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b); // never throws
    +

    Compares the stored pointers of the two smart pointers. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    The operator< overload is provided to define an ordering so that shared_ptr +objects can be used in associative containers such as std::map. +The implementation uses std::less<T*> to perform the +comparison. This ensures that the comparison is handled correctly, since the +standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 8).

    + +

    swap

    +
    template<typename T>
    +  void swap(shared_ptr<T> & a, shared_ptr<T> & b) // never throws
    +

    Equivalent to a.swap(b). Matches the interface of std::swap. +Provided as an aid to generic programming.

    + +

    shared_static_cast

    +
    template<typename T, typename U>
    +  shared_ptr<T> shared_static_cast(shared_ptr<U> const & r); // never throws
    +

    Perform a static_cast on the stored pointer, returning another shared_ptr. +The resulting smart pointer will share its use count with the original pointer.

    +

    Note that the seemingly equivalent expression

    +
    shared_ptr<T>(static_cast<T*>(r.get()))
    +

    will eventually result in undefined behavior, attempting to delete the same object twice.

    + +

    shared_dynamic_cast

    +
    template<typename T, typename U>
    +  shared_ptr<T> shared_dynamic_cast(shared_ptr<U> const & r);
    +

    Perform a dynamic_cast on the stored pointer, returning another shared_ptr. +The resulting smart pointer will share its use count with the original pointer unless the result of the +cast is 0. The only exception which may be thrown is std::bad_alloc, which may be thrown during the +construction of the new shared_ptr if the result of the cast is 0. If an exception is thrown, the +cast has no effect.

    +

    Note that the seemingly equivalent expression

    +
    shared_ptr<T>(dynamic_cast<T*>(r.get()))
    +

    will eventually result in undefined behavior, attempting to delete the same object twice.

    + +

    Example

    + +

    See shared_ptr_example.cpp for a complete example program. +The program builds a std::vector and std::set of shared_ptr objects.

    + +

    Note that after the containers have been populated, some of the shared_ptr objects +will have a use count of 1 rather than a use count of 2, since the set is a std::set +rather than a std::multiset, and thus does not contain duplicate entries. +Furthermore, the use count may be even higher +at various times while push_back and insert 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.

    +variety of circumstances. Getting the memory management and exception handling in this +example right without a smart pointer would be a nightmare.

    +

    Handle/Body Idiom

    +

    One common usage of shared_ptr is to implement a handle/body (also called pimpl) idiom which avoids exposing the body (implementation) in the header file.

    +

    The shared_ptr_example2_test.cpp sample program includes a header file, shared_ptr_example2.hpp, which uses a shared_ptr<> to an incomplete type to hide the -implementation.   The -instantiation of member functions which require a complete type occurs in the shared_ptr_example2.cpp -implementation file.

    +implementation. The +instantiation of member functions which require a complete type occurs in the +shared_ptr_example2.cpp +implementation file. +Note that there is no need for an explicit destructor. +Unlike ~scoped_ptr, ~shared_ptr does not require that T be a complete type.

    +

    Frequently Asked Questions

    +

    Q. Why doesn't shared_ptr have template parameters supplying traits or policies to allow extensive user customization?
    -A. Parameterization discourages users.  Shared_ptr is +A. Parameterization discourages users. The shared_ptr template is carefully crafted to meet common needs without extensive parameterization. -Someday a highly configurable smart pointer may be invented that is also very -easy to use and very hard to misuse.  Until then, shared_ptr is the -smart pointer of choice for a wide range of applications.  (Those -interested in policy based smart pointers should read Modern -C++ Design by Andrei Alexandrescu.)

    +Some day a highly configurable smart pointer may be invented that is also very +easy to use and very hard to misuse. Until then, shared_ptr is the +smart pointer of choice for a wide range of applications. (Those +interested in policy based smart pointers should read +Modern C++ Design by Andrei Alexandrescu.)

    +

    Q. Why doesn't shared_ptr use a linked list implementation?
    A. A linked list implementation does not offer enough advantages to -offset the added cost of an extra pointer.  See timings +offset the added cost of an extra pointer. See timings page.

    -

    Q. Why don't shared_ptr (and the other Boost smart pointers) + +

    Q. Why doesn't shared_ptr (or any of the other Boost smart pointers) supply an automatic conversion to T*?
    A. Automatic conversion is believed to be too error prone.

    +

    Q. Why does shared_ptr supply use_count()?
    A. As an aid to writing test cases and debugging displays. One of the progenitors had use_count(), and it was useful in tracking down bugs in a complex project that turned out to have cyclic-dependencies.

    +

    Q. Why doesn't shared_ptr specify complexity requirements?
    -A. Because complexity limit implementors and complicate the specification without apparent benefit to -shared_ptr users. For example, error-checking implementations might become non-conforming if they +A. Because complexity requirements limit implementors and complicate the +specification without apparent benefit to shared_ptr users. +For example, error-checking implementations might become non-conforming if they had to meet stringent complexity requirements.

    +

    Q. Why doesn't shared_ptr provide a release() function?
    A. shared_ptr cannot give away ownership unless it's unique() because the other copy will still destroy the object.

    Consider:

    -
    -
    shared_ptr<int> a(new int);
    +
    shared_ptr<int> a(new int);
     shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2
     
     int * p = a.release();
     
    -// Who owns p now? b will still call delete on it in its destructor.
    -
    -

    [Provided by Peter Dimov]

    +// Who owns p now? b will still call delete on it in its destructor.
    +

    Q. Why doesn't shared_ptr provide (your pet feature here)?
    -A. Because (your pet feature here) would mandate a reference counted (or a link-list, or ...) implementation. This is not the intent. -[Provided by Peter Dimov]

    -


    -

    +A. Because (your pet feature here) would mandate a reference counted +implementation or a linked list implementation, or some other specific implementation. +This is not the intent.

    +
    -

    Revised 11 January, 2002 -

    -

    © 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" -without express or implied warranty, and with no claim as to its suitability for -any purpose.

    + +

    Revised 1 February 2002

    + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

    - \ No newline at end of file + diff --git a/shared_ptr_example.cpp b/shared_ptr_example.cpp index 988efbd..4db8fc6 100644 --- a/shared_ptr_example.cpp +++ b/shared_ptr_example.cpp @@ -20,7 +20,7 @@ #include #include #include -#include +#include // The application will produce a series of // objects of type Foo which later must be diff --git a/shared_ptr_example2.cpp b/shared_ptr_example2.cpp index eddc806..746dd48 100644 --- a/shared_ptr_example2.cpp +++ b/shared_ptr_example2.cpp @@ -17,5 +17,3 @@ example & example::operator=( const example & s ) void example::do_something() { std::cout << "use_count() is " << _imp.use_count() << "\n"; } - -example::~example() {} diff --git a/shared_ptr_example2.hpp b/shared_ptr_example2.hpp index fea810b..93b77e5 100644 --- a/shared_ptr_example2.hpp +++ b/shared_ptr_example2.hpp @@ -1,6 +1,6 @@ // Boost shared_ptr_example2 header file -----------------------------------// -#include +#include // This example demonstrates the handle/body idiom (also called pimpl and // several other names). It separates the interface (in this header file) @@ -16,7 +16,6 @@ class example { public: example(); - ~example(); example( const example & ); example & operator=( const example & ); void do_something(); diff --git a/smart_ptr.htm b/smart_ptr.htm index 07538e4..0f5246d 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -1,126 +1,175 @@ + + - - -Smart Pointer Classes +Smart Pointers -

    c++boost.gif (8819 bytes)Smart +

    c++boost.gif (8819 bytes)Smart Pointers

    -

    Smart pointers are classes which store pointers to dynamically allocated -(heap) objects.  They behave much like built-in C++ pointers except that + +

    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 +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 header boost/smart_ptr.hpp -provides four smart pointer template classes:

    + +

    The smart pointer library provides five smart pointer class templates:

    +
    - + + - - + + + - + + - + + + + + + +
    -

    scoped_ptr

    scoped_ptr<boost/scoped_ptr.hpp> Simple sole ownership of single objects. Noncopyable.
    scoped_arraySimple sole ownership of arrays. Noncopyable.scoped_array<boost/scoped_array.hpp>Simple sole ownership of arrays. Noncopyable.
    shared_ptrshared_ptr<boost/shared_ptr.hpp> Object ownership shared among multiple pointers
    shared_arrayshared_array<boost/shared_array.hpp> Array ownership shared among multiple pointers.
    weak_ptr<boost/weak_ptr.hpp>Non-owning observers of an object owned by shared_ptr.
    -

    These classes are designed to complement the C++ Standard Library auto_ptr -class.

    + +

    These templates are designed to complement the std::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.cpp) is + +

    A test program, smart_ptr_test.cpp, is provided to verify correct operation.

    -

    A page on Smart Pointer Timings will be of + +

    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.

    -

    Common requirements

    -

    These smart pointer classes have a template parameter, T, which -specifies the type of the object pointed to by the smart pointer.  The -behavior of all four classes 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 + +

    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 checked_delete().

    +including deletion of an incomplete type. +See the description of the checked_delete +function template.

    +

    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.  + +

    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.

    -

    Exception safety

    + +

    Note that scoped_ptr requires that T be a complete type +at destruction time, but shared_ptr does not.

    + +

    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 +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.

    +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.

    +

    Exception-specifications

    -

    Exception-specifications are not used; see exception-specification + +

    Exception-specifications are not used; see +exception-specification rationale.

    -

    All four classes contain member functions which can never throw exceptions, + +

    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.

    +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.

    -

    History and acknowledgements

    -

    May, 2001. Vladimir Prus suggested requiring a complete type on -destruction.  Refinement evolved in discussions including Dave Abrahams, +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. 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 + +

    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.  See the -revision history in smart_ptr.hpp + +

    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. See the +revision history in smart_ptr.hpp for the specific changes made as a result of their constructive criticism.

    -

    Oct, 1998.  In 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.  -The committee document was 94-168/N0555, Exception Safe Smart Pointers.  In + +

    October 1998. In 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. +The committee document was 94-168/N0555, Exception Safe Smart Pointers. 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.

    -

    Beman Dawes proposed reviving the original semantics under the names safe_ptr -and counted_ptr at an October, 1998, meeting of Per Andersson, +not followed by the full committee, counted_ptr was rejected +and surprising transfer-of-ownership semantics were added to auto_ptr.

    + +

    Beman Dawes proposed reviving the original semantics under the names safe_ptr +and counted_ptr at an October, 1998, meeting of Per Andersson, Matt Austern, Greg Colvin, Sean Corfield, Pete Becker, Nico Josuttis, Dietmar -Kühl, Nathan Myers, Chichiang Wan and Judy Ward.  During the discussion, +Kühl, Nathan Myers, Chichiang Wan and Judy Ward. During the discussion, the four class names were finalized, it was decided that there was no need to -exactly follow the std::auto_ptr interface, and various +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, + +

    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 +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.
    • @@ -129,22 +178,29 @@ elsewhere. Each of those variants have themselves two major variants:
    • 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 + +

    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ühl suggested an elegant partial template +difference. Kevlin Henney provided a paper he wrote on "Counted Body +Techniques." Dietmar Kü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.

    +

    See the Revision History section of the header for further contributors.

    +
    -

    Revised  24 May 20011 February 2002

    -

    © Copyright Greg Colvin and Beman Dawes 1999. Permission to copy, use, + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, and with no claim as to its suitability for diff --git a/smart_ptr_test.cpp b/smart_ptr_test.cpp index 62c97ca..8d41070 100644 --- a/smart_ptr_test.cpp +++ b/smart_ptr_test.cpp @@ -13,13 +13,23 @@ // 20 Jul 99 header name changed to .hpp // 20 Apr 99 additional error tests added. +#include +#include +#include +#include + #define BOOST_INCLUDE_MAIN #include -#include + #include #include #include +bool boost_error(char const *, char const *, char const *, long) +{ + return true; // fail with assert() +} + class Incomplete; Incomplete * get_ptr( boost::shared_ptr& incomplete ) @@ -48,7 +58,7 @@ class UDT { explicit UDT( long value=0 ) : value_(value) { ++UDT_use_count; } ~UDT() { --UDT_use_count; - cout << "UDT with value " << value_ << " being destroyed" << endl; + cout << "UDT with value " << value_ << " being destroyed\n"; } long value() const { return value_; } void value( long v ) { value_ = v;; } @@ -71,7 +81,7 @@ Incomplete * check_incomplete( shared_ptr& incomplete, shared_ptr& i2 ) { incomplete.swap(i2); - cout << incomplete.use_count() << " " << incomplete.unique() << endl; + cout << incomplete.use_count() << ' ' << incomplete.unique() << '\n'; return incomplete.get(); } // main --------------------------------------------------------------------// @@ -186,12 +196,10 @@ int test_main( int, char ** ) { BOOST_TEST( *cp4 == 87654 ); BOOST_TEST( cp2.get() == 0 ); -#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION set< shared_ptr > scp; scp.insert(cp4); BOOST_TEST( scp.find(cp4) != scp.end() ); BOOST_TEST( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); -#endif // test shared_array with a built-in type char * cap = new char [ 100 ]; @@ -232,12 +240,10 @@ int test_main( int, char ** ) { BOOST_TEST( strcmp( ca4.get(), "Not dog with mustard and relish" ) == 0 ); BOOST_TEST( ca3.get() == 0 ); -#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION set< shared_array > sca; sca.insert(ca4); BOOST_TEST( sca.find(ca4) != sca.end() ); BOOST_TEST( sca.find(ca4) == sca.find( shared_array(ca4) ) ); -#endif // test shared_array with user defined type shared_array udta ( new UDT[3] ); @@ -280,7 +286,7 @@ int test_main( int, char ** ) { BOOST_TEST( sup.use_count() == 2 ); BOOST_TEST( sup2.use_count() == 2 ); - cout << "OK" << endl; + cout << "OK\n"; new char[12345]; // deliberate memory leak to verify leaks detected diff --git a/smart_ptr_test2.cpp b/smart_ptr_test2.cpp deleted file mode 100644 index 4ea305e..0000000 --- a/smart_ptr_test2.cpp +++ /dev/null @@ -1,295 +0,0 @@ -// smart pointer test program ----------------------------------------------// - -// (C) Copyright Beman Dawes 1998, 1999. 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. - -// 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 -#include -#include -#include - -#define BOOST_INCLUDE_MAIN -#include - -#include -#include -#include - -bool boost_error(char const *, char const *, char const *, long) -{ - return true; // fail with assert() -} - -class Incomplete; - -Incomplete * get_ptr( boost::shared_ptr& incomplete ) -{ - return incomplete.get(); -} - -using namespace std; -using boost::scoped_ptr; -using boost::scoped_array; -using boost::shared_ptr; -using boost::shared_array; - -template -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; - cout << "UDT with value " << value_ << " being destroyed" << endl; - } - 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( scoped_ptr& incomplete ) -{ - return incomplete.get(); -} - -Incomplete * check_incomplete( shared_ptr& incomplete, - shared_ptr& i2 ) -{ - incomplete.swap(i2); - cout << incomplete.use_count() << " " << incomplete.unique() << endl; - return incomplete.get(); -} -// main --------------------------------------------------------------------// - -// This isn't a very systematic test; it just hits some of the basics. - -int test_main( int, char ** ) { - - BOOST_TEST( UDT_use_count == 0 ); // reality check - - // test scoped_ptr with a built-in type - long * lp = new long; - scoped_ptr 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(sp.get()), 1234568901L ); - ck( lp, *sp ); - - sp.reset(); - BOOST_TEST( sp.get() == 0 ); - - // test scoped_ptr with a user defined type - scoped_ptr 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 ]; - scoped_array 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; - shared_ptr 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(cp.get()), 54321 ); - ck( static_cast(ip), *cp ); - - shared_ptr 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(cp2.get()), 54321 ); - ck( static_cast(ip), *cp2 ); - - shared_ptr 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 ); - BOOST_TEST( cp.use_count() == 1 ); - 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 ); - - shared_ptr cp4; - swap( cp2, cp4 ); - BOOST_TEST( cp4.use_count() == 3 ); - BOOST_TEST( *cp4 == 87654 ); - BOOST_TEST( cp2.get() == 0 ); - - set< shared_ptr > scp; - scp.insert(cp4); - BOOST_TEST( scp.find(cp4) != scp.end() ); - BOOST_TEST( scp.find(cp4) == scp.find( shared_ptr(cp4) ) ); - - // test shared_array with a built-in type - char * cap = new char [ 100 ]; - shared_array 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' ); - - shared_array ca2 ( ca ); - shared_array 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 ); - - shared_array 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 ); - - set< shared_array > sca; - sca.insert(ca4); - BOOST_TEST( sca.find(ca4) != sca.end() ); - BOOST_TEST( sca.find(ca4) == sca.find( shared_array(ca4) ) ); - - // test shared_array with user defined type - shared_array udta ( new UDT[3] ); - - udta[0].value( 111 ); - udta[1].value( 222 ); - udta[2].value( 333 ); - shared_array 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; - shared_ptr 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 ); - - shared_ptr 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 ); - - cout << "OK" << endl; - - new char[12345]; // deliberate memory leak to verify leaks detected - - return 0; - } // main - diff --git a/smarttests.htm b/smarttests.htm index dcd8fda..84fd16e 100644 --- a/smarttests.htm +++ b/smarttests.htm @@ -1,13 +1,15 @@ + + + -boost: smart pointer tests +Smart Pointer Timings - + + -

    c++boost.gif (8819 bytes)Smart -Pointers Timings

    +

    c++boost.gif (8819 bytes)Smart Pointer Timings

    In late January 2000, Mark Borgerding put forward a suggestion to boost for a new design of smart pointer whereby an intrusive doubly linked list is used @@ -19,9 +21,10 @@ Pointers Timings mailing list and the tests which this page describes were performed to provide a guide for current and future investigations into smart pointer implementation strategies.

    -

    Thanks are due to Dave Abrahams, - Gavin Collings, Greg Colvin and - Beman Dawes +

    Thanks are due to Dave Abrahams, +Gavin Collings, +Greg Colvin and +Beman Dawes for test code and trial implementations, the final version of which can be found in .zip format here.

    Description

    @@ -75,7 +78,7 @@ Pointers Timings   - + MSVC speed graph   @@ -85,7 +88,7 @@ Pointers Timings   - + GCC speed graph   @@ -530,7 +533,7 @@ Pointers Timings spreads its information as in the case of linked pointer.
    -

    Revised 19 Aug 2001 +

    Revised 19 August 2001

    © Copyright Gavin Collings 2000. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all diff --git a/weak_ptr.htm b/weak_ptr.htm new file mode 100644 index 0000000..fb97eac --- /dev/null +++ b/weak_ptr.htm @@ -0,0 +1,233 @@ + + + + + + +weak_ptr + + + + +

    c++boost.gif (8819 bytes)weak_ptr class template

    + +

    Introduction
    +Synopsis
    +Members
    +Free Functions
    +Example
    +Handle/Body Idiom
    +Frequently Asked Questions
    +Smart Pointer Timings

    + +

    Introduction

    + +

    The weak_ptr class template stores a pointer to an +object that's already managed by a shared_ptr. When the +object last shared_ptr to the object goes away and the object +is deleted, all weak_ptr objects have their stored pointers +set to 0.

    + +

    Every weak_ptr meets the CopyConstructible +and Assignable requirements of the C++ Standard Library, and so +can be used in standard library containers. Comparison operators +are supplied so that weak_ptr works with +the standard library's associative containers.

    + +

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

    + +

    Synopsis

    + +
    namespace boost {
    +
    +  template<typename T> class weak_ptr {
    +
    +    public:
    +      typedef T element_type;
    +
    +      explicit weak_ptr();
    +      template<typename Y> weak_ptr(shared_ptr<Y> const & r); // never throws
    +      ~weak_ptr(); // never throws
    +
    +      weak_ptr(weak_ptr const & r); // never throws
    +      template<typename Y> weak_ptr(weak_ptr<Y> const & r); // never throws
    +
    +      weak_ptr & operator=(weak_ptr const & r); // never throws  
    +      template<typename Y> weak_ptr & operator=(weak_ptr<Y> const & r); // never throws
    +      template<typename Y> weak_ptr & operator=(shared_ptr<Y> const & r); // never throws
    +
    +      void reset(); // never throws
    +
    +      T & operator*() const; // never throws
    +      T * operator->() const; // never throws
    +      T * get() const; // never throws
    +
    +      long use_count() const; // never throws
    +
    +      void swap(weak_ptr<T> & b); // never throws
    +  };
    +
    +  template<typename T, typename U>
    +    bool operator==(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +  template<typename T, typename U>
    +    bool operator!=(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +  template<typename T, typename U>
    +    bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +
    +  template<typename T> void swap(weak_ptr<T> & a, weak_ptr<T> & b); // never throws
    +
    +  template<typename T, typename U>
    +    weak_ptr<T> shared_static_cast(weak_ptr<U> const & r); // never throws
    +  template<typename T, typename U>
    +    weak_ptr<T> shared_dynamic_cast(weak_ptr<U> const & r);
    +
    +}
    + +

    Members

    + +

    element_type

    +
    typedef T element_type;
    +

    Provides the type of the stored pointer.

    + +

    constructors

    + +
    explicit weak_ptr();
    +

    Constructs a weak_ptr, with 0 as its stored pointer. +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, the constructor has no effect.

    + +
    template<typename Y> weak_ptr(shared_ptr<Y> const & r); // never throws
    +

    Constructs a weak_ptr, as if by storing a copy of the pointer stored in r. +Afterwards, the use count for all copies is unchanged. +When the last shared_ptr is destroyed, the use count and stored pointer become 0. +The only exception which may be thrown by this constructor is std::bad_alloc. +If an exception is thrown, the constructor has no effect.

    + +
    weak_ptr(weak_ptr const & r); // never throws
    +template<typename Y> weak_ptr(weak_ptr<Y> const & r); // never throws
    +

    Constructs a weak_ptr, as if by storing a copy of the +pointer stored in r.

    + +

    destructor

    + +
    ~weak_ptr(); // never throws
    +

    Destroys this weak_ptr but has no effect on the object its stored pointer points to. +T need not be a complete type. +See the smart pointer common requirements.

    + +

    assignment

    + +
    weak_ptr & operator=(weak_ptr const & r); // never throws
    +template<typename Y> weak_ptr & operator=(weak_ptr<Y> const & r); // never throws
    +template<typename Y> weak_ptr & operator=(shared_ptr<Y> const & r); // never throws
    +

    Constructs a new weak_ptr as described above, +then replaces this weak_ptr with the new one, destroying the replaced object.

    + +

    reset

    + +
    void reset();
    +

    Constructs a new weak_ptr as described above, +then replaces this weak_ptr with the new one, destroying the replaced object. +The only exception which may be thrown is std::bad_alloc. If +an exception is thrown, the reset has no effect.

    + +

    indirection

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

    Returns a reference to the object pointed to by the stored pointer. +Behavior is undefined if the stored pointer is 0. +Note that the stored pointer becomes 0 if all shared_ptr objects for that +pointer are destroyed.

    + +
    T * operator->() const; // never throws
    +

    Returns the stored pointer. +Behavior is undefined if the stored pointer is 0. +Note that the stored pointer becomes 0 if all shared_ptr objects for that +pointer are destroyed.

    + +

    get

    +
    T * get() const; // never throws
    +

    Returns the stored pointer. +Note that the stored pointer becomes 0 if all shared_ptr objects for that +pointer are destroyed. +T need not be a complete type. +See the smart pointer +common requirements.

    + +

    use_count

    +
    long use_count() const; // never throws
    +

    Returns the number of shared_ptr objects sharing ownership of the +stored pointer. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    Because use_count is not necessarily efficient to implement for +implementations of weak_ptr that do not use an explicit reference +count, it might be removed from some future version. Thus it should +be used for debugging purposes only, and get should be used for +production code.

    + +

    swap

    +
    void swap(weak_ptr & b); // never throws
    +

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

    + +

    Free Functions

    + +

    comparison

    +
    template<typename T, typename U>
    +  bool operator==(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +template<typename T, typename U>
    +  bool operator!=(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +template<typename T, typename U>
    +  bool operator<(weak_ptr<T> const & a, weak_ptr<U> const & b); // never throws
    +

    Compares the stored pointers of the two smart pointers. +T need not be a complete type. +See the smart pointer +common requirements.

    +

    The operator< overload is provided to define an ordering so that weak_ptr +objects can be used in associative containers such as std::map. +The implementation uses std::less<T*> to perform the +comparison. This ensures that the comparison is handled correctly, since the +standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 8).

    + +

    swap

    +
    template<typename T>
    +  void swap(weak_ptr<T> & a, weak_ptr<T> & b) // never throws
    +

    Equivalent to a.swap(b). Matches the interface of std::swap. +Provided as an aid to generic programming.

    + +

    shared_static_cast

    +
    template<typename T, typename U>
    +  weak_ptr<T> shared_static_cast(weak_ptr<U> const & r); // never throws
    +

    Perform a static_cast on the stored pointer, returning another weak_ptr. +The resulting smart pointer will share its use count with the original pointer.

    + +

    shared_dynamic_cast

    +
    template<typename T, typename U>
    +  weak_ptr<T> shared_dynamic_cast(weak_ptr<U> const & r);
    +

    Perform a dynamic_cast on the stored pointer, returning another weak_ptr. +The resulting smart pointer will share its use count with the original pointer unless the result of the +cast is 0. The only exception which may be thrown is std::bad_alloc, which may be thrown during the +construction of the new weak_ptr if the result of the cast is 0. If an exception is thrown, the +cast has no effect.

    + +
    + +

    Revised 1 February 2002

    + +

    Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. +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" without express or implied warranty, +and with no claim as to its suitability for any purpose.

    + + + + From ddd6d5442637e1f47f5b8eeb3c995d13f9d7b8e3 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sun, 3 Feb 2002 15:19:04 +0000 Subject: [PATCH 45/53] Need to include utility.hpp to get noncopyable [SVN r12669] --- scoped_ptr_example.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/scoped_ptr_example.hpp b/scoped_ptr_example.hpp index ae1fb8d..c4b6bf5 100644 --- a/scoped_ptr_example.hpp +++ b/scoped_ptr_example.hpp @@ -1,5 +1,6 @@ // Boost scoped_ptr_example header file ------------------------------------// +#include #include // The point of this example is to prove that even though From 590757e2b22b9a014ce37bc4d4902d8447115ed0 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sun, 3 Feb 2002 15:21:41 +0000 Subject: [PATCH 46/53] Don't give default arguments to function parameters what subsequent parameters do not have default arguments [SVN r12670] --- include/boost/shared_array.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/shared_array.hpp b/include/boost/shared_array.hpp index 4d400dc..3c8775f 100644 --- a/include/boost/shared_array.hpp +++ b/include/boost/shared_array.hpp @@ -75,7 +75,7 @@ public: this_type(p).swap(*this); } - template void reset(T * p = 0, D d) + template void reset(T * p, D d) { this_type(p, d).swap(*this); } From 758954a93f0ac047907ac58c4d3ea0a5df2580b1 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Sun, 3 Feb 2002 17:46:08 +0000 Subject: [PATCH 47/53] Documentation updates. [SVN r12675] --- shared_array.htm | 2 +- shared_ptr.htm | 2 +- weak_ptr.htm | 11 ----------- 3 files changed, 2 insertions(+), 13 deletions(-) diff --git a/shared_array.htm b/shared_array.htm index 8d21823..2b818e7 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -97,7 +97,7 @@ If an exception is thrown, delete[] p is called.

    template<typename D> shared_array(T * p, D d);

    Constructs a shared_array, storing a copy of p and of d. Afterwards, the use count is 1. -D's copy constructor must not throw. +D's copy constructor and destructor must not throw. When the the time comes to delete the array pointed to by p, the object d is used in the statement d(p). Invoking the object d with parameter p in this way must not throw. diff --git a/shared_ptr.htm b/shared_ptr.htm index fc6d0d6..7773c2b 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -118,7 +118,7 @@ If an exception is thrown, delete p is called.

    template<typename D> shared_ptr(T * p, D d);

    Constructs a shared_ptr, storing a copy of p and of d. Afterwards, the use count is 1. -D's copy constructor must not throw. +D's copy constructor and destructor must not throw. When the the time comes to delete the object pointed to by p, the object d is used in the statement d(p). Invoking the object d with parameter p in this way must not throw. diff --git a/weak_ptr.htm b/weak_ptr.htm index fb97eac..545195f 100644 --- a/weak_ptr.htm +++ b/weak_ptr.htm @@ -11,17 +11,6 @@

    c++boost.gif (8819 bytes)weak_ptr class template

    -

    Introduction
    -Synopsis
    -Members
    -Free Functions
    -Example
    -Handle/Body Idiom
    -Frequently Asked Questions
    -Smart Pointer Timings

    - -

    Introduction

    -

    The weak_ptr class template stores a pointer to an object that's already managed by a shared_ptr. When the object last shared_ptr to the object goes away and the object From ee3d3bd1e199033c4ee55030aed6aa069ef86e01 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Sun, 3 Feb 2002 17:50:11 +0000 Subject: [PATCH 48/53] Mention thread safety feature. [SVN r12676] --- compatibility.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compatibility.htm b/compatibility.htm index 65acfd7..fb4d0a5 100644 --- a/compatibility.htm +++ b/compatibility.htm @@ -58,7 +58,7 @@ Calling reset with a pointer to the object that's already owned by the

      -
    • The manipulation of use counts is now thread safe on Windows, Linux, and platforms +
    • The manipulation of use counts is now thread safe on Windows, Linux, and platforms that support pthreads. See the <boost/detail/atomic_count.hpp> file for details
    • From 6ed07733cba5f13abf631642a171aa9b3209a098 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Mon, 4 Feb 2002 08:00:20 +0000 Subject: [PATCH 49/53] Fix atomic_count.hpp includes, convert Macintosh newlines to Unix ones, since Metrowerks supports either and the rest of Boost uses Unix ones. [SVN r12683] --- include/boost/detail/atomic_count.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boost/detail/atomic_count.hpp b/include/boost/detail/atomic_count.hpp index 0f33ac4..6486755 100644 --- a/include/boost/detail/atomic_count.hpp +++ b/include/boost/detail/atomic_count.hpp @@ -91,15 +91,15 @@ typedef long atomic_count; #elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) -#include "atomic_count_win32.hpp" +#include #elif defined(linux) || defined(__linux) || defined(__linux__) -#include "atomic_count_linux.hpp" +#include #elif defined(BOOST_HAS_PTHREADS) -#include "atomic_count_pthreads.hpp" +#include #else From c29cc62d66616ef76e70f6d7250d868c4fcf1e78 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 4 Feb 2002 11:15:40 +0000 Subject: [PATCH 50/53] Minor tweaks. [SVN r12684] --- include/boost/detail/shared_array_nmt.hpp | 2 +- include/boost/detail/shared_ptr_nmt.hpp | 2 +- include/boost/scoped_array.hpp | 3 ++- include/boost/scoped_ptr.hpp | 3 ++- include/boost/shared_array.hpp | 2 +- include/boost/shared_ptr.hpp | 4 +++- include/boost/weak_ptr.hpp | 4 +++- weak_ptr.htm | 4 +--- 8 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/boost/detail/shared_array_nmt.hpp b/include/boost/detail/shared_array_nmt.hpp index 570af13..3ff1954 100644 --- a/include/boost/detail/shared_array_nmt.hpp +++ b/include/boost/detail/shared_array_nmt.hpp @@ -12,7 +12,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 for most recent version including documentation. +// See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation. // #include diff --git a/include/boost/detail/shared_ptr_nmt.hpp b/include/boost/detail/shared_ptr_nmt.hpp index 5364e77..79d5b5d 100644 --- a/include/boost/detail/shared_ptr_nmt.hpp +++ b/include/boost/detail/shared_ptr_nmt.hpp @@ -12,7 +12,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 for most recent version including documentation. +// See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. // #include diff --git a/include/boost/scoped_array.hpp b/include/boost/scoped_array.hpp index 74ecebe..c4fa3d0 100644 --- a/include/boost/scoped_array.hpp +++ b/include/boost/scoped_array.hpp @@ -9,7 +9,8 @@ // 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. +// See http://www.boost.org/libs/smart_ptr/scoped_array.htm for documentation. +// #include #include // in case ptrdiff_t not in std diff --git a/include/boost/scoped_ptr.hpp b/include/boost/scoped_ptr.hpp index 6648f09..e2a3ddf 100644 --- a/include/boost/scoped_ptr.hpp +++ b/include/boost/scoped_ptr.hpp @@ -9,7 +9,8 @@ // 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. +// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation. +// #include diff --git a/include/boost/shared_array.hpp b/include/boost/shared_array.hpp index 3c8775f..a820abc 100644 --- a/include/boost/shared_array.hpp +++ b/include/boost/shared_array.hpp @@ -12,7 +12,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 for most recent version including documentation. +// See http://www.boost.org/libs/smart_ptr/shared_array.htm for documentation. // #include // for broken compiler workarounds diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index d601bab..e6411c3 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -12,7 +12,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 for most recent version including documentation. +// See http://www.boost.org/libs/smart_ptr/shared_ptr.htm for documentation. // #include // for broken compiler workarounds @@ -109,7 +109,9 @@ public: shared_ptr(shared_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { if (px == 0) // need to allocate new counter -- the cast failed + { pn = detail::shared_count(static_cast(0), deleter()); + } } #ifndef BOOST_NO_AUTO_PTR diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp index 087ab12..f96856a 100644 --- a/include/boost/weak_ptr.hpp +++ b/include/boost/weak_ptr.hpp @@ -11,7 +11,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 for most recent version including documentation. +// See http://www.boost.org/libs/smart_ptr/weak_ptr.htm for documentation. // #include @@ -68,7 +68,9 @@ public: weak_ptr(weak_ptr const & r, detail::dynamic_cast_tag): px(dynamic_cast(r.px)), pn(r.pn) { if (px == 0) // need to allocate new counter -- the cast failed + { pn = detail::weak_count(); + } } template diff --git a/weak_ptr.htm b/weak_ptr.htm index 545195f..10a0c2c 100644 --- a/weak_ptr.htm +++ b/weak_ptr.htm @@ -90,9 +90,7 @@ If an exception is thrown, the constructor has no effect.

      template<typename Y> weak_ptr(shared_ptr<Y> const & r); // never throws

      Constructs a weak_ptr, as if by storing a copy of the pointer stored in r. Afterwards, the use count for all copies is unchanged. -When the last shared_ptr is destroyed, the use count and stored pointer become 0. -The only exception which may be thrown by this constructor is std::bad_alloc. -If an exception is thrown, the constructor has no effect.

      +When the last shared_ptr is destroyed, the use count and stored pointer become 0.

      weak_ptr(weak_ptr const & r); // never throws
       template<typename Y> weak_ptr(weak_ptr<Y> const & r); // never throws
      From 08df55159baad83c9ccfa9166bac4bd78d0d3383 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 4 Feb 2002 18:51:16 +0000 Subject: [PATCH 51/53] Fix broken links [SVN r12697] --- smart_ptr.htm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/smart_ptr.htm b/smart_ptr.htm index 0f5246d..682a675 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -29,27 +29,27 @@ thus responsible for deletion of the object when it is no longer needed.

      - + - + - + - + - +
      scoped_ptr<boost/scoped_ptr.hpp><boost/scoped_ptr.hpp> Simple sole ownership of single objects. Noncopyable.
      scoped_array<boost/scoped_array.hpp><boost/scoped_array.hpp> Simple sole ownership of arrays. Noncopyable.
      shared_ptr<boost/shared_ptr.hpp><boost/shared_ptr.hpp> Object ownership shared among multiple pointers
      shared_array<boost/shared_array.hpp><boost/shared_array.hpp> Array ownership shared among multiple pointers.
      weak_ptr<boost/weak_ptr.hpp><boost/weak_ptr.hpp> Non-owning observers of an object owned by shared_ptr.
      @@ -196,7 +196,7 @@ implementation.


      Revised 1 February 200204 February 2002

      Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. From e1567707b1f6921cd796236b08bac52dcdd259f4 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 4 Feb 2002 19:45:11 +0000 Subject: [PATCH 52/53] Tab removed :-) [SVN r12701] --- smart_ptr_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart_ptr_test.cpp b/smart_ptr_test.cpp index 8d41070..baee93d 100644 --- a/smart_ptr_test.cpp +++ b/smart_ptr_test.cpp @@ -27,7 +27,7 @@ bool boost_error(char const *, char const *, char const *, long) { - return true; // fail with assert() + return true; // fail with assert() } class Incomplete; From f9782387d9524b91092a7df63f4e8ccf4524c3e3 Mon Sep 17 00:00:00 2001 From: Darin Adler Date: Mon, 4 Feb 2002 22:52:48 +0000 Subject: [PATCH 53/53] Tweaks. [SVN r12715] --- include/boost/scoped_array.hpp | 6 +++--- shared_array.htm | 4 ++-- shared_ptr.htm | 4 ++-- smart_ptr.htm | 2 +- weak_ptr.htm | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/boost/scoped_array.hpp b/include/boost/scoped_array.hpp index c4fa3d0..6734c5e 100644 --- a/include/boost/scoped_array.hpp +++ b/include/boost/scoped_array.hpp @@ -27,7 +27,7 @@ template class scoped_array // noncopyable { private: - T* ptr; + T * ptr; scoped_array(scoped_array const &); scoped_array & operator=(scoped_array const &); @@ -57,14 +57,14 @@ public: } } - T& operator[](std::ptrdiff_t i) const // never throws + T & operator[](std::ptrdiff_t i) const // never throws { BOOST_ASSERT(ptr != 0); BOOST_ASSERT(i >= 0); return ptr[i]; } - T* get() const // never throws + T * get() const // never throws { return ptr; } diff --git a/shared_array.htm b/shared_array.htm index 2b818e7..12c353e 100644 --- a/shared_array.htm +++ b/shared_array.htm @@ -197,10 +197,10 @@ See the smart pointer common requirements.

      The operator< overload is provided to define an ordering so that shared_array objects can be used in associative containers such as std::map. -The implementation uses std::less<T*> to perform the +The implementation uses std::less<T *> to perform the comparison. This ensures that the comparison is handled correctly, since the standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] -paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] paragraph 8).

      swap

      diff --git a/shared_ptr.htm b/shared_ptr.htm index 7773c2b..42e23fe 100644 --- a/shared_ptr.htm +++ b/shared_ptr.htm @@ -230,10 +230,10 @@ See the smart pointer common requirements.

      The operator< overload is provided to define an ordering so that shared_ptr objects can be used in associative containers such as std::map. -The implementation uses std::less<T*> to perform the +The implementation uses std::less<T *> to perform the comparison. This ensures that the comparison is handled correctly, since the standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] -paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] paragraph 8).

      swap

      diff --git a/smart_ptr.htm b/smart_ptr.htm index 682a675..c6782a4 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -196,7 +196,7 @@ implementation.


      Revised 04 February 20024 February 2002

      Copyright 1999 Greg Colvin and Beman Dawes. Copyright 2002 Darin Adler. diff --git a/weak_ptr.htm b/weak_ptr.htm index 10a0c2c..a28232b 100644 --- a/weak_ptr.htm +++ b/weak_ptr.htm @@ -178,10 +178,10 @@ See the smart pointer common requirements.

      The operator< overload is provided to define an ordering so that weak_ptr objects can be used in associative containers such as std::map. -The implementation uses std::less<T*> to perform the +The implementation uses std::less<T *> to perform the comparison. This ensures that the comparison is handled correctly, since the standard mandates that relational operations on pointers are unspecified (5.9 [expr.rel] -paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] +paragraph 2) but std::less<> on pointers is well-defined (20.3.3 [lib.comparisons] paragraph 8).

      swap