cheatah
Source

stdlib/parsers/json/array.hpp

1// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).
2// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.
3#pragma once
5// cheatah::parsers::json — the JSON array token, now EXACTLY symmetric with String<S>: a class
6// template over its backing storage type S, constrained by the ArrayStorage concept. MOVE-ONLY
7// (no array is ever copied; all are moved by default). Both backings read uniformly as a
8// std::span<const Node> via value():
9// - Array<std::vector<Node>> (OwnedArray) — owns its elements.
10// - Array<std::span<const Node>> (ArrayView) — views elements stored elsewhere, no copy.
11//
12// No runtime polymorphism: the backing is selected at compile time by the template argument.
14#include <span>
15#include <utility>
16#include <vector>
18#include "fwd.hpp" // forward declarations + the ArrayStorage concept (standalone-clean)
20namespace cheatah::parsers::json {
22/**
23 * @brief A JSON array token, templated on its backing storage @p S: Array<std::vector<Node>>
24 * (OwnedArray) owns its elements, Array<std::span<const Node>> (ArrayView) views elements
25 * stored elsewhere with no copy. Move-only; both backings read uniformly via value().
26 * @tparam S the element storage: std::vector<Node> (owning) or std::span<const Node> (viewing).
27 */
28template <ArrayStorage S>
29class Array {
30private:
31 S value_;
33public:
34 /**
35 * Construct from the backing storage (moved in).
36 * @param value the element storage (an owning vector or a non-owning span).
37 * @complexity O(1) — a vector move or a span copy; the elements are never copied.
38 * @alloc none.
39 * @test CheatahParsersJson.ContainerTokenLifecycle
40 */
41 Array(S value) : value_(std::move(value)) {}
42 ~Array() = default;
44 // Move-only: no array is copied; all are moved by default.
45 Array(const Array&) = delete;
46 Array& operator=(const Array&) = delete;
47 /**
48 * Move-construct, taking over the other array's storage.
49 * @param other the array to move from.
50 */
51 Array(Array&& other) noexcept = default;
52 /**
53 * Move-assign, taking over the other array's storage.
54 * @param other the array to move from.
55 * @return reference to this array.
56 */
57 Array& operator=(Array&& other) noexcept = default;
59 /**
60 * Read the elements uniformly as a view, whether owned or viewed (no setter).
61 * @return a std::span<const Node> over the array's elements.
62 * @complexity O(1).
63 * @alloc none.
64 * @test CheatahParsersJson.ContainerTokenLifecycle
65 */
66 [[nodiscard]] std::span<const Node> value() const noexcept { return value_; }
67};
69} // namespace cheatah::parsers::json