mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Wrap C arrays with rfl::Array.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_ARRAY_HPP_
|
||||
#define RFL_ARRAY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "internal/to_std_array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T>
|
||||
requires std::is_array_v<T>
|
||||
struct Array : internal::to_std_array_t<T> {
|
||||
using Type = T;
|
||||
using Base = internal::to_std_array_t<T>;
|
||||
using Base::Base;
|
||||
|
||||
Array() = default;
|
||||
Array(const Base &t) : Base(t) {}
|
||||
Array(Base &&t) : Base(std::move(t)) {}
|
||||
Array(const T &t) : Base(reinterpret_cast<const Base &>(t)) {}
|
||||
Array(T &&t) : Base(reinterpret_cast<Base &&>(t)) {}
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+7
-23
@@ -7,6 +7,7 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "Array.hpp"
|
||||
#include "Literal.hpp"
|
||||
#include "default.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
@@ -134,31 +135,14 @@ 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));
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class T>
|
||||
inline auto make_field(const T& _value) {
|
||||
return Field<_name, T>(_value);
|
||||
using T0 = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_array_v<T0>) {
|
||||
return Field<_name, T0>(rfl::Array<T0>(std::forward<T>(_value)));
|
||||
} else {
|
||||
return Field<_name, T0>(std::forward<T>(_value));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -43,20 +43,16 @@ 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_(get(std::make_tuple(std::move(_fields.value_)...))) {
|
||||
: values_(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_(get(std::make_tuple(_fields.value_...))) {
|
||||
: values_(std::make_tuple(_fields.value_...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "is_flatten_field.hpp"
|
||||
#include "is_named_tuple.hpp"
|
||||
#include "ptr_tuple_t.hpp"
|
||||
@@ -95,14 +96,18 @@ auto make_tuple_from_element(T (*arr)[n]) {
|
||||
}
|
||||
}
|
||||
|
||||
auto make_tuple_from_element(auto &e) {
|
||||
return std::make_tuple(e);
|
||||
template <class T>
|
||||
auto make_tuple_from_element(Array<T>* arr) {
|
||||
return make_tuple_from_element(reinterpret_cast<T*>(arr));
|
||||
}
|
||||
|
||||
auto flatten_c_arrays(const auto &tuple) {
|
||||
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)>>>());
|
||||
}(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
|
||||
@@ -113,7 +118,8 @@ T move_from_tuple(TupleType&& _t) {
|
||||
|
||||
using TargetTupleType = tuple_t<std::remove_cvref_t<T>>;
|
||||
|
||||
auto pointers = flatten_c_arrays(unflatten_ptr_tuple<TargetTupleType>(ptr_tuple));
|
||||
auto pointers =
|
||||
flatten_c_arrays(unflatten_ptr_tuple<TargetTupleType>(ptr_tuple));
|
||||
return move_from_pointers<T>(pointers);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "internal/is_named_tuple.hpp"
|
||||
#include "internal/ptr_named_tuple_t.hpp"
|
||||
#include "to_named_tuple.hpp"
|
||||
#include "wrap_in_rfl_array_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -17,7 +18,9 @@ struct remove_ptr;
|
||||
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct remove_ptr<Field<_name, T>> {
|
||||
using FieldType = Field<_name, std::remove_cvref_t<std::remove_pointer_t<T>>>;
|
||||
using FieldType =
|
||||
Field<_name,
|
||||
wrap_in_rfl_array_t<std::remove_cvref_t<std::remove_pointer_t<T>>>>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "Parser_reference_wrapper.hpp"
|
||||
#include "Parser_rename.hpp"
|
||||
#include "Parser_result.hpp"
|
||||
#include "Parser_rfl_array.hpp"
|
||||
#include "Parser_rfl_variant.hpp"
|
||||
#include "Parser_shared_ptr.hpp"
|
||||
#include "Parser_tagged_union.hpp"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/to_std_array.hpp"
|
||||
@@ -25,7 +26,8 @@ struct Parser<R, W, T[_size]> {
|
||||
using CArray = T[_size];
|
||||
using StdArray = internal::to_std_array_t<T[_size]>;
|
||||
|
||||
static auto read(const R& _r, const InputVarType& _var) noexcept {
|
||||
static Result<Array<T[_size]>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
#define RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Array.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/to_std_array.hpp"
|
||||
#include "Parser_array.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, Array<T>>
|
||||
struct Parser<R, W, Array<T>> {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using RflArray = Array<T>;
|
||||
using StdArray = internal::to_std_array_t<T>;
|
||||
|
||||
static Result<RflArray> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const RflArray& _arr) noexcept {
|
||||
return Parser<R, W, StdArray>::write(_w, _arr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_WRAP_IN_RFL_ARRAY_T_
|
||||
#define RFL_WRAP_IN_RFL_ARRAY_T_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "Array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
template <class T>
|
||||
struct wrap_in_rfl_array {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
requires std::is_array_v<T>
|
||||
struct wrap_in_rfl_array<T> {
|
||||
using type = Array<T>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using wrap_in_rfl_array_t = typename wrap_in_rfl_array<T>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user