BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bsl::weak_ptr< ELEMENT_TYPE > Class Template Reference

#include <bslstl_sharedptr.h>

Public Types

typedef bsl::remove_extent< ELEMENT_TYPE >::type element_type
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (weak_ptr< ELEMENT_TYPE >, bsl::is_nothrow_move_constructible)
 
BSLS_KEYWORD_CONSTEXPR weak_ptr () BSLS_KEYWORD_NOEXCEPT
 
 weak_ptr (BloombergLP::bslmf::MovableRef< weak_ptr > original) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DECLARE_IF_COMPATIBLE>
 weak_ptr (BloombergLP::bslmf::MovableRef< weak_ptr< COMPATIBLE_TYPE > > other) BSLS_KEYWORD_NOEXCEPT
 
 weak_ptr (const weak_ptr &original) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DECLARE_IF_COMPATIBLE>
 weak_ptr (const shared_ptr< COMPATIBLE_TYPE > &other) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DECLARE_IF_COMPATIBLE>
 weak_ptr (const weak_ptr< COMPATIBLE_TYPE > &other) BSLS_KEYWORD_NOEXCEPT
 
 ~weak_ptr ()
 
weak_ptroperator= (BloombergLP::bslmf::MovableRef< weak_ptr > rhs) BSLS_KEYWORD_NOEXCEPT
 
weak_ptroperator= (const weak_ptr &rhs) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr & >::type operator= (BloombergLP::bslmf::MovableRef< weak_ptr< COMPATIBLE_TYPE > > rhs) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr & >::type operator= (const shared_ptr< COMPATIBLE_TYPE > &rhs) BSLS_KEYWORD_NOEXCEPT
 
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr & >::type operator= (const weak_ptr< COMPATIBLE_TYPE > &rhs) BSLS_KEYWORD_NOEXCEPT
 
void reset () BSLS_KEYWORD_NOEXCEPT
 
void swap (weak_ptr &other) BSLS_KEYWORD_NOEXCEPT
 
bool expired () const BSLS_KEYWORD_NOEXCEPT
 
shared_ptr< ELEMENT_TYPE > lock () const BSLS_KEYWORD_NOEXCEPT
 
template<class ANY_TYPE >
bool owner_before (const shared_ptr< ANY_TYPE > &other) const BSLS_KEYWORD_NOEXCEPT
 
template<class ANY_TYPE >
bool owner_before (const weak_ptr< ANY_TYPE > &other) const BSLS_KEYWORD_NOEXCEPT
 
template<class ANY_TYPE >
bool owner_equal (const shared_ptr< ANY_TYPE > &other) const BSLS_KEYWORD_NOEXCEPT
 
template<class ANY_TYPE >
bool owner_equal (const weak_ptr< ANY_TYPE > &other) const BSLS_KEYWORD_NOEXCEPT
 
size_t owner_hash () const BSLS_KEYWORD_NOEXCEPT
 
BloombergLP::bslma::SharedPtrRep * rep () const BSLS_KEYWORD_NOEXCEPT
 
long use_count () const BSLS_KEYWORD_NOEXCEPT
 
shared_ptr< ELEMENT_TYPE > acquireSharedPtr () const BSLS_KEYWORD_NOEXCEPT
 
int numReferences () const BSLS_KEYWORD_NOEXCEPT
 
template<class CONVERTIBLE_TYPE BSLSTL_SHAREDPTR_DEFINE_IF_CONVERTIBLE>
 weak_ptr (BloombergLP::bslmf::MovableRef< weak_ptr< CONVERTIBLE_TYPE > > original) BSLS_KEYWORD_NOEXCEPT
 

Friends

template<class COMPATIBLE_TYPE >
class weak_ptr
 
struct BloombergLP::bslstl::SharedPtr_ImpUtil
 

Detailed Description

template<class ELEMENT_TYPE>
class bsl::weak_ptr< ELEMENT_TYPE >

This class provides a mechanism to create weak references to reference-counted shared (shared_ptr) objects. A weak reference provides conditional access to a shared object managed by a shared_ptr, but, unlike a shared (or "strong") reference, does not affect the shared object's lifetime.

