Remove simdjson_move_result

This commit is contained in:
John Keiser 2020-03-26 11:55:41 -07:00
parent c79cf8d6bf
commit 006cc2ed60
5 changed files with 27 additions and 82 deletions

View File

@ -926,7 +926,7 @@ private:
class document::element_result : public simdjson_result<document::element> {
public:
really_inline element_result() noexcept;
really_inline element_result(element value) noexcept;
really_inline element_result(element &&value) noexcept;
really_inline element_result(error_code error) noexcept;
/** Whether this is a JSON `null` */

View File

@ -85,12 +85,12 @@ struct simdjson_result : public std::pair<T, error_code> {
/**
* Move the value and the error to the provided variables.
*/
void tie(T& t, error_code & e) {
void tie(T& t, error_code & e) && noexcept {
// on the clang compiler that comes with current macOS (Apple clang version 11.0.0),
// tie(width, error) = size["w"].as_uint64_t();
// fails with "error: no viable overloaded '='""
t = std::move(this->first);
e = std::move(this->second);
t = std::forward<simdjson_result<T>>(*this).first;
e = this->second;
}
/**
@ -105,17 +105,29 @@ struct simdjson_result : public std::pair<T, error_code> {
*
* @throw simdjson_error if there was an error.
*/
T get() noexcept(false) {
T& get() noexcept(false) {
if (error()) { throw simdjson_error(error()); }
return this->first;
};
/**
* The value of the function.
*
* @throw simdjson_error if there was an error.
*/
T&& take() && {
if (error()) { throw simdjson_error(error()); }
return std::forward<T>(this->first);
};
/**
* Cast to the value (will throw on error).
*
* @throw simdjson_error if there was an error.
*/
operator T() noexcept(false) { return get(); }
operator T&&() && {
return std::forward<simdjson_result<T>>(*this).take();
}
#endif // SIMDJSON_EXCEPTIONS
@ -127,84 +139,17 @@ struct simdjson_result : public std::pair<T, error_code> {
/**
* Create a new error result.
*/
simdjson_result(error_code _error) noexcept : std::pair<T, error_code>({}, _error) {}
simdjson_result(error_code error) noexcept : std::pair<T, error_code>(T{}, error) {}
/**
* Create a new successful result.
*/
simdjson_result(T _value) noexcept : std::pair<T, error_code>(_value, SUCCESS) {}
simdjson_result(T &&value) noexcept : std::pair<T, error_code>(std::forward<T>(value), SUCCESS) {}
/**
* Create a new result with both things (use if you don't want to branch when creating the result).
*/
simdjson_result(T value, error_code error) noexcept : std::pair<T, error_code>(value, error) {}
};
/**
* The result of a simd operation that could fail.
*
* This class is for values that must be *moved*, like padded_string and document.
*
* Gives the option of reading error codes, or throwing an exception by casting to the desired result.
*/
template<typename T>
struct simdjson_move_result : std::pair<T, error_code> {
/**
* Move the value and the error to the provided variables.
*/
void tie(T& t, error_code & e) {
// on the clang compiler that comes with current macOS (Apple clang version 11.0.0),
// std::tie(this->json, error) = padded_string::load(filename);
// fails with "benchmark/benchmarker.h:266:33: error: no viable overloaded '='""
t = std::move(this->first);
e = std::move(this->second);
}
/**
* The error.
*/
error_code error() const { return this->second; }
#if SIMDJSON_EXCEPTIONS
/**
* The value of the function.
*
* @throw simdjson_error if there was an error.
*/
T move() noexcept(false) {
if (error()) { throw simdjson_error(error()); }
return std::move(this->first);
};
/**
* Cast to the value (will throw on error).
*
* @throw simdjson_error if there was an error.
*/
operator T() noexcept(false) { return move(); }
#endif
/**
* Create a new empty result with error = UNINITIALIZED.
*/
simdjson_move_result() noexcept : simdjson_move_result(UNINITIALIZED) {}
/**
* Create a new error result.
*/
simdjson_move_result(error_code error) noexcept : std::pair<T, error_code>(T(), error) {}
/**
* Create a new successful result.
*/
simdjson_move_result(T value) noexcept : std::pair<T, error_code>(std::move(value), SUCCESS) {}
/**
* Create a new result with both things (use if you don't want to branch when creating the result).
*/
simdjson_move_result(T value, error_code error) noexcept : std::pair<T, error_code>(std::move(value), error) {}
simdjson_result(T &&value, error_code error) noexcept : std::pair<T, error_code>(std::forward<T>(value), error) {}
};
/**

View File

@ -17,7 +17,7 @@ namespace simdjson {
// element_result inline implementation
//
really_inline document::element_result::element_result() noexcept : simdjson_result<element>() {}
really_inline document::element_result::element_result(element value) noexcept : simdjson_result<element>(value) {}
really_inline document::element_result::element_result(element &&value) noexcept : simdjson_result<element>((element&&)value) {}
really_inline document::element_result::element_result(error_code error) noexcept : simdjson_result<element>(error) {}
inline simdjson_result<bool> document::element_result::is_null() const noexcept {
if (error()) { return error(); }
@ -113,7 +113,7 @@ inline document::element_result::operator document::object() const noexcept(fals
// array_result inline implementation
//
really_inline document::array_result::array_result() noexcept : simdjson_result<array>() {}
really_inline document::array_result::array_result(array value) noexcept : simdjson_result<array>(value) {}
really_inline document::array_result::array_result(array value) noexcept : simdjson_result<array>((array&&)value) {}
really_inline document::array_result::array_result(error_code error) noexcept : simdjson_result<array>(error) {}
#if SIMDJSON_EXCEPTIONS
@ -149,7 +149,7 @@ inline document::element_result document::array_result::at(size_t index) const n
// object_result inline implementation
//
really_inline document::object_result::object_result() noexcept : simdjson_result<object>() {}
really_inline document::object_result::object_result(object value) noexcept : simdjson_result<object>(value) {}
really_inline document::object_result::object_result(object value) noexcept : simdjson_result<object>((object&&)value) {}
really_inline document::object_result::object_result(error_code error) noexcept : simdjson_result<object>(error) {}
inline document::element_result document::object_result::operator[](std::string_view json_pointer) const noexcept {

View File

@ -98,7 +98,7 @@ inline const char *padded_string::data() const noexcept { return data_ptr; }
inline char *padded_string::data() noexcept { return data_ptr; }
inline simdjson_move_result<padded_string> padded_string::load(const std::string &filename) noexcept {
inline simdjson_result<padded_string> padded_string::load(const std::string &filename) noexcept {
// Open the file
std::FILE *fp = std::fopen(filename.c_str(), "rb");
if (fp == nullptr) {
@ -131,7 +131,7 @@ inline simdjson_move_result<padded_string> padded_string::load(const std::string
return IO_ERROR;
}
return std::move(s);
return s;
}
} // namespace simdjson

View File

@ -94,7 +94,7 @@ struct padded_string final {
*
* @param path the path to the file.
**/
inline static simdjson_move_result<padded_string> load(const std::string &path) noexcept;
inline static simdjson_result<padded_string> load(const std::string &path) noexcept;
private:
padded_string &operator=(const padded_string &o) = delete;