simdjson  3.1.1
Ridiculously Fast JSON
dom_parser_implementation.h
1 #ifndef SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
2 #define SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/error.h"
6 #include <memory>
7 
8 namespace simdjson {
9 
10 namespace dom {
11 class document;
12 } // namespace dom
13 
22 enum class stage1_mode { regular, streaming_partial, streaming_final};
23 
27 inline bool is_streaming(stage1_mode mode) {
28  // performance note: it is probably faster to check that mode is different
29  // from regular than checking that it is either streaming_partial or streaming_final.
30  return (mode != stage1_mode::regular);
31  // return (mode == stage1_mode::streaming_partial || mode == stage1_mode::streaming_final);
32 }
33 
34 
35 namespace internal {
36 
37 
44 class dom_parser_implementation {
45 public:
46 
60  simdjson_warn_unused virtual error_code parse(const uint8_t *buf, size_t len, dom::document &doc) noexcept = 0;
61 
76  simdjson_warn_unused virtual error_code stage1(const uint8_t *buf, size_t len, stage1_mode streaming) noexcept = 0;
77 
90  simdjson_warn_unused virtual error_code stage2(dom::document &doc) noexcept = 0;
91 
103  simdjson_warn_unused virtual error_code stage2_next(dom::document &doc) noexcept = 0;
104 
119  simdjson_warn_unused virtual uint8_t *parse_string(const uint8_t *src, uint8_t *dst) const noexcept = 0;
120 
133  virtual error_code set_capacity(size_t capacity) noexcept = 0;
134 
144  virtual error_code set_max_depth(size_t max_depth) noexcept = 0;
145 
149  virtual ~dom_parser_implementation() = default;
150 
152  uint32_t n_structural_indexes{0};
154  std::unique_ptr<uint32_t[]> structural_indexes{};
156  uint32_t next_structural_index{0};
157 
163  simdjson_inline size_t capacity() const noexcept;
164 
170  simdjson_inline size_t max_depth() const noexcept;
171 
180  simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth) noexcept;
181 
182 
183 protected:
189  size_t _capacity{0};
190 
196  size_t _max_depth{0};
197 
198  // Declaring these so that subclasses can use them to implement their constructors.
199  simdjson_inline dom_parser_implementation() noexcept;
200  simdjson_inline dom_parser_implementation(dom_parser_implementation &&other) noexcept;
201  simdjson_inline dom_parser_implementation &operator=(dom_parser_implementation &&other) noexcept;
202 
203  simdjson_inline dom_parser_implementation(const dom_parser_implementation &) noexcept = delete;
204  simdjson_inline dom_parser_implementation &operator=(const dom_parser_implementation &other) noexcept = delete;
205 }; // class dom_parser_implementation
206 
207 simdjson_inline dom_parser_implementation::dom_parser_implementation() noexcept = default;
208 simdjson_inline dom_parser_implementation::dom_parser_implementation(dom_parser_implementation &&other) noexcept = default;
209 simdjson_inline dom_parser_implementation &dom_parser_implementation::operator=(dom_parser_implementation &&other) noexcept = default;
210 
211 simdjson_inline size_t dom_parser_implementation::capacity() const noexcept {
212  return _capacity;
213 }
214 
215 simdjson_inline size_t dom_parser_implementation::max_depth() const noexcept {
216  return _max_depth;
217 }
218 
219 simdjson_warn_unused
220 inline error_code dom_parser_implementation::allocate(size_t capacity, size_t max_depth) noexcept {
221  if (this->max_depth() != max_depth) {
222  error_code err = set_max_depth(max_depth);
223  if (err) { return err; }
224  }
225  if (_capacity != capacity) {
226  error_code err = set_capacity(capacity);
227  if (err) { return err; }
228  }
229  return SUCCESS;
230 }
231 
232 } // namespace internal
233 } // namespace simdjson
234 
235 #endif // SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
We want to support argument-dependent lookup (ADL).
bool is_streaming(stage1_mode mode)
Returns true if mode == streaming_partial or mode == streaming_final.
error_code
All possible errors returned by simdjson.
Definition: error.h:17
@ SUCCESS
No error.
Definition: error.h:18
stage1_mode
This enum is used with the dom_parser_implementation::stage1 function.