Implement read operation of Parser for c-arrays.

This commit is contained in:
hazby2002
2024-01-02 13:38:51 +08:00
parent df26f6df6c
commit fe956b3779
6 changed files with 88 additions and 7 deletions
+18
View File
@@ -10,6 +10,7 @@
#include "Literal.hpp"
#include "default.hpp"
#include "internal/StringLiteral.hpp"
#include "internal/to_std_array.hpp"
namespace rfl {
@@ -133,6 +134,23 @@ struct Field {
Type value_;
};
/// Used to define a field in the NamedTuple.
template <internal::StringLiteral _name, class T, std::size_t _n>
struct Field<_name, T[_n]> : Field<_name, internal::to_std_array_t<T[_n]>> {
/// The underlying type.
using Type = T[_n];
using StdArray = internal::to_std_array_t<T[_n]>;
using Field<_name, StdArray>::Field;
Type& c_array() { return reinterpret_cast<Type&>(this->value_); }
const Type& c_array() const {
return reinterpret_cast<const Type&>(this->value_);
}
};
template <internal::StringLiteral _name, class T>
inline auto make_field(T&& _value) {
return Field<_name, T>(std::forward<T>(_value));
+6 -2
View File
@@ -43,16 +43,20 @@ class NamedTuple {
"Duplicate field names are not allowed");
}
static auto get(auto &&tuple) {
return reinterpret_cast<std::tuple<typename std::remove_cvref<FieldTypes>::type::Type...>&&>(tuple);
}
/// Construct from the fields.
NamedTuple(FieldTypes&&... _fields)
: values_(std::make_tuple(std::move(_fields.value_)...)) {
: values_(get(std::make_tuple(std::move(_fields.value_)...))) {
static_assert(no_duplicate_field_names(),
"Duplicate field names are not allowed");
}
/// Construct from the fields.
NamedTuple(const FieldTypes&... _fields)
: values_(std::make_tuple(_fields.value_...)) {
: values_(get(std::make_tuple(_fields.value_...))) {
static_assert(no_duplicate_field_names(),
"Duplicate field names are not allowed");
}
+10 -1
View File
@@ -10,6 +10,8 @@
#include <variant>
#include <vector>
#include "internal/to_std_array.hpp"
namespace rfl {
/// To be returned
@@ -342,7 +344,14 @@ class Result {
/// 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>> {};
struct Result<T[_n]> : public Result<internal::to_std_array_t<T[_n]>> {
using StdArray = internal::to_std_array_t<T[_n]>;
using Base = Result<StdArray>;
using Base::Base;
Result(const Base& other) : Base(other) {}
Result(Base&& other) : Base(std::move(other)) {}
};
} // namespace rfl
+24 -2
View File
@@ -82,6 +82,29 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
}
}
template <class T, std::size_t n>
auto make_tuple_from_element(T (*arr)[n]) {
if constexpr (std::is_array_v<T>) {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::tuple_cat(make_tuple_from_element(&(*arr)[i])...);
}(std::make_index_sequence<n>());
} else {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::make_tuple(&(*arr)[i]...);
}(std::make_index_sequence<n>());
}
}
auto make_tuple_from_element(auto &e) {
return std::make_tuple(e);
}
auto flatten_c_arrays(const auto &tuple) {
return [&]<std::size_t... i>(std::index_sequence<i...>) {
return std::tuple_cat(make_tuple_from_element(std::get<i>(tuple))...);
}(std::make_index_sequence<std::tuple_size_v<std::remove_cvref_t<decltype(tuple)>>>());
}
/// Creates a struct of type T from a tuple by moving the underlying
/// fields.
template <class T, class TupleType>
@@ -90,8 +113,7 @@ T move_from_tuple(TupleType&& _t) {
using TargetTupleType = tuple_t<std::remove_cvref_t<T>>;
auto pointers = unflatten_ptr_tuple<TargetTupleType>(ptr_tuple);
auto pointers = flatten_c_arrays(unflatten_ptr_tuple<TargetTupleType>(ptr_tuple));
return move_from_pointers<T>(pointers);
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_
#include <cstdint>
#include <array>
namespace rfl::internal {
template <class T>
struct to_std_array {
using type = T;
};
template <class T, std::size_t _n>
struct to_std_array<T[_n]> {
using type = std::array<typename to_std_array<std::remove_cvref_t<T>>::type, _n>;
};
template <class T>
using to_std_array_t = to_std_array<T>::type;
}
#endif
+6 -2
View File
@@ -5,6 +5,8 @@
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/to_std_array.hpp"
#include "Parser_array.hpp"
#include "Parser_base.hpp"
namespace rfl {
@@ -21,8 +23,10 @@ struct Parser<R, W, T[_size]> {
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");
using StdArray = internal::to_std_array_t<T[_size]>;
static auto read(const R& _r, const InputVarType& _var) noexcept {
return Parser<R, W, StdArray>::read(_r, _var);
}
static OutputVarType write(const W& _w, const CArray& _arr) noexcept {