simdjson  3.3.0
Ridiculously Fast JSON
error.h
1 #ifndef SIMDJSON_ERROR_H
2 #define SIMDJSON_ERROR_H
3 
4 #include "simdjson/base.h"
5 
6 #include <string>
7 #include <ostream>
8 
9 namespace simdjson {
10 
19 enum error_code {
20  SUCCESS = 0,
51  NUM_ERROR_CODES
52 };
53 
64 inline const char *error_message(error_code error) noexcept;
65 
69 inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept;
70 
74 struct simdjson_error : public std::exception {
79  simdjson_error(error_code error) noexcept : _error{error} { }
81  const char *what() const noexcept { return error_message(error()); }
83  error_code error() const noexcept { return _error; }
84 private:
86  error_code _error;
87 };
88 
89 namespace internal {
90 
111 template<typename T>
112 struct simdjson_result_base : protected std::pair<T, error_code> {
113 
117  simdjson_inline simdjson_result_base() noexcept;
118 
122  simdjson_inline simdjson_result_base(error_code error) noexcept;
123 
127  simdjson_inline simdjson_result_base(T &&value) noexcept;
128 
132  simdjson_inline simdjson_result_base(T &&value, error_code error) noexcept;
133 
140  simdjson_inline void tie(T &value, error_code &error) && noexcept;
141 
147  simdjson_inline error_code get(T &value) && noexcept;
148 
152  simdjson_inline error_code error() const noexcept;
153 
154 #if SIMDJSON_EXCEPTIONS
155 
161  simdjson_inline T& value() & noexcept(false);
162 
168  simdjson_inline T&& value() && noexcept(false);
169 
175  simdjson_inline T&& take_value() && noexcept(false);
176 
182  simdjson_inline operator T&&() && noexcept(false);
183 #endif // SIMDJSON_EXCEPTIONS
184 
189  simdjson_inline const T& value_unsafe() const& noexcept;
190 
195  simdjson_inline T&& value_unsafe() && noexcept;
196 
197 }; // struct simdjson_result_base
198 
199 } // namespace internal
200 
206 template<typename T>
207 struct simdjson_result : public internal::simdjson_result_base<T> {
211  simdjson_inline simdjson_result() noexcept;
215  simdjson_inline simdjson_result(T &&value) noexcept;
219  simdjson_inline simdjson_result(error_code error_code) noexcept;
223  simdjson_inline simdjson_result(T &&value, error_code error) noexcept;
224 
231  simdjson_inline void tie(T &value, error_code &error) && noexcept;
232 
238  simdjson_warn_unused simdjson_inline error_code get(T &value) && noexcept;
239 
243  simdjson_inline error_code error() const noexcept;
244 
245 #if SIMDJSON_EXCEPTIONS
246 
252  simdjson_inline T& value() & noexcept(false);
253 
259  simdjson_inline T&& value() && noexcept(false);
260 
266  simdjson_inline T&& take_value() && noexcept(false);
267 
273  simdjson_inline operator T&&() && noexcept(false);
274 #endif // SIMDJSON_EXCEPTIONS
275 
280  simdjson_inline const T& value_unsafe() const& noexcept;
281 
286  simdjson_inline T&& value_unsafe() && noexcept;
287 
288 }; // struct simdjson_result
289 
290 #if SIMDJSON_EXCEPTIONS
291 
292 template<typename T>
293 inline std::ostream& operator<<(std::ostream& out, simdjson_result<T> value) { return out << value.value(); }
294 #endif // SIMDJSON_EXCEPTIONS
295 
296 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
300 using ErrorValues [[deprecated("This is an alias and will be removed, use error_code instead")]] = error_code;
301 
305 [[deprecated("Error codes should be stored and returned as `error_code`, use `error_message()` instead.")]]
306 inline const std::string error_message(int error) noexcept;
307 #endif // SIMDJSON_DISABLE_DEPRECATED_API
308 } // namespace simdjson
309 
310 #endif // SIMDJSON_ERROR_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
std::ostream & operator<<(std::ostream &out, error_code error) noexcept
Write the error message to the output stream.
Definition: error-inl.h:35
const char * error_message(error_code error) noexcept
Get the error message for the given error code.
Definition: error-inl.h:20
error_code
All possible errors returned by simdjson.
Definition: error.h:19
@ SCALAR_DOCUMENT_AS_VALUE
A scalar document is treated as a value.
Definition: error.h:48
@ DEPTH_ERROR
Your document exceeds the user-specified depth limitation.
Definition: error.h:24
@ UNCLOSED_STRING
missing quote at the end
Definition: error.h:34
@ INCORRECT_TYPE
JSON element has a different type than user expected.
Definition: error.h:36
@ CAPACITY
This parser can't support a document that big.
Definition: error.h:21
@ OUT_OF_ORDER_ITERATION
tried to iterate an array or object out of order
Definition: error.h:45
@ UTF8_ERROR
the input is not valid UTF-8
Definition: error.h:30
@ OUT_OF_BOUNDS
Attempted to access location outside of document.
Definition: error.h:49
@ TAPE_ERROR
Something went wrong, this is a generic error.
Definition: error.h:23
@ NO_SUCH_FIELD
JSON field not found in object.
Definition: error.h:39
@ UNSUPPORTED_ARCHITECTURE
unsupported architecture
Definition: error.h:35
@ N_ATOM_ERROR
Problem while parsing an atom starting with the letter 'n'.
Definition: error.h:28
@ EMPTY
no structural element found
Definition: error.h:32
@ INVALID_URI_FRAGMENT
Invalid URI fragment.
Definition: error.h:42
@ INDEX_OUT_OF_BOUNDS
JSON array index too large.
Definition: error.h:38
@ NUMBER_OUT_OF_RANGE
JSON number does not fit in 64 bits.
Definition: error.h:37
@ STRING_ERROR
Problem while parsing a string.
Definition: error.h:25
@ MEMALLOC
Error allocating memory, most likely out of memory.
Definition: error.h:22
@ SUCCESS
No error.
Definition: error.h:20
@ T_ATOM_ERROR
Problem while parsing an atom starting with the letter 't'.
Definition: error.h:26
@ TRAILING_CONTENT
Unexpected trailing content in the JSON input.
Definition: error.h:50
@ INCOMPLETE_ARRAY_OR_OBJECT
The document ends early.
Definition: error.h:47
@ UNEXPECTED_ERROR
indicative of a bug in simdjson
Definition: error.h:43
@ UNINITIALIZED
unknown error, or uninitialized document
Definition: error.h:31
@ UNESCAPED_CHARS
found unescaped characters in a string.
Definition: error.h:33
@ IO_ERROR
Error reading a file.
Definition: error.h:40
@ NUMBER_ERROR
Problem while parsing a number.
Definition: error.h:29
@ F_ATOM_ERROR
Problem while parsing an atom starting with the letter 'f'.
Definition: error.h:27
@ PARSER_IN_USE
parser is already in use.
Definition: error.h:44
@ INSUFFICIENT_PADDING
The JSON doesn't have enough padding for simdjson to safely parse it.
Definition: error.h:46
@ INVALID_JSON_POINTER
Invalid JSON pointer reference.
Definition: error.h:41
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:74
const char * what() const noexcept
The error message.
Definition: error.h:81
error_code error() const noexcept
The error code.
Definition: error.h:83
simdjson_error(error_code error) noexcept
Create an exception from a simdjson error code.
Definition: error.h:79
The result of a simdjson operation that could fail.
Definition: error.h:207
simdjson_inline const T & value_unsafe() const &noexcept
Get the result value.
simdjson_inline T && value() &&noexcept(false)
Take the result value (move it).
simdjson_inline T & value() &noexcept(false)
Get the result value.