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