mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +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:
parent
5491e0cdcc
commit
00915e8948
Notes:
sideshowbarker
2024-07-18 11:34:55 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/00915e89482 Pull-request: https://github.com/SerenityOS/serenity/pull/8150 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/alimpfard ✅
4 changed files with 22 additions and 2 deletions
|
@ -154,8 +154,7 @@ inline NonnullOwnPtr<T> adopt_own(T& object)
|
|||
#endif
|
||||
|
||||
template<class T, class... Args>
|
||||
inline NonnullOwnPtr<T>
|
||||
make(Args&&... args)
|
||||
inline NonnullOwnPtr<T> make(Args&&... args)
|
||||
{
|
||||
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T(forward<Args>(args)...));
|
||||
}
|
||||
|
|
|
@ -335,6 +335,12 @@ inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)
|
|||
a.swap(b);
|
||||
}
|
||||
|
||||
template<typename T, class... Args>
|
||||
inline NonnullRefPtr<T> create(Args&&... args)
|
||||
{
|
||||
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T(forward<Args>(args)...));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -346,4 +352,5 @@ struct Traits<NonnullRefPtr<T>> : public GenericTraits<NonnullRefPtr<T>> {
|
|||
};
|
||||
|
||||
using AK::adopt_ref;
|
||||
using AK::create;
|
||||
using AK::NonnullRefPtr;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -485,8 +485,15 @@ inline RefPtr<T> adopt_ref_if_nonnull(T* object)
|
|||
return {};
|
||||
}
|
||||
|
||||
template<typename T, class... Args>
|
||||
inline RefPtr<T> try_create(Args&&... args)
|
||||
{
|
||||
return adopt_ref_if_nonnull(new (nothrow) T(forward<Args>(args)...));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::adopt_ref_if_nonnull;
|
||||
using AK::RefPtr;
|
||||
using AK::static_ptr_cast;
|
||||
using AK::try_create;
|
||||
|
|
Loading…
Reference in a new issue