Source
stdlib/parsers/json/object.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 once5
// cheatah::parsers::json — the JSON object token, mirroring Array<S>: a class template over its6
// backing storage type S (ObjectStorage), MOVE-ONLY. Members read uniformly as a7
// std::span<const Member> via value(), whether owned or viewed:8
// - Object<std::vector<Member>> (OwnedObject) — owns its members (self-contained).9
// - Object<std::span<const Member>> (ObjectView) — views members in a Parser's pool, no copy.10
//11
// Being a template, its members instantiate lazily (so the constructor can be in-class even12
// though Node is recursive — no out-of-line workaround needed). No runtime polymorphism.14
#include <span>15
#include <utility>16
#include <vector>18
#include "fwd.hpp" // forward declarations + the ObjectStorage concept (standalone-clean)20
namespace cheatah::parsers::json {22
/**23
* @brief A JSON object token, templated on its backing storage @p S: Object<std::vector<Member>>24
* (OwnedObject) owns its members, Object<std::span<const Member>> (ObjectView) views members25
* in a Parser's pool with no copy. Move-only; both backings read uniformly via value().26
* @tparam S the member storage: std::vector<Member> (owning) or std::span<const Member> (viewing).27
*/28
template <ObjectStorage S>29
class Object {30
private:31
S value_;33
public:34
/**35
* Construct from the backing storage (moved in).36
* @param value the member storage (an owning vector or a non-owning span).37
* @complexity O(1) — a vector move or a span copy; the members are never copied.38
* @alloc none.39
* @test CheatahParsersJson.ContainerTokenLifecycle40
*/41
Object(S value) : value_(std::move(value)) {}42
~Object() = default;44
// Move-only: no object is copied; all are moved by default.45
Object(const Object&) = delete;46
Object& operator=(const Object&) = delete;47
/**48
* Move-construct, taking over the other object's storage.49
* @param other the object to move from.50
*/51
Object(Object&& other) noexcept = default;52
/**53
* Move-assign, taking over the other object's storage.54
* @param other the object to move from.55
* @return reference to this object.56
*/57
Object& operator=(Object&& other) noexcept = default;59
/**60
* Read the members uniformly as a view, whether owned or viewed (no setter).61
* @return a std::span<const Member> over the object's key/value members.62
* @complexity O(1).63
* @alloc none.64
* @test CheatahParsersJson.ContainerTokenLifecycle65
*/66
[[nodiscard]] std::span<const Member> value() const noexcept { return value_; }67
};69
} // namespace cheatah::parsers::json