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

Detailed Description

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

A shared pointer class that gets the deleter as a template parameter.

A shared pointer shares ownership of a single pointer with other shared pointers. How many shared pointers actually own a pointer is kept track of as a reference count. Copying shared pointers increases the count by one, while destroying shared pointers decrases the count by one. If the count reaches zero, the object that is pointed to will be destroyed.

The class is implemented using std::shared_ptr, so it will inherit most of its traits. This means that the class also uses type erasure for its deleter and internal ref count.

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

Public Types

typedef std::shared_ptr< Type > impl_type
 The type of the std::shared_ptr used to implement this class. More...
 
typedef impl_type::element_type element_type
 The element type, which is Type. More...
 
typedef element_type value_type
 Same as element_type. More...
 
typedef element_typepointer
 The pointer type, same as value_type * More...
 
typedef std::add_lvalue_reference_t< element_typereference
 The reference type, same as value_type & More...
 

Public Member Functions

template<typename Other >
 shared_ptr (Other *pointer)
 Constructs a shared_ptr from a compatible pointer type. More...
 
template<typename Other , typename Alloc >
 shared_ptr (Other *pointer, Alloc const &allocator)
 Constructs a shared_ptr from a compatible pointer type and allocator. More...
 
template<typename Other >
 shared_ptr (fcppt::weak_ptr< Other, Deleter > const &ref)
 Constructs a shared_ptr from a compatible weak_ptr. More...
 
 shared_ptr (shared_ptr const &)
 
 shared_ptr (shared_ptr &&)
 
template<typename Other >
 shared_ptr (fcppt::shared_ptr< Other, Deleter > const &ref)
 Constructs a shared_ptr from a compatible shared_ptr. More...
 
template<typename Other >
 shared_ptr (fcppt::shared_ptr< Other > const &ref, pointer data)
 Constructs a shared_ptr that shares ownership with another. More...
 
template<typename Other >
 shared_ptr (std::unique_ptr< Other, Deleter > &&ref)
 
template<typename Other >
 shared_ptr (fcppt::unique_ptr< Other, Deleter > &&ref)
 Constructs a shared_ptr from a compatible unique_ptr. More...
 
shared_ptroperator= (shared_ptr const &)
 
shared_ptroperator= (shared_ptr &&)
 
template<typename Other >
shared_ptroperator= (fcppt::shared_ptr< Other, Deleter > const &ref)
 Assigns a shared_ptr from a compatible shared_ptr. More...
 
template<typename Other >
shared_ptroperator= (fcppt::unique_ptr< Other, Deleter > &&ref)
 Assigns a shared_ptr from a compatible unique_ptr. More...
 
 ~shared_ptr ()
 Destroys this shared_ptr. More...
 
reference operator* () const
 Dereferences the owned pointer. More...
 
pointer operator-> () const
 Dereferences a member of the owned object. More...
 
pointer get_pointer () const
 Returns a pointer to the owned object. More...
 
bool unique () const
 Returns if this shared_ptr is the only owner of the current object. More...
 
long use_count () const
 The use count. More...
 
void swap (shared_ptr &other)
 Swaps the shared_ptr. More...
 
impl_type std_ptr () const
 Returns the underlying std::shared_ptr object. More...
 

Friends

template<typename Other , typename OtherDeleter >
class shared_ptr
 
class fcppt::weak_ptr< Type, Deleter >
 

Member Typedef Documentation

◆ element_type

template<typename Type , typename Deleter >
typedef impl_type::element_type fcppt::shared_ptr< Type, Deleter >::element_type

The element type, which is Type.

◆ impl_type

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

The type of the std::shared_ptr used to implement this class.

◆ pointer

template<typename Type , typename Deleter >
typedef element_type* fcppt::shared_ptr< Type, Deleter >::pointer

The pointer type, same as value_type *

◆ reference

template<typename Type , typename Deleter >
typedef std::add_lvalue_reference_t< element_type > fcppt::shared_ptr< Type, Deleter >::reference

The reference type, same as value_type &

◆ value_type

template<typename Type , typename Deleter >
typedef element_type fcppt::shared_ptr< Type, Deleter >::value_type

Same as element_type.

Constructor & Destructor Documentation

◆ shared_ptr() [1/9]

template<typename Type , typename Deleter >
template<typename Other >
fcppt::shared_ptr< Type, Deleter >::shared_ptr ( Other *  pointer)
explicit

Constructs a shared_ptr from a compatible pointer type.

Contructs a shared_ptr from pointer, taking ownership over it.

Template Parameters
OtherA type, so that Other * is implicitly convertible to Type *
Parameters
pointerThe pointer to take ownership of

◆ shared_ptr() [2/9]

template<typename Type , typename Deleter >
template<typename Other , typename Alloc >
fcppt::shared_ptr< Type, Deleter >::shared_ptr ( Other *  pointer,
Alloc const &  allocator 
)

Constructs a shared_ptr from a compatible pointer type and allocator.

Contructs a shared_ptr from pointer, taking ownership over it, also using allocator to manage the reference counts.

Template Parameters
OtherA type, so that Other * is implicitly convertible to Type *
AllocAn allocator type
Parameters
pointerThe pointer to take ownership of
allocatorThe allocator to use for reference counting

◆ shared_ptr() [3/9]

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

Constructs a shared_ptr from a compatible weak_ptr.

