added a new method of implementing a custom parser using a Reflector … (#142)

Co-authored-by: Daedie <daedie@gmail.com>
This commit is contained in:
BjornSAIM
2024-07-07 22:41:31 +02:00
committed by GitHub
co-authored by Daedie
parent 1ff7f762f1
commit 6dadbc2506
4 changed files with 91 additions and 3 deletions
@@ -4,6 +4,8 @@
#include <type_traits>
namespace rfl {
template <typename T>
struct Reflector;
namespace internal {
template <typename Wrapper>
@@ -23,6 +25,13 @@ struct has_refl_m<Wrapper, std::void_t<reflection_method_t<Wrapper>>>
template <typename Wrapper>
constexpr bool has_reflection_method_v = has_refl_m<Wrapper>::value;
template <typename Type>
concept has_reflector = requires(Type&& item) {
Reflector<Type>::from(item);
} && requires(const typename Reflector<Type>::ReflType& item) {
Reflector<Type>::to(item);
};
} // namespace internal
} // namespace rfl
+15 -2
View File
@@ -40,7 +40,17 @@ struct Parser {
/// Expresses the variables as type T.
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
if constexpr (R::template has_custom_constructor<T>) {
if constexpr (internal::has_reflector<T>) {
const auto wrap_in_t = [](auto _named_tuple) -> Result<T> {
try {
return Reflector<T>::to(_named_tuple);
} catch (std::exception& e) {
return Error(e.what());
}
};
return Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::read(_r, _var).and_then(
wrap_in_t);
} else if constexpr (R::template has_custom_constructor<T>) {
return _r.template use_custom_constructor<T>(_var);
} else {
if constexpr (internal::has_reflection_type_v<T>) {
@@ -68,7 +78,10 @@ struct Parser {
template <class P>
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
if constexpr (internal::has_reflection_type_v<T>) {
if constexpr (internal::has_reflector<T>) {
Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::write(
_w, Reflector<T>::from(_var), _parent);
} else if constexpr (internal::has_reflection_type_v<T>) {
using ReflectionType = std::remove_cvref_t<typename T::ReflectionType>;
if constexpr (internal::has_reflection_method_v<T>) {
Parser<R, W, ReflectionType, ProcessorsType>::write(