simdjson  0.9.0
Ridiculously Fast JSON
error.h
1 #ifndef SIMDJSON_ERROR_H
2 #define SIMDJSON_ERROR_H
3 
4 #include "simdjson/common_defs.h"
5 #include <string>
6 
7 namespace simdjson {
8 
12 enum error_code {
13  SUCCESS = 0,
14  CAPACITY,
15  MEMALLOC,
16  TAPE_ERROR,
17  DEPTH_ERROR,
18  STRING_ERROR,
19  T_ATOM_ERROR,
20  F_ATOM_ERROR,
21  N_ATOM_ERROR,
22  NUMBER_ERROR,
23  UTF8_ERROR,
24  UNINITIALIZED,
25  EMPTY,
26  UNESCAPED_CHARS,
27  UNCLOSED_STRING,
28  UNSUPPORTED_ARCHITECTURE,
29  INCORRECT_TYPE,
30  NUMBER_OUT_OF_RANGE,
31  INDEX_OUT_OF_BOUNDS,
32  NO_SUCH_FIELD,
33  IO_ERROR,
34  INVALID_JSON_POINTER,
35  INVALID_URI_FRAGMENT,
36  UNEXPECTED_ERROR,
37  PARSER_IN_USE,
38  OUT_OF_ORDER_ITERATION,
39  INSUFFICIENT_PADDING,
40  NUM_ERROR_CODES
41 };
42 
53 inline const char *error_message(error_code error) noexcept;
54 
58 inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept;
59 
63 struct simdjson_error : public std::exception {
68  simdjson_error(error_code error) noexcept : _error{error} { }
70  const char *what() const noexcept { return error_message(error()); }
72  error_code error() const noexcept { return _error; }
73 private:
75  error_code _error;
76 };
77 
78 namespace internal {
79 
100 template<typename T>
101 struct simdjson_result_base : protected std::pair<T, error_code> {
102 
106  simdjson_really_inline simdjson_result_base() noexcept;
107 
111  simdjson_really_inline simdjson_result_base(error_code error) noexcept;
112 
116  simdjson_really_inline simdjson_result_base(T &&value) noexcept;
117 
121  simdjson_really_inline simdjson_result_base(T &&value, error_code error) noexcept;
122 
129  simdjson_really_inline void tie(T &value, error_code &error) && noexcept;
130 
136  simdjson_really_inline error_code get(T &value) && noexcept;
137 
143  simdjson_really_inline const T &value(error_code &error) const & noexcept;
144 
148  simdjson_really_inline error_code error() const noexcept;
149 
150 #if SIMDJSON_EXCEPTIONS
151 
157  simdjson_really_inline T& value() & noexcept(false);
158 
164  simdjson_really_inline T&& value() && noexcept(false);
165 
171  simdjson_really_inline T&& take_value() && noexcept(false);
172 
178  simdjson_really_inline operator T&&() && noexcept(false);
179 #endif // SIMDJSON_EXCEPTIONS
180 
185  simdjson_really_inline const T& value_unsafe() const& noexcept;
186 
191  simdjson_really_inline T&& value_unsafe() && noexcept;
192 
193 }; // struct simdjson_result_base
194 
195 } // namespace internal
196 
202 template<typename T>
203 struct simdjson_result : public internal::simdjson_result_base<T> {
207  simdjson_really_inline simdjson_result() noexcept;
211  simdjson_really_inline simdjson_result(T &&value) noexcept;
215  simdjson_really_inline simdjson_result(error_code error_code) noexcept;
219  simdjson_really_inline simdjson_result(T &&value, error_code error) noexcept;
220 
227  simdjson_really_inline void tie(T &value, error_code &error) && noexcept;
228 
234  simdjson_warn_unused simdjson_really_inline error_code get(T &value) && noexcept;
235 
239  simdjson_really_inline error_code error() const noexcept;
240 
241 #if SIMDJSON_EXCEPTIONS
242 
248  simdjson_really_inline T& value() & noexcept(false);
249 
255  simdjson_really_inline T&& value() && noexcept(false);
256 
262  simdjson_really_inline T&& take_value() && noexcept(false);
263 
269  simdjson_really_inline operator T&&() && noexcept(false);
270 #endif // SIMDJSON_EXCEPTIONS
271 
276  simdjson_really_inline const T& value_unsafe() const& noexcept;
277 
282  simdjson_really_inline T&& value_unsafe() && noexcept;
283 
284 }; // struct simdjson_result
285 
286 #if SIMDJSON_EXCEPTIONS
287 
288 template<typename T>
289 inline std::ostream& operator<<(std::ostream& out, simdjson_result<T> value) noexcept { return out << value.value(); }
290 
291 #endif // SIMDJSON_EXCEPTIONS
292 
293 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
294 
297 using ErrorValues [[deprecated("This is an alias and will be removed, use error_code instead")]] = error_code;
298 
302 [[deprecated("Error codes should be stored and returned as `error_code`, use `error_message()` instead.")]]
303 inline const std::string error_message(int error) noexcept;
304 #endif // SIMDJSON_DISABLE_DEPRECATED_API
305 } // namespace simdjson
306 
307 #endif // SIMDJSON_ERROR_H
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:203
simdjson::simdjson_error::simdjson_error
simdjson_error(error_code error) noexcept
Create an exception from a simdjson error code.
Definition: error.h:68
simdjson::simdjson_result::value
simdjson_really_inline T & value() &noexcept(false)
Get the result value.
simdjson::simdjson_result::value_unsafe
simdjson_really_inline const T & value_unsafe() const &noexcept
Get the result value.
simdjson::simdjson_error
Exception thrown when an exception-supporting simdjson method is called.
Definition: error.h:63
simdjson::simdjson_error::error
error_code error() const noexcept
The error code.
Definition: error.h:72
simdjson::simdjson_error::what
const char * what() const noexcept
The error message.
Definition: error.h:70
simdjson::simdjson_result::value
simdjson_really_inline T && value() &&noexcept(false)
Take the result value (move it).