/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace AK { class JsonArray { template using CallbackErrorType = decltype(declval()(declval()).release_error()); public: JsonArray() = default; ~JsonArray() = default; explicit JsonArray(size_t initial_size) { resize(initial_size); } JsonArray(JsonArray const& other) : m_values(other.m_values) { } JsonArray(JsonArray&& other) : m_values(move(other.m_values)) { } template ContainerT> JsonArray(ContainerT&& source) { for (auto& value : source) m_values.append(move(value)); } template ContainerT> JsonArray(ContainerT const& source) { for (auto const& value : source) m_values.append(value); } JsonArray& operator=(JsonArray const& other) { if (this != &other) m_values = other.m_values; return *this; } JsonArray& operator=(JsonArray&& other) { if (this != &other) m_values = move(other.m_values); return *this; } [[nodiscard]] size_t size() const { return m_values.size(); } [[nodiscard]] bool is_empty() const { return m_values.is_empty(); } [[nodiscard]] JsonValue const& at(size_t index) const { return m_values.at(index); } [[nodiscard]] JsonValue const& operator[](size_t index) const { return at(index); } [[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); } void must_append(JsonValue value) { m_values.append(move(value)); } void clear() { m_values.clear(); } ErrorOr append(JsonValue value) { return m_values.try_append(move(value)); } void set(size_t index, JsonValue value) { m_values.at(index) = move(value); } String serialized() const; void serialize(StringBuilder&) const; template void for_each(Callback callback) { for (auto& value : m_values) callback(value); } template void for_each(Callback callback) const { for (auto const& value : m_values) callback(value); } template Callback> ErrorOr> try_for_each(Callback&& callback) { for (auto& value : m_values) TRY(callback(value)); return {}; } template Callback> ErrorOr> try_for_each(Callback&& callback) const { for (auto const& value : m_values) TRY(callback(value)); return {}; } [[nodiscard]] Vector& values() { return m_values; } [[nodiscard]] Vector const& values() const { return m_values; } void ensure_capacity(size_t capacity) { m_values.ensure_capacity(capacity); } void resize(size_t size) { m_values.resize(size); } private: Vector m_values; }; } #if USING_AK_GLOBALLY using AK::JsonArray; #endif