Made the .to_class(...) method optional

This commit is contained in:
Patrick Urbanke
2023-10-07 13:53:16 +02:00
parent 499011c8ba
commit 2b50fb8ce0
4 changed files with 108 additions and 2 deletions
@@ -0,0 +1,28 @@
#ifndef RFL_INTERNAL_HASTOCLASSMETHODV_HPP_
#define RFL_INTERNAL_HASTOCLASSMETHODV_HPP_
#include <type_traits>
namespace rfl {
namespace internal {
template <typename Wrapper>
using to_class_method_t = decltype(std::declval<const Wrapper>().to_class());
template <typename Wrapper, typename = std::void_t<>>
struct has_to_class_m : std::false_type {};
template <typename Wrapper>
struct has_to_class_m<Wrapper, std::void_t<to_class_method_t<Wrapper>>>
: 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 <typename Wrapper>
constexpr bool has_to_class_method_v = has_to_class_m<Wrapper>::value;
} // namespace internal
} // namespace rfl
#endif
+13 -2
View File
@@ -2,7 +2,10 @@
#define RFL_PARSING_CUSTOMPARSER_HPP_
#include <exception>
#include <tuple>
#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 <class ReaderType, class WriterType, class OriginalClass,
struct CustomParser {
static Result<OriginalClass> read(const ReaderType& _r,
auto* _var) noexcept {
const auto to_class = [](const auto& _h) -> Result<OriginalClass> {
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
try {
return _h.to_class();
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
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());
}
+65
View File
@@ -0,0 +1,65 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace tcc4 {
struct Person {
Person(const std::string& _first_name,
const rfl::Box<std::string>& _last_name, int _age)
: first_name_(_first_name),
last_name_(rfl::make_box<std::string>(*_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<std::string> last_name_;
int age_;
};
struct PersonImpl {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", rfl::Box<std::string>> 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<std::string>(*_p.last_name()),
.age = _p.age()};
}
};
} // namespace tcc4
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, tcc4::Person>
: public CustomParser<ReaderType, WriterType, tcc4::Person,
tcc4::PersonImpl> {};
} // namespace parsing
} // namespace rfl
void test_custom_class4() {
std::cout << "test_custom_class4" << std::endl;
const auto bart =
tcc4::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
write_and_read(bart,
R"({"firstName":"Bart","lastName":"Simpson","age":10})");
}
+2
View File
@@ -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();