3.0.0
Freundlich's C++ toolkit
Public Types | Public Member Functions
fcppt::weak_ptr< Type, Deleter > Class Template Reference

Detailed Description

template<typename Type, typename Deleter>
class fcppt::weak_ptr< Type, Deleter >

A weak reference to an object owned by a shared pointer.

A weak pointer contributes to the weak reference count of on object owned by shared pointers. The difference is that a weak pointer won't stop the object from being destructed. Such a weak pointer can be used to observe if there are any shared pointers left.

Template Parameters
TypeThe type the weak pointer points to
DeleterA deleter class that must be callable with a pointer to Type.

Public Types

typedef std::weak_ptr< Type > impl_type
 The type if the std::weak_ptr used to implement this class. More...
 
typedef fcppt::shared_ptr< Type, Deleter > shared_ptr
 The corresponding shared pointer class. More...
 
typedef Type element_type
 The element type, which is Type. More...
 

Public Member Functions

 weak_ptr ()
 Constructs an empty weak ptr. More...
 
template<typename Other >
 weak_ptr (fcppt::weak_ptr< Other, Deleter > const &ref)
 Constructs a weak_ptr from a compatible weak_ptr type. More...
 
template<typename Other >
 weak_ptr (fcppt::shared_ptr< Other, Deleter > const &ref)
 Constructs a weak_ptr from a compatible shared_ptr. More...
 
fcppt::optional::object< shared_ptrlock () const
 Returns a shared_ptr pointing to the shared object of this weak_ptr. More...
 
long use_count () const noexcept
 The use count. More...
 
bool expired () const noexcept
 Returns if the weak_ptr still points to a shared object. More...
 
void swap (weak_ptr &other) noexcept
 Swaps the weak_ptr. More...
 
impl_type std_ptr () const
 Returns the underlying std::weak_ptr object. More...
 

Member Typedef Documentation

◆ element_type

template<typename Type , typename Deleter >
typedef Type fcppt::weak_ptr< Type, Deleter >::element_type

The element type, which is Type.

◆ impl_type

template<typename Type , typename Deleter >
typedef std::weak_ptr< Type > fcppt::weak_ptr< Type, Deleter >::impl_type

The type if the std::weak_ptr used to implement this class.

◆ shared_ptr

template<typename Type , typename Deleter >
typedef fcppt::shared_ptr< Type, Deleter > fcppt::weak_ptr< Type, Deleter >::shared_ptr

The corresponding shared pointer class.

Constructor & Destructor Documentation

◆ weak_ptr() [1/3]

template<typename Type , typename Deleter >
fcppt::weak_ptr< Type, Deleter >::weak_ptr ( )

Constructs an empty weak ptr.

◆ weak_ptr() [2/3]

template<typename Type , typename Deleter >
template<typename Other >
fcppt::weak_ptr< Type, Deleter >::weak_ptr ( fcppt::weak_ptr< Other, Deleter > const &  ref)

Constructs a weak_ptr from a compatible weak_ptr type.

Constructs a weak_ptr from the weak_ptr ref. This weak_ptr will belong to the same shared_ptrs as ref does.

Template Parameters
OtherA type, so that Other * is implicitly convertible to Type *
Parameters
refThe weak_ptr to copy

◆ weak_ptr() [3/3]

template<typename Type , typename Deleter >
template<typename Other >
fcppt::weak_ptr< Type, Deleter >::weak_ptr ( fcppt::shared_ptr< Other, Deleter > const &  ref)

Constructs a weak_ptr from a compatible shared_ptr.

Constructs a weak_ptr from the shared_ptr ref which will keep track of the shared count of the shared_ptr.

Template Parameters
OtherA type, so that Other * is implicitly convertible to Type *
Parameters
refThe shared_ptr to monitor

Member Function Documentation

◆ expired()

template<typename Type , typename Deleter >
bool fcppt::weak_ptr< Type, Deleter >::expired ( ) const
noexcept

Returns if the weak_ptr still points to a shared object.

If this weak_ptr is empty, false will be returned. Otherwise if the shared count is still greater than zero, true will be returned.

◆ lock()

template<typename Type , typename Deleter >
fcppt::optional::object< shared_ptr > fcppt::weak_ptr< Type, Deleter >::lock ( ) const

Returns a shared_ptr pointing to the shared object of this weak_ptr.

If the shared object is still alive (which means that the reference count is still greater than zero), then a new shared_ptr also owning that object will be returned. If all shared_ptrs have been destroyed, then an empty optional will be returned.

◆ std_ptr()

template<typename Type , typename Deleter >
impl_type fcppt::weak_ptr< Type, Deleter >::std_ptr ( ) const

Returns the underlying std::weak_ptr object.

◆ swap()

template<typename Type , typename Deleter >
void fcppt::weak_ptr< Type, Deleter >::swap ( weak_ptr< Type, Deleter > &  other)
noexcept

Swaps the weak_ptr.

Swaps the weak_ptr with other.

Parameters
otherThe shared_ptr to swap with

◆ use_count()

template<typename Type , typename Deleter >
long fcppt::weak_ptr< Type, Deleter >::use_count ( ) const
noexcept

The use count.

If this weak_ptr is empty, zero will be returned. Otherwise the shared count of the shared object will be returned.

Note
This type is long because std::shared_ptr also uses long.