1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 17:37:34 +00:00

AK: Add factory methods for creating smart pointers

These functions abstract away the need to call the proper new operator
("throwing" or "non-throwing") and manually adopt the resulting raw
pointer. Modelled after the existing `NonnullOwnPtr<T> make()`
functions, these forward their parameters to the object's constructor.

Note: These can't be used in the common "factory method" idiom, as
private constructors can't be called from a standalone function.

The naming is consistent with AK's and Shell's previous implementation
of these:
- `make` creates a `NonnullOwnPtr<T>` and aborts if the allocation could
  not be performed.
- `try_make` creates an `OwnPtr<T>`, which may be null if the allocation
  failed.
- `create` creates a `NonnullRefPtr<T>`, and aborts on allocation
  failure.
- `try_create` creates a `RefPtr<T>`, which may be null if the
  allocation was not successful.
This commit is contained in:
Daniel Bertalan 2021-06-20 10:04:41 +02:00 committed by Ali Mohammad Pur
parent 5491e0cdcc
commit 00915e8948
4 changed files with 22 additions and 2 deletions

View file

@ -206,6 +206,12 @@ inline OwnPtr<T> adopt_own_if_nonnull(T* object)
return {};
}
template<typename T, class... Args>
inline OwnPtr<T> try_make(Args&&... args)
{
return adopt_own_if_nonnull(new (nothrow) T(forward<Args>(args)...));
}
template<typename T>
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using PeekType = T*;
@ -218,3 +224,4 @@ struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using AK::adopt_own_if_nonnull;
using AK::OwnPtr;
using AK::try_make;