Constructs a shared_ptr from the weak_ptr ref. If the weak_ptr still refers to a shared_ptr, then this constructor will behave as if the copy constructor with that shared_ptr was invoked instead. Otherwise, the shared_ptr will be constructed as empty.

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

◆ shared_ptr() [4/9]

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

◆ shared_ptr() [5/9]

template<typename Type , typename Deleter >
fcppt::shared_ptr< Type, Deleter >::shared_ptr ( shared_ptr< Type, Deleter > &&  )

◆ shared_ptr() [6/9]

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

Constructs a shared_ptr from a compatible shared_ptr.

Template Parameters
OtherA type, so that Other * is convertible to Type *

◆ shared_ptr() [7/9]

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

Constructs a shared_ptr that shares ownership with another.

Constructs a shared_ptr that shares ownership with ref and stores data. This is useful for implementing dynamic_pointer_cast and so on.

Template Parameters
OtherA type, so that Other * is convertible to Type *
Parameters
refThe shared pointer to share ownership with
dataThe pointer this shared_ptr will point to

◆ shared_ptr() [8/9]

template<typename Type , typename Deleter >
template<typename Other >
fcppt::shared_ptr< Type, Deleter >::shared_ptr ( std::unique_ptr< Other, Deleter > &&  ref)
explicit

◆ shared_ptr() [9/9]

template<typename Type , typename Deleter >
template<typename Other >
fcppt::shared_ptr< Type, Deleter >::shared_ptr ( fcppt::unique_ptr< Other, Deleter > &&  ref)
explicit

Constructs a shared_ptr from a compatible unique_ptr.

Constructs a shared_ptr from the unique_ptr ref. If the unique_ptr holds a pointer, then this shared_ptr will take ownership. Otherwise, the shared_ptr will be empy.

Template Parameters
OtherA type, so that Other * is implicitly convertible to Type *
Parameters
refThe unique_ptr to take ownership from

◆ ~shared_ptr()

template<typename Type , typename Deleter >
fcppt::shared_ptr< Type, Deleter >::~shared_ptr ( )

Destroys this shared_ptr.

If this shared_ptr is empty, nothing happens. Otherwise, the shared count will be decreased by one, possibly leading to the destruction of the object. Deletion will be done calling Deleter().

Member Function Documentation

◆ get_pointer()

template<typename Type , typename Deleter >
pointer fcppt::shared_ptr< Type, Deleter >::get_pointer ( ) const

Returns a pointer to the owned object.

Returns a pointer to the owned object or the null pointer if the shared_ptr is empty.

◆ operator*()

template<typename Type , typename Deleter >
reference fcppt::shared_ptr< Type, Deleter >::operator* ( ) const

Dereferences the owned pointer.

Returns a reference to the owned object.

Warning
The behaviour is undefined if the shared_ptr is empty.

◆ operator->()

template<typename Type , typename Deleter >
pointer fcppt::shared_ptr< Type, Deleter >::operator-> ( ) const

Dereferences a member of the owned object.

Returns a pointer to the owned object.

Warning
The behaviour is undefined if the shared_ptr is empty.

◆ operator=() [1/4]

template<typename Type , typename Deleter >
shared_ptr& fcppt::shared_ptr< Type, Deleter >::operator= ( shared_ptr< Type, Deleter > const &  )

◆ operator=() [2/4]

template<typename Type , typename Deleter >
shared_ptr& fcppt::shared_ptr< Type, Deleter >::operator= ( shared_ptr< Type, Deleter > &&  )

◆ operator=() [3/4]

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

Assigns a shared_ptr from a compatible shared_ptr.

Assigns this shared_ptr from ref. In any case, the reference count of the current shared_ptr will be decreased by one, possibly leading to the destruction of the object. If ref is empty, then this shared_ptr will also be empty. Otherwise, the shared count of ref will be increased by one and this shared_ptr will also take ownership.

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

◆ operator=() [4/4]

template<typename Type , typename Deleter >
template<typename Other >
shared_ptr& fcppt::shared_ptr< Type, Deleter >::operator= ( fcppt::unique_ptr< Other, Deleter > &&  ref)

Assigns a shared_ptr from a compatible unique_ptr.

Assigns this shared_ptr from ref. In any case, the reference count of the current shared_ptr will be decreased by one, possibly leading to the destruction of the object. If ref is empty, then this shared_ptr will also be empty. Otherwise, this shared_ptr will take onwerhsip of the pointer from ref.

Template Parameters
OtherA type, so that Other * is implicitly convertible to T *
Parameters
refThe shared_ptr to assign from

◆ std_ptr()

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

Returns the underlying std::shared_ptr object.

◆ swap()

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

Swaps the shared_ptr.

Swaps the shared_ptr with other.

Parameters
otherThe shared_ptr to swap with

◆ unique()

template<typename Type , typename Deleter >
bool fcppt::shared_ptr< Type, Deleter >::unique ( ) const

Returns if this shared_ptr is the only owner of the current object.

If the shared_ptr is empty, the behaviour is unspecified. Otherwise, true will be returned if this shared_ptr is the only shared_ptr that takes part in the ownership of the currently owned object.

◆ use_count()

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

The use count.

If this shared_ptr is empty, 0 will be returned. Otherwise, the number of shared_ptr objects owning the currently owned object will be returned.

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

Friends And Related Function Documentation

◆ shared_ptr

template<typename Type , typename Deleter >
template<typename Other , typename OtherDeleter >
friend class shared_ptr
friend

◆ fcppt::weak_ptr< Type, Deleter >

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