throwing_ptr
Smart pointers that throw on dereference if null
|
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced. More...
Public Types | |
typedef std::shared_ptr< T >::element_type | element_type |
the type pointed to. More... | |
Public Member Functions | |
TSP_CONSTEXPR | shared_ptr () TSP_NOEXCEPT=default |
Constructs a shared_ptr with no managed object, i.e. empty shared_ptr. More... | |
TSP_CONSTEXPR | shared_ptr (std::nullptr_t ptr) TSP_NOEXCEPT |
Constructs a shared_ptr with no managed object, i.e. empty shared_ptr. More... | |
template<class Y > | |
shared_ptr (Y *ptr) | |
Constructs a shared_ptr with ptr as the pointer to the managed object. More... | |
template<class Y , class Deleter > | |
shared_ptr (Y *ptr, Deleter d) | |
Constructs a shared_ptr with ptr as the pointer to the managed object. More... | |
template<class Deleter > | |
shared_ptr (std::nullptr_t ptr, Deleter d) | |
Constructs a shared_ptr with ptr as the pointer to the managed object. More... | |
template<class Y , class Deleter , class Alloc > | |
shared_ptr (Y *ptr, Deleter d, Alloc alloc) | |
Constructs a shared_ptr with ptr as the pointer to the managed object. More... | |
template<class Y > | |
shared_ptr (const shared_ptr< Y > &r, element_type *ptr) TSP_NOEXCEPT | |
The aliasing constructor. More... | |
shared_ptr (const shared_ptr &r) TSP_NOEXCEPT | |
Constructs a shared_ptr which shares ownership of the object managed by r. More... | |
template<class Y > | |
shared_ptr (const shared_ptr< Y > &r) TSP_NOEXCEPT | |
Constructs a shared_ptr which shares ownership of the object managed by r. More... | |
shared_ptr (shared_ptr &&r) TSP_NOEXCEPT | |
Move-constructs a shared_ptr from r. More... | |
template<class Y > | |
shared_ptr (shared_ptr< Y > &&r) TSP_NOEXCEPT | |
Move-constructs a shared_ptr from r. More... | |
shared_ptr (const std::shared_ptr< T > &r) TSP_NOEXCEPT | |
Constructs a shared_ptr which shares ownership of the object managed by r. More... | |
template<class Y > | |
shared_ptr (const std::shared_ptr< Y > &r) TSP_NOEXCEPT | |
Constructs a shared_ptr which shares ownership of the object managed by r. More... | |
shared_ptr (std::shared_ptr< T > &&r) TSP_NOEXCEPT | |
Move-constructs a shared_ptr from r. More... | |
template<class Y > | |
shared_ptr (std::shared_ptr< Y > &&r) TSP_NOEXCEPT | |
Move-constructs a shared_ptr from r. More... | |
template<class Y > | |
shared_ptr (const std::weak_ptr< Y > &r) | |
Constructs a shared_ptr which shares ownership of the object managed by r. More... | |
template<class Y , class Deleter > | |
shared_ptr (std::unique_ptr< Y, Deleter > &&r) | |
Constructs a shared_ptr which manages the object currently managed by r. More... | |
~shared_ptr ()=default | |
Destructor If *this owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than its previous value. More... | |
shared_ptr & | operator= (const shared_ptr &r) TSP_NOEXCEPT |
Assignment operator. More... | |
template<class Y > | |
shared_ptr & | operator= (const shared_ptr< Y > &r) TSP_NOEXCEPT |
Assignment operator. More... | |
shared_ptr & | operator= (shared_ptr &&r) TSP_NOEXCEPT |
Assignment operator. More... | |
template<class Y > | |
shared_ptr & | operator= (shared_ptr< Y > &&r) TSP_NOEXCEPT |
Assignment operator. More... | |
template<class Y , class Deleter > | |
shared_ptr & | operator= (std::unique_ptr< Y, Deleter > &&r) |
Assignment operator. More... | |
void | swap (shared_ptr &r) TSP_NOEXCEPT |
Exchanges the contents of *this and r. More... | |
void | reset () TSP_NOEXCEPT |
Releases the ownership of the managed object, if any. More... | |
template<class Y > | |
void | reset (Y *ptr) |
Replaces the managed object with an object pointed to by ptr. More... | |
template<class Y , class Deleter > | |
void | reset (Y *ptr, Deleter d) |
Replaces the managed object with an object pointed to by ptr. More... | |
template<class Y , class Deleter , class Alloc > | |
void | reset (Y *ptr, Deleter d, Alloc alloc) |
Replaces the managed object with an object pointed to by ptr. More... | |
element_type * | get () const TSP_NOEXCEPT |
Returns the stored pointer. More... | |
const std::shared_ptr< T > & | get_std_shared_ptr () const TSP_NOEXCEPT |
Returns the underlying std::shared_pointer. More... | |
std::shared_ptr< T > & | get_std_shared_ptr () TSP_NOEXCEPT |
Returns the underlying std::shared_pointer. More... | |
T & | operator* () const |
Dereferences the stored pointer. More... | |
T * | operator-> () const |
Dereferences the stored pointer. More... | |
long | use_count () const TSP_NOEXCEPT |
Returns the number of different shared_ptr instances (this included) managing the current object. More... | |
operator bool () const TSP_NOEXCEPT | |
Checks if *this stores a non-null pointer, i.e. whether get() != nullptr. More... | |
template<class Y > | |
bool | owner_before (const shared_ptr< Y > &other) const TSP_NOEXCEPT |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More... | |
template<class Y > | |
bool | owner_before (const std::shared_ptr< Y > &other) const TSP_NOEXCEPT |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More... | |
template<class Y > | |
bool | owner_before (const std::weak_ptr< Y > &other) const TSP_NOEXCEPT |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. More... | |
Friends | |
template<typename Y > | |
class | shared_ptr |
Wrapper aroung std::shared_ptr that throws when a wrapped null pointer is dereferenced.
throwing::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:
The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.
A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators. The managed pointer is the one passed to the deleter when use count reaches zero.
A shared_ptr may also own no objects, in which case it is called empty (an empty shared_ptr may have a non-null stored pointer if the aliasing constructor was used to create it).
All specializations of shared_ptr meet the requirements of CopyConstructible, CopyAssignable, and LessThanComparable and are contextually convertible to bool.
All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr overloads of atomic functions can be used to prevent the data race.
The underlying std::shared_ptr is available via get_std_shared_ptr()
Definition at line 63 of file shared_ptr.hpp.
typedef std::shared_ptr<T>::element_type throwing::shared_ptr< T >::element_type |
the type pointed to.
Definition at line 66 of file shared_ptr.hpp.
|
default |
Constructs a shared_ptr with no managed object, i.e. empty shared_ptr.
|
inline |
Constructs a shared_ptr with no managed object, i.e. empty shared_ptr.
Definition at line 79 of file shared_ptr.hpp.
|
inlineexplicit |
Constructs a shared_ptr with ptr as the pointer to the managed object.
Definition at line 84 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr with ptr as the pointer to the managed object.
Uses the specified deleter d as the deleter.
The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.
The construction of d and of the stored deleter from d must not throw exceptions.
Definition at line 98 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr with ptr as the pointer to the managed object.
Uses the specified deleter d as the deleter.
The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.
The construction of d and of the stored deleter from d must not throw exceptions.
Definition at line 112 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr with ptr as the pointer to the managed object.
Uses the specified deleter d as the deleter.
The expression d(ptr) must be well formed, have well-defined behavior and not throw any exceptions.
The construction of d and of the stored deleter from d must not throw exceptions.
Uses a copy of alloc for allocation of data for internal use.
Alloc must be a Allocator.
Definition at line 130 of file shared_ptr.hpp.
|
inline |
The aliasing constructor.
Constructs a shared_ptr which shares ownership information with r, but holds an unrelated and unmanaged pointer ptr. Even if this shared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed by r.
However, calling get() on this will always return a copy of ptr.
It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()
Definition at line 147 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr which shares ownership of the object managed by r.
If r manages no object, *this manages no object too.
Definition at line 155 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr which shares ownership of the object managed by r.
If r manages no object, *this manages no object too.
Definition at line 163 of file shared_ptr.hpp.
|
inline |
Move-constructs a shared_ptr from r.
After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.
Definition at line 170 of file shared_ptr.hpp.
|
inline |
Move-constructs a shared_ptr from r.
After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.
Definition at line 178 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr which shares ownership of the object managed by r.
If r manages no object, *this manages no object too.
Definition at line 185 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr which shares ownership of the object managed by r.
If r manages no object, *this manages no object too.
Definition at line 193 of file shared_ptr.hpp.
|
inline |
Move-constructs a shared_ptr from r.
After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.
Definition at line 200 of file shared_ptr.hpp.
|
inline |
Move-constructs a shared_ptr from r.
After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null.
Definition at line 208 of file shared_ptr.hpp.
|
inlineexplicit |
Constructs a shared_ptr which shares ownership of the object managed by r.
Y* must be implicitly convertible to T*. (until C++17)
This overload only participates in overload resolution if Y* is compatible with T*. (since C++17)
Note that r.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr in that case.
Definition at line 223 of file shared_ptr.hpp.
|
inline |
Constructs a shared_ptr which manages the object currently managed by r.
The deleter associated with r is stored for future deletion of the managed object.
r manages no object after the call.
This overload doesn't participate in overload resolution if std::unique_ptr<Y, Deleter>::pointer is not compatible with T*. If r.get() is a null pointer, this overload is equivalent to the default constructor (1). (since C++17)
If Deleter is a reference type, equivalent to shared_ptr(r.release(), std::ref(r.get_deleter()). Otherwise, equivalent to shared_ptr(r.release(), r.get_deleter())
Definition at line 243 of file shared_ptr.hpp.
|
default |
Destructor If *this owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than its previous value.
Notes:
Unlike throwing::unique_ptr, the throwing of std::shared_ptr is invoked even if the managed pointer is null.
|
inline |
|
inline |
Returns the underlying std::shared_pointer.
Definition at line 411 of file shared_ptr.hpp.
|
inline |
Returns the underlying std::shared_pointer.
Definition at line 417 of file shared_ptr.hpp.
|
inlineexplicit |
Checks if *this stores a non-null pointer, i.e. whether get() != nullptr.
Definition at line 475 of file shared_ptr.hpp.
|
inline |
Dereferences the stored pointer.
null_ptr_exception<T> | if the pointer is null |
Definition at line 423 of file shared_ptr.hpp.
|
inline |
Dereferences the stored pointer.
null_ptr_exception<T> | if the pointer is null |
Definition at line 434 of file shared_ptr.hpp.
|
inline |
Assignment operator.
Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.
Shares ownership of the object managed by r. If r manages no object, *this manages no object too. Equivalent to shared_ptr<T>(r).swap(*this).
Definition at line 270 of file shared_ptr.hpp.
|
inline |
Assignment operator.
Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.
Shares ownership of the object managed by r. If r manages no object, *this manages no object too. Equivalent to shared_ptr<T>(r).swap(*this).
Definition at line 287 of file shared_ptr.hpp.
|
inline |
Assignment operator.
Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.
Move-assigns a shared_ptr from r. After the assignment, *this contains a copy of the previous state of r, r is empty. Equivalent to shared_ptr<T>(std::move(r)).swap(*this)
Definition at line 303 of file shared_ptr.hpp.
|
inline |
Assignment operator.
Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.
Move-assigns a shared_ptr from r. After the assignment, *this contains a copy of the previous state of r, r is empty. Equivalent to shared_ptr<T>(std::move(r)).swap(*this)
Definition at line 319 of file shared_ptr.hpp.
|
inline |
Assignment operator.
Replaces the managed object with the one managed by r. If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.
Transfers the ownership of the object managed by r to *this. The deleter associated to r is stored for future deletion of the managed object. r manages no object after the call. Equivalent to shared_ptr<T>(std::move(r)).swap(*this).
Definition at line 337 of file shared_ptr.hpp.
|
inline |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.
The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.
Definition at line 489 of file shared_ptr.hpp.
|
inline |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.
The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.
Definition at line 505 of file shared_ptr.hpp.
|
inline |
Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order.
The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)
This ordering is used to make shared and weak pointers usable as keys in associative containers, typically through std::owner_less.
Definition at line 521 of file shared_ptr.hpp.
|
inline |
Releases the ownership of the managed object, if any.
If *this already owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. If the object pointed to by ptr is already owned, the function results in undefined behavior.
After the call, *this manages no object. Equivalent to shared_ptr().swap(*this);
Definition at line 356 of file shared_ptr.hpp.
Replaces the managed object with an object pointed to by ptr.
Y must be a complete type and implicitly convertible to T.
Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions.
Equivalent to shared_ptr<T>(ptr).swap(*this);
Definition at line 368 of file shared_ptr.hpp.
|
inline |
Replaces the managed object with an object pointed to by ptr.
Y must be a complete type and implicitly convertible to T.
Uses the specified deleter d as the deleter. Deleter must be callable for the type T, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter must be CopyConstructible, and its copy constructor and destructor must not throw exceptions.
Equivalent to shared_ptr<T>(ptr, d).swap(*this);
Definition at line 381 of file shared_ptr.hpp.
|
inline |
Replaces the managed object with an object pointed to by ptr.
Y must be a complete type and implicitly convertible to T.
Uses the specified deleter d as the deleter. Deleter must be callable for the type T, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter must be CopyConstructible, and its copy constructor and destructor must not throw exceptions.
Additionally uses a copy of alloc for allocation of data for internal use. Alloc must be a Allocator. The copy constructor and destructor must not throw exceptions.
Equivalent to shared_ptr<T>(ptr, d, alloc).swap(*this);
Definition at line 401 of file shared_ptr.hpp.
|
inline |
Exchanges the contents of *this and r.
Definition at line 344 of file shared_ptr.hpp.
|
inline |
Returns the number of different shared_ptr instances (this included) managing the current object.
If there is no managed object, 0 is returned.
In multithreaded environment, the value returned by use_count is approximate (typical implementations use a memory_order_relaxed load)
Definition at line 470 of file shared_ptr.hpp.
Definition at line 69 of file shared_ptr.hpp.