mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
removing expand workaround (#2431)
* removing expland workaround * minor fixes
This commit is contained in:
@@ -35,14 +35,16 @@ if (TARGET benchmark::benchmark)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(SIMDJSON_STATIC_REFLECTION)
|
||||
add_subdirectory(static_reflect)
|
||||
endif(SIMDJSON_STATIC_REFLECTION)
|
||||
|
||||
|
||||
include(CheckCXXCompilerFlag)
|
||||
check_cxx_compiler_flag("-std=c++20" SIMDJSON_COMPILER_SUPPORTS_CXX20)
|
||||
if(SIMDJSON_EXCEPTIONS AND SIMDJSON_COMPILER_SUPPORTS_CXX20)
|
||||
add_subdirectory(from)
|
||||
add_subdirectory(car_builder)
|
||||
endif()
|
||||
if(SIMDJSON_STATIC_REFLECTION)
|
||||
add_subdirectory(static_reflect)
|
||||
else()
|
||||
if(SIMDJSON_EXCEPTIONS AND SIMDJSON_COMPILER_SUPPORTS_CXX20)
|
||||
add_subdirectory(from)
|
||||
add_subdirectory(car_builder)
|
||||
endif()
|
||||
endif(SIMDJSON_STATIC_REFLECTION)
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "simdjson/error.h"
|
||||
#include "simdjson/portability.h"
|
||||
#include "simdjson/concepts.h"
|
||||
#include "simdjson/constevalutil.h"
|
||||
|
||||
/**
|
||||
* @brief The top level simdjson namespace, containing everything the library provides.
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef SIMDJSON_CONSTEVALUTIL_H
|
||||
#define SIMDJSON_CONSTEVALUTIL_H
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <array>
|
||||
|
||||
#if SIMDJSON_CONSTEVAL
|
||||
namespace simdjson {
|
||||
namespace constevalutil {
|
||||
|
||||
constexpr static std::array<uint8_t, 256> json_quotable_character = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
constexpr static std::array<std::string_view, 32> control_chars = {
|
||||
"\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006",
|
||||
"\\u0007", "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r",
|
||||
"\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014",
|
||||
"\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", "\\u001b",
|
||||
"\\u001c", "\\u001d", "\\u001e", "\\u001f"};
|
||||
// unoptimized, meant for compile-time execution
|
||||
consteval std::string consteval_to_quoted_escaped(std::string_view input) {
|
||||
std::string out = "\"";
|
||||
for (char c : input) {
|
||||
if (json_quotable_character[uint8_t(c)]) {
|
||||
if (c == '"') {
|
||||
out.append("\\\"");
|
||||
} else if (c == '\\') {
|
||||
out.append("\\\\");
|
||||
} else {
|
||||
std::string_view v = control_chars[uint8_t(c)];
|
||||
out.append(v);
|
||||
}
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
out.push_back('"');
|
||||
return out;
|
||||
}
|
||||
} // namespace constevalutil
|
||||
} // namespace simdjson
|
||||
#endif // SIMDJSON_CONSTEVAL
|
||||
#endif // SIMDJSON_CONSTEVALUTIL_H
|
||||
@@ -89,9 +89,6 @@ template<typename number_type,
|
||||
constexpr void atom(string_builder &b, const number_type t) {
|
||||
b.append(t);
|
||||
}
|
||||
#if SIMDJSON_CONSTEVAL
|
||||
consteval std::string consteval_to_quoted_escaped(std::string_view input);
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
requires(std::is_class_v<T> && !container_but_not_string<T> &&
|
||||
@@ -106,10 +103,10 @@ template <class T>
|
||||
constexpr void atom(string_builder &b, const T &t) {
|
||||
int i = 0;
|
||||
b.append('{');
|
||||
[:expand(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked())):] >> [&]<auto dm>() {
|
||||
template for (constexpr auto dm : std::define_static_array(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
|
||||
if (i != 0)
|
||||
b.append(',');
|
||||
constexpr auto key = std::define_static_string(consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
|
||||
constexpr auto key = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
|
||||
b.append_raw(key);
|
||||
b.append(':');
|
||||
atom(b, t.[:dm:]);
|
||||
@@ -143,21 +140,16 @@ template <typename T>
|
||||
requires(std::is_enum_v<T>)
|
||||
void atom(string_builder &b, const T &e) {
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
std::string_view result = "<unnamed>";
|
||||
[:expand(std::meta::enumerators_of(^^T)):] >> [&]<auto enum_val>{
|
||||
constexpr auto enumerators = std::define_static_array(std::meta::enumerators_of(^^T));
|
||||
template for (constexpr auto enum_val : enumerators) {
|
||||
constexpr auto enum_str = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(enum_val)));
|
||||
if (e == [:enum_val:]) {
|
||||
result = std::meta::identifier_of(enum_val);
|
||||
b.append_raw(enum_str);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if (result != "<unnamed>") {
|
||||
b.append_raw("\"");
|
||||
b.append_raw(result);
|
||||
b.append_raw("\"");
|
||||
} else {
|
||||
// Fallback to integer if enum value not found
|
||||
atom(b, static_cast<std::underlying_type_t<T>>(e));
|
||||
}
|
||||
// Fallback to integer if enum value not found
|
||||
atom(b, static_cast<std::underlying_type_t<T>>(e));
|
||||
#else
|
||||
// Fallback: serialize as integer if reflection not available
|
||||
atom(b, static_cast<std::underlying_type_t<T>>(e));
|
||||
@@ -241,10 +233,10 @@ template <class Z>
|
||||
void append(string_builder &b, const Z &z) {
|
||||
int i = 0;
|
||||
b.append('{');
|
||||
[:expand(std::meta::nonstatic_data_members_of(^^Z, std::meta::access_context::unchecked())):] >> [&]<auto dm>() {
|
||||
template for (constexpr auto dm : std::define_static_array(std::meta::nonstatic_data_members_of(^^Z, std::meta::access_context::unchecked()))) {
|
||||
if (i != 0)
|
||||
b.append(',');
|
||||
constexpr auto key = std::define_static_string(consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
|
||||
constexpr auto key = std::define_static_string(constevalutil::consteval_to_quoted_escaped(std::meta::identifier_of(dm)));
|
||||
b.append_raw(key);
|
||||
b.append(':');
|
||||
atom(b, z.[:dm:]);
|
||||
|
||||
@@ -206,29 +206,6 @@ inline size_t write_string_escaped(const std::string_view input, char *out) {
|
||||
return out - initout;
|
||||
}
|
||||
|
||||
#if SIMDJSON_CONSTEVAL
|
||||
// unoptimized, meant for compile-time execution
|
||||
consteval std::string consteval_to_quoted_escaped(std::string_view input) {
|
||||
std::string out = "\"";
|
||||
for (char c : input) {
|
||||
if (json_quotable_character[uint8_t(c)]) {
|
||||
if (c == '"') {
|
||||
out.append("\\\"");
|
||||
} else if (c == '\\') {
|
||||
out.append("\\\\");
|
||||
} else {
|
||||
std::string_view v = control_chars[uint8_t(c)];
|
||||
out.append(v);
|
||||
}
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
out.push_back('"');
|
||||
return out;
|
||||
}
|
||||
#endif // SIMDJSON_CONSTEVAL
|
||||
|
||||
simdjson_inline string_builder::string_builder(size_t initial_capacity)
|
||||
: buffer(new(std::nothrow) char[initial_capacity]), position(0),
|
||||
capacity(buffer.get() != nullptr ? initial_capacity : 0),
|
||||
|
||||
@@ -275,32 +275,6 @@ constexpr bool user_defined_type = (std::is_class_v<T>
|
||||
!concepts::appendable_containers<T> && !require_custom_serialization<T>);
|
||||
|
||||
|
||||
// workaround from
|
||||
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2996r10.html#back-and-forth
|
||||
// for missing expansion statements
|
||||
namespace __impl {
|
||||
template<auto... vals>
|
||||
struct replicator_type {
|
||||
template<typename F>
|
||||
constexpr void operator>>(F body) const {
|
||||
(body.template operator()<vals>(), ...);
|
||||
}
|
||||
};
|
||||
|
||||
template<auto... vals>
|
||||
replicator_type<vals...> replicator = {};
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
consteval auto expand(R range) {
|
||||
std::vector<std::meta::info> args;
|
||||
for (auto r : range) {
|
||||
args.push_back(reflect_constant(r));
|
||||
}
|
||||
return substitute(^^__impl::replicator, args);
|
||||
}
|
||||
// end of workaround
|
||||
|
||||
template <typename T, typename ValT>
|
||||
requires(user_defined_type<T> && std::is_class_v<T>)
|
||||
error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept {
|
||||
@@ -311,9 +285,8 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept {
|
||||
SIMDJSON_TRY(val.get_object().get(obj));
|
||||
}
|
||||
error_code e = simdjson::SUCCESS;
|
||||
|
||||
[:expand(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked())):] >> [&]<auto mem>() {
|
||||
if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
|
||||
template for (constexpr auto mem : std::define_static_array(std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()))) {
|
||||
if constexpr (!std::meta::is_const(mem) && std::meta::is_public(mem)) {
|
||||
constexpr std::string_view key = std::define_static_string(std::meta::identifier_of(mem));
|
||||
// Note: removed static assert as optional types are now handled generically
|
||||
// as long we are succesful or the field is not found, we continue
|
||||
@@ -332,16 +305,15 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept {
|
||||
#if SIMDJSON_STATIC_REFLECTION
|
||||
std::string_view str;
|
||||
SIMDJSON_TRY(val.get_string().get(str));
|
||||
|
||||
bool found = false;
|
||||
[:expand(std::meta::enumerators_of(^^T)):] >> [&]<auto enum_val>{
|
||||
if (!found && str == std::meta::identifier_of(enum_val)) {
|
||||
constexpr auto enumerators = std::define_static_array(std::meta::enumerators_of(^^T));
|
||||
template for (constexpr auto enum_val : enumerators) {
|
||||
if (str == std::meta::identifier_of(enum_val)) {
|
||||
out = [:enum_val:];
|
||||
found = true;
|
||||
return SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
return found ? SUCCESS : INCORRECT_TYPE;
|
||||
return INCORRECT_TYPE;
|
||||
#else
|
||||
// Fallback: deserialize as integer if reflection not available
|
||||
std::underlying_type_t<T> int_val;
|
||||
|
||||
@@ -131,6 +131,7 @@ namespace builder_tests {
|
||||
|
||||
// Deserialize
|
||||
ondemand::parser parser;
|
||||
std::cout << json << std::endl;
|
||||
auto doc_result = parser.iterate(pad(json));
|
||||
ASSERT_SUCCESS(doc_result);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user