simdjson  3.11.0
Ridiculously Fast JSON
error-inl.h
1 #ifndef SIMDJSON_ERROR_INL_H
2 #define SIMDJSON_ERROR_INL_H
3 
4 #include "simdjson/error.h"
5 
6 #include <iostream>
7 
8 namespace simdjson {
9 namespace internal {
10  // We store the error code so we can validate the error message is associated with the right code
11  struct error_code_info {
12  error_code code;
13  const char* message; // do not use a fancy std::string where a simple C string will do (no alloc, no destructor)
14  };
15  // These MUST match the codes in error_code. We check this constraint in basictests.
16  extern SIMDJSON_DLLIMPORTEXPORT const error_code_info error_codes[];
17 } // namespace internal
18 
19 
20 inline const char *error_message(error_code error) noexcept {
21  // If you're using error_code, we're trusting you got it from the enum.
22  return internal::error_codes[int(error)].message;
23 }
24 
25 // deprecated function
26 #ifndef SIMDJSON_DISABLE_DEPRECATED_API
27 inline const std::string error_message(int error) noexcept {
28  if (error < 0 || error >= error_code::NUM_ERROR_CODES) {
29  return internal::error_codes[UNEXPECTED_ERROR].message;
30  }
31  return internal::error_codes[error].message;
32 }
33 #endif // SIMDJSON_DISABLE_DEPRECATED_API
34 
35 inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept {
36  return out << error_message(error);
37 }
38 
39 namespace internal {
40 
41 //
42 // internal::simdjson_result_base<T> inline implementation
43 //
44 
45 template<typename T>
46 simdjson_inline void simdjson_result_base<T>::tie(T &value, error_code &error) && noexcept {
47  error = this->second;
48  if (!error) {
49  value = std::forward<simdjson_result_base<T>>(*this).first;
50  }
51 }
52 
53 template<typename T>
54 simdjson_warn_unused simdjson_inline error_code simdjson_result_base<T>::get(T &value) && noexcept {
55  error_code error;
56  std::forward<simdjson_result_base<T>>(*this).tie(value, error);
57  return error;
58 }
59 
60 template<typename T>
61 simdjson_inline error_code simdjson_result_base<T>::error() const noexcept {
62  return this->second;
63 }
64 
65 #if SIMDJSON_EXCEPTIONS
66 
67 template<typename T>
68 simdjson_inline T& simdjson_result_base<T>::value() & noexcept(false) {
69  if (error()) { throw simdjson_error(error()); }
70  return this->first;
71 }
72 
73 template<typename T>
74 simdjson_inline T&& simdjson_result_base<T>::value() && noexcept(false) {
75  return std::forward<simdjson_result_base<T>>(*this).take_value();
76 }
77 
78 template<typename T>
79 simdjson_inline T&& simdjson_result_base<T>::take_value() && noexcept(false) {
80  if (error()) { throw simdjson_error(error()); }
81  return std::forward<T>(this->first);
82 }
83 
84 template<typename T>
85 simdjson_inline simdjson_result_base<T>::operator T&&() && noexcept(false) {
86  return std::forward<simdjson_result_base<T>>(*this).take_value();
87 }
88 
89 #endif // SIMDJSON_EXCEPTIONS
90 
91 template<typename T>
92 simdjson_inline const T& simdjson_result_base<T>::value_unsafe() const& noexcept {
93  return this->first;
94 }
95 
96 template<typename T>
97 simdjson_inline T&& simdjson_result_base<T>::value_unsafe() && noexcept {
98  return std::forward<T>(this->first);
99 }
100 
101 template<typename T>
102 simdjson_inline simdjson_result_base<T>::simdjson_result_base(T &&value, error_code error) noexcept
103  : std::pair<T, error_code>(std::forward<T>(value), error) {}
104 template<typename T>
105 simdjson_inline simdjson_result_base<T>::simdjson_result_base(error_code error) noexcept
106  : simdjson_result_base(T{}, error) {}
107 template<typename T>
108 simdjson_inline simdjson_result_base<T>::simdjson_result_base(T &&value) noexcept
109  : simdjson_result_base(std::forward<T>(value), SUCCESS) {}
110 template<typename T>
111 simdjson_inline simdjson_result_base<T>::simdjson_result_base() noexcept
112  : simdjson_result_base(T{}, UNINITIALIZED) {}
113 
114 } // namespace internal
115 
119 
120 template<typename T>
121 simdjson_inline void simdjson_result<T>::tie(T &value, error_code &error) && noexcept {
122  std::forward<internal::simdjson_result_base<T>>(*this).tie(value, error);
123 }
124 
125 template<typename T>
126 simdjson_warn_unused simdjson_inline error_code simdjson_result<T>::get(T &value) && noexcept {
127  return std::forward<internal::simdjson_result_base<T>>(*this).get(value);
128 }
129 
130 template<typename T>
131 simdjson_inline error_code simdjson_result<T>::error() const noexcept {
132  return internal::simdjson_result_base<T>::error();
133 }
134 
135 #if SIMDJSON_EXCEPTIONS
136 
137 template<typename T>
138 simdjson_inline T& simdjson_result<T>::value() & noexcept(false) {
139  return internal::simdjson_result_base<T>::value();
140 }
141 
142 template<typename T>
143 simdjson_inline T&& simdjson_result<T>::value() && noexcept(false) {
144  return std::forward<internal::simdjson_result_base<T>>(*this).value();
145 }
146 
147 template<typename T>
148 simdjson_inline T&& simdjson_result<T>::take_value() && noexcept(false) {
149  return std::forward<internal::simdjson_result_base<T>>(*this).take_value();
150 }
151 
152 template<typename T>
153 simdjson_inline simdjson_result<T>::operator T&&() && noexcept(false) {
154  return std::forward<internal::simdjson_result_base<T>>(*this).take_value();
155 }
156 
157 #endif // SIMDJSON_EXCEPTIONS
158 
159 template<typename T>
160 simdjson_inline const T& simdjson_result<T>::value_unsafe() const& noexcept {
161  return internal::simdjson_result_base<T>::value_unsafe();
162 }
163 
164 template<typename T>
165 simdjson_inline T&& simdjson_result<T>::value_unsafe() && noexcept {
166  return std::forward<internal::simdjson_result_base<T>>(*this).value_unsafe();
167 }
168 
169 template<typename T>
170 simdjson_inline simdjson_result<T>::simdjson_result(T &&value, error_code error) noexcept
171  : internal::simdjson_result_base<T>(std::forward<T>(value), error) {}
172 template<typename T>
173 simdjson_inline simdjson_result<T>::simdjson_result(error_code error) noexcept
174  : internal::simdjson_result_base<T>(error) {}
175 template<typename T>
176 simdjson_inline simdjson_result<T>::simdjson_result(T &&value) noexcept
177  : internal::simdjson_result_base<T>(std::forward<T>(value)) {}
178 template<typename T>
179 simdjson_inline simdjson_result<T>::simdjson_result() noexcept
180  : internal::simdjson_result_base<T>() {}
181 
182 } // namespace simdjson
183 
184 #endif // SIMDJSON_ERROR_INL_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
It is the convention throughout the code that the macro SIMDJSON_DEVELOPMENT_CHECKS determines whethe...
Definition: error-inl.h:20
error_code
All possible errors returned by simdjson.
Definition: error.h:19
@ SUCCESS
No error.
Definition: error.h:20
@ UNEXPECTED_ERROR
indicative of a bug in simdjson
Definition: error.h:44
@ UNINITIALIZED
unknown error, or uninitialized document
Definition: error.h:32
The result of a simdjson operation that could fail.
Definition: error.h:215
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131
simdjson_inline const T & value_unsafe() const &noexcept
Get the result value.
simdjson_inline T && take_value() &&noexcept(false)
Take the result value (move it).
Definition: error-inl.h:148
simdjson_inline void tie(T &value, error_code &error) &&noexcept
Move the value and the error to the provided variables.
Definition: error-inl.h:121
simdjson_warn_unused simdjson_inline error_code get(T &value) &&noexcept
Move the value to the provided variable.
Definition: error-inl.h:126
simdjson_inline T & value() &noexcept(false)
Get the result value.