From 0362b15895295afeab98308fd070ccacd599c2b4 Mon Sep 17 00:00:00 2001 From: creator1creeper1 Date: Mon, 10 Jan 2022 15:30:52 +0100 Subject: [PATCH] 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. --- AK/FixedArray.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/AK/FixedArray.h b/AK/FixedArray.h index e5e3e9fab17..8f0d1a9b418 100644 --- a/AK/FixedArray.h +++ b/AK/FixedArray.h @@ -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 + static ErrorOr> try_create(T(&&array)[N]) + { + if (N == 0) + return FixedArray(); + T* elements = static_cast(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(N, elements); + } + ErrorOr> try_clone() const { if (m_size == 0)