Skip to content

Commit 006cc2e

Browse files
committed
Remove simdjson_move_result
1 parent c79cf8d commit 006cc2e

File tree

5 files changed

+21
-76
lines changed

5 files changed

+21
-76
lines changed

include/simdjson/document.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ class document::key_value_pair {
926926
class document::element_result : public simdjson_result<document::element> {
927927
public:
928928
really_inline element_result() noexcept;
929-
really_inline element_result(element value) noexcept;
929+
really_inline element_result(element &&value) noexcept;
930930
really_inline element_result(error_code error) noexcept;
931931

932932
/** Whether this is a JSON `null` */

include/simdjson/error.h

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ struct simdjson_result : public std::pair<T, error_code> {
8585
/**
8686
* Move the value and the error to the provided variables.
8787
*/
88-
void tie(T& t, error_code & e) {
88+
void tie(T& t, error_code & e) && noexcept {
8989
// on the clang compiler that comes with current macOS (Apple clang version 11.0.0),
9090
// tie(width, error) = size["w"].as_uint64_t();
9191
// fails with "error: no viable overloaded '='""
92-
t = std::move(this->first);
93-
e = std::move(this->second);
92+
t = std::forward<simdjson_result<T>>(*this).first;
93+
e = this->second;
9494
}
9595

9696
/**
@@ -105,106 +105,51 @@ struct simdjson_result : public std::pair<T, error_code> {
105105
*
106106
* @throw simdjson_error if there was an error.
107107
*/
108-
T get() noexcept(false) {
108+
T& get() noexcept(false) {
109109
if (error()) { throw simdjson_error(error()); }
110110
return this->first;
111111
};
112112

113-
/**
114-
* Cast to the value (will throw on error).
115-
*
116-
* @throw simdjson_error if there was an error.
117-
*/
118-
operator T() noexcept(false) { return get(); }
119-
120-
#endif // SIMDJSON_EXCEPTIONS
121-
122-
/**
123-
* Create a new empty result with error = UNINITIALIZED.
124-
*/
125-
simdjson_result() noexcept : simdjson_result(UNINITIALIZED) {}
126-
127-
/**
128-
* Create a new error result.
129-
*/
130-
simdjson_result(error_code _error) noexcept : std::pair<T, error_code>({}, _error) {}
131-
132-
/**
133-
* Create a new successful result.
134-
*/
135-
simdjson_result(T _value) noexcept : std::pair<T, error_code>(_value, SUCCESS) {}
136-
137-
/**
138-
* Create a new result with both things (use if you don't want to branch when creating the result).
139-
*/
140-
simdjson_result(T value, error_code error) noexcept : std::pair<T, error_code>(value, error) {}
141-
};
142-
143-
/**
144-
* The result of a simd operation that could fail.
145-
*
146-
* This class is for values that must be *moved*, like padded_string and document.
147-
*
148-
* Gives the option of reading error codes, or throwing an exception by casting to the desired result.
149-
*/
150-
template<typename T>
151-
struct simdjson_move_result : std::pair<T, error_code> {
152-
/**
153-
* Move the value and the error to the provided variables.
154-
*/
155-
void tie(T& t, error_code & e) {
156-
// on the clang compiler that comes with current macOS (Apple clang version 11.0.0),
157-
// std::tie(this->json, error) = padded_string::load(filename);
158-
// fails with "benchmark/benchmarker.h:266:33: error: no viable overloaded '='""
159-
t = std::move(this->first);
160-
e = std::move(this->second);
161-
}
162-
163-
/**
164-
* The error.
165-
*/
166-
error_code error() const { return this->second; }
167-
168-
#if SIMDJSON_EXCEPTIONS
169-
170113
/**
171114
* The value of the function.
172115
*
173116
* @throw simdjson_error if there was an error.
174117
*/
175-
T move() noexcept(false) {
118+
T&& take() && {
176119
if (error()) { throw simdjson_error(error()); }
177-
return std::move(this->first);
120+
return std::forward<T>(this->first);
178121
};
179122

180123
/**
181124
* Cast to the value (will throw on error).
182125
*
183126
* @throw simdjson_error if there was an error.
184127
*/
185-
operator T() noexcept(false) { return move(); }
128+
operator T&&() && {
129+
return std::forward<simdjson_result<T>>(*this).take();
130+
}
186131

187-
#endif
132+
#endif // SIMDJSON_EXCEPTIONS
188133

189134
/**
190135
* Create a new empty result with error = UNINITIALIZED.
191136
*/
192-
simdjson_move_result() noexcept : simdjson_move_result(UNINITIALIZED) {}
137+
simdjson_result() noexcept : simdjson_result(UNINITIALIZED) {}
193138

194139
/**
195140
* Create a new error result.
196141
*/
197-
simdjson_move_result(error_code error) noexcept : std::pair<T, error_code>(T(), error) {}
142+
simdjson_result(error_code error) noexcept : std::pair<T, error_code>(T{}, error) {}
198143

199144
/**
200145
* Create a new successful result.
201146
*/
202-
simdjson_move_result(T value) noexcept : std::pair<T, error_code>(std::move(value), SUCCESS) {}
147+
simdjson_result(T &&value) noexcept : std::pair<T, error_code>(std::forward<T>(value), SUCCESS) {}
203148

204149
/**
205150
* Create a new result with both things (use if you don't want to branch when creating the result).
206151
*/
207-
simdjson_move_result(T value, error_code error) noexcept : std::pair<T, error_code>(std::move(value), error) {}
152+
simdjson_result(T &&value, error_code error) noexcept : std::pair<T, error_code>(std::forward<T>(value), error) {}
208153
};
209154

210155
/**

include/simdjson/inline/document.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace simdjson {
1717
// element_result inline implementation
1818
//
1919
really_inline document::element_result::element_result() noexcept : simdjson_result<element>() {}
20-
really_inline document::element_result::element_result(element value) noexcept : simdjson_result<element>(value) {}
20+
really_inline document::element_result::element_result(element &&value) noexcept : simdjson_result<element>((element&&)value) {}
2121
really_inline document::element_result::element_result(error_code error) noexcept : simdjson_result<element>(error) {}
2222
inline simdjson_result<bool> document::element_result::is_null() const noexcept {
2323
if (error()) { return error(); }
@@ -113,7 +113,7 @@ inline document::element_result::operator document::object() const noexcept(fals
113113
// array_result inline implementation
114114
//
115115
really_inline document::array_result::array_result() noexcept : simdjson_result<array>() {}
116-
really_inline document::array_result::array_result(array value) noexcept : simdjson_result<array>(value) {}
116+
really_inline document::array_result::array_result(array value) noexcept : simdjson_result<array>((array&&)value) {}
117117
really_inline document::array_result::array_result(error_code error) noexcept : simdjson_result<array>(error) {}
118118

119119
#if SIMDJSON_EXCEPTIONS
@@ -149,7 +149,7 @@ inline document::element_result document::array_result::at(size_t index) const n
149149
// object_result inline implementation
150150
//
151151
really_inline document::object_result::object_result() noexcept : simdjson_result<object>() {}
152-
really_inline document::object_result::object_result(object value) noexcept : simdjson_result<object>(value) {}
152+
really_inline document::object_result::object_result(object value) noexcept : simdjson_result<object>((object&&)value) {}
153153
really_inline document::object_result::object_result(error_code error) noexcept : simdjson_result<object>(error) {}
154154

155155
inline document::element_result document::object_result::operator[](std::string_view json_pointer) const noexcept {

include/simdjson/inline/padded_string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ inline const char *padded_string::data() const noexcept { return data_ptr; }
9898

9999
inline char *padded_string::data() noexcept { return data_ptr; }
100100

101-
inline simdjson_move_result<padded_string> padded_string::load(const std::string &filename) noexcept {
101+
inline simdjson_result<padded_string> padded_string::load(const std::string &filename) noexcept {
102102
// Open the file
103103
std::FILE *fp = std::fopen(filename.c_str(), "rb");
104104
if (fp == nullptr) {
@@ -131,7 +131,7 @@ inline simdjson_move_result<padded_string> padded_string::load(const std::string
131131
return IO_ERROR;
132132
}
133133

134-
return std::move(s);
134+
return s;
135135
}
136136

137137
} // namespace simdjson

include/simdjson/padded_string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct padded_string final {
9494
*
9595
* @param path the path to the file.
9696
**/
97-
inline static simdjson_move_result<padded_string> load(const std::string &path) noexcept;
97+
inline static simdjson_result<padded_string> load(const std::string &path) noexcept;
9898

9999
private:
100100
padded_string &operator=(const padded_string &o) = delete;

0 commit comments

Comments
 (0)