mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Made the .to_class(...) method optional
This commit is contained in:
@@ -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
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user