diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3fa79f2..0f6d3b4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,4 +13,5 @@ target_link_libraries(example2 PRIVATE simdjson::serialization simdjson::simdjso add_executable(example3 example3.cpp) target_link_libraries(example3 PRIVATE simdjson::serialization simdjson::simdjson) - +add_executable(example4 example4.cpp) +target_link_libraries(example4 PRIVATE simdjson::serialization) \ No newline at end of file diff --git a/examples/example4.cpp b/examples/example4.cpp new file mode 100644 index 0000000..f188e00 --- /dev/null +++ b/examples/example4.cpp @@ -0,0 +1,95 @@ +#include "simdjson/json_builder/json_builder.h" +#include "simdjson/json_builder/string_builder.h" +#include +#include +#include +#include +#include +#include +#include +struct Z { + int x; +}; + +struct Y { + int g; + std::string h; + std::vector i; + Z z; +}; + +struct X { + char a; + int b; + int c; + std::string d; + std::vector e; + std::vector f; + Y y; +}; + + +// A simple method name/value data structure + template + struct to { + T *pointer; + std::string_view m_key; + constexpr to(std::string_view const inp_key) noexcept + : pointer{}, m_key{} {} + constexpr to(std::string_view const inp_key, T &obj_ref) noexcept + : pointer{std::addressof(obj_ref)}, m_key{inp_key} {} + }; + + +// just a bogus concept +template +concept Keyed = requires(T t) { + { t.m_key } -> std::same_as; + { *t.pointer }; +}; + +// extract takes instances of the form 'to'. +template +bool extract(Funcs&&... endpoints) { + ((std::cout << endpoints.m_key << '\n'), ...); + return true; +} + +///////////////////// +// Begin struct_to_tuple +namespace __impl { + template + struct replicator_type { + template + constexpr auto operator>>(F body) const -> decltype(auto) { + return body.template operator()(); + } + }; + + template + replicator_type replicator = {}; +} + +template +consteval auto expand_all(R range) { + std::vector args; + for (auto r : range) { + args.push_back(reflect_value(r)); + } + return substitute(^__impl::replicator, args); +} + +template +constexpr auto struct_to_tuple(T const& t) { + return [: expand_all(nonstatic_data_members_of(^T)) :] >> [&]{ + return std::make_tuple(to(std::meta::identifier_of(members), t.[:members:])...); + }; +} +// End struct_to_tuple +//////////////////////// + +int main() { + X x; + std::apply([](auto&&... xs){ extract(xs...);}, struct_to_tuple(x)); + return EXIT_SUCCESS; +} \ No newline at end of file