4.6.0
Freundlich's C++ toolkit
Loading...
Searching...
No Matches
fcppt::weak_ptr< Type, Deleter > Class Template Reference

Detailed Description

template<typename Type, typename Deleter = fcppt::default_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

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

Public Member Functions

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

Member Typedef Documentation

◆ count_type

template<typename Type , typename Deleter = fcppt::default_deleter>
using fcppt::weak_ptr< Type, Deleter >::count_type
Initial value:
long

The reference count type.

◆ element_type

template<typename Type , typename Deleter = fcppt::default_deleter>
using fcppt::weak_ptr< Type, Deleter >::element_type = Type

The element type, which is Type.

◆ impl_type

template<typename Type , typename Deleter = fcppt::default_deleter>
using fcppt::weak_ptr< Type, Deleter >::impl_type = std::weak_ptr<Type>

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

◆ shared_ptr

template<typename Type , typename Deleter = fcppt::default_deleter>
using fcppt::weak_ptr< Type, Deleter >::shared_ptr = fcppt::shared_ptr<Type, Deleter>

The corresponding shared pointer class.

Constructor & Destructor Documentation

◆ weak_ptr() [1/3]

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

Constructs an empty weak ptr.

◆ weak_ptr() [2/3]

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

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 = fcppt::default_deleter>
template<typename Other >
fcppt::weak_ptr< Type, Deleter >::weak_ptr ( fcppt::shared_ptr< Other, Deleter > const & ref)
explicit

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 = fcppt::default_deleter>
bool fcppt::weak_ptr< Type, Deleter >::expired ( ) const
nodiscardnoexcept

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::default_deleter>
fcppt::optional::object< shared_ptr > fcppt::weak_ptr< Type, Deleter >::lock ( ) const
nodiscard

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 = fcppt::default_deleter>
impl_type fcppt::weak_ptr< Type, Deleter >::std_ptr ( ) const
nodiscard

Returns the underlying std::weak_ptr object.

◆ swap()

template<typename Type , typename Deleter = fcppt::default_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 = fcppt::default_deleter>
count_type fcppt::weak_ptr< Type, Deleter >::use_count ( ) const
nodiscardnoexcept

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.