simdjson  3.11.0
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/base.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 
120  simdjson_warn_unused virtual uint8_t *parse_string(const uint8_t *src, uint8_t *dst, bool allow_replacement) const noexcept = 0;
121 
136  simdjson_warn_unused virtual uint8_t *parse_wobbly_string(const uint8_t *src, uint8_t *dst) const noexcept = 0;
137 
150  virtual error_code set_capacity(size_t capacity) noexcept = 0;
151 
161  virtual error_code set_max_depth(size_t max_depth) noexcept = 0;
162 
166  virtual ~dom_parser_implementation() = default;
167 
169  uint32_t n_structural_indexes{0};
171  std::unique_ptr<uint32_t[]> structural_indexes{};
173  uint32_t next_structural_index{0};
174 
180  simdjson_pure simdjson_inline size_t capacity() const noexcept;
181 
187  simdjson_pure simdjson_inline size_t max_depth() const noexcept;
188 
197  simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth) noexcept;
198 
199 
200 protected:
206  size_t _capacity{0};
207 
213  size_t _max_depth{0};
214 
215  // Declaring these so that subclasses can use them to implement their constructors.
216  simdjson_inline dom_parser_implementation() noexcept;
217  simdjson_inline dom_parser_implementation(dom_parser_implementation &&other) noexcept;
218  simdjson_inline dom_parser_implementation &operator=(dom_parser_implementation &&other) noexcept;
219 
220  simdjson_inline dom_parser_implementation(const dom_parser_implementation &) noexcept = delete;
221  simdjson_inline dom_parser_implementation &operator=(const dom_parser_implementation &other) noexcept = delete;
222 }; // class dom_parser_implementation
223 
224 simdjson_inline dom_parser_implementation::dom_parser_implementation() noexcept = default;
225 simdjson_inline dom_parser_implementation::dom_parser_implementation(dom_parser_implementation &&other) noexcept = default;
226 simdjson_inline dom_parser_implementation &dom_parser_implementation::operator=(dom_parser_implementation &&other) noexcept = default;
227 
228 simdjson_pure simdjson_inline size_t dom_parser_implementation::capacity() const noexcept {
229  return _capacity;
230 }
231 
232 simdjson_pure simdjson_inline size_t dom_parser_implementation::max_depth() const noexcept {
233  return _max_depth;
234 }
235 
236 simdjson_warn_unused
237 inline error_code dom_parser_implementation::allocate(size_t capacity, size_t max_depth) noexcept {
238  if (this->max_depth() != max_depth) {
239  error_code err = set_max_depth(max_depth);
240  if (err) { return err; }
241  }
242  if (_capacity != capacity) {
243  error_code err = set_capacity(capacity);
244  if (err) { return err; }
245  }
246  return SUCCESS;
247 }
248 
249 } // namespace internal
250 } // namespace simdjson
251 
252 #endif // SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
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:19
@ SUCCESS
No error.
Definition: error.h:20
stage1_mode
This enum is used with the dom_parser_implementation::stage1 function.