mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:45:06 +00:00
AK: Allow creating a FixedArray from an initializer list
This commit is contained in:
parent
65f34504e9
commit
09df8f812a
1 changed files with 12 additions and 0 deletions
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/Iterator.h>
|
#include <AK/Iterator.h>
|
||||||
#include <AK/Span.h>
|
#include <AK/Span.h>
|
||||||
#include <AK/kmalloc.h>
|
#include <AK/kmalloc.h>
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -21,6 +22,17 @@ class FixedArray {
|
||||||
public:
|
public:
|
||||||
FixedArray() = default;
|
FixedArray() = default;
|
||||||
|
|
||||||
|
static ErrorOr<FixedArray<T>> try_create(std::initializer_list<T> initializer)
|
||||||
|
{
|
||||||
|
auto array = TRY(try_create(initializer.size()));
|
||||||
|
auto it = initializer.begin();
|
||||||
|
for (size_t i = 0; i < array.size(); ++i) {
|
||||||
|
array[i] = move(*it);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
static ErrorOr<FixedArray<T>> try_create(size_t size)
|
static ErrorOr<FixedArray<T>> try_create(size_t size)
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue