simdjson  3.1.1
Ridiculously Fast JSON
implementation.h
1 #ifndef SIMDJSON_IMPLEMENTATION_H
2 #define SIMDJSON_IMPLEMENTATION_H
3 
4 #include "simdjson/common_defs.h"
5 #include "simdjson/internal/dom_parser_implementation.h"
6 #include "simdjson/internal/isadetection.h"
7 #include <string>
8 #include <atomic>
9 #include <vector>
10 
11 namespace simdjson {
12 
20 simdjson_warn_unused bool validate_utf8(const char * buf, size_t len) noexcept;
27 simdjson_inline simdjson_warn_unused bool validate_utf8(const std::string_view sv) noexcept {
28  return validate_utf8(sv.data(), sv.size());
29 }
30 
37 simdjson_inline simdjson_warn_unused bool validate_utf8(const std::string& s) noexcept {
38  return validate_utf8(s.data(), s.size());
39 }
40 
41 namespace dom {
42  class document;
43 } // namespace dom
44 
52 public:
53 
62  virtual const std::string &name() const { return _name; }
63 
72  virtual const std::string &description() const { return _description; }
73 
82 
90  virtual uint32_t required_instruction_sets() const { return _required_instruction_sets; }
91 
103  virtual error_code create_dom_parser_implementation(
104  size_t capacity,
105  size_t max_depth,
106  std::unique_ptr<internal::dom_parser_implementation> &dst
107  ) const noexcept = 0;
108 
122  simdjson_warn_unused virtual error_code minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept = 0;
123 
124 
134  simdjson_warn_unused virtual bool validate_utf8(const char *buf, size_t len) const noexcept = 0;
135 
136 protected:
138  simdjson_inline implementation(
139  std::string_view name,
140  std::string_view description,
141  uint32_t required_instruction_sets
142  ) :
143  _name(name),
144  _description(description),
145  _required_instruction_sets(required_instruction_sets)
146  {
147  }
148  virtual ~implementation()=default;
149 
150 private:
154  const std::string _name;
155 
159  const std::string _description;
160 
164  const uint32_t _required_instruction_sets;
165 };
166 
168 namespace internal {
169 
173 class available_implementation_list {
174 public:
176  simdjson_inline available_implementation_list() {}
178  size_t size() const noexcept;
180  const implementation * const *begin() const noexcept;
182  const implementation * const *end() const noexcept;
183 
197  const implementation * operator[](const std::string_view &name) const noexcept {
198  for (const implementation * impl : *this) {
199  if (impl->name() == name) { return impl; }
200  }
201  return nullptr;
202  }
203 
216  const implementation *detect_best_supported() const noexcept;
217 };
218 
219 template<typename T>
220 class atomic_ptr {
221 public:
222  atomic_ptr(T *_ptr) : ptr{_ptr} {}
223 
224  operator const T*() const { return ptr.load(); }
225  const T& operator*() const { return *ptr; }
226  const T* operator->() const { return ptr.load(); }
227 
228  operator T*() { return ptr.load(); }
229  T& operator*() { return *ptr; }
230  T* operator->() { return ptr.load(); }
231  atomic_ptr& operator=(T *_ptr) { ptr = _ptr; return *this; }
232 
233 private:
234  std::atomic<T*> ptr;
235 };
236 
237 } // namespace internal
238 
242 extern SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list& get_available_implementations();
243 
249 extern SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>& get_active_implementation();
250 
251 } // namespace simdjson
252 
253 #endif // SIMDJSON_IMPLEMENTATION_H
An implementation of simdjson for a particular CPU architecture.
virtual const std::string & name() const
The name of this implementation.
virtual const std::string & description() const
The description of this implementation.
virtual simdjson_warn_unused bool validate_utf8(const char *buf, size_t len) const noexcept=0
Validate the UTF-8 string.
bool supported_by_runtime_system() const
The instruction sets this implementation is compiled against and the current CPU match.
We want to support argument-dependent lookup (ADL).
simdjson_warn_unused bool validate_utf8(const char *buf, size_t len) noexcept
Validate the UTF-8 string.
error_code
All possible errors returned by simdjson.
Definition: error.h:17
SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list & get_available_implementations()
The list of available implementations compiled into simdjson.
SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr< const implementation > & get_active_implementation()
The active implementation.