mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
52 lines
1.5 KiB
C++
52 lines
1.5 KiB
C++
#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 {
|
|
namespace parsing {
|
|
|
|
template <class R, class W, class... FieldTypes>
|
|
requires AreReaderAndWriter<R, W, rfl::Variant<FieldTypes...>>
|
|
struct Parser<R, W, rfl::Variant<FieldTypes...>> {
|
|
using InputVarType = typename R::InputVarType;
|
|
using OutputVarType = typename W::OutputVarType;
|
|
|
|
/// Expresses the variables as type T.
|
|
static Result<rfl::Variant<FieldTypes...>> read(
|
|
const R& _r, const InputVarType& _var) noexcept {
|
|
const auto to_rfl_variant = [](auto&& _var) {
|
|
return rfl::Variant<FieldTypes...>(
|
|
std::forward<std::variant<FieldTypes...>>(_var));
|
|
};
|
|
return Parser<R, W, std::variant<FieldTypes...>>::read(_r, _var).transform(
|
|
to_rfl_variant);
|
|
}
|
|
|
|
template <class P>
|
|
static void write(const W& _w, const rfl::Variant<FieldTypes...>& _variant,
|
|
const P& _parent) noexcept {
|
|
Parser<R, W, std::variant<FieldTypes...>>::write(_w, _variant.variant(),
|
|
_parent);
|
|
}
|
|
|
|
static schema::Type to_schema(
|
|
std::map<std::string, schema::Type>* _definitions) {
|
|
return Parser<R, W, std::variant<FieldTypes...>>::to_schema(_definitions);
|
|
}
|
|
};
|
|
|
|
} // namespace parsing
|
|
} // namespace rfl
|
|
|
|
#endif
|