Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::interprocess::weak_ptr< T, A, D > Class Template Reference

The weak_ptr class template stores a "weak reference" to an object that's already managed by a shared_ptr. More...

#include <weak_ptr.hpp>

Inheritance diagram for boost::interprocess::weak_ptr< T, A, D >:

Public Types

typedef T element_type
 
typedef T value_type
 

Public Member Functions

 weak_ptr ()
 Effects: Constructs an empty weak_ptr. More...
 
template<class Y >
 weak_ptr (weak_ptr< Y, A, D > const &r)
 Effects: If r is empty, constructs an empty weak_ptr; otherwise, constructs a weak_ptr that shares ownership with r as if by storing a copy of the pointer stored in r. More...
 
template<class Y >
 weak_ptr (shared_ptr< Y, A, D > const &r)
 Effects: If r is empty, constructs an empty weak_ptr; otherwise, constructs a weak_ptr that shares ownership with r as if by storing a copy of the pointer stored in r. More...
 
template<class Y >
weak_ptroperator= (weak_ptr< Y, A, D > const &r)
 Effects: Equivalent to weak_ptr(r).swap(*this). More...
 
template<class Y >
weak_ptroperator= (shared_ptr< Y, A, D > const &r)
 Effects: Equivalent to weak_ptr(r).swap(*this). More...
 
shared_ptr< T, A, D > lock () const
 Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this). More...
 
long use_count () const
 Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects that share ownership with *this. More...
 
bool expired () const
 Returns: Returns: use_count() == 0. More...
 
void reset ()
 Effects: Equivalent to: weak_ptr().swap(*this). More...
 
void swap (this_type &other)
 Effects: Exchanges the contents of the two smart pointers. More...
 
template<class T2 , class A2 , class D2 >
bool _internal_less (weak_ptr< T2, A2, D2 > const &rhs) const
 
template<class Y >
void _internal_assign (const ipcdetail::shared_count< Y, A, D > &pn2)
 

Friends

template<class T2 , class A2 , class D2 >
class shared_ptr
 
template<class T2 , class A2 , class D2 >
class weak_ptr
 

Detailed Description

template<class T, class A, class D>
class boost::interprocess::weak_ptr< T, A, D >

The weak_ptr class template stores a "weak reference" to an object that's already managed by a shared_ptr.

To access the object, a weak_ptr can be converted to a shared_ptr using the shared_ptr constructor or the member function lock. When the last shared_ptr to the object goes away and the object is deleted, the attempt to obtain a shared_ptr from the weak_ptr instances that refer to the deleted object will fail: the constructor will throw an exception of type bad_weak_ptr, and weak_ptr::lock will return an empty shared_ptr.

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.

weak_ptr operations never throw exceptions.

The class template is parameterized on T, the type of the object pointed to.

Member Typedef Documentation

template<class T, class A, class D>
typedef T boost::interprocess::weak_ptr< T, A, D >::element_type
template<class T, class A, class D>
typedef T boost::interprocess::weak_ptr< T, A, D >::value_type

Constructor & Destructor Documentation

template<class T, class A, class D>
boost::interprocess::weak_ptr< T, A, D >::weak_ptr ( )
inline

Effects: Constructs an empty weak_ptr.

Postconditions: use_count() == 0.

template<class T, class A, class D>
template<class Y >
boost::interprocess::weak_ptr< T, A, D >::weak_ptr ( weak_ptr< Y, A, D > const &  r)
inline

Effects: If r is empty, constructs an empty weak_ptr; otherwise, constructs a weak_ptr that shares ownership with r as if by storing a copy of the pointer stored in r.

Postconditions: use_count() == r.use_count().

Throws: nothing.

template<class T, class A, class D>
template<class Y >
boost::interprocess::weak_ptr< T, A, D >::weak_ptr ( shared_ptr< Y, A, D > const &  r)
inline

Effects: If r is empty, constructs an empty weak_ptr; otherwise, constructs a weak_ptr that shares ownership with r as if by storing a copy of the pointer stored in r.

Postconditions: use_count() == r.use_count().

Throws: nothing.

Member Function Documentation

template<class T, class A, class D>
template<class Y >
void boost::interprocess::weak_ptr< T, A, D >::_internal_assign ( const ipcdetail::shared_count< Y, A, D > &  pn2)
inline
template<class T, class A, class D>
template<class T2 , class A2 , class D2 >
bool boost::interprocess::weak_ptr< T, A, D >::_internal_less ( weak_ptr< T2, A2, D2 > const &  rhs) const
inline
template<class T, class A, class D>
bool boost::interprocess::weak_ptr< T, A, D >::expired ( ) const
inline

Returns: Returns: use_count() == 0.

Throws: nothing.

Notes: expired() may be faster than use_count().

Referenced by boost::interprocess::weak_ptr< element_type, A, D >::lock().

template<class T, class A, class D>
shared_ptr<T, A, D> boost::interprocess::weak_ptr< T, A, D >::lock ( ) const
inline

Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).

Throws: nothing.

template<class T, class A, class D>
template<class Y >
weak_ptr& boost::interprocess::weak_ptr< T, A, D >::operator= ( weak_ptr< Y, A, D > const &  r)
inline

Effects: Equivalent to weak_ptr(r).swap(*this).

Throws: nothing.

Notes: The implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary.

template<class T, class A, class D>
template<class Y >
weak_ptr& boost::interprocess::weak_ptr< T, A, D >::operator= ( shared_ptr< Y, A, D > const &  r)
inline

Effects: Equivalent to weak_ptr(r).swap(*this).

Throws: nothing.

Notes: The implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary.

template<class T, class A, class D>
void boost::interprocess::weak_ptr< T, A, D >::reset ( )
inline

Effects: Equivalent to: weak_ptr().swap(*this).

template<class T, class A, class D>
void boost::interprocess::weak_ptr< T, A, D >::swap ( this_type other)
inline

Effects: Exchanges the contents of the two smart pointers.

Throws: nothing.

template<class T, class A, class D>
long boost::interprocess::weak_ptr< T, A, D >::use_count ( ) const
inline

Returns: 0 if *this is empty; otherwise, the number of shared_ptr objects that share ownership with *this.

Throws: nothing.

Notes: use_count() is not necessarily efficient. Use only for debugging and testing purposes, not for production code.

Friends And Related Function Documentation

template<class T, class A, class D>
template<class T2 , class A2 , class D2 >
friend class weak_ptr
friend
template<class T, class A, class D>
template<class T2 , class A2 , class D2 >
friend class shared_ptr
friend

The documentation for this class was generated from the following files: