#ifndef RFL_PARSING_CUSTOMPARSER_HPP_ #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 { namespace parsing { template struct CustomParser { static Result read(const ReaderType& _r, auto* _var) noexcept { const auto to_class = [](auto&& _h) -> Result { try { 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()); } }; return Parser::read(_r, _var) .and_then(to_class); } static auto write(const WriterType& _w, const OriginalClass& _p) noexcept { return Parser::write( _w, HelperStruct::from_class(_p)); } }; } // namespace parsing } // namespace rfl #endif