mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add a constructor from C-style arrays for FixedArray
We really want to be able to construct FixedArray from a list of non-copyable but movable objects. This constructor is useful for such things.
This commit is contained in:
parent
6576d0291c
commit
0362b15895
Notes:
sideshowbarker
2024-07-17 20:50:29 +09:00
Author: https://github.com/creator1creeper1 Commit: https://github.com/SerenityOS/serenity/commit/0362b158952 Pull-request: https://github.com/SerenityOS/serenity/pull/11843 Reviewed-by: https://github.com/IdanHo ✅ Reviewed-by: https://github.com/bgianfo
1 changed files with 17 additions and 0 deletions
|
@ -38,6 +38,23 @@ public:
|
||||||
return MUST(try_create(size));
|
return MUST(try_create(size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE:
|
||||||
|
// Even though it may look like there will be a template instantiation of this function for every size,
|
||||||
|
// the compiler will inline this anyway and therefore not generate any duplicate code.
|
||||||
|
|
||||||
|
template<size_t N>
|
||||||
|
static ErrorOr<FixedArray<T>> try_create(T(&&array)[N])
|
||||||
|
{
|
||||||
|
if (N == 0)
|
||||||
|
return FixedArray<T>();
|
||||||
|
T* elements = static_cast<T*>(kmalloc_array(N, sizeof(T)));
|
||||||
|
if (!elements)
|
||||||
|
return Error::from_errno(ENOMEM);
|
||||||
|
for (size_t i = 0; i < N; ++i)
|
||||||
|
new (&elements[i]) T(move(array[i]));
|
||||||
|
return FixedArray<T>(N, elements);
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<FixedArray<T>> try_clone() const
|
ErrorOr<FixedArray<T>> try_clone() const
|
||||||
{
|
{
|
||||||
if (m_size == 0)
|
if (m_size == 0)
|
||||||
|
|
Loading…
Reference in a new issue