See bslstl_sharedptr

Member Typedef Documentation

◆ element_type

template<class ELEMENT_TYPE >
typedef bsl::remove_extent<ELEMENT_TYPE>::type bsl::weak_ptr< ELEMENT_TYPE >::element_type

For weak pointers to non-array types, element_type is an alias to the ELEMENT_TYPE template parameter. Otherwise, it is an alias to the type contained in the array.

Constructor & Destructor Documentation

◆ weak_ptr() [1/7]

template<class ELEMENT_TYPE >
BSLS_KEYWORD_CONSTEXPR bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( )
inline

Create a weak pointer in the empty state and referring to no object, i.e., a weak pointer having no representation.

◆ weak_ptr() [2/7]

template<class ELEMENT_TYPE >
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( BloombergLP::bslmf::MovableRef< weak_ptr< ELEMENT_TYPE > >  original)

Create a weak pointer that refers to the same object (if any) as the specified original weak pointer, and reset original to an empty state.

◆ weak_ptr() [3/7]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DECLARE_IF_COMPATIBLE>
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( BloombergLP::bslmf::MovableRef< weak_ptr< COMPATIBLE_TYPE > >  other)

Create a weak pointer that refers to the same object (if any) as the specified other weak pointer, and reset original to an empty state. Note that this operation does not involve any change to reference counts.

◆ weak_ptr() [4/7]

template<class ELEMENT_TYPE >
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( const weak_ptr< ELEMENT_TYPE > &  original)

Create a weak pointer that refers to the same object (if any) as the specified original weak pointer, and increment the number of weak references to the object managed by original (if any). Note that if original is in the empty state, this weak pointer will be initialized to the empty state.

◆ weak_ptr() [5/7]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DEFINE_IF_COMPATIBLE>
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( const shared_ptr< COMPATIBLE_TYPE > &  other)

Create a weak pointer that refers to the same object (if any) as the specified other (shared or weak) pointer of the (template parameter) COMPATIBLE_TYPE, and increment the number of weak references to the object managed by other (if any). If COMPATIBLE_TYPE * is not implicitly convertible to ELEMENT_TYPE *, then a compiler diagnostic will be emitted. Note that if other is in the empty state, this weak pointer will be initialized to the empty state.

◆ weak_ptr() [6/7]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE BSLSTL_SHAREDPTR_DEFINE_IF_COMPATIBLE>
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( const weak_ptr< COMPATIBLE_TYPE > &  other)

◆ ~weak_ptr()

template<class ELEMENT_TYPE >
bsl::weak_ptr< ELEMENT_TYPE >::~weak_ptr ( )
inline

Destroy this weak pointer object. If this weak pointer manages a (possibly shared) object, release the weak reference to that object.

◆ weak_ptr() [7/7]

template<class ELEMENT_TYPE >
template<class CONVERTIBLE_TYPE BSLSTL_SHAREDPTR_DEFINE_IF_CONVERTIBLE>
bsl::weak_ptr< ELEMENT_TYPE >::weak_ptr ( BloombergLP::bslmf::MovableRef< weak_ptr< CONVERTIBLE_TYPE > >  original)

Member Function Documentation

◆ acquireSharedPtr()

template<class ELEMENT_TYPE >
shared_ptr< ELEMENT_TYPE > bsl::weak_ptr< ELEMENT_TYPE >::acquireSharedPtr ( ) const
inline

Return a shared pointer to the object referred to by this weak pointer and managing the same object as that managed by this weak pointer (if any) if false == expired(), and a shared pointer in the empty state otherwise. Note that the behavior of this method is the same as that of lock.

◆ BSLMF_NESTED_TRAIT_DECLARATION()

template<class ELEMENT_TYPE >
bsl::weak_ptr< ELEMENT_TYPE >::BSLMF_NESTED_TRAIT_DECLARATION ( weak_ptr< ELEMENT_TYPE >  ,
bsl::is_nothrow_move_constructible   
)

