/src/simdjson/include/simdjson/dom/document.h
Line | Count | Source |
1 | | #ifndef SIMDJSON_DOM_DOCUMENT_H |
2 | | #define SIMDJSON_DOM_DOCUMENT_H |
3 | | |
4 | | #include "simdjson/dom/base.h" |
5 | | |
6 | | #include <memory> |
7 | | |
8 | | namespace simdjson { |
9 | | namespace dom { |
10 | | |
11 | | /** |
12 | | * A parsed JSON document. |
13 | | * |
14 | | * This class cannot be copied, only moved, to avoid unintended allocations. |
15 | | */ |
16 | | class document { |
17 | | public: |
18 | | /** |
19 | | * Create a document container with zero capacity. |
20 | | * |
21 | | * The parser will allocate capacity as needed. |
22 | | */ |
23 | 151k | document() noexcept = default; |
24 | 327k | ~document() noexcept = default; |
25 | | |
26 | | /** |
27 | | * Take another document's buffers. |
28 | | * |
29 | | * @param other The document to take. Its capacity is zeroed and it is invalidated. |
30 | | */ |
31 | 175k | document(document &&other) noexcept = default; |
32 | | /** @private */ |
33 | | document(const document &) = delete; // Disallow copying |
34 | | /** |
35 | | * Take another document's buffers. |
36 | | * |
37 | | * @param other The document to take. Its capacity is zeroed. |
38 | | */ |
39 | 334k | document &operator=(document &&other) noexcept = default; |
40 | | /** @private */ |
41 | | document &operator=(const document &) = delete; // Disallow copying |
42 | | |
43 | | /** |
44 | | * Get the root element of this document as a JSON array. |
45 | | */ |
46 | | element root() const noexcept; |
47 | | |
48 | | /** |
49 | | * @private Dump the raw tape for debugging. |
50 | | * |
51 | | * @param os the stream to output to. |
52 | | * @return false if the tape is likely wrong (e.g., you did not parse a valid JSON). |
53 | | */ |
54 | | bool dump_raw_tape(std::ostream &os) const noexcept; |
55 | | |
56 | | /** @private Structural values. */ |
57 | | std::unique_ptr<uint64_t[]> tape{}; |
58 | | |
59 | | /** @private String values. |
60 | | * |
61 | | * Should be at least byte_capacity. |
62 | | */ |
63 | | std::unique_ptr<uint8_t[]> string_buf{}; |
64 | | /** @private Allocate memory to support |
65 | | * input JSON documents of up to len bytes. |
66 | | * |
67 | | * When calling this function, you lose |
68 | | * all the data. |
69 | | * |
70 | | * The memory allocation is strict: you |
71 | | * can you use this function to increase |
72 | | * or lower the amount of allocated memory. |
73 | | * Passing zero clears the memory. |
74 | | */ |
75 | | error_code allocate(size_t len) noexcept; |
76 | | /** @private Capacity in bytes, in terms |
77 | | * of how many bytes of input JSON we can |
78 | | * support. |
79 | | */ |
80 | | size_t capacity() const noexcept; |
81 | | |
82 | | |
83 | | private: |
84 | | size_t allocated_capacity{0}; |
85 | | friend class parser; |
86 | | }; // class document |
87 | | |
88 | | } // namespace dom |
89 | | } // namespace simdjson |
90 | | |
91 | | #endif // SIMDJSON_DOM_DOCUMENT_H |