diff --git a/include/rfl/internal/has_to_class_method_v.hpp b/include/rfl/internal/has_to_class_method_v.hpp new file mode 100644 index 00000000..92857fb0 --- /dev/null +++ b/include/rfl/internal/has_to_class_method_v.hpp @@ -0,0 +1,28 @@ +#ifndef RFL_INTERNAL_HASTOCLASSMETHODV_HPP_ +#define RFL_INTERNAL_HASTOCLASSMETHODV_HPP_ + +#include + +namespace rfl { +namespace internal { + +template +using to_class_method_t = decltype(std::declval().to_class()); + +template > +struct has_to_class_m : std::false_type {}; + +template +struct has_to_class_m>> + : std::true_type {}; + +/// Utility parameter for named tuple parsing, can be used by the +/// parsers to determine whether a class or struct has a method +/// called "to_class". +template +constexpr bool has_to_class_method_v = has_to_class_m::value; + +} // namespace internal +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/CustomParser.hpp b/include/rfl/parsing/CustomParser.hpp index b7db81dc..bbe3424e 100644 --- a/include/rfl/parsing/CustomParser.hpp +++ b/include/rfl/parsing/CustomParser.hpp @@ -2,7 +2,10 @@ #define RFL_PARSING_CUSTOMPARSER_HPP_ #include +#include +#include "rfl/internal/has_to_class_method_v.hpp" +#include "rfl/internal/to_ptr_field_tuple.hpp" #include "rfl/parsing/Parser.hpp" namespace rfl { @@ -13,9 +16,17 @@ template read(const ReaderType& _r, auto* _var) noexcept { - const auto to_class = [](const auto& _h) -> Result { + const auto to_class = [](auto&& _h) -> Result { try { - return _h.to_class(); + if constexpr (internal::has_to_class_method_v) { + return _h.to_class(); + } else { + auto ptr_field_tuple = internal::to_ptr_field_tuple(_h); + const auto class_from_ptrs = [](auto&... _ptrs) { + return OriginalClass(std::move(*_ptrs.value_)...); + }; + return std::apply(class_from_ptrs, ptr_field_tuple); + } } catch (std::exception& e) { return Error(e.what()); } diff --git a/tests/test_custom_class4.hpp b/tests/test_custom_class4.hpp new file mode 100644 index 00000000..e7004b7d --- /dev/null +++ b/tests/test_custom_class4.hpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace tcc4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{ + .first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace tcc4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +void test_custom_class4() { + std::cout << "test_custom_class4" << std::endl; + + const auto bart = + tcc4::Person("Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart, + R"({"firstName":"Bart","lastName":"Simpson","age":10})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 2c2b5e13..178893f0 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -3,6 +3,7 @@ #include "test_custom_class1.hpp" #include "test_custom_class2.hpp" #include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" #include "test_default_values.hpp" #include "test_field_variant.hpp" #include "test_flatten.hpp" @@ -31,6 +32,7 @@ int main() { test_custom_class1(); test_custom_class2(); test_custom_class3(); + test_custom_class4(); test_array(); test_timestamp(); test_flatten();