1namespace simdjson {
2namespace SIMDJSON_IMPLEMENTATION {
3
4// This is a near copy of include/error.h's implementation_simdjson_result_base, except it doesn't use std::pair
5// so we can avoid inlining errors
6// TODO reconcile these!
7/**
8 * The result of a simdjson operation that could fail.
9 *
10 * Gives the option of reading error codes, or throwing an exception by casting to the desired result.
11 *
12 * This is a base class for implementations that want to add functions to the result type for
13 * chaining.
14 *
15 * Override like:
16 *
17 * struct simdjson_result<T> : public internal::implementation_simdjson_result_base<T> {
18 * simdjson_result() noexcept : internal::implementation_simdjson_result_base<T>() {}
19 * simdjson_result(error_code error) noexcept : internal::implementation_simdjson_result_base<T>(error) {}
20 * simdjson_result(T &&value) noexcept : internal::implementation_simdjson_result_base<T>(std::forward(value)) {}
21 * simdjson_result(T &&value, error_code error) noexcept : internal::implementation_simdjson_result_base<T>(value, error) {}
22 * // Your extra methods here
23 * }
24 *
25 * Then any method returning simdjson_result<T> will be chainable with your methods.
26 */
27template<typename T>
28struct implementation_simdjson_result_base {
29
30 /**
31 * Create a new empty result with error = UNINITIALIZED.
32 */
33 simdjson_inline implementation_simdjson_result_base() noexcept = default;
34
35 /**
36 * Create a new error result.
37 */
38 simdjson_inline implementation_simdjson_result_base(error_code error) noexcept;
39
40 /**
41 * Create a new successful result.
42 */
43 simdjson_inline implementation_simdjson_result_base(T &&value) noexcept;
44
45 /**
46 * Create a new result with both things (use if you don't want to branch when creating the result).
47 */
48 simdjson_inline implementation_simdjson_result_base(T &&value, error_code error) noexcept;
49
50 /**
51 * Move the value and the error to the provided variables.
52 *
53 * @param value The variable to assign the value to. May not be set if there is an error.
54 * @param error The variable to assign the error to. Set to SUCCESS if there is no error.
55 */
56 simdjson_inline void tie(T &value, error_code &error) && noexcept;
57
58 /**
59 * Move the value to the provided variable.
60 *
61 * @param value The variable to assign the value to. May not be set if there is an error.
62 */
63 simdjson_inline error_code get(T &value) && noexcept;
64
65 /**
66 * The error.
67 */
68 simdjson_inline error_code error() const noexcept;
69
70#if SIMDJSON_EXCEPTIONS
71
72 /**
73 * Get the result value.
74 *
75 * @throw simdjson_error if there was an error.
76 */
77 simdjson_inline T& value() & noexcept(false);
78
79 /**
80 * Take the result value (move it).
81 *
82 * @throw simdjson_error if there was an error.
83 */
84 simdjson_inline T&& value() && noexcept(false);
85
86 /**
87 * Take the result value (move it).
88 *
89 * @throw simdjson_error if there was an error.
90 */
91 simdjson_inline T&& take_value() && noexcept(false);
92
93 /**
94 * Cast to the value (will throw on error).
95 *
96 * @throw simdjson_error if there was an error.
97 */
98 simdjson_inline operator T&&() && noexcept(false);
99
100
101#endif // SIMDJSON_EXCEPTIONS
102
103 /**
104 * Get the result value. This function is safe if and only
105 * the error() method returns a value that evaluates to false.
106 */
107 simdjson_inline const T& value_unsafe() const& noexcept;
108 /**
109 * Get the result value. This function is safe if and only
110 * the error() method returns a value that evaluates to false.
111 */
112 simdjson_inline T& value_unsafe() & noexcept;
113 /**
114 * Take the result value (move it). This function is safe if and only
115 * the error() method returns a value that evaluates to false.
116 */
117 simdjson_inline T&& value_unsafe() && noexcept;
118protected:
119 /** users should never directly access first and second. **/
120 T first{}; /** Users should never directly access 'first'. **/
121 error_code second{UNINITIALIZED}; /** Users should never directly access 'second'. **/
122}; // struct implementation_simdjson_result_base
123
124} // namespace SIMDJSON_IMPLEMENTATION
125} // namespace simdjson
126