draconisplusplus/include/rfl/parsing/Parser_pair.hpp

67 lines
1.9 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_PARSING_PARSER_PAIR_HPP_
#define RFL_PARSING_PARSER_PAIR_HPP_
#include <map>
#include <type_traits>
#include <utility>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "Parser_base.hpp"
#include "schema/Type.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace parsing {
2024-06-08 15:53:06 -04:00
template <
class R,
class W,
class FirstType,
class SecondType,
class ProcessorsType>
2024-06-08 14:10:59 -04:00
requires AreReaderAndWriter<R, W, std::pair<FirstType, SecondType>>
struct Parser<R, W, std::pair<FirstType, SecondType>, 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<std::pair<FirstType, SecondType>>
read(const R& _r, const InputVarType& _var) noexcept {
2024-06-08 14:10:59 -04:00
const auto to_pair = [&](auto&& _t) {
2024-06-08 15:53:06 -04:00
return std::make_pair(
std::move(std::get<0>(_t)), std::move(std::get<1>(_t))
);
2024-06-08 14:10:59 -04:00
};
2024-06-08 15:53:06 -04:00
return Parser<R, W, std::tuple<FirstType, SecondType>, ProcessorsType>::
read(_r, _var)
.transform(to_pair);
2024-06-08 14:10:59 -04:00
}
template <class P>
2024-06-08 15:53:06 -04:00
static void write(
const W& _w,
const std::pair<FirstType, SecondType>& _p,
const P& _parent
) noexcept {
2024-06-08 14:10:59 -04:00
const auto tup = std::make_tuple(&_p.first, &_p.second);
2024-06-08 15:53:06 -04:00
Parser<
2024-06-09 18:55:00 -04:00
R,
W,
std::tuple<const FirstType*, const SecondType*>,
2024-06-08 15:53:06 -04:00
ProcessorsType>::write(_w, tup, _parent);
2024-06-08 14:10:59 -04:00
}
static schema::Type to_schema(
2024-06-08 15:53:06 -04:00
std::map<std::string, schema::Type>* _definitions
) {
return Parser<R, W, std::tuple<FirstType, SecondType>, 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