diff --git a/include/simdjson/generic/ondemand/document-inl.h b/include/simdjson/generic/ondemand/document-inl.h index cb80dfad9..be1479590 100644 --- a/include/simdjson/generic/ondemand/document-inl.h +++ b/include/simdjson/generic/ondemand/document-inl.h @@ -215,11 +215,6 @@ simdjson_really_inline simdjson_result simdjson_result(first).get(); } -template<> -simdjson_really_inline simdjson_result simdjson_result::get() && noexcept { - if (error()) { return error(); } - return std::forward(first); -} template simdjson_really_inline error_code simdjson_result::get(T &out) & noexcept { if (error()) { return error(); } @@ -230,9 +225,17 @@ simdjson_really_inline error_code simdjson_result(first).get(out); } -template<> -simdjson_really_inline error_code simdjson_result::get(SIMDJSON_IMPLEMENTATION::ondemand::document &out) && noexcept { - return std::forward>(*this).SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base::get(out); + +template<> simdjson_really_inline simdjson_result simdjson_result::get() & noexcept = delete; +template<> simdjson_really_inline simdjson_result simdjson_result::get() && noexcept { + if (error()) { return error(); } + return std::forward(first); +} +template<> simdjson_really_inline error_code simdjson_result::get(SIMDJSON_IMPLEMENTATION::ondemand::document &out) & noexcept = delete; +template<> simdjson_really_inline error_code simdjson_result::get(SIMDJSON_IMPLEMENTATION::ondemand::document &out) && noexcept { + if (error()) { return error(); } + out = std::forward(first); + return SUCCESS; } #if SIMDJSON_EXCEPTIONS diff --git a/include/simdjson/generic/ondemand/value-inl.h b/include/simdjson/generic/ondemand/value-inl.h index f8141a390..f8bfdc4b0 100644 --- a/include/simdjson/generic/ondemand/value-inl.h +++ b/include/simdjson/generic/ondemand/value-inl.h @@ -108,6 +108,7 @@ template<> simdjson_really_inline simdjson_result value::get() & noexc template<> simdjson_really_inline simdjson_result value::get() & noexcept { return get_int64(); } template<> simdjson_really_inline simdjson_result value::get() & noexcept { return get_bool(); } +template<> simdjson_really_inline simdjson_result value::get() && noexcept { return std::forward(*this); } template<> simdjson_really_inline simdjson_result value::get() && noexcept { return std::forward(*this).get_array(); } template<> simdjson_really_inline simdjson_result value::get() && noexcept { return std::forward(*this).get_object(); } template<> simdjson_really_inline simdjson_result value::get() && noexcept { return std::forward(*this).get_raw_json_string(); } @@ -310,27 +311,35 @@ simdjson_really_inline bool simdjson_result(first).is_null(); } -template -simdjson_really_inline simdjson_result simdjson_result::get() & noexcept { +template simdjson_really_inline simdjson_result simdjson_result::get() & noexcept { if (error()) { return error(); } return first.get(); } -template -simdjson_really_inline simdjson_result simdjson_result::get() && noexcept { +template simdjson_really_inline simdjson_result simdjson_result::get() && noexcept { if (error()) { return error(); } return std::forward(first).get(); } -template -simdjson_really_inline error_code simdjson_result::get(T &out) & noexcept { +template simdjson_really_inline error_code simdjson_result::get(T &out) & noexcept { if (error()) { return error(); } return first.get(out); } -template -simdjson_really_inline error_code simdjson_result::get(T &out) && noexcept { +template simdjson_really_inline error_code simdjson_result::get(T &out) && noexcept { if (error()) { return error(); } return std::forward(first).get(out); } +template<> simdjson_really_inline simdjson_result simdjson_result::get() & noexcept = delete; +template<> simdjson_really_inline simdjson_result simdjson_result::get() && noexcept { + if (error()) { return error(); } + return std::forward(first); +} +template<> simdjson_really_inline error_code simdjson_result::get(SIMDJSON_IMPLEMENTATION::ondemand::value &out) & noexcept = delete; +template<> simdjson_really_inline error_code simdjson_result::get(SIMDJSON_IMPLEMENTATION::ondemand::value &out) && noexcept { + if (error()) { return error(); } + out = std::forward(first); + return SUCCESS; +} + #if SIMDJSON_EXCEPTIONS simdjson_really_inline simdjson_result::operator SIMDJSON_IMPLEMENTATION::ondemand::array() noexcept(false) { if (error()) { throw simdjson_error(error()); } diff --git a/tests/ondemand/ondemand_basictests.cpp b/tests/ondemand/ondemand_basictests.cpp index 9ad9f278d..917094851 100644 --- a/tests/ondemand/ondemand_basictests.cpp +++ b/tests/ondemand/ondemand_basictests.cpp @@ -342,73 +342,100 @@ namespace dom_api_tests { TEST_SUCCEED(); } -// bool string_value() { -// TEST_START(); -// auto json = R"([ "hi", "has backslash\\" ])"_padded; -// ondemand::parser parser; -// ondemand::array array; -// ASSERT_SUCCESS( parser.iterate(json).get(array) ); + template + bool test_scalar_value(const padded_string &json, const T &expected) { + SUBTEST( "simdjson_result", test_ondemand_doc(json, [&](auto doc_result) { + T actual; + ASSERT_SUCCESS( doc_result.get(actual) ); + ASSERT_EQUAL( expected, actual ); + return true; + })); + SUBTEST( "document", test_ondemand_doc(json, [&](auto doc_result) { + T actual; + ASSERT_SUCCESS( doc_result.get(actual) ); + ASSERT_EQUAL( expected, actual ); + return true; + })); + padded_string array_json = std::string("[") + std::string(json) + "]"; + SUBTEST( "simdjson_result", test_ondemand_doc(array_json, [&](auto doc_result) { + int count = 0; + for (simdjson_result val_result : doc_result) { + T actual; + ASSERT_SUCCESS( val_result.get(actual) ); + ASSERT_EQUAL(expected, actual); + count++; + } + ASSERT_EQUAL(count, 1); + return true; + })); + SUBTEST( "ondemand::value", test_ondemand_doc(array_json, [&](auto doc_result) { + int count = 0; + for (simdjson_result val_result : doc_result) { + ondemand::value val; + ASSERT_SUCCESS( std::move(val_result).get(val) ); + T actual; + ASSERT_SUCCESS( val.get(actual) ); + ASSERT_EQUAL(expected, actual); + count++; + } + ASSERT_EQUAL(count, 1); + return true; + })); + TEST_SUCCEED(); + } + bool string_value() { + TEST_START(); + return test_scalar_value(R"("hi")"_padded, std::string_view("hi")); + } -// auto iter = array.begin(); -// std::string_view val; -// ASSERT_SUCCESS( (*iter).get(val) ); -// ASSERT_EQUAL( val, "hi" ); + // bool numeric_values() { + // TEST_START(); + // auto json = R"([ 0, 1, -1, 1.1 ])"_padded; + // ondemand::parser parser; + // ondemand::array array; + // ASSERT_SUCCESS( parser.iterate(json).get(array) ); -// ++iter; -// ASSERT_SUCCESS( (*iter).get(val) ); -// ASSERT_EQUAL( val, "has backslash\\" ); + // auto iter = array.begin(); + // ASSERT_EQUAL( (*iter).get().first, 0 ); + // ASSERT_EQUAL( (*iter).get().first, 0 ); + // ASSERT_EQUAL( (*iter).get().first, 0 ); + // ++iter; + // ASSERT_EQUAL( (*iter).get().first, 1 ); + // ASSERT_EQUAL( (*iter).get().first, 1 ); + // ASSERT_EQUAL( (*iter).get().first, 1 ); + // ++iter; + // ASSERT_EQUAL( (*iter).get().first, -1 ); + // ASSERT_EQUAL( (*iter).get().first, -1 ); + // ++iter; + // ASSERT_EQUAL( (*iter).get().first, 1.1 ); + // return true; + // } -// return true; -// } + // bool boolean_values() { + // TEST_START(); + // auto json = R"([ true, false ])"_padded; + // ondemand::parser parser; + // ondemand::array array; + // ASSERT_SUCCESS( parser.iterate(json).get(array) ); -// bool numeric_values() { -// TEST_START(); -// auto json = R"([ 0, 1, -1, 1.1 ])"_padded; -// ondemand::parser parser; -// ondemand::array array; -// ASSERT_SUCCESS( parser.iterate(json).get(array) ); + // auto val = array.begin(); + // ASSERT_EQUAL( (*val).get().first, true ); + // ++val; + // ASSERT_EQUAL( (*val).get().first, false ); + // return true; + // } -// auto iter = array.begin(); -// ASSERT_EQUAL( (*iter).get().first, 0 ); -// ASSERT_EQUAL( (*iter).get().first, 0 ); -// ASSERT_EQUAL( (*iter).get().first, 0 ); -// ++iter; -// ASSERT_EQUAL( (*iter).get().first, 1 ); -// ASSERT_EQUAL( (*iter).get().first, 1 ); -// ASSERT_EQUAL( (*iter).get().first, 1 ); -// ++iter; -// ASSERT_EQUAL( (*iter).get().first, -1 ); -// ASSERT_EQUAL( (*iter).get().first, -1 ); -// ++iter; -// ASSERT_EQUAL( (*iter).get().first, 1.1 ); -// return true; -// } + // bool null_value() { + // TEST_START(); + // auto json = R"([ null ])"_padded; + // ondemand::parser parser; + // ondemand::array array; + // ASSERT_SUCCESS( parser.iterate(json).get(array) ); -// bool boolean_values() { -// TEST_START(); -// auto json = R"([ true, false ])"_padded; -// ondemand::parser parser; -// ondemand::array array; -// ASSERT_SUCCESS( parser.iterate(json).get(array) ); - -// auto val = array.begin(); -// ASSERT_EQUAL( (*val).get().first, true ); -// ++val; -// ASSERT_EQUAL( (*val).get().first, false ); -// return true; -// } - -// bool null_value() { -// TEST_START(); -// auto json = R"([ null ])"_padded; -// ondemand::parser parser; -// ondemand::array array; -// ASSERT_SUCCESS( parser.iterate(json).get(array) ); - -// auto val = array.begin(); -// ASSERT_EQUAL( !(*val).is_null(), 0 ); -// return true; -// } + // auto val = array.begin(); + // ASSERT_EQUAL( !(*val).is_null(), 0 ); + // return true; + // } // bool document_object_index() { // TEST_START(); @@ -684,7 +711,7 @@ namespace dom_api_tests { iterate_empty_array() && iterate_object() && iterate_empty_object() && -// string_value() && + string_value() && // numeric_values() && // boolean_values() && // null_value() &&