cheatah
Source

stdlib/parsers/json/fwd.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 — forward declarations for the whole value model (NOT a second Node;
6// the Node class lives only in node.hpp). These forwards must precede array.hpp / object.hpp so
7// that each token header can stand on its own (and so IntelliSense can compile them in isolation)
8// without pulling in the recursive Node class definition.
9//
10// Node is a forward-declarable class, which is what lets Array AND Object be templated on their
11// storage TYPE with real concepts (ArrayStorage / ObjectStorage), each with an owning backing
12// (std::vector, self-contained) and a viewing backing (std::span, into a Parser's pool).
14#include <span>
15#include <type_traits>
16#include <vector>
18namespace cheatah::parsers::json {
20class Node; // the value type; defined in node.hpp
22// ---- arrays ------------------------------------------------------------------
24// Backing storage for an Array: owning std::vector<Node> or non-owning std::span<const Node>.
25template <typename S>
26concept ArrayStorage =
27 std::is_same_v<S, std::vector<Node>> || std::is_same_v<S, std::span<const Node>>;
29template <ArrayStorage S>
30class Array; // defined in array.hpp
32using OwnedArray = Array<std::vector<Node>>; // owns its elements (self-contained)
33using ArrayView = Array<std::span<const Node>>; // views elements in a pool (zero-copy)
35// ---- objects -----------------------------------------------------------------
37// One object member: a key (a string Node) and its value. A forward-declared STRUCT (not a
38// std::pair alias) so it can be named here while Node is incomplete and defined in node.hpp after
39// Node — std::span<const Member> would otherwise eagerly instantiate pair<Node,Node> (via span's
40// iterator concepts) while Node is still incomplete. (Node itself is forward-declarable, so
41// span<const Node> for ArrayView does not have this problem.)
42struct Member;
44// Backing storage for an Object, mirroring ArrayStorage: owning vector or non-owning span.
45template <typename S>
46concept ObjectStorage =
47 std::is_same_v<S, std::vector<Member>> || std::is_same_v<S, std::span<const Member>>;
49template <ObjectStorage S>
50class Object; // defined in object.hpp
52using OwnedObject = Object<std::vector<Member>>; // owns its members (self-contained)
53using ObjectView = Object<std::span<const Member>>; // views members in a pool (zero-copy)
55} // namespace cheatah::parsers::json