storage_ptr
A smart pointer to a memory resource.
Synopsis
Defined in header <boost/json/storage_ptr.hpp>.
class storage_ptr;
Member Functions
Name | Description |
---|---|
Return a pointer to the memory resource. |
|
Return |
|
Return |
|
Return |
|
Return a reference to the memory resource. |
|
Return a pointer to the memory resource. |
|
Move assignment. |
|
|
Constructor. |
|
Destructor. |
Friends
Name | Description |
---|---|
Return shared ownership of a new, dynamically allocated memory resource. |
Description
This container is used to hold a pointer to a memory resource. The pointed-to resource is always valid. Depending on the means of construction, the ownership will be either:
-
Non-owning, when constructing from a raw pointer to
boost::container::pmr::memory_resource
or from aboost::container::pmr::polymorphic_allocator
. In this case the caller is responsible for ensuring that the lifetime of the memory resource extends until there are no more calls to allocate or deallocate. -
Owning, when constructing using the function
make_shared_resource
. In this case ownership is shared; the lifetime of the memory resource extends until the last copy of thestorage_ptr
is destroyed.
Examples
These statements create a memory resource on the stack and construct a pointer from it without taking ownership:
monotonic_resource mr; // Create our memory resource on the stack
storage_ptr sp( &mr ); // Construct a non-owning pointer to the resource
This function creates a pointer to a memory resource using shared ownership and returns it. The lifetime of the memory resource extends until the last copy of the pointer is destroyed:
// Create a counted memory resource and return it
storage_ptr make_storage()
{
return make_shared_resource< monotonic_resource >();
}
Thread Safety
Instances of this type provide the default level of thread safety for all C++ objects. Specifically, it conforms to 16.4.6.10 Data race avoidance.
See Also
make_shared_resource
, boost::container::pmr::polymorphic_allocator
, boost::container::pmr::memory_resource
.
Convenience header <boost/json.hpp>.