/src/build-dir/_deps/simdjson-src/include/simdjson/implementation.h
Line | Count | Source |
1 | | #ifndef SIMDJSON_IMPLEMENTATION_H |
2 | | #define SIMDJSON_IMPLEMENTATION_H |
3 | | |
4 | | #include "simdjson/internal/atomic_ptr.h" |
5 | | #include "simdjson/internal/dom_parser_implementation.h" |
6 | | |
7 | | #include <memory> |
8 | | |
9 | | namespace simdjson { |
10 | | |
11 | | /** |
12 | | * Validate the UTF-8 string. |
13 | | * |
14 | | * @param buf the string to validate. |
15 | | * @param len the length of the string in bytes. |
16 | | * @return true if the string is valid UTF-8. |
17 | | */ |
18 | | simdjson_warn_unused bool validate_utf8(const char * buf, size_t len) noexcept; |
19 | | /** |
20 | | * Validate the UTF-8 string. |
21 | | * |
22 | | * @param sv the string_view to validate. |
23 | | * @return true if the string is valid UTF-8. |
24 | | */ |
25 | 0 | simdjson_inline simdjson_warn_unused bool validate_utf8(const std::string_view sv) noexcept { |
26 | 0 | return validate_utf8(sv.data(), sv.size()); |
27 | 0 | } |
28 | | |
29 | | /** |
30 | | * Validate the UTF-8 string. |
31 | | * |
32 | | * @param p the string to validate. |
33 | | * @return true if the string is valid UTF-8. |
34 | | */ |
35 | 0 | simdjson_inline simdjson_warn_unused bool validate_utf8(const std::string& s) noexcept { |
36 | 0 | return validate_utf8(s.data(), s.size()); |
37 | 0 | } |
38 | | |
39 | | /** |
40 | | * An implementation of simdjson for a particular CPU architecture. |
41 | | * |
42 | | * Also used to maintain the currently active implementation. The active implementation is |
43 | | * automatically initialized on first use to the most advanced implementation supported by the host. |
44 | | */ |
45 | | class implementation { |
46 | | public: |
47 | | |
48 | | /** |
49 | | * The name of this implementation. |
50 | | * |
51 | | * const implementation *impl = simdjson::get_active_implementation(); |
52 | | * cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl; |
53 | | * |
54 | | * @return the name of the implementation, e.g. "haswell", "westmere", "arm64". |
55 | | */ |
56 | 0 | virtual std::string name() const { return std::string(_name); } |
57 | | |
58 | | /** |
59 | | * The description of this implementation. |
60 | | * |
61 | | * const implementation *impl = simdjson::get_active_implementation(); |
62 | | * cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl; |
63 | | * |
64 | | * @return the description of the implementation, e.g. "Intel/AMD AVX2", "Intel/AMD SSE4.2", "ARM NEON". |
65 | | */ |
66 | 0 | virtual std::string description() const { return std::string(_description); } |
67 | | |
68 | | /** |
69 | | * The instruction sets this implementation is compiled against |
70 | | * and the current CPU match. This function may poll the current CPU/system |
71 | | * and should therefore not be called too often if performance is a concern. |
72 | | * |
73 | | * @return true if the implementation can be safely used on the current system (determined at runtime). |
74 | | */ |
75 | | bool supported_by_runtime_system() const; |
76 | | |
77 | | /** |
78 | | * @private For internal implementation use |
79 | | * |
80 | | * The instruction sets this implementation is compiled against. |
81 | | * |
82 | | * @return a mask of all required `internal::instruction_set::` values. |
83 | | */ |
84 | 0 | virtual uint32_t required_instruction_sets() const { return _required_instruction_sets; } |
85 | | |
86 | | /** |
87 | | * @private For internal implementation use |
88 | | * |
89 | | * const implementation *impl = simdjson::get_active_implementation(); |
90 | | * cout << "simdjson is optimized for " << impl->name() << "(" << impl->description() << ")" << endl; |
91 | | * |
92 | | * @param capacity The largest document that will be passed to the parser. |
93 | | * @param max_depth The maximum JSON object/array nesting this parser is expected to handle. |
94 | | * @param dst The place to put the resulting parser implementation. |
95 | | * @return the error code, or SUCCESS if there was no error. |
96 | | */ |
97 | | virtual error_code create_dom_parser_implementation( |
98 | | size_t capacity, |
99 | | size_t max_depth, |
100 | | std::unique_ptr<internal::dom_parser_implementation> &dst |
101 | | ) const noexcept = 0; |
102 | | |
103 | | /** |
104 | | * @private For internal implementation use |
105 | | * |
106 | | * Minify the input string assuming that it represents a JSON string, does not parse or validate. |
107 | | * |
108 | | * Overridden by each implementation. |
109 | | * |
110 | | * @param buf the json document to minify. |
111 | | * @param len the length of the json document. |
112 | | * @param dst the buffer to write the minified document to. *MUST* be allocated up to len + SIMDJSON_PADDING bytes. |
113 | | * @param dst_len the number of bytes written. Output only. |
114 | | * @return the error code, or SUCCESS if there was no error. |
115 | | */ |
116 | | simdjson_warn_unused virtual error_code minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept = 0; |
117 | | |
118 | | |
119 | | /** |
120 | | * Validate the UTF-8 string. |
121 | | * |
122 | | * Overridden by each implementation. |
123 | | * |
124 | | * @param buf the string to validate. |
125 | | * @param len the length of the string in bytes. |
126 | | * @return true if and only if the string is valid UTF-8. |
127 | | */ |
128 | | simdjson_warn_unused virtual bool validate_utf8(const char *buf, size_t len) const noexcept = 0; |
129 | | |
130 | | protected: |
131 | | /** @private Construct an implementation with the given name and description. For subclasses. */ |
132 | | simdjson_inline implementation( |
133 | | std::string_view name, |
134 | | std::string_view description, |
135 | | uint32_t required_instruction_sets |
136 | | ) : |
137 | 0 | _name(name), |
138 | 0 | _description(description), |
139 | 0 | _required_instruction_sets(required_instruction_sets) |
140 | 0 | { |
141 | 0 | } |
142 | | protected: |
143 | | ~implementation() = default; |
144 | | |
145 | | private: |
146 | | /** |
147 | | * The name of this implementation. |
148 | | */ |
149 | | std::string_view _name; |
150 | | |
151 | | /** |
152 | | * The description of this implementation. |
153 | | */ |
154 | | std::string_view _description; |
155 | | |
156 | | /** |
157 | | * Instruction sets required for this implementation. |
158 | | */ |
159 | | const uint32_t _required_instruction_sets; |
160 | | }; |
161 | | |
162 | | /** @private */ |
163 | | namespace internal { |
164 | | |
165 | | /** |
166 | | * The list of available implementations compiled into simdjson. |
167 | | */ |
168 | | class available_implementation_list { |
169 | | public: |
170 | | /** Get the list of available implementations compiled into simdjson */ |
171 | 0 | simdjson_inline available_implementation_list() {} |
172 | | /** Number of implementations */ |
173 | | size_t size() const noexcept; |
174 | | /** STL const begin() iterator */ |
175 | | const implementation * const *begin() const noexcept; |
176 | | /** STL const end() iterator */ |
177 | | const implementation * const *end() const noexcept; |
178 | | |
179 | | /** |
180 | | * Get the implementation with the given name. |
181 | | * |
182 | | * Case sensitive. |
183 | | * |
184 | | * const implementation *impl = simdjson::get_available_implementations()["westmere"]; |
185 | | * if (!impl) { exit(1); } |
186 | | * if (!imp->supported_by_runtime_system()) { exit(1); } |
187 | | * simdjson::get_active_implementation() = impl; |
188 | | * |
189 | | * @param name the implementation to find, e.g. "westmere", "haswell", "arm64" |
190 | | * @return the implementation, or nullptr if the parse failed. |
191 | | */ |
192 | 0 | const implementation * operator[](const std::string_view &name) const noexcept { |
193 | 0 | for (const implementation * impl : *this) { |
194 | 0 | if (impl->name() == name) { return impl; } |
195 | 0 | } |
196 | 0 | return nullptr; |
197 | 0 | } |
198 | | |
199 | | /** |
200 | | * Detect the most advanced implementation supported by the current host. |
201 | | * |
202 | | * This is used to initialize the implementation on startup. |
203 | | * |
204 | | * const implementation *impl = simdjson::available_implementation::detect_best_supported(); |
205 | | * simdjson::get_active_implementation() = impl; |
206 | | * |
207 | | * @return the most advanced supported implementation for the current host, or an |
208 | | * implementation that returns UNSUPPORTED_ARCHITECTURE if there is no supported |
209 | | * implementation. Will never return nullptr. |
210 | | */ |
211 | | const implementation *detect_best_supported() const noexcept; |
212 | | }; |
213 | | |
214 | | } // namespace internal |
215 | | |
216 | | /** |
217 | | * The list of available implementations compiled into simdjson. |
218 | | */ |
219 | | extern SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list& get_available_implementations(); |
220 | | |
221 | | /** |
222 | | * The active implementation. |
223 | | * |
224 | | * Automatically initialized on first use to the most advanced implementation supported by this hardware. |
225 | | */ |
226 | | extern SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr<const implementation>& get_active_implementation(); |
227 | | |
228 | | } // namespace simdjson |
229 | | |
230 | | #endif // SIMDJSON_IMPLEMENTATION_H |