1 #ifndef SIMDJSON_IMPLEMENTATION_H
2 #define SIMDJSON_IMPLEMENTATION_H
8 #include "simdjson/document.h"
28 virtual const std::string &
name()
const {
return _name; }
38 virtual const std::string &
description()
const {
return _description; }
47 virtual uint32_t required_instruction_sets()
const {
return _required_instruction_sets; };
61 WARN_UNUSED
virtual error_code parse(
const uint8_t *buf,
size_t len, dom::parser &parser)
const noexcept = 0;
76 WARN_UNUSED
virtual error_code minify(
const uint8_t *buf,
size_t len, uint8_t *dst,
size_t &dst_len)
const noexcept = 0;
91 WARN_UNUSED
virtual error_code stage1(
const uint8_t *buf,
size_t len, dom::parser &parser,
bool streaming)
const noexcept = 0;
105 WARN_UNUSED
virtual error_code stage2(
const uint8_t *buf,
size_t len, dom::parser &parser)
const noexcept = 0;
120 WARN_UNUSED
virtual error_code stage2(
const uint8_t *buf,
size_t len, dom::parser &parser,
size_t &next_json)
const noexcept = 0;
124 really_inline implementation(
125 std::string_view
name,
127 uint32_t required_instruction_sets
131 _required_instruction_sets(required_instruction_sets)
139 const std::string _name;
144 const std::string _description;
149 const uint32_t _required_instruction_sets;
158 class available_implementation_list {
161 really_inline available_implementation_list() {}
163 size_t size() const noexcept;
165 const implementation * const *begin() const noexcept;
167 const implementation * const *end() const noexcept;
181 const implementation * operator[](const std::string_view &name) const noexcept {
182 for (
const implementation * impl : *
this) {
183 if (impl->name() == name) {
return impl; }
200 const implementation *detect_best_supported() const noexcept;
206 class detect_best_supported_implementation_on_first_use final : public implementation {
208 const std::string &name() const noexcept final {
return set_best()->name(); }
209 const std::string &description() const noexcept final {
return set_best()->description(); }
210 uint32_t required_instruction_sets() const noexcept final {
return set_best()->required_instruction_sets(); }
211 WARN_UNUSED error_code parse(
const uint8_t *buf,
size_t len, dom::parser &parser)
const noexcept
final {
212 return set_best()->parse(buf, len, parser);
214 WARN_UNUSED error_code minify(
const uint8_t *buf,
size_t len, uint8_t *dst,
size_t &dst_len)
const noexcept
final {
215 return set_best()->minify(buf, len, dst, dst_len);
217 WARN_UNUSED error_code stage1(
const uint8_t *buf,
size_t len, dom::parser &parser,
bool streaming)
const noexcept
final {
218 return set_best()->stage1(buf, len, parser, streaming);
220 WARN_UNUSED error_code stage2(
const uint8_t *buf,
size_t len, dom::parser &parser)
const noexcept
final {
221 return set_best()->stage2(buf, len, parser);
223 WARN_UNUSED error_code stage2(
const uint8_t *buf,
size_t len, dom::parser &parser,
size_t &next_json)
const noexcept
final {
224 return set_best()->stage2(buf, len, parser, next_json);
227 really_inline detect_best_supported_implementation_on_first_use() noexcept : implementation("best_supported_detector", "Detects the best supported implementation and sets it", 0) {}
229 const implementation *set_best() const noexcept;
232 inline const detect_best_supported_implementation_on_first_use detect_best_supported_implementation_on_first_use_singleton;
237 atomic_ptr(T *_ptr) : ptr{_ptr} {}
239 operator const T*()
const {
return ptr.load(); }
240 const T& operator*()
const {
return *ptr; }
241 const T* operator->()
const {
return ptr.load(); }
243 operator T*() {
return ptr.load(); }
244 T& operator*() {
return *ptr; }
245 T* operator->() {
return ptr.load(); }
246 T* operator=(T *_ptr) {
return ptr = _ptr; }
257 inline const internal::available_implementation_list available_implementations;
266 inline internal::atomic_ptr<const implementation> active_implementation = &internal::detect_best_supported_implementation_on_first_use_singleton;
270 #endif // SIMDJSON_IMPLEMENTATION_H