◆ expired()

template<class ELEMENT_TYPE >
bool bsl::weak_ptr< ELEMENT_TYPE >::expired ( ) const
inline

Return true if this weak pointer is in the empty state or the object that it originally referenced has been destroyed, and false otherwise.

◆ lock()

template<class ELEMENT_TYPE >
shared_ptr< ELEMENT_TYPE > bsl::weak_ptr< ELEMENT_TYPE >::lock ( ) const

Return a shared pointer to the object referred to by this weak pointer if false == expired(), and a shared pointer in the empty state otherwise.

◆ numReferences()

template<class ELEMENT_TYPE >
int bsl::weak_ptr< ELEMENT_TYPE >::numReferences ( ) const
inline

[DEPRECATED] Use use_count instead.

Return a "snapshot" of the current number of shared pointers that share ownership of the object referred to by this weak pointer, or 0 if this weak pointer is in the empty state. Note that the behavior of this method is the same as that of use_count , and the result may be unreliable in multi-threaded code for the same reasons.

◆ operator=() [1/5]

template<class ELEMENT_TYPE >
weak_ptr< ELEMENT_TYPE > & bsl::weak_ptr< ELEMENT_TYPE >::operator= ( BloombergLP::bslmf::MovableRef< weak_ptr< ELEMENT_TYPE > >  rhs)

Make this weak pointer refer to the same object (if any) as the specified rhs weak pointer. If rhs is not a reference to this weak pointer, decrement the number of weak references to the object this weak pointer managed (if any), and reset rhs to an empty state. Return a reference providing modifiable access to this weak pointer. Note that if rhs is in an empty state, this weak pointer will be set to an empty state.

◆ operator=() [2/5]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr< ELEMENT_TYPE > & >::type bsl::weak_ptr< ELEMENT_TYPE >::operator= ( BloombergLP::bslmf::MovableRef< weak_ptr< COMPATIBLE_TYPE > >  rhs)

Make this weak pointer refer to the same object (if any) as the specified rhs weak pointer. Decrement the number of weak references to the object this weak pointer managed (if any), and reset rhs to an empty state. Return a reference providing modifiable access to this weak pointer. This function does not exist unless a pointer to (the template parameter) COMPATIBLE_TYPE is convertible to a pointer to (the template parameter) ELEMENT_TYPE. Note that if rhs is in an empty state, this weak pointer will be set to an empty state.

◆ operator=() [3/5]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr< ELEMENT_TYPE > & >::type bsl::weak_ptr< ELEMENT_TYPE >::operator= ( const shared_ptr< COMPATIBLE_TYPE > &  rhs)

◆ operator=() [4/5]

template<class ELEMENT_TYPE >
weak_ptr< ELEMENT_TYPE > & bsl::weak_ptr< ELEMENT_TYPE >::operator= ( const weak_ptr< ELEMENT_TYPE > &  rhs)

Make this weak pointer refer to the same object (if any) as the specified rhs weak pointer. Decrement the number of weak references to the object this weak pointer manages (if any), and increment the number of weak references to the object managed by rhs (if any). Return a reference providing modifiable access to this weak pointer. Note that if rhs is in an empty state, this weak pointer will be set to an empty state.

◆ operator=() [5/5]

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE >
enable_if< is_convertible< COMPATIBLE_TYPE *, ELEMENT_TYPE * >::value, weak_ptr< ELEMENT_TYPE > & >::type bsl::weak_ptr< ELEMENT_TYPE >::operator= ( const weak_ptr< COMPATIBLE_TYPE > &  rhs)

Make this weak pointer refer to the same object (if any) as the specified rhs (shared or weak) pointer to the (template parameter) COMPATIBLE_TYPE. Decrement the number of weak references to the object to which this weak pointer currently manages (if any), and increment the number of weak references to the object managed by rhs (if any). Return a reference providing modifiable access to this weak pointer. If COMPATIBLE_TYPE * is not implicitly convertible to TYPE *, then a compiler diagnostic will be emitted. Note that if rhs is in the empty state, this weak pointer will be set to the empty state.

