Minimal tag_invokes for STL types

This commit is contained in:
M. Bahoosh
2024-09-26 09:27:38 -10:00
parent 49b9860899
commit bbb3cb80a6
14 changed files with 347 additions and 128 deletions
+1
View File
@@ -30,6 +30,7 @@ add_cpp_test(ondemand_wrong_type_error_tests LABELS ondemand acceptance
add_cpp_test(ondemand_iterate_many_csv LABELS ondemand acceptance per_implementation)
add_cpp_test(ondemand_custom_types_tests LABELS ondemand acceptance per_implementation)
add_cpp_test(ondemand_custom_types_document_tests LABELS ondemand acceptance per_implementation)
add_cpp_test(ondemand_stl_types_tests LABELS ondemand acceptance per_implementation)
if(NOT SIMDJSON_SANITIZE)
add_cpp_test(ondemand_cacheline LABELS ondemand acceptance per_implementation)
endif()
@@ -1,5 +1,6 @@
#include "simdjson.h"
#include "test_ondemand.h"
#include "simdjson/std/vector.h"
#include <string>
#include <vector>
@@ -63,10 +64,10 @@ struct Car {
}
};
static_assert(simdjson::deserializable<std::unique_ptr<Car>, simdjson::ondemand::value>, "It should be invocable");
static_assert(simdjson::deserializable<std::unique_ptr<Car>, simdjson::ondemand::document>, "Tag_invoke should work with document as well.");
static_assert(simdjson::deserializable<std::vector<Car>, simdjson::ondemand::value>, "It should be invocable");
static_assert(simdjson::deserializable<std::vector<Car>, simdjson::ondemand::document>, "Tag_invoke should work with document as well.");
static_assert(simdjson::custom_deserializable<std::unique_ptr<Car>, simdjson::ondemand::value>, "It should be invocable");
static_assert(simdjson::custom_deserializable<std::unique_ptr<Car>, simdjson::ondemand::document>, "Tag_invoke should work with document as well.");
static_assert(simdjson::custom_deserializable<std::vector<Car>, simdjson::ondemand::value>, "It should be invocable");
static_assert(simdjson::custom_deserializable<std::vector<Car>, simdjson::ondemand::document>, "Tag_invoke should work with document as well.");
bool custom_test() {
TEST_START();
@@ -1,10 +1,11 @@
#include "simdjson.h"
#include "simdjson/std/vector.h"
#include "test_ondemand.h"
#include <string>
#include <vector>
#ifdef __cpp_concepts
#ifdef SIMDJSON_SUPPORTS_DESERIALIZATION
template <typename T>
struct is_unique_ptr : std::false_type {
@@ -27,7 +28,7 @@ namespace simdjson {
// This tag_invoke MUST be inside simdjson namespace
template <typename T>
requires is_unique_ptr_v<T>
auto tag_invoke(deserialize_tag, ondemand::value &val, T& out) {
auto tag_invoke(deserialize_tag, auto &val, T& out) {
using type = typename T::element_type;
out = std::make_unique<type>(val.template get<type>());
return SUCCESS;
@@ -36,11 +37,11 @@ auto tag_invoke(deserialize_tag, ondemand::value &val, T& out) {
} // namespace simdjson
#endif // __cpp_concepts
#endif // SIMDJSON_SUPPORTS_DESERIALIZATION
namespace custom_types_tests {
#if SIMDJSON_EXCEPTIONS && defined(__cpp_concepts)
#if SIMDJSON_EXCEPTIONS && defined(SIMDJSON_SUPPORTS_DESERIALIZATION)
struct Car {
std::string make{};
std::string model{};
@@ -87,7 +88,7 @@ struct Car {
}
};
static_assert(simdjson::deserializable<std::unique_ptr<Car>>, "It should be deserializable");
static_assert(simdjson::custom_deserializable<std::unique_ptr<Car>>, "It should be deserializable");
bool custom_uniqueptr_test() {
TEST_START();
@@ -214,7 +215,7 @@ bool custom_no_except() {
#endif // SIMDJSON_EXCEPTIONS
bool run() {
return
#if SIMDJSON_EXCEPTIONS && defined(__cpp_concepts)
#if SIMDJSON_EXCEPTIONS && defined(SIMDJSON_SUPPORTS_DESERIALIZATION)
custom_test() &&
custom_uniqueptr_test() &&
custom_no_except() &&
@@ -0,0 +1,50 @@
#include "simdjson.h"
#include "simdjson/std/memory.h"
#include "simdjson/std/vector.h"
#include "test_ondemand.h"
#include <string>
#include <vector>
namespace stl_types {
#if SIMDJSON_EXCEPTIONS && defined(SIMDJSON_SUPPORTS_DESERIALIZATION)
bool basic_general_madness() {
TEST_START();
simdjson::padded_string json =
R"( [ { "codes": [1.2, 3.4, 5.6, 7.8, 9.0] },
{ "codes": [2.2, 4.4, 6.6, 8.8, 10.0] } ])"_padded;
simdjson::ondemand::parser parser;
simdjson::ondemand::document doc = parser.iterate(json);
std::vector<std::unique_ptr<float>> codes;
for (auto val : doc) {
simdjson::ondemand::object obj;
SIMDJSON_TRY(val.get_object().get(obj));
obj["codes"].get(codes); // append to it
}
if (codes.size() != 10) {
return false;
}
if (*codes[5] != 2.2f || *codes[7] != 6.6f || *codes[9] != 10.0f) {
return false;
}
TEST_SUCCEED();
}
#endif // SIMDJSON_EXCEPTIONS
bool run() {
return
#if SIMDJSON_EXCEPTIONS && defined(SIMDJSON_SUPPORTS_DESERIALIZATION)
basic_general_madness() &&
#endif // SIMDJSON_EXCEPTIONS
true;
}
} // namespace stl_types
int main(int argc, char *argv[]) {
return test_main(argc, argv, stl_types::run);
}