mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:37:36 +00:00
AK: Convert the try_make<T> factory function to use ErrorOr
This allows more ergonomic memory allocation failure related error checking using the TRY macro.
This commit is contained in:
parent
ba0a2a3e2f
commit
18b98f8c28
2 changed files with 8 additions and 7 deletions
|
@ -33,13 +33,14 @@ There is a `make<T>()` helper that constructs a new object and returns it wrappe
|
|||
}
|
||||
```
|
||||
|
||||
The `try_make<T>()` helper attempts to construct a new object wrapped in an `OwnPtr`. All arguments passed to it are forwarded to `T`'s constructor. In case of allocation failure, a null pointer is returned. This allows the calling code to handle allocation failure as it wishes.
|
||||
The `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. This allows the calling code to handle allocation failure as it wishes.
|
||||
|
||||
```cpp
|
||||
OwnPtr<Foo> my_object = try_make<Foo>();
|
||||
if (!my_object) {
|
||||
auto my_object_or_error = try_make<Foo>();
|
||||
if (my_object_or_error.is_error()) {
|
||||
// handle allocation failure...
|
||||
}
|
||||
auto my_object = my_object_or_error.release_value();
|
||||
my_object->do_stuff();
|
||||
```
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue