A std::shared_ptr<T>-like type that integrates with pw::Allocator.
This is a RAII smart pointer that deallocates any memory it points to when every pointer to the same object has gone out of scope.
Notable differences from std::shared_ptr<T> include:
T*. Use Allocator::MakeShared<T>(...) instead.SharedPtr from a UniquePtr takes an lvalue-reference, and is fallible..| T | The type being pointed to. This may be an array type, e.g. pw::SharedPtr<T[]>. |
Public Types | |
| using | element_type = typename Base::element_type |
| using | weak_type = WeakPtr< T > |
Public Member Functions | |
| ~SharedPtr () | |
| Releases any currently-held value. | |
| constexpr | SharedPtr () noexcept=default |
| constexpr | SharedPtr (element_type *value, ControlBlock *control_block) |
| constexpr | SharedPtr (std::nullptr_t) noexcept |
| constexpr | SharedPtr (const SharedPtr &other) noexcept |
Copy-constructs a SharedPtr<T> from a SharedPtr<T>. | |
| template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> | |
| constexpr | SharedPtr (const SharedPtr< U > &other) noexcept |
| template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> | |
| SharedPtr (SharedPtr< U > &&other) noexcept | |
| SharedPtr (UniquePtr< T > &owned) noexcept | |
| constexpr SharedPtr & | operator= (const SharedPtr &other) noexcept |
| template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> | |
| constexpr SharedPtr & | operator= (const SharedPtr< U > &other) noexcept |
| template<typename U , typename = std::enable_if_t<std::is_assignable_v<T*&, U*>>> | |
| SharedPtr & | operator= (SharedPtr< U > &&other) noexcept |
| SharedPtr & | operator= (std::nullptr_t) noexcept |
| template<typename U > | |
| constexpr | operator SharedPtr< U > () const |
| constexpr size_t | size () const |
| constexpr int32_t | use_count () const |
| template<typename PtrType > | |
| bool | owner_before (const PtrType &other) const noexcept |
| void | reset () noexcept |
| void | swap (SharedPtr &other) noexcept |
| Swaps the managed pointer and deallocator of this and another object. | |
| Allocator * | allocator () const |
| ControlBlock * | GetControlBlock (const ControlBlockHandle &) const |
| template<typename... Args> | |
| SharedPtr< T > | Create (Allocator *allocator, Args &&... args) |
| template<typename U , typename > | |
| constexpr SharedPtr< T > & | operator= (const SharedPtr< U > &other) noexcept |
| template<typename U , typename > | |
| SharedPtr< T > & | operator= (SharedPtr< U > &&other) noexcept |
Static Public Member Functions | |
| template<typename... Args> | |
| static SharedPtr | Create (Allocator *allocator, Args &&... args) |
| static SharedPtr | Create (Allocator *allocator, size_t count, size_t alignment) |
Friends | |
| template<typename > | |
| class | WeakPtr |
| template<typename To , typename From > | |
| constexpr SharedPtr< To > | static_pointer_cast (const SharedPtr< From > &p) noexcept |
| template<typename To , typename From > | |
| constexpr SharedPtr< To > | const_pointer_cast (const SharedPtr< From > &p) noexcept |
| constexpr bool | operator== (const SharedPtr &lhs, std::nullptr_t) |
| constexpr bool | operator== (std::nullptr_t, const SharedPtr &rhs) |
| constexpr bool | operator== (const SharedPtr &lhs, const SharedPtr &rhs) |
| constexpr bool | operator!= (const SharedPtr &lhs, std::nullptr_t) |
| constexpr bool | operator!= (std::nullptr_t, const SharedPtr &rhs) |
| constexpr bool | operator!= (const SharedPtr &lhs, const SharedPtr &rhs) |
|
constexprdefaultnoexcept |
Creates an empty (nullptr) instance.
NOTE: Instances of this type are most commonly constructed using Allocator::MakeShared.
|
inlineconstexpr |
Constructs a SharedPtr from an already-allocated value.
NOTE: Instances of this type are most commonly constructed using Allocator::MakeShared.
|
inlineconstexprnoexcept |
Creates an empty (nullptr) instance.
NOTE: Instances of this type are most commonly constructed using Allocator::MakeShared.
|
inlineconstexprnoexcept |
|
inlinenoexcept |
|
noexcept |
Constructs a SharedPtr<T> from a UniquePtr<T>.
NOTE: This constructor differs from SharedPtr(std::unique_ptr&&) in that it takes an l-value reference to the UniquePtr. This constructor must allocate a shared pointer control block. If that allocation fails, the UniquePtr is unmodified and still retains ownership of its object, and an empty SharedPtr is returned. If the allocation succeeds, the UniquePtr will be reset and a SharedPtr to the object will be returned.
|
inline |
|
static |
Constructs and object of type T from the given args, and wraps it in a SharedPtr.
The returned value may contain null if allocating memory for the object fails. Callers must check for null before using the SharedPtr.
NOTE: Instances of this type are most commonly constructed using Allocator::MakeShared.
| [in] | args | Arguments passed to the object constructor. |
|
static |
Constructs an array of count objects, and wraps it in a UniquePtr
The returned value may contain null if allocating memory for the object fails. Callers must check for null before using the UniquePtr.
NOTE: Instances of this type are most commonly constructed using Allocator::MakeShared.
| [in] | allocator | Used to allocate memory. |
| [in] | count | Number of objects to allocate. |
| [in] | alignment | Alignment requirement for the array. |
|
inline |
Returns the control block for this shared pointer.
Requires the caller to be able to construct a control block handle, which effectively restricts this to upstream Pigweed.
|
inlineexplicitconstexpr |
Explicit conversion operator for downcasting.
If an arbitrary type A derives from another type B, a shared pointer to A can be automatically upcast to one of type B. This operator performs the reverse operation with an explicit cast.
|
inlineconstexprnoexcept |
|
constexprnoexcept |
|
noexcept |
|
noexcept |
|
inlinenoexcept |
Checks whether this precedes other based on an ordering of their control blocks.
|
noexcept |
Resets this object to an empty state.
If this was the last shared pointer to the associated object, it is destroyed. If this is the last shared or weak pointer associated with the control block, it is deallocated.
|
inlineconstexpr |
Returns the number of elements allocated.
This will fail to compile if it is called on a non-array type SharedPtr.
|
inlineconstexpr |
Returns the number of shared pointers to the associated object, or 0 if this object is empty.
|
friend |
Creates a new SharedPtr by const casting the given shared pointer.
For an arbitrary type A, a shared pointer to A can be automatically converted to one of type const A. This function can perform the reverse operation.
|
friend |
Creates a new SharedPtr by static casting the given shared pointer.
If an arbitrary type A derives from another type B, a shared pointer to A can be automatically upcast to one of type B. This function can perform the reverse operation and allows specifying only the element type.