draconisplusplus/include/rfl/parsing/Parser_rfl_variant.hpp

52 lines
1.6 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PARSING_PARSER_RFL_VARIANT_HPP_
#define RFL_PARSING_PARSER_RFL_VARIANT_HPP_
#include <map>
#include <type_traits>
#include <variant>
#include "../Result.hpp"
#include "../Variant.hpp"
#include "../always_false.hpp"
#include "FieldVariantParser.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace parsing {
template <class R, class W, class... FieldTypes, class ProcessorsType>
requires AreReaderAndWriter<R, W, rfl::Variant<FieldTypes...>>
struct Parser<R, W, rfl::Variant<FieldTypes...>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
using OutputVarType = typename W::OutputVarType;
/// Expresses the variables as type T.
2024-06-08 15:53:06 -04:00
static Result<rfl::Variant<FieldTypes...>>
read(const R& _r, const InputVarType& _var) noexcept {
2024-06-08 14:10:59 -04:00
const auto to_rfl_variant = [](auto&& _var) {
2024-06-16 00:13:15 -04:00
return rfl::Variant<FieldTypes...>(std::forward<std::variant<FieldTypes...>>(_var));
2024-06-08 14:10:59 -04:00
};
2024-06-16 00:13:15 -04:00
return Parser<R, W, std::variant<FieldTypes...>, ProcessorsType>::read(_r, _var).transform(
to_rfl_variant
);
2024-06-08 14:10:59 -04:00
}
template <class P>
2024-06-16 00:13:15 -04:00
static void
write(const W& _w, const rfl::Variant<FieldTypes...>& _variant, const P& _parent) noexcept {
2024-06-08 14:10:59 -04:00
Parser<R, W, std::variant<FieldTypes...>, ProcessorsType>::write(
2024-06-16 00:13:15 -04:00
_w, _variant.variant(), _parent
2024-06-08 15:53:06 -04:00
);
2024-06-08 14:10:59 -04:00
}
2024-06-16 00:13:15 -04:00
static schema::Type to_schema(std::map<std::string, schema::Type>* _definitions) {
return Parser<R, W, std::variant<FieldTypes...>, ProcessorsType>::to_schema(_definitions);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
};
2024-06-08 14:10:59 -04:00
} // namespace parsing
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif