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:
creator1creeper1 2022-01-10 15:30:52 +01:00 committed by Idan Horowitz
parent 6576d0291c
commit 0362b15895
Notes: sideshowbarker 2024-07-17 20:50:29 +09:00

View file

@ -38,6 +38,23 @@ public:
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
{
if (m_size == 0)