OwnPtr<T> and NonnullOwnPtr<T>
OwnPtr is used for single-owner objects. An object held in an OwnPtr is owned by that OwnPtr, and not by anybody else.
This means that the OwnPtr is responsible for deleting the pointee when the OwnPtr goes out of scope.
These pointers cannot be copied. Transferring ownership is done by moving the pointer.
NonnullOwnPtr is a special variant of OwnPtr with one additional property: it cannot be null. NonnullOwnPtr is suitable as a return type from functions that are guaranteed to never return null, and as an argument type where ownership is transferred, and the argument may not be null. In other words, if OwnPtr is ”*”, then NonnullOwnPtr is ”&”.
Construction using Helper Functions
There is amake<T>() helper that constructs a new object and returns it wrapped in a NonnullOwnPtr. All arguments passed to it are forwarded to T’s constructor. If it fails to allocate heap memory for the object, it terminates the program.
try_make<T>() helper attempts to construct a new object wrapped in an ErrorOr<NonnullOwnPtr<T>>. All arguments passed to it are forwarded to T’s constructor. In case of allocation failure, an ENOMEM error is returned.
Manual Construction
The helper functions cannot access private constructors, so in some cases, smart pointers need to be created manually. This is done by “adopting” a raw pointer, which moves its ownership to the smart pointer. Known non-null pointers can be turned into aNonnullOwnPtr by the global adopt_own() function.
T can be turned into an OwnPtr<T> by the global adopt_own_if_nonnull() function.
new should be used to construct the raw pointer, which returns null if the allocation fails, instead of aborting the program.
Always prefer the helper functions to manual construction.
RefPtr<T> and NonnullRefPtr<T>
RefPtr is used for multiple-owner objects. An object held by a RefPtr is owned together by every pointer pointing to that object.
Shared ownership is implemented via reference counting.
NonnullRefPtr is a special variant of RefPtr with one additional property: it cannot be null. NonnullRefPtr is suitable as a return type from functions that are guaranteed to never return null, and as an argument type where the argument may not be null. In other words, if RefPtr is ”*”, then NonnullRefPtr is ”&”.
Objects can only be held by RefPtr if they meet certain criteria. Specifically, they need to implement the functions ref() and unref().
A
NonnullRefPtr can be assigned to a RefPtr but not vice versa. To transform a known-non-null RefPtr into a NonnullRefPtr, either use RefPtr::release_nonnull() or simply dereference the RefPtr using its operator*.Construction using Helper Functions
There is amake_ref_counted<T>() global helper function that constructs a new object and returns it wrapped in a NonnullRefPtr. All arguments passed to it are forwarded to T’s constructor. If memory cannot be allocated for the object, the program is terminated.
try_make_ref_counted<T>() function constructs an object wrapped in ErrorOr<NonnullRefPtr<T>> which may be an error if the allocation does not succeed.
In the above examples, the Bar object will only be deleted once both
our_object and another_owner are gone.Manual Construction
The helper functions cannot access private constructors, so in some cases, objects need to be manually wrapped into smart pointers. When constructing an object that derives fromRefCounted, the reference count starts out at 1 (since 0 would mean that the object has no owners and should be deleted). The object must therefore be “adopted” by someone who takes responsibility of that 1.
A known non-null raw pointer can be turned into a NonnullRefPtr by the global adopt_ref() function.
RefPtr by the global adopt_ref_if_nonnull() function.
new should be used to construct the raw pointer, which returns null if the allocation fails, instead of aborting the program.
Always prefer the helper functions to manual construction.
WeakPtr<T>
WeakPtr is used for objects that somebody else owns. When the pointee of a WeakPtr is deleted, the WeakPtr will magically become null.
Behind the scenes, this is implemented using the Weakable template. If you want to make it possible for a class T to be weakly-pointed-to, have it inherit from Weakable<T>.
To create a WeakPtr to a weakable object, use make_weak_ptr():
