Implement write operation of Parser for c-arrays.

This commit is contained in:
hazby2002
2024-01-02 13:35:33 +08:00
parent 2d45ddfdc8
commit df26f6df6c
3 changed files with 46 additions and 0 deletions
+5
View File
@@ -3,6 +3,7 @@
#include <optional>
#include <ranges>
#include <span>
#include <stdexcept>
#include <tuple>
#include <type_traits>
@@ -339,6 +340,10 @@ class Result {
std::variant<T, Error> t_or_err_;
};
/// This is simply to get the compilation to pass.
template <class T, std::size_t _n>
class Result<T[_n]> : public Result<std::span<T, _n>> {};
} // namespace rfl
#endif
+1
View File
@@ -4,6 +4,7 @@
#include "Parser_array.hpp"
#include "Parser_base.hpp"
#include "Parser_box.hpp"
#include "Parser_c_array.hpp"
#include "Parser_default.hpp"
#include "Parser_map_like.hpp"
#include "Parser_named_tuple.hpp"
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_PARSING_PARSER_C_ARRAY_HPP_
#define RFL_PARSING_PARSER_C_ARRAY_HPP_
#include <type_traits>
#include "../Result.hpp"
#include "../always_false.hpp"
#include "Parser_base.hpp"
namespace rfl {
namespace parsing {
template <class R, class W, class T, size_t _size>
requires AreReaderAndWriter<R, W, T[_size]>
struct Parser<R, W, T[_size]> {
public:
using InputArrayType = typename R::InputArrayType;
using InputVarType = typename R::InputVarType;
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;
using CArray = T[_size];
static Result<CArray> read(const R& _r, const InputVarType& _var) noexcept {
static_assert(always_false_v<T>, "Unimplemented");
}
static OutputVarType write(const W& _w, const CArray& _arr) noexcept {
auto arr = _w.new_array();
for (const auto& e : _arr) {
_w.add(Parser<R, W, std::remove_cvref_t<T>>::write(_w, e), &arr);
}
return OutputVarType(arr);
}
};
} // namespace parsing
} // namespace rfl
#endif