4.6.0
Freundlich's C++ toolkit
|
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.
Type | The type the shared pointer points to |
Deleter | A deleter class that must be callable with a pointer to Type. |
Public Types | |
using | impl_type = std::shared_ptr<Type> |
The type of the std::shared_ptr used to implement this class. | |
using | element_type = typename impl_type::element_type |
The element type, which is Type. | |
using | value_type = element_type |
Same as element_type. | |
using | pointer = element_type * |
The pointer type, same as value_type * | |
using | reference = std::add_lvalue_reference_t<element_type> |
The reference type, same as value_type & | |
using | count_type |
The reference count type. | |
Public Member Functions | |
template<typename Other > | |
shared_ptr (Other *pointer) | |
Constructs a shared_ptr from a compatible pointer type. | |
template<typename Other , typename Alloc > | |
shared_ptr (Other *pointer, Alloc const &allocator) | |
Constructs a shared_ptr from a compatible pointer type and allocator. | |
template<typename Other > | |
shared_ptr (fcppt::weak_ptr< Other, Deleter > const &ref) | |
Constructs a shared_ptr from a compatible weak_ptr. | |
shared_ptr (shared_ptr const &) | |
shared_ptr (shared_ptr &&) noexcept | |
template<typename Other > | |
shared_ptr (fcppt::shared_ptr< Other, Deleter > const &ref) | |
Constructs a shared_ptr from a compatible shared_ptr. | |
template<typename Other > | |
shared_ptr (fcppt::shared_ptr< Other > const &ref, pointer data) | |
Constructs a shared_ptr that shares ownership with another. | |
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. | |
shared_ptr & | operator= (shared_ptr const &) |
shared_ptr & | operator= (shared_ptr &&) noexcept |
template<typename Other > | |
shared_ptr & | operator= (fcppt::shared_ptr< Other, Deleter > const &ref) |
Assigns a shared_ptr from a compatible shared_ptr. | |
template<typename Other > | |
shared_ptr & | operator= (fcppt::unique_ptr< Other, Deleter > &&ref) |
Assigns a shared_ptr from a compatible unique_ptr. | |
~shared_ptr () noexcept | |
Destroys this shared_ptr. | |
reference | operator* () const |
Dereferences the owned pointer. | |
pointer | operator-> () const |
Dereferences a member of the owned object. | |
pointer | get_pointer () const |
Returns a pointer to the owned object. | |
bool | unique () const |
Returns if this shared_ptr is the only owner of the current object. | |
count_type | use_count () const |
The use count. | |
void | swap (shared_ptr &other) noexcept |
Swaps the shared_ptr. | |
impl_type | std_ptr () const |
Returns the underlying std::shared_ptr object. | |
template<typename Other > | |
shared_ptr (fcppt::detail::make_shared_wrapper< Other > &&) | |
Friends | |
template<typename Other , typename OtherDeleter > | |
class | shared_ptr |
class | fcppt::weak_ptr< Type, Deleter > |
using fcppt::shared_ptr< Type, Deleter >::count_type |
The reference count type.
using fcppt::shared_ptr< Type, Deleter >::element_type = typename impl_type::element_type |
The element type, which is Type.
using fcppt::shared_ptr< Type, Deleter >::impl_type = std::shared_ptr<Type> |
The type of the std::shared_ptr
used to implement this class.
using fcppt::shared_ptr< Type, Deleter >::pointer = element_type * |
The pointer type, same as value_type *
using fcppt::shared_ptr< Type, Deleter >::reference = std::add_lvalue_reference_t<element_type> |
The reference type, same as value_type &
using fcppt::shared_ptr< Type, Deleter >::value_type = element_type |
Same as element_type.
|
explicit |
Constructs a shared_ptr from a compatible pointer type.
Contructs a shared_ptr from pointer, taking ownership over it.
Other | A type, so that Other * is implicitly convertible to Type * |
pointer | The pointer to take ownership of |
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.
Other | A type, so that Other * is implicitly convertible to Type * |
Alloc | An allocator type |
pointer | The pointer to take ownership of |
allocator | The allocator to use for reference counting |
|
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.
Other | A type, so that Other * is implicitly convertible to Type * |
ref | The weak_ptr to copy from |
fcppt::shared_ptr< Type, Deleter >::shared_ptr | ( | shared_ptr< Type, Deleter > const & | ) |
|
noexcept |
|
explicit |
Constructs a shared_ptr from a compatible shared_ptr.
Other | A type, so that Other * is convertible to Type * |
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.
Other | A type, so that Other * is convertible to Type * |
ref | The shared pointer to share ownership with |
data | The pointer this shared_ptr will point to |
|
explicit |
|
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.
Other | A type, so that Other * is implicitly convertible to Type * |
ref | The unique_ptr to take ownership from |
|
noexcept |
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().
|
explicit |
|
nodiscard |
Returns a pointer to the owned object.
Returns a pointer to the owned object or the null pointer if the shared_ptr is empty.
|
nodiscard |
Dereferences the owned pointer.
Returns a reference to the owned object.
|
nodiscard |
Dereferences a member of the owned object.
Returns a pointer to the owned object.
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.
Other | A type, so that Other * is implicitly convertible to Type * |
ref | The shared_ptr to assign from |
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.
Other | A type, so that Other * is implicitly convertible to T * |
ref | The shared_ptr to assign from |
|
noexcept |
shared_ptr & fcppt::shared_ptr< Type, Deleter >::operator= | ( | shared_ptr< Type, Deleter > const & | ) |
|
nodiscard |
Returns the underlying std::shared_ptr
object.
|
noexcept |
|
nodiscard |
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.
|
nodiscard |
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.
long
because std::shared_ptr
also uses long
.
|
friend |
|
friend |