diff options
author | creator1creeper1 <creator1creeper1@airmail.cc> | 2022-01-10 15:30:52 +0100 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-15 22:16:00 +0200 |
commit | 0362b15895295afeab98308fd070ccacd599c2b4 (patch) | |
tree | e909e681e5984e901e1787632e28e0720c9f6dd4 /AK/FixedArray.h | |
parent | 6576d0291cf5900a1e4b777dbc17287629cea259 (diff) | |
download | serenity-0362b15895295afeab98308fd070ccacd599c2b4.zip |
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.
Diffstat (limited to 'AK/FixedArray.h')
-rw-r--r-- | AK/FixedArray.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/AK/FixedArray.h b/AK/FixedArray.h index e5e3e9fab1..8f0d1a9b41 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<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) |