simdjson  0.3.1
Ridiculously Fast JSON
error.h
1 #ifndef SIMDJSON_INLINE_ERROR_H
2 #define SIMDJSON_INLINE_ERROR_H
3 
4 #include "simdjson/error.h"
5 #include <string>
6 
7 namespace simdjson::internal {
8  // We store the error code so we can validate the error message is associated with the right code
9  struct error_code_info {
10  error_code code;
11  std::string message;
12  };
13  // These MUST match the codes in error_code. We check this constraint in basictests.
14  inline const error_code_info error_codes[] {
15  { SUCCESS, "No error" },
16  { SUCCESS_AND_HAS_MORE, "No error and buffer still has more data" },
17  { CAPACITY, "This parser can't support a document that big" },
18  { MEMALLOC, "Error allocating memory, we're most likely out of memory" },
19  { TAPE_ERROR, "Something went wrong while writing to the tape" },
20  { DEPTH_ERROR, "The JSON document was too deep (too many nested objects and arrays)" },
21  { STRING_ERROR, "Problem while parsing a string" },
22  { T_ATOM_ERROR, "Problem while parsing an atom starting with the letter 't'" },
23  { F_ATOM_ERROR, "Problem while parsing an atom starting with the letter 'f'" },
24  { N_ATOM_ERROR, "Problem while parsing an atom starting with the letter 'n'" },
25  { NUMBER_ERROR, "Problem while parsing a number" },
26  { UTF8_ERROR, "The input is not valid UTF-8" },
27  { UNINITIALIZED, "Uninitialized" },
28  { EMPTY, "Empty: no JSON found" },
29  { UNESCAPED_CHARS, "Within strings, some characters must be escaped, we found unescaped characters" },
30  { UNCLOSED_STRING, "A string is opened, but never closed." },
31  { UNSUPPORTED_ARCHITECTURE, "simdjson does not have an implementation supported by this CPU architecture (perhaps it's a non-SIMD CPU?)." },
32  { INCORRECT_TYPE, "The JSON element does not have the requested type." },
33  { NUMBER_OUT_OF_RANGE, "The JSON number is too large or too small to fit within the requested type." },
34  { INDEX_OUT_OF_BOUNDS, "Attempted to access an element of a JSON array that is beyond its length." },
35  { NO_SUCH_FIELD, "The JSON field referenced does not exist in this object." },
36  { IO_ERROR, "Error reading the file." },
37  { INVALID_JSON_POINTER, "Invalid JSON pointer syntax." },
38  { INVALID_URI_FRAGMENT, "Invalid URI fragment syntax." },
39  { UNEXPECTED_ERROR, "Unexpected error, consider reporting this problem as you may have found a bug in simdjson" }
40  }; // error_messages[]
41 } // namespace simdjson::internal
42 
43 namespace simdjson {
44 
45 inline const char *error_message(error_code error) noexcept {
46  // If you're using error_code, we're trusting you got it from the enum.
47  return internal::error_codes[int(error)].message.c_str();
48 }
49 
50 inline const std::string &error_message(int error) noexcept {
51  if (error < 0 || error >= error_code::NUM_ERROR_CODES) {
52  return internal::error_codes[UNEXPECTED_ERROR].message;
53  }
54  return internal::error_codes[error].message;
55 }
56 
57 inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept {
58  return out << error_message(error);
59 }
60 
61 namespace internal {
62 
63 //
64 // internal::simdjson_result_base<T> inline implementation
65 //
66 
67 template<typename T>
68 really_inline void simdjson_result_base<T>::tie(T &value, error_code &error) && noexcept {
69  // on the clang compiler that comes with current macOS (Apple clang version 11.0.0),
70  // tie(width, error) = size["w"].get<uint64_t>();
71  // fails with "error: no viable overloaded '='""
72  value = std::forward<simdjson_result_base<T>>(*this).first;
73  error = this->second;
74 }
75 
76 template<typename T>
77 really_inline error_code simdjson_result_base<T>::error() const noexcept {
78  return this->second;
79 }
80 
81 #if SIMDJSON_EXCEPTIONS
82 
83 template<typename T>
84 really_inline T& simdjson_result_base<T>::value() noexcept(false) {
85  if (error()) { throw simdjson_error(error()); }
86  return this->first;
87 }
88 
89 template<typename T>
90 really_inline T&& simdjson_result_base<T>::take_value() && noexcept(false) {
91  if (error()) { throw simdjson_error(error()); }
92  return std::forward<T>(this->first);
93 }
94 
95 template<typename T>
96 really_inline simdjson_result_base<T>::operator T&&() && noexcept(false) {
97  return std::forward<simdjson_result_base<T>>(*this).take_value();
98 }
99 
100 #endif // SIMDJSON_EXCEPTIONS
101 
102 template<typename T>
103 really_inline simdjson_result_base<T>::simdjson_result_base(T &&value, error_code error) noexcept
104  : std::pair<T, error_code>(std::forward<T>(value), error) {}
105 template<typename T>
106 really_inline simdjson_result_base<T>::simdjson_result_base(error_code error) noexcept
107  : simdjson_result_base(T{}, error) {}
108 template<typename T>
109 really_inline simdjson_result_base<T>::simdjson_result_base(T &&value) noexcept
110  : simdjson_result_base(std::forward<T>(value), SUCCESS) {}
111 template<typename T>
112 really_inline simdjson_result_base<T>::simdjson_result_base() noexcept
113  : simdjson_result_base(T{}, UNINITIALIZED) {}
114 
115 } // namespace internal
116 
120 
121 template<typename T>
122 really_inline void simdjson_result<T>::tie(T &value, error_code &error) && noexcept {
123  std::forward<internal::simdjson_result_base<T>>(*this).tie(value, error);
124 }
125 
126 template<typename T>
127 really_inline error_code simdjson_result<T>::error() const noexcept {
128  return internal::simdjson_result_base<T>::error();
129 }
130 
131 #if SIMDJSON_EXCEPTIONS
132 
133 template<typename T>
134 really_inline T& simdjson_result<T>::value() noexcept(false) {
135  return internal::simdjson_result_base<T>::value();
136 }
137 
138 template<typename T>
139 really_inline T&& simdjson_result<T>::take_value() && noexcept(false) {
140  return std::forward<internal::simdjson_result_base<T>>(*this).take_value();
141 }
142 
143 template<typename T>
144 really_inline simdjson_result<T>::operator T&&() && noexcept(false) {
145  return std::forward<internal::simdjson_result_base<T>>(*this).take_value();
146 }
147 
148 #endif // SIMDJSON_EXCEPTIONS
149 
150 template<typename T>
151 really_inline simdjson_result<T>::simdjson_result(T &&value, error_code error) noexcept
152  : internal::simdjson_result_base<T>(std::forward<T>(value), error) {}
153 template<typename T>
154 really_inline simdjson_result<T>::simdjson_result(error_code error) noexcept
155  : internal::simdjson_result_base<T>(error) {}
156 template<typename T>
157 really_inline simdjson_result<T>::simdjson_result(T &&value) noexcept
158  : internal::simdjson_result_base<T>(std::forward<T>(value)) {}
159 template<typename T>
160 really_inline simdjson_result<T>::simdjson_result() noexcept
161  : internal::simdjson_result_base<T>() {}
162 
163 } // namespace simdjson
164 
165 #endif // SIMDJSON_INLINE_ERROR_H
simdjson::simdjson_result::value
really_inline T & value() noexcept(false)
Get the result value.
Definition: error.h:134
simdjson::simdjson_result
The result of a simdjson operation that could fail.
Definition: error.h:166
simdjson::simdjson_result::take_value
really_inline T && take_value() &&noexcept(false)
Take the result value (move it).
Definition: error.h:139
simdjson::simdjson_result::tie
really_inline void tie(T &t, error_code &e) &&noexcept
Move the value and the error to the provided variables.
Definition: error.h:122
simdjson::simdjson_result::error
really_inline error_code error() const noexcept
The error.
Definition: error.h:127