1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#ifndef RFL_PARSING_CUSTOMPARSER_HPP_
#define RFL_PARSING_CUSTOMPARSER_HPP_
#include <concepts>
#include <exception>
#include <tuple>
#include "../Tuple.hpp"
#include "../internal/has_to_class_method_v.hpp"
#include "../internal/to_ptr_field_tuple.hpp"
#include "Parser.hpp"
#include "schema/Type.hpp"
namespace rfl {
namespace parsing {
template <class R, class W, class ProcessorsType, class OriginalClass,
class HelperStruct>
struct CustomParser {
using CustomParserHelperStruct = std::remove_cvref_t<HelperStruct>;
static Result<OriginalClass> read(const R& _r, const auto& _var) noexcept {
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
try {
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 rfl::apply(class_from_ptrs, ptr_field_tuple);
}
} catch (std::exception& e) {
return error(e.what());
}
};
return Parser<R, W, CustomParserHelperStruct, ProcessorsType>::read(_r,
_var)
.and_then(to_class);
}
template <class P>
static auto write(const W& _w, const OriginalClass& _p,
const P& _parent) noexcept {
Parser<R, W, CustomParserHelperStruct, ProcessorsType>::write(
_w, HelperStruct::from_class(_p), _parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
return Parser<R, W, CustomParserHelperStruct, ProcessorsType>::to_schema(
_definitions);
}
};
} // namespace parsing
} // namespace rfl
#endif
|