Skip to content

Commit

Permalink
Documentation: Add a paragraph about NonnullOwnPtr to SmartPointers.md
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Aug 1, 2019
1 parent 1c50dce commit f4bae89
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Documentation/SmartPointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ The reason for using these pointers is to make it explicit through code who owns


----
## OwnPtr<T>
## OwnPtr<T> and NonnullOwnPtr<T>

OwnPtr is used for single-owner objects. An object held by 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.

There is a make<T>() helper that creates a new object and returns it wrapped in an OwnPtr.
NonnullOwnPtr<T> 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 "&".

There is a make<T>() helper that creates a new object and returns it wrapped in an NonnullOwnPtr.

{
OwnPtr<Foo> my_object = make<Foo>();
NonnullOwnPtr<Foo> my_object = make<Foo>();
my_object->do_stuff();
// my_object goes out of scope here, and the Foo will be deleted.
}
Expand Down

0 comments on commit f4bae89

Please sign in to comment.