simdjson  0.3.1
Ridiculously Fast JSON
implementation.h
1 #ifndef SIMDJSON_IMPLEMENTATION_H
2 #define SIMDJSON_IMPLEMENTATION_H
3 
4 #include <optional>
5 #include <string>
6 #include <atomic>
7 #include <vector>
8 #include "simdjson/document.h"
9 
10 namespace simdjson {
11 
19 public:
28  virtual const std::string &name() const { return _name; }
29 
38  virtual const std::string &description() const { return _description; }
39 
47  virtual uint32_t required_instruction_sets() const { return _required_instruction_sets; };
48 
61  WARN_UNUSED virtual error_code parse(const uint8_t *buf, size_t len, dom::parser &parser) const noexcept = 0;
62 
76  WARN_UNUSED virtual error_code minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept = 0;
77 
91  WARN_UNUSED virtual error_code stage1(const uint8_t *buf, size_t len, dom::parser &parser, bool streaming) const noexcept = 0;
92 
105  WARN_UNUSED virtual error_code stage2(const uint8_t *buf, size_t len, dom::parser &parser) const noexcept = 0;
106 
120  WARN_UNUSED virtual error_code stage2(const uint8_t *buf, size_t len, dom::parser &parser, size_t &next_json) const noexcept = 0;
121 
122 protected:
124  really_inline implementation(
125  std::string_view name,
126  std::string_view description,
127  uint32_t required_instruction_sets
128  ) :
129  _name(name),
130  _description(description),
131  _required_instruction_sets(required_instruction_sets)
132  {
133  }
134 
135 private:
139  const std::string _name;
140 
144  const std::string _description;
145 
149  const uint32_t _required_instruction_sets;
150 };
151 
153 namespace internal {
154 
158 class available_implementation_list {
159 public:
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;
168 
181  const implementation * operator[](const std::string_view &name) const noexcept {
182  for (const implementation * impl : *this) {
183  if (impl->name() == name) { return impl; }
184  }
185  return nullptr;
186  }
187 
200  const implementation *detect_best_supported() const noexcept;
201 };
202 
206 class detect_best_supported_implementation_on_first_use final : public implementation {
207 public:
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);
213  }
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);
216  }
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);
219  }
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);
222  }
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);
225  }
226 
227  really_inline detect_best_supported_implementation_on_first_use() noexcept : implementation("best_supported_detector", "Detects the best supported implementation and sets it", 0) {}
228 private:
229  const implementation *set_best() const noexcept;
230 };
231 
232 inline const detect_best_supported_implementation_on_first_use detect_best_supported_implementation_on_first_use_singleton;
233 
234 template<typename T>
235 class atomic_ptr {
236 public:
237  atomic_ptr(T *_ptr) : ptr{_ptr} {}
238 
239  operator const T*() const { return ptr.load(); }
240  const T& operator*() const { return *ptr; }
241  const T* operator->() const { return ptr.load(); }
242 
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; }
247 
248 private:
249  std::atomic<T*> ptr;
250 };
251 
252 } // namespace [simdjson::]internal
253 
257 inline const internal::available_implementation_list available_implementations;
258 
266 inline internal::atomic_ptr<const implementation> active_implementation = &internal::detect_best_supported_implementation_on_first_use_singleton;
267 
268 } // namespace simdjson
269 
270 #endif // SIMDJSON_IMPLEMENTATION_H
simdjson::implementation::name
virtual const std::string & name() const
The name of this implementation.
Definition: implementation.h:28
simdjson::implementation
An implementation of simdjson for a particular CPU architecture.
Definition: implementation.h:18
simdjson::implementation::description
virtual const std::string & description() const
The description of this implementation.
Definition: implementation.h:38