◆ owner_before() [1/2]

template<class ELEMENT_TYPE >
template<class ANY_TYPE >
bool bsl::weak_ptr< ELEMENT_TYPE >::owner_before ( const shared_ptr< ANY_TYPE > &  other) const
inline

◆ owner_before() [2/2]

template<class ELEMENT_TYPE >
template<class ANY_TYPE >
bool bsl::weak_ptr< ELEMENT_TYPE >::owner_before ( const weak_ptr< ANY_TYPE > &  other) const
inline

Return true if the address of the BloombergLP::bslma::SharedPtrRep object used by this weak pointer is ordered before the address of the BloombergLP::bslma::SharedPtrRep object used by the specified other shared pointer under the total ordering defined by std::less<BloombergLP::bslma::SharedPtrRep *>, and false otherwise.

◆ owner_equal() [1/2]

template<class ELEMENT_TYPE >
template<class ANY_TYPE >
bool bsl::weak_ptr< ELEMENT_TYPE >::owner_equal ( const shared_ptr< ANY_TYPE > &  other) const
inline

◆ owner_equal() [2/2]

template<class ELEMENT_TYPE >
template<class ANY_TYPE >
bool bsl::weak_ptr< ELEMENT_TYPE >::owner_equal ( const weak_ptr< ANY_TYPE > &  other) const
inline

Return true if the address of the BloombergLP::bslma::SharedPtrRep object used by this shared pointer is equal to the address of the BloombergLP::bslma::SharedPtrRep object used by the specified other shared pointer, and false otherwise.

◆ owner_hash()

template<class ELEMENT_TYPE >
size_t bsl::weak_ptr< ELEMENT_TYPE >::owner_hash ( ) const
inline

Return an unspecified value such that, for any object x where owner_equal(x) is true, owner_hash() == x.owner_hash() is true. Note that this is based on the hash of the address of the BloombergLP::bslma::SharedPtrRep object used by this object. Note also that for two empty smart pointers x and y, x.owner_hash() == y.owner_hash() is true.

◆ rep()

template<class ELEMENT_TYPE >
BloombergLP::bslma::SharedPtrRep * bsl::weak_ptr< ELEMENT_TYPE >::rep ( ) const
inline

Return the address providing modifiable access to the BloombergLP::bslma::SharedPtrRep object held by this weak pointer, or 0 if this weak pointer is in the empty state.

◆ reset()

template<class ELEMENT_TYPE >
void bsl::weak_ptr< ELEMENT_TYPE >::reset ( )
inline

Reset this weak pointer to the empty state. If this weak pointer manages a (possibly shared) object, then decrement the number of weak references to that object.

◆ swap()

template<class ELEMENT_TYPE >
void bsl::weak_ptr< ELEMENT_TYPE >::swap ( weak_ptr< ELEMENT_TYPE > &  other)
inline

Efficiently exchange the states of this weak pointer and the specified other weak pointer such that each will refer to the object (if any) and representation (if any) formerly referred to and managed by the other.

◆ use_count()

template<class ELEMENT_TYPE >
long bsl::weak_ptr< ELEMENT_TYPE >::use_count ( ) const
inline

Return a "snapshot" of the current number of shared pointers that share ownership of the object referred to by this weak pointer, or 0 if this weak pointer is in the empty state. Note that any result other than 0 may be unreliable in a multi-threaded program, where another pointer sharing ownership in a different thread may be copied or destroyed, or another weak pointer may be locked in the case that 1 is returned (that would otherwise indicate unique ownership).

Friends And Related Symbol Documentation

◆ BloombergLP::bslstl::SharedPtr_ImpUtil

template<class ELEMENT_TYPE >
friend struct BloombergLP::bslstl::SharedPtr_ImpUtil
friend

◆ weak_ptr

template<class ELEMENT_TYPE >
template<class COMPATIBLE_TYPE >
friend class weak_ptr
friend

This friend declaration provides access to the internal data members while constructing a weak pointer from a weak pointer of a different type.


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