mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Merge branch 'hazby2002-main'
This commit is contained in:
+11
-8
@@ -9,7 +9,10 @@
|
||||
|
||||
#include "Literal.hpp"
|
||||
#include "default.hpp"
|
||||
#include "internal/Array.hpp"
|
||||
#include "internal/StringLiteral.hpp"
|
||||
#include "internal/to_std_array.hpp"
|
||||
#include "internal/wrap_in_rfl_array_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
@@ -17,7 +20,7 @@ namespace rfl {
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct Field {
|
||||
/// The underlying type.
|
||||
using Type = T;
|
||||
using Type = internal::wrap_in_rfl_array_t<T>;
|
||||
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
@@ -28,7 +31,7 @@ struct Field {
|
||||
|
||||
Field(Field<_name, T>&& _field) noexcept = default;
|
||||
|
||||
Field(const Field<_name, Type>& _field) = default;
|
||||
Field(const Field<_name, T>& _field) = default;
|
||||
|
||||
template <class U>
|
||||
Field(const Field<_name, U>& _field) : value_(_field.get()) {}
|
||||
@@ -135,12 +138,12 @@ struct Field {
|
||||
|
||||
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>(internal::Array<T0>(std::forward<T>(_value)));
|
||||
} else {
|
||||
return Field<_name, T0>(std::forward<T>(_value));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace rfl {
|
||||
template <class T>
|
||||
struct Flatten {
|
||||
/// The underlying type.
|
||||
using Type = std::decay_t<T>;
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
|
||||
Flatten(const Type& _value) : value_(_value) {}
|
||||
|
||||
|
||||
+17
-14
@@ -21,20 +21,23 @@ namespace rfl {
|
||||
template <class... FieldTypes>
|
||||
class NamedTuple {
|
||||
public:
|
||||
using Fields = std::tuple<std::decay_t<FieldTypes>...>;
|
||||
using Values = std::tuple<typename std::decay<FieldTypes>::type::Type...>;
|
||||
using Fields = std::tuple<std::remove_cvref_t<FieldTypes>...>;
|
||||
using Values =
|
||||
std::tuple<typename std::remove_cvref<FieldTypes>::type::Type...>;
|
||||
|
||||
public:
|
||||
/// Construct from the values.
|
||||
NamedTuple(typename std::decay<FieldTypes>::type::Type&&... _values)
|
||||
: values_(std::forward<typename std::decay<FieldTypes>::type::Type>(
|
||||
_values)...) {
|
||||
NamedTuple(typename std::remove_cvref<FieldTypes>::type::Type&&... _values)
|
||||
: values_(
|
||||
std::forward<typename std::remove_cvref<FieldTypes>::type::Type>(
|
||||
_values)...) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
}
|
||||
|
||||
/// Construct from the values.
|
||||
NamedTuple(const typename std::decay<FieldTypes>::type::Type&... _values)
|
||||
NamedTuple(
|
||||
const typename std::remove_cvref<FieldTypes>::type::Type&... _values)
|
||||
: values_(std::make_tuple(_values...)) {
|
||||
static_assert(no_duplicate_field_names(),
|
||||
"Duplicate field names are not allowed");
|
||||
@@ -97,11 +100,11 @@ class NamedTuple {
|
||||
template <class Head, class... Tail>
|
||||
auto add(Head&& _head, Tail&&... _tail) {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)))
|
||||
.add(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)));
|
||||
}
|
||||
}
|
||||
@@ -110,11 +113,11 @@ class NamedTuple {
|
||||
template <class Head, class... Tail>
|
||||
auto add(Head&& _head, Tail&&... _tail) const {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)))
|
||||
.add(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)));
|
||||
}
|
||||
}
|
||||
@@ -331,7 +334,7 @@ class NamedTuple {
|
||||
// When we add additional fields, it is more intuitive to add
|
||||
// them to the end, that is why we do it like this.
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
using T = std::remove_cvref_t<typename FieldType::Type>;
|
||||
return make_fields<num_additional_fields>(
|
||||
FieldType(std::forward<T>(std::get<i>(values_))),
|
||||
std::forward<Args>(_args)...);
|
||||
@@ -392,7 +395,7 @@ class NamedTuple {
|
||||
/// Replaced the field signified by the field type.
|
||||
template <class Field, class T>
|
||||
NamedTuple<FieldTypes...> replace_value(T&& _val) {
|
||||
using FieldType = std::decay_t<Field>;
|
||||
using FieldType = std::remove_cvref_t<Field>;
|
||||
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
|
||||
return make_replaced<index, Values, T>(std::forward<Values>(values_),
|
||||
std::forward<T>(_val));
|
||||
@@ -401,7 +404,7 @@ class NamedTuple {
|
||||
/// Replaced the field signified by the field type.
|
||||
template <class Field, class T>
|
||||
NamedTuple<FieldTypes...> replace_value(T&& _val) const {
|
||||
using FieldType = std::decay_t<Field>;
|
||||
using FieldType = std::remove_cvref_t<Field>;
|
||||
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
|
||||
auto values = values_;
|
||||
return make_replaced<index, Values, T>(std::move(values),
|
||||
@@ -446,7 +449,7 @@ class NamedTuple {
|
||||
|
||||
using FieldType = typename std::tuple_element<size, Fields>::type;
|
||||
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
using T = std::remove_cvref_t<typename FieldType::Type>;
|
||||
|
||||
return retrieve_fields(
|
||||
std::forward<std::tuple<OtherFieldTypes...>>(_other_fields),
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "internal/to_std_array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// To be returned
|
||||
@@ -79,7 +82,7 @@ class Result {
|
||||
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
if constexpr (!std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
|
||||
return _f(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return std::forward<TOrError>(_t_or_err);
|
||||
@@ -212,7 +215,7 @@ class Result {
|
||||
Result<T> or_else(const F& _f) {
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> Result<T> {
|
||||
if constexpr (std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
|
||||
return _f(std::forward<Error>(_t_or_err));
|
||||
} else {
|
||||
return std::forward<T>(_t_or_err);
|
||||
@@ -252,7 +255,7 @@ class Result {
|
||||
|
||||
const auto handle_variant =
|
||||
[&]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
|
||||
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
|
||||
if constexpr (!std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
|
||||
return _f(std::forward<TOrError>(_t_or_err));
|
||||
} else {
|
||||
return std::forward<TOrError>(_t_or_err);
|
||||
@@ -311,7 +314,7 @@ class Result {
|
||||
/// Returns the value or a default.
|
||||
T value_or(T&& _default) noexcept {
|
||||
const auto handle_variant = [&]<class TOrError>(TOrError&& _t_or_err) -> T {
|
||||
using Type = std::decay_t<TOrError>;
|
||||
using Type = std::remove_cvref_t<TOrError>;
|
||||
if constexpr (!std::is_same<Type, Error>()) {
|
||||
return std::forward<T>(_t_or_err);
|
||||
} else {
|
||||
@@ -342,4 +345,3 @@ class Result {
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace rfl {
|
||||
/// Returns a rfl::Literal containing the field names of struct T.
|
||||
template <class T>
|
||||
using field_names_t = typename std::invoke_result<
|
||||
decltype(internal::get_field_names<std::decay_t<T>>)>::type;
|
||||
decltype(internal::get_field_names<std::remove_cvref_t<T>>)>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -59,22 +59,22 @@ struct Reader {
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
if (!_var.IsString()) {
|
||||
return rfl::Error("Could not cast to string.");
|
||||
}
|
||||
return std::string(_var.AsString().c_str());
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!_var.IsBool()) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
return _var.AsBool();
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
return rfl::Error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(_var.AsDouble());
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (!_var.IsNumeric()) {
|
||||
return rfl::Error("Could not cast to int.");
|
||||
}
|
||||
|
||||
@@ -115,13 +115,13 @@ struct Writer {
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string& _name,
|
||||
const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
fbb_->String(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
fbb_->Bool(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Double(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Int(_name.c_str(), _var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
@@ -131,13 +131,13 @@ struct Writer {
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
fbb_->String(_var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
fbb_->Bool(_var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Double(_var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
fbb_->Int(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
|
||||
@@ -16,14 +16,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from flexbuf var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _obj) {
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const char* _bytes, const size_t _size) {
|
||||
auto read(const char* _bytes, const size_t _size) {
|
||||
const InputVarType root =
|
||||
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
|
||||
return read<T>(root);
|
||||
@@ -31,13 +31,13 @@ Result<T> read(const char* _bytes, const size_t _size) {
|
||||
|
||||
/// Parses an object from flexbuf using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::vector<char>& _bytes) {
|
||||
auto read(const std::vector<char>& _bytes) {
|
||||
return read<T>(_bytes.data(), _bytes.size());
|
||||
}
|
||||
|
||||
/// Parses an object directly from a stream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
std::istreambuf_iterator<char> begin(_stream), end;
|
||||
const auto bytes = std::vector<char>(begin, end);
|
||||
return read<T>(bytes.data(), bytes.size());
|
||||
|
||||
@@ -14,9 +14,10 @@ namespace rfl {
|
||||
|
||||
/// Generates the struct T from a named tuple.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::decay_t<rfl::named_tuple_t<T>>;
|
||||
if constexpr (!std::is_same<std::decay_t<NamedTupleType>, RequiredType>()) {
|
||||
auto from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
|
||||
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
return from_named_tuple<T>(RequiredType(std::forward<NamedTupleType>(_n)));
|
||||
} else if constexpr (internal::has_fields<T>()) {
|
||||
if constexpr (std::is_lvalue_reference<NamedTupleType>{}) {
|
||||
@@ -35,9 +36,10 @@ T from_named_tuple(NamedTupleType&& _n) {
|
||||
|
||||
/// Generates the struct T from a named tuple.
|
||||
template <class T, class NamedTupleType>
|
||||
T from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::decay_t<rfl::named_tuple_t<T>>;
|
||||
if constexpr (!std::is_same<std::decay_t<NamedTupleType>, RequiredType>()) {
|
||||
auto from_named_tuple(const NamedTupleType& _n) {
|
||||
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
|
||||
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
return from_named_tuple<T>(RequiredType(_n));
|
||||
} else if constexpr (internal::has_fields<T>()) {
|
||||
return internal::copy_from_named_tuple<T>(_n);
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef RFL_INTERNAL_ARRAY_HPP_
|
||||
#define RFL_INTERNAL_ARRAY_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
|
||||
#include "to_std_array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
requires std::is_array_v<T>
|
||||
struct Array {
|
||||
using Type = T;
|
||||
using StdArrayType = to_std_array_t<T>;
|
||||
|
||||
Array() = default;
|
||||
Array(const StdArrayType &_arr) : arr_(_arr) {}
|
||||
Array(StdArrayType &&_arr) : arr_(std::move(_arr)) {}
|
||||
Array(const T &_arr) : arr_(to_std_array(_arr)) {}
|
||||
Array(T &&_arr) : arr_(to_std_array(_arr)) {}
|
||||
|
||||
~Array() = default;
|
||||
|
||||
StdArrayType arr_;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+154
-156
@@ -21,66 +21,64 @@ struct Getter;
|
||||
/// Default case - anything that cannot be explicitly matched.
|
||||
template <class NamedTupleType>
|
||||
struct Getter {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
return std::get<_index>(_tup.values());
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<_field_name, typename NamedTupleType::Fields>();
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(const NamedTupleType& _tup) {
|
||||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -88,69 +86,69 @@ struct Getter {
|
||||
/// For handling std::variant.
|
||||
template <class... NamedTupleTypes>
|
||||
struct Getter<std::variant<NamedTupleTypes...>> {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_index>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_index>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](auto& _t) -> auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _tup) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_tup)>;
|
||||
return Getter<NamedTupleType>::template get_const<_index>(_tup);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _tup) -> const auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_tup)>;
|
||||
return Getter<NamedTupleType>::template get_const<_index>(_tup);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<_field_name>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::decay_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
const auto apply = [](const auto& _t) -> const auto& {
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(_t)>;
|
||||
return Getter<NamedTupleType>::template get_const<Field>(_t);
|
||||
};
|
||||
return std::visit(apply, _tup);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -158,54 +156,54 @@ struct Getter<std::variant<NamedTupleTypes...>> {
|
||||
/// For handling std::variant.
|
||||
template <StringLiteral _discriminator, class... NamedTupleTypes>
|
||||
struct Getter<TaggedUnion<_discriminator, NamedTupleTypes...>> {
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
public:
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_field_name>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_index>(_tu.variant_);
|
||||
}
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<_index>(
|
||||
_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
Field>(_tu.variant_);
|
||||
}
|
||||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<Field>(
|
||||
_tu.variant_);
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,15 +16,15 @@ template <class FieldNames, class... Fields>
|
||||
auto copy_flattened_tuple_to_named_tuple(const auto& _flattened_tuple,
|
||||
Fields&&... _fields) {
|
||||
constexpr auto size =
|
||||
std::tuple_size_v<std::decay_t<decltype(_flattened_tuple)>>;
|
||||
std::tuple_size_v<std::remove_cvref_t<decltype(_flattened_tuple)>>;
|
||||
constexpr auto i = sizeof...(_fields);
|
||||
if constexpr (i == size) {
|
||||
return make_named_tuple(std::move(_fields)...);
|
||||
} else {
|
||||
const auto name_literal = FieldNames::template name_of<i>();
|
||||
auto new_field =
|
||||
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
|
||||
std::get<i>(_flattened_tuple));
|
||||
auto new_field = rfl::make_field<
|
||||
lit_name_v<std::remove_cvref_t<decltype(name_literal)>>>(
|
||||
std::get<i>(_flattened_tuple));
|
||||
return copy_flattened_tuple_to_named_tuple<FieldNames>(
|
||||
_flattened_tuple, std::move(_fields)..., std::move(new_field));
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ struct extract_discriminators;
|
||||
|
||||
template <StringLiteral _discriminator, class... NamedTupleType>
|
||||
struct extract_discriminators<TaggedUnion<_discriminator, NamedTupleType...>> {
|
||||
using type = define_literal_t<
|
||||
std::decay_t<field_type_t<_discriminator, NamedTupleType>>...>;
|
||||
using type = define_literal_t<
|
||||
std::remove_cvref_t<field_type_t<_discriminator, NamedTupleType>>...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -12,7 +12,8 @@ namespace internal {
|
||||
/// Finds the index of the field signified by _field_name
|
||||
template <StringLiteral _field_name, class Fields, int I = 0>
|
||||
constexpr static int find_index() {
|
||||
using FieldType = std::decay_t<typename std::tuple_element<I, Fields>::type>;
|
||||
using FieldType =
|
||||
std::remove_cvref_t<typename std::tuple_element<I, Fields>::type>;
|
||||
|
||||
constexpr bool name_i_matches = (FieldType::name_ == _field_name);
|
||||
|
||||
|
||||
@@ -75,13 +75,13 @@ auto get_field_names();
|
||||
|
||||
template <auto ptr>
|
||||
auto get_field_name() {
|
||||
using Type = std::decay_t<std::remove_pointer_t<
|
||||
using Type = std::remove_cvref_t<std::remove_pointer_t<
|
||||
typename std::remove_pointer_t<decltype(ptr)>::Type>>;
|
||||
if constexpr (is_rename_v<Type>) {
|
||||
using Name = typename Type::Name;
|
||||
return Name();
|
||||
} else if constexpr (is_flatten_field_v<Type>) {
|
||||
return get_field_names<std::decay_t<typename Type::Type>>();
|
||||
return get_field_names<std::remove_cvref_t<typename Type::Type>>();
|
||||
} else {
|
||||
return rfl::Literal<get_field_name_str_lit<ptr>()>();
|
||||
}
|
||||
@@ -114,7 +114,7 @@ template <class T>
|
||||
#endif
|
||||
#endif
|
||||
auto get_field_names() {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
|
||||
return get_field_names<std::remove_pointer_t<T>>();
|
||||
} else {
|
||||
constexpr auto ptr_tuple = to_ptr_tuple(fake_object<T>);
|
||||
|
||||
@@ -18,7 +18,7 @@ constexpr bool all_fields_or_flatten() {
|
||||
if constexpr (_i == std::tuple_size_v<TupleType>) {
|
||||
return true;
|
||||
} else {
|
||||
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
|
||||
using T = std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>;
|
||||
if constexpr (is_flatten_field_v<T>) {
|
||||
return all_fields_or_flatten<
|
||||
ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>() &&
|
||||
@@ -34,7 +34,7 @@ constexpr bool some_fields_or_flatten() {
|
||||
if constexpr (_i == std::tuple_size_v<TupleType>) {
|
||||
return false;
|
||||
} else {
|
||||
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
|
||||
using T = std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>;
|
||||
if constexpr (is_flatten_field_v<T>) {
|
||||
return some_fields_or_flatten<
|
||||
ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>() ||
|
||||
|
||||
@@ -15,7 +15,7 @@ constexpr bool has_flatten_fields() {
|
||||
if constexpr (_i == std::tuple_size_v<TupleType>) {
|
||||
return false;
|
||||
} else {
|
||||
using T = std::decay_t<std::tuple_element_t<_i, TupleType>>;
|
||||
using T = std::remove_cvref_t<std::tuple_element_t<_i, TupleType>>;
|
||||
return is_flatten_field_v<T> || has_flatten_fields<TupleType, _i + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
#ifndef RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#if __GNUC__
|
||||
#ifndef __clang__
|
||||
#pragma GCC system_header
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
struct any {
|
||||
template <typename T>
|
||||
constexpr operator T() const noexcept;
|
||||
};
|
||||
|
||||
template <typename T, typename Is, typename = void>
|
||||
struct constructible_with_n_fields_impl : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... Is>
|
||||
struct constructible_with_n_fields_impl<
|
||||
T, std::index_sequence<Is...>,
|
||||
std::void_t<decltype(T{(void(Is), any{})...})>> : std::true_type {};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
using constructible_with_n_fields =
|
||||
constructible_with_n_fields_impl<T, std::make_index_sequence<N>>;
|
||||
|
||||
template <class T, int _n>
|
||||
constexpr bool has_n_fields = constructible_with_n_fields<T, _n>::value &&
|
||||
!constructible_with_n_fields<T, _n + 1>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -28,7 +28,7 @@ template <class Type>
|
||||
class is_attribute<const Attribute<Type>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_attribute_v = is_attribute<std::decay_t<T>>::value;
|
||||
constexpr bool is_attribute_v = is_attribute<std::remove_cvref_t<T>>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -8,10 +8,11 @@ namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_basic_type_v = std::is_floating_point_v<std::decay_t<T>> ||
|
||||
std::is_integral_v<std::decay_t<T>> ||
|
||||
std::is_same<std::decay_t<T>, std::string>() ||
|
||||
std::is_same<std::decay_t<T>, bool>();
|
||||
constexpr bool is_basic_type_v =
|
||||
std::is_floating_point_v<std::remove_cvref_t<T>> ||
|
||||
std::is_integral_v<std::remove_cvref_t<T>> ||
|
||||
std::is_same<std::remove_cvref_t<T>, std::string>() ||
|
||||
std::is_same<std::remove_cvref_t<T>, bool>();
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -11,22 +11,22 @@ namespace internal {
|
||||
|
||||
template <class FieldTuple, class... Args>
|
||||
auto move_and_flatten_field_tuple(FieldTuple&& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::move(_args)...);
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<FieldTuple>>) {
|
||||
return std::tuple_cat(std::move(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::remove_cvref_t<FieldTuple>>;
|
||||
if constexpr (is_flatten_field<T>::value) {
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
move_and_flatten_field_tuple(
|
||||
move_to_field_tuple(std::move(std::get<i>(_t).value_))));
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<FieldTuple>>;
|
||||
if constexpr (is_flatten_field<T>::value) {
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
move_and_flatten_field_tuple(
|
||||
move_to_field_tuple(std::move(std::get<i>(_t).value_))));
|
||||
} else {
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
std::make_tuple(std::move(std::get<i>(_t))));
|
||||
}
|
||||
return move_and_flatten_field_tuple(
|
||||
std::move(_t), std::move(_args)...,
|
||||
std::make_tuple(std::move(std::get<i>(_t))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -13,17 +13,17 @@ namespace internal {
|
||||
|
||||
template <class FieldTuple>
|
||||
auto move_field_tuple_to_named_tuple(FieldTuple&& _field_tuple) {
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(std::move(_fields)...);
|
||||
};
|
||||
const auto ft_to_nt = []<class... Fields>(Fields&&... _fields) {
|
||||
return make_named_tuple(std::move(_fields)...);
|
||||
};
|
||||
|
||||
if constexpr (!has_flatten_fields<std::decay_t<FieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_field_tuple));
|
||||
} else {
|
||||
auto flattened_tuple =
|
||||
move_and_flatten_field_tuple(std::move(_field_tuple));
|
||||
return std::apply(ft_to_nt, std::move(flattened_tuple));
|
||||
}
|
||||
if constexpr (!has_flatten_fields<std::remove_cvref_t<FieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_field_tuple));
|
||||
} else {
|
||||
auto flattened_tuple =
|
||||
move_and_flatten_field_tuple(std::move(_field_tuple));
|
||||
return std::apply(ft_to_nt, std::move(flattened_tuple));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -17,13 +17,15 @@ template <class PtrFieldTupleType, class PtrNamedTupleType, class... Args>
|
||||
auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
|
||||
constexpr auto size = std::tuple_size_v<std::decay_t<PtrFieldTupleType>>;
|
||||
constexpr auto size =
|
||||
std::tuple_size_v<std::remove_cvref_t<PtrFieldTupleType>>;
|
||||
|
||||
if constexpr (i == size) {
|
||||
return std::make_tuple(_args...);
|
||||
} else {
|
||||
using Field = std::decay_t<std::tuple_element_t<i, PtrFieldTupleType>>;
|
||||
using T = std::decay_t<std::remove_pointer_t<typename Field::Type>>;
|
||||
using Field =
|
||||
std::remove_cvref_t<std::tuple_element_t<i, PtrFieldTupleType>>;
|
||||
using T = std::remove_cvref_t<std::remove_pointer_t<typename Field::Type>>;
|
||||
|
||||
if constexpr (is_named_tuple_v<T>) {
|
||||
using SubPtrNamedTupleType =
|
||||
@@ -34,7 +36,7 @@ auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) {
|
||||
_n, _args..., SubPtrNamedTupleType(_n).fields());
|
||||
|
||||
} else if constexpr (is_flatten_field<Field>::value) {
|
||||
using SubPtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
using SubPtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
return make_ptr_fields<PtrFieldTupleType>(
|
||||
_n, _args..., make_ptr_fields<SubPtrFieldTupleType>(_n));
|
||||
@@ -49,10 +51,10 @@ auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) {
|
||||
template <class T, class Pointers, class... Args>
|
||||
auto move_from_ptr_fields(Pointers& _ptrs, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<Pointers>>) {
|
||||
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<Pointers>>) {
|
||||
return T{std::move(_args)...};
|
||||
} else {
|
||||
using FieldType = std::tuple_element_t<i, std::decay_t<Pointers>>;
|
||||
using FieldType = std::tuple_element_t<i, std::remove_cvref_t<Pointers>>;
|
||||
|
||||
if constexpr (is_field_v<FieldType>) {
|
||||
return move_from_ptr_fields<T>(
|
||||
@@ -61,9 +63,9 @@ auto move_from_ptr_fields(Pointers& _ptrs, Args&&... _args) {
|
||||
std::move(*std::get<i>(_ptrs).value())));
|
||||
|
||||
} else {
|
||||
using PtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
using U = std::decay_t<std::remove_pointer_t<
|
||||
using U = std::remove_cvref_t<std::remove_pointer_t<
|
||||
typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
|
||||
|
||||
return move_from_ptr_fields<T>(
|
||||
@@ -77,16 +79,16 @@ auto move_from_ptr_fields(Pointers& _ptrs, Args&&... _args) {
|
||||
/// fields.
|
||||
template <class T, class NamedTupleType>
|
||||
T move_from_named_tuple(NamedTupleType&& _n) {
|
||||
using RequiredType = std::decay_t<named_tuple_t<T>>;
|
||||
using RequiredType = std::remove_cvref_t<named_tuple_t<T>>;
|
||||
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
if constexpr (is_named_tuple_v<std::remove_cvref_t<T>>) {
|
||||
return std::move(_n);
|
||||
|
||||
} else if constexpr (std::is_same<std::decay_t<NamedTupleType>,
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
auto ptr_named_tuple = nt_to_ptr_named_tuple(_n);
|
||||
|
||||
using PtrFieldTupleType = std::decay_t<ptr_field_tuple_t<T>>;
|
||||
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
auto pointers = make_ptr_fields<PtrFieldTupleType>(ptr_named_tuple);
|
||||
|
||||
|
||||
@@ -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"
|
||||
@@ -32,12 +33,12 @@ template <class TargetTupleType, class PtrTupleType, int _j = 0, class... Args>
|
||||
auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
|
||||
constexpr auto size = std::tuple_size_v<std::decay_t<TargetTupleType>>;
|
||||
constexpr auto size = std::tuple_size_v<std::remove_cvref_t<TargetTupleType>>;
|
||||
|
||||
if constexpr (i == size) {
|
||||
return std::make_tuple(_args...);
|
||||
} else {
|
||||
using T = std::decay_t<
|
||||
using T = std::remove_cvref_t<
|
||||
std::remove_pointer_t<std::tuple_element_t<i, TargetTupleType>>>;
|
||||
|
||||
if constexpr (is_flatten_field_v<T>) {
|
||||
@@ -61,19 +62,19 @@ auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) {
|
||||
template <class T, class Pointers, class... Args>
|
||||
auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<Pointers>>) {
|
||||
return T{std::move(_args)...};
|
||||
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<Pointers>>) {
|
||||
return std::remove_cvref_t<T>{std::move(_args)...};
|
||||
} else {
|
||||
using FieldType = std::tuple_element_t<i, std::decay_t<Pointers>>;
|
||||
using FieldType = std::tuple_element_t<i, std::remove_cvref_t<Pointers>>;
|
||||
|
||||
if constexpr (std::is_pointer_v<FieldType>) {
|
||||
return move_from_pointers<T>(_ptrs, std::move(_args)...,
|
||||
std::move(*std::get<i>(_ptrs)));
|
||||
|
||||
} else {
|
||||
using PtrTupleType = ptr_tuple_t<std::decay_t<T>>;
|
||||
using PtrTupleType = ptr_tuple_t<std::remove_cvref_t<T>>;
|
||||
|
||||
using U = std::decay_t<typename std::remove_pointer_t<
|
||||
using U = std::remove_cvref_t<typename std::remove_pointer_t<
|
||||
typename std::tuple_element_t<i, PtrTupleType>>::Type>;
|
||||
|
||||
return move_from_pointers<T>(_ptrs, std::move(_args)...,
|
||||
@@ -82,15 +83,46 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
auto flatten_array(T* _v) {
|
||||
return std::make_tuple(_v);
|
||||
}
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
auto flatten_array(std::array<T, _n>* _arr) {
|
||||
const auto fct = [](auto&... _v) {
|
||||
return std::tuple_cat(flatten_array(&_v)...);
|
||||
};
|
||||
return std::apply(fct, *_arr);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
auto make_tuple_from_element(T _v) {
|
||||
return std::make_tuple(_v);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
auto make_tuple_from_element(Array<T>* _arr) {
|
||||
return flatten_array(&(_arr->arr_));
|
||||
}
|
||||
|
||||
auto flatten_c_arrays(const auto& _tup) {
|
||||
const auto fct = [](auto... _v) {
|
||||
return std::tuple_cat(make_tuple_from_element(_v)...);
|
||||
};
|
||||
return std::apply(fct, _tup);
|
||||
}
|
||||
|
||||
/// Creates a struct of type T from a tuple by moving the underlying
|
||||
/// fields.
|
||||
template <class T, class TupleType>
|
||||
T move_from_tuple(TupleType&& _t) {
|
||||
auto move_from_tuple(TupleType&& _t) {
|
||||
auto ptr_tuple = tup_to_ptr_tuple(_t);
|
||||
|
||||
using TargetTupleType = tuple_t<std::decay_t<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);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "../field_names_t.hpp"
|
||||
#include "Array.hpp"
|
||||
#include "bind_to_tuple.hpp"
|
||||
#include "has_fields.hpp"
|
||||
#include "is_named_tuple.hpp"
|
||||
@@ -15,14 +16,22 @@ namespace internal {
|
||||
|
||||
template <class OriginalStruct>
|
||||
auto move_to_field_tuple(OriginalStruct&& _t) {
|
||||
using T = std::decay_t<OriginalStruct>;
|
||||
using T = std::remove_cvref_t<OriginalStruct>;
|
||||
if constexpr (is_named_tuple_v<T>) {
|
||||
return _t.fields();
|
||||
} else if constexpr (has_fields<T>()) {
|
||||
return bind_to_tuple(_t, [](auto& x) { return std::move(x); });
|
||||
} else {
|
||||
using FieldNames = field_names_t<T>;
|
||||
auto tup = bind_to_tuple(_t, [](auto& x) { return std::move(x); });
|
||||
const auto fct = []<class T>(T& _v) {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_array_v<Type>) {
|
||||
return Array<Type>(_v);
|
||||
} else {
|
||||
return std::move(_v);
|
||||
}
|
||||
};
|
||||
auto tup = bind_to_tuple(_t, fct);
|
||||
return wrap_in_fields<FieldNames>(std::move(tup));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,33 +8,31 @@ namespace internal {
|
||||
|
||||
template <class Fields, int _i = 1, int _j = 0>
|
||||
constexpr inline bool no_duplicate_field_names() {
|
||||
constexpr auto num_fields = std::tuple_size_v<Fields>;
|
||||
constexpr auto num_fields = std::tuple_size_v<Fields>;
|
||||
|
||||
if constexpr (num_fields <= 1) {
|
||||
return true;
|
||||
if constexpr (num_fields <= 1) {
|
||||
return true;
|
||||
} else {
|
||||
if constexpr (_i == num_fields) {
|
||||
return true;
|
||||
} else if constexpr (_j == -1) {
|
||||
return no_duplicate_field_names<Fields, _i + 1, _i>();
|
||||
} else {
|
||||
if constexpr (_i == num_fields) {
|
||||
return true;
|
||||
} else if constexpr (_j == -1) {
|
||||
return no_duplicate_field_names<Fields, _i + 1, _i>();
|
||||
} else {
|
||||
using FieldType1 =
|
||||
std::decay_t<typename std::tuple_element<_i, Fields>::type>;
|
||||
using FieldType2 =
|
||||
std::decay_t<typename std::tuple_element<_j, Fields>::type>;
|
||||
using FieldType1 =
|
||||
std::remove_cvref_t<typename std::tuple_element<_i, Fields>::type>;
|
||||
using FieldType2 =
|
||||
std::remove_cvref_t<typename std::tuple_element<_j, Fields>::type>;
|
||||
|
||||
constexpr auto field_name_i = FieldType1::name_;
|
||||
constexpr auto field_name_j = FieldType2::name_;
|
||||
constexpr auto field_name_i = FieldType1::name_;
|
||||
constexpr auto field_name_j = FieldType2::name_;
|
||||
|
||||
constexpr bool no_duplicate = (field_name_i != field_name_j);
|
||||
constexpr bool no_duplicate = (field_name_i != field_name_j);
|
||||
|
||||
static_assert(no_duplicate,
|
||||
"Duplicate field names are not allowed");
|
||||
static_assert(no_duplicate, "Duplicate field names are not allowed");
|
||||
|
||||
return no_duplicate &&
|
||||
no_duplicate_field_names<Fields, _i, _j - 1>();
|
||||
}
|
||||
return no_duplicate && no_duplicate_field_names<Fields, _i, _j - 1>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -22,7 +22,7 @@ auto nt_to_ptr_named_tuple(NamedTupleType& _nt, AlreadyExtracted... _a) {
|
||||
return make_named_tuple(_a...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
using T = std::remove_cvref_t<typename FieldType::Type>;
|
||||
return nt_to_ptr_named_tuple(
|
||||
_nt, _a..., Field<FieldType::name_, T*>(&std::get<i>(_nt.values())));
|
||||
}
|
||||
@@ -41,7 +41,7 @@ auto nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) {
|
||||
return make_named_tuple(_a...);
|
||||
} else {
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
using T = std::remove_cvref_t<typename FieldType::Type>;
|
||||
return nt_to_ptr_named_tuple(
|
||||
_nt, _a...,
|
||||
Field<FieldType::name_, const T*>(&std::get<i>(_nt.values())));
|
||||
|
||||
@@ -1,26 +1,115 @@
|
||||
#ifndef RFL_INTERNAL_NUM_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_NUM_FIELDS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
/*
|
||||
We infer the number of fields using by figuring out how many fields
|
||||
we need to construct it. This is done by implementing the constructible
|
||||
concept, see below.
|
||||
|
||||
#include "has_n_fields.hpp"
|
||||
However, there is a problem with C arrays. Suppose you have a struct
|
||||
like this:
|
||||
|
||||
struct A{
|
||||
int arr[3];
|
||||
};
|
||||
|
||||
Then, the struct can be initialized like this:
|
||||
|
||||
const auto a = A{1, 2, 3};
|
||||
|
||||
This is a problem, because a naive logic would believe that A
|
||||
has three fields, when in fact it has only one.
|
||||
|
||||
That is why we use the constructible concept to get the maximum
|
||||
possible number of fields and then try to subdivide them into arrays
|
||||
in order to figure out which of these fields is in fact an array.
|
||||
|
||||
Basically, for every field there is, we try to squeeze as many variables into
|
||||
the potential array as we can without missing variables in subsequent fields.
|
||||
This is the purpose of get_nested_array_size().
|
||||
*/
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#if __GNUC__
|
||||
#ifndef __clang__
|
||||
#pragma GCC system_header
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
struct num_fields_impl {
|
||||
static constexpr std::size_t value = num_fields_impl<T, _n + 1>::value;
|
||||
struct any {
|
||||
any(std::size_t);
|
||||
template <typename T>
|
||||
constexpr operator T() const noexcept;
|
||||
};
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
requires has_n_fields<T, _n>
|
||||
struct num_fields_impl<T, _n> {
|
||||
static constexpr std::size_t value = _n;
|
||||
template <typename T>
|
||||
struct CountFieldsHelper {
|
||||
template <std::size_t n>
|
||||
static consteval bool constructible() {
|
||||
return []<std::size_t... is>(std::index_sequence<is...>) {
|
||||
return requires { T{any(is)...}; };
|
||||
}
|
||||
(std::make_index_sequence<n>());
|
||||
}
|
||||
|
||||
template <std::size_t l, std::size_t nested, std::size_t r>
|
||||
static consteval bool constructible_with_nested() {
|
||||
return []<std::size_t... i, std::size_t... j, std::size_t... k>(
|
||||
std::index_sequence<i...>, std::index_sequence<j...>,
|
||||
std::index_sequence<k...>) {
|
||||
return requires { T{any(i)..., {any(j)...}, any(k)...}; };
|
||||
}
|
||||
(std::make_index_sequence<l>(), std::make_index_sequence<nested>(),
|
||||
std::make_index_sequence<r>());
|
||||
}
|
||||
|
||||
template <std::size_t n = 0>
|
||||
static consteval std::size_t count_max_fields() {
|
||||
static_assert(n <= static_cast<std::size_t>(sizeof(T)));
|
||||
if constexpr (constructible<n>() && !constructible<n + 1>()) {
|
||||
return n;
|
||||
} else {
|
||||
return count_max_fields<n + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t index, std::size_t size, std::size_t rest>
|
||||
static consteval std::size_t get_nested_array_size() {
|
||||
if constexpr (size < 1) {
|
||||
return 1;
|
||||
} else if constexpr (constructible_with_nested<index, size, rest>() &&
|
||||
!constructible_with_nested<index, size, rest + 1>()) {
|
||||
return size;
|
||||
} else {
|
||||
return get_nested_array_size<index, size - 1, rest + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t index, std::size_t max>
|
||||
static consteval std::size_t count_fields_impl() {
|
||||
static_assert(index <= max);
|
||||
if constexpr (index == max) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 +
|
||||
count_fields_impl<
|
||||
index + get_nested_array_size<index, max - index, 0>(), max>();
|
||||
}
|
||||
}
|
||||
|
||||
static consteval std::size_t count_fields() {
|
||||
return count_fields_impl<0, count_max_fields()>();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
constexpr std::size_t num_fields = num_fields_impl<T, 0>::value;
|
||||
constexpr std::size_t num_fields = CountFieldsHelper<T>::count_fields();
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
@@ -29,12 +29,12 @@ struct remove_single_field<_OldNamedTupleType, _name, _NewNamedTupleType, 0> {
|
||||
template <class _OldNamedTupleType, StringLiteral _name,
|
||||
class _NewNamedTupleType, int _i>
|
||||
struct remove_single_field {
|
||||
using OldNamedTupleType = std::decay_t<_OldNamedTupleType>;
|
||||
using OldNamedTupleType = std::remove_cvref_t<_OldNamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size_v<typename OldNamedTupleType::Fields>;
|
||||
|
||||
using FieldType = std::decay_t<typename std::tuple_element<
|
||||
using FieldType = std::remove_cvref_t<typename std::tuple_element<
|
||||
num_fields - _i, typename OldNamedTupleType::Fields>::type>;
|
||||
|
||||
using NewNamedTupleType =
|
||||
@@ -53,7 +53,7 @@ struct remove_fields;
|
||||
/// Special case - only head is left.
|
||||
template <class _NamedTupleType, StringLiteral _head>
|
||||
struct remove_fields<_NamedTupleType, _head> {
|
||||
using NamedTupleType = std::decay_t<_NamedTupleType>;
|
||||
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size_v<typename NamedTupleType::Fields>;
|
||||
@@ -65,7 +65,7 @@ struct remove_fields<_NamedTupleType, _head> {
|
||||
/// General case.
|
||||
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
|
||||
struct remove_fields {
|
||||
using NamedTupleType = std::decay_t<_NamedTupleType>;
|
||||
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
|
||||
|
||||
constexpr static int num_fields =
|
||||
std::tuple_size_v<typename NamedTupleType::Fields>;
|
||||
|
||||
@@ -16,7 +16,8 @@ struct remove_ptrs_tup;
|
||||
|
||||
template <class... Ts>
|
||||
struct remove_ptrs_tup<std::tuple<Ts...>> {
|
||||
using TupleType = std::tuple<std::decay_t<std::remove_pointer_t<Ts>>...>;
|
||||
using TupleType =
|
||||
std::tuple<std::remove_cvref_t<std::remove_pointer_t<Ts>>...>;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -15,10 +15,10 @@ auto flatten_ptr_tuple(PtrTuple&& _t, Args... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == 0 && !has_flatten_fields<PtrTuple>()) {
|
||||
return std::forward<PtrTuple>(_t);
|
||||
} else if constexpr (i == std::tuple_size_v<std::decay_t<PtrTuple>>) {
|
||||
} else if constexpr (i == std::tuple_size_v<std::remove_cvref_t<PtrTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrTuple>>;
|
||||
using T = std::tuple_element_t<i, std::remove_cvref_t<PtrTuple>>;
|
||||
if constexpr (is_flatten_field_v<T>) {
|
||||
return flatten_ptr_tuple(
|
||||
std::forward<PtrTuple>(_t), std::forward<Args>(_args)...,
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace internal {
|
||||
|
||||
template <class T>
|
||||
auto to_ptr_field_tuple(T& _t) {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
|
||||
return to_ptr_field_tuple(*_t);
|
||||
} else if constexpr (is_named_tuple_v<T>) {
|
||||
return nt_to_ptr_named_tuple(_t).fields();
|
||||
|
||||
@@ -21,10 +21,10 @@ namespace internal {
|
||||
template <class PtrFieldTuple, class... Args>
|
||||
auto flatten_ptr_field_tuple(PtrFieldTuple& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
using T = std::tuple_element_t<i, std::remove_cvref_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
const auto subtuple =
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
@@ -43,7 +43,7 @@ auto field_tuple_to_named_tuple(PtrFieldTuple& _ptr_field_tuple) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
|
||||
if constexpr (!has_flatten_fields<std::remove_cvref_t<PtrFieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
|
||||
@@ -55,10 +55,10 @@ auto field_tuple_to_named_tuple(PtrFieldTuple& _ptr_field_tuple) {
|
||||
/// the struct.
|
||||
template <class T>
|
||||
auto to_ptr_named_tuple(T&& _t) {
|
||||
if constexpr (has_fields<std::decay_t<T>>()) {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
if constexpr (has_fields<std::remove_cvref_t<T>>()) {
|
||||
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
|
||||
return to_ptr_named_tuple(*_t);
|
||||
} else if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
} else if constexpr (is_named_tuple_v<std::remove_cvref_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace internal {
|
||||
|
||||
template <class T>
|
||||
constexpr auto to_ptr_tuple(T& _t) {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
if constexpr (std::is_pointer_v<std::remove_cvref_t<T>>) {
|
||||
return to_ptr_tuple(*_t);
|
||||
} else {
|
||||
return bind_to_tuple(_t, [](auto&& x) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
|
||||
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rfl::internal {
|
||||
|
||||
template <class T>
|
||||
struct StdArrayType {
|
||||
using Type = T;
|
||||
};
|
||||
|
||||
template <class T, size_t _n>
|
||||
struct StdArrayType<T[_n]> {
|
||||
using Type =
|
||||
std::array<typename StdArrayType<std::remove_cvref_t<T>>::Type, _n>;
|
||||
using ValueType = std::remove_cvref_t<T>;
|
||||
constexpr static size_t size = _n;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using to_std_array_t = StdArrayType<T>::Type;
|
||||
|
||||
template <class T>
|
||||
auto to_std_array(T&& _t) {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_array_v<Type>) {
|
||||
constexpr size_t n = StdArrayType<Type>::size;
|
||||
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
|
||||
return to_std_array_t<Type>({to_std_array(
|
||||
std::forward<typename StdArrayType<Type>::ValueType>(_t[_i]))...});
|
||||
};
|
||||
return fct(std::make_index_sequence<n>());
|
||||
} else {
|
||||
return std::forward<T>(_t);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
auto to_std_array(const T& _t) {
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_array_v<Type>) {
|
||||
constexpr size_t n = StdArrayType<Type>::size;
|
||||
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
|
||||
return to_std_array_t<Type>({to_std_array(_t[_i])...});
|
||||
};
|
||||
return fct(std::make_index_sequence<n>());
|
||||
} else {
|
||||
return _t;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
#endif
|
||||
@@ -13,25 +13,26 @@ namespace internal {
|
||||
|
||||
template <class FieldNames, int j = 0, class... Fields>
|
||||
auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) {
|
||||
constexpr auto size = std::tuple_size_v<std::decay_t<decltype(_tuple)>>;
|
||||
constexpr auto size =
|
||||
std::tuple_size_v<std::remove_cvref_t<decltype(_tuple)>>;
|
||||
constexpr auto i = sizeof...(_fields);
|
||||
if constexpr (i == size) {
|
||||
return std::make_tuple(std::move(_fields)...);
|
||||
} else {
|
||||
auto value = std::move(std::get<i>(_tuple));
|
||||
using Type = std::decay_t<std::remove_pointer_t<decltype(value)>>;
|
||||
using Type = std::remove_cvref_t<std::remove_pointer_t<decltype(value)>>;
|
||||
if constexpr (is_flatten_field_v<Type>) {
|
||||
// The problem here is that the FieldNames are already flattened, but this
|
||||
// is not, so we need to determine how many field names to skip.
|
||||
constexpr auto n_skip = std::tuple_size_v<
|
||||
std::decay_t<flattened_ptr_tuple_t<typename Type::Type>>>;
|
||||
std::remove_cvref_t<flattened_ptr_tuple_t<typename Type::Type>>>;
|
||||
return wrap_in_fields<FieldNames, j + n_skip>(
|
||||
std::move(_tuple), std::move(_fields)..., std::move(value));
|
||||
} else {
|
||||
const auto name_literal = FieldNames::template name_of<j>();
|
||||
auto new_field =
|
||||
rfl::make_field<lit_name_v<std::decay_t<decltype(name_literal)>>>(
|
||||
std::move(value));
|
||||
auto new_field = rfl::make_field<
|
||||
lit_name_v<std::remove_cvref_t<decltype(name_literal)>>>(
|
||||
std::move(value));
|
||||
return wrap_in_fields<FieldNames, j + 1>(
|
||||
std::move(_tuple), std::move(_fields)..., std::move(new_field));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_INTERNAL_WRAP_IN_RFL_ARRAY_T_
|
||||
#define RFL_INTERNAL_WRAP_IN_RFL_ARRAY_T_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "Array.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
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 internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -63,23 +63,23 @@ struct Reader {
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
const auto r = yyjson_get_str(_var.val_);
|
||||
if (r == NULL) {
|
||||
return rfl::Error("Could not cast to string.");
|
||||
}
|
||||
return std::string(r);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
if (!yyjson_is_bool(_var.val_)) {
|
||||
return rfl::Error("Could not cast to boolean.");
|
||||
}
|
||||
return yyjson_get_bool(_var.val_);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
if (!yyjson_is_num(_var.val_)) {
|
||||
return rfl::Error("Could not cast to double.");
|
||||
}
|
||||
return static_cast<T>(yyjson_get_num(_var.val_));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
if (!yyjson_is_int(_var.val_)) {
|
||||
return rfl::Error("Could not cast to int.");
|
||||
}
|
||||
|
||||
@@ -143,13 +143,13 @@ class Writer {
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -17,23 +18,26 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a JSON var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _obj) {
|
||||
auto read(const InputVarType& _obj) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _obj);
|
||||
}
|
||||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _json_str) {
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _json_str) {
|
||||
using PtrType = std::unique_ptr<yyjson_doc, void (*)(yyjson_doc*)>;
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
if (!doc) {
|
||||
return Error("Could not parse document!");
|
||||
}
|
||||
const auto ptr = PtrType(doc, yyjson_doc_free);
|
||||
return read<T>(InputVarType(yyjson_doc_get_root(doc)));
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(json_str);
|
||||
|
||||
@@ -11,15 +11,15 @@ namespace rfl {
|
||||
/// to explitly defined the field types.
|
||||
template <class... FieldTypes>
|
||||
auto make_named_tuple(FieldTypes&&... _args) {
|
||||
return NamedTuple<std::decay_t<FieldTypes>...>(
|
||||
std::forward<FieldTypes>(_args)...);
|
||||
return NamedTuple<std::remove_cvref_t<FieldTypes>...>(
|
||||
std::forward<FieldTypes>(_args)...);
|
||||
}
|
||||
|
||||
/// Convenience constructor that doesn't require you
|
||||
/// to explitly defined the field types.
|
||||
template <class Head, class... Tail>
|
||||
auto make_named_tuple(const Head& _h, const Tail&... _t) {
|
||||
return NamedTuple<Head, Tail...>(_h, _t...);
|
||||
return NamedTuple<Head, Tail...>(_h, _t...);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace rfl {
|
||||
|
||||
/// Convenience class to retrieve the name of a field.
|
||||
template <class FieldType>
|
||||
using name_t = typename std::decay_t<FieldType>::Name;
|
||||
using name_t = typename std::remove_cvref_t<FieldType>::Name;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "NamedTuple.hpp"
|
||||
#include "internal/is_named_tuple.hpp"
|
||||
#include "internal/ptr_named_tuple_t.hpp"
|
||||
#include "internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "to_named_tuple.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::decay_t<std::remove_pointer_t<T>>>;
|
||||
using FieldType =
|
||||
Field<_name, internal::wrap_in_rfl_array_t<
|
||||
std::remove_cvref_t<std::remove_pointer_t<T>>>>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
|
||||
@@ -61,7 +61,7 @@ struct FieldVariantParser {
|
||||
|
||||
const auto handle = [&](const auto& _field) {
|
||||
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
|
||||
Parser<R, W, NamedTupleType>::write(_w, named_tuple, _parent);
|
||||
};
|
||||
|
||||
@@ -79,10 +79,10 @@ struct FieldVariantParser {
|
||||
"'" +
|
||||
_disc_value + "'.");
|
||||
} else {
|
||||
using FieldType = std::decay_t<
|
||||
using FieldType = std::remove_cvref_t<
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type>;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
|
||||
|
||||
const auto key = FieldType::name_.str();
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <string_view>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -41,7 +43,9 @@ concept IsReader = requires(R r, std::string name,
|
||||
|
||||
/// Transforms var to a basic type (bool, integral,
|
||||
/// floating point, std::string)
|
||||
{ r.template to_basic_type<T>(var) } -> std::same_as<rfl::Result<T>>;
|
||||
{
|
||||
r.template to_basic_type<internal::wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<internal::wrap_in_rfl_array_t<T>>>;
|
||||
|
||||
/// _fct is a function that turns the field name into the field index of the
|
||||
/// struct. It returns -1, if the fields does not exist on the struct. This
|
||||
@@ -71,7 +75,9 @@ concept IsReader = requires(R r, std::string name,
|
||||
|
||||
/// Uses the custom constructor, if it has been determined that T has one
|
||||
/// (see above).
|
||||
{ r.template use_custom_constructor<T>(var) } -> std::same_as<rfl::Result<T>>;
|
||||
{
|
||||
r.template use_custom_constructor<internal::wrap_in_rfl_array_t<T>>(var)
|
||||
} -> std::same_as<rfl::Result<internal::wrap_in_rfl_array_t<T>>>;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -23,8 +23,9 @@ struct MapParser {
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using KeyType = std::decay_t<typename MapType::value_type::first_type>;
|
||||
using ValueType = std::decay_t<typename MapType::value_type::second_type>;
|
||||
using KeyType = std::remove_cvref_t<typename MapType::value_type::first_type>;
|
||||
using ValueType =
|
||||
std::remove_cvref_t<typename MapType::value_type::second_type>;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
@@ -44,21 +45,23 @@ struct MapParser {
|
||||
std::is_floating_point_v<ReflT>) {
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k.reflection()), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
Parser<R, W, std::remove_cvref_t<ValueType>>::write(_w, v,
|
||||
new_parent);
|
||||
} else {
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{k.reflection(), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
Parser<R, W, std::remove_cvref_t<ValueType>>::write(_w, v,
|
||||
new_parent);
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral_v<KeyType> ||
|
||||
std::is_floating_point_v<KeyType>) {
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
Parser<R, W, std::remove_cvref_t<ValueType>>::write(_w, v, new_parent);
|
||||
} else {
|
||||
const auto new_parent = typename ParentType::Object{k, &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
Parser<R, W, std::remove_cvref_t<ValueType>>::write(_w, v, new_parent);
|
||||
}
|
||||
}
|
||||
_w.end_object(&obj);
|
||||
@@ -109,7 +112,7 @@ struct MapParser {
|
||||
auto pair = std::make_pair(std::move(_pair.first), std::move(_val));
|
||||
return make_key(pair);
|
||||
};
|
||||
return Parser<R, W, std::decay_t<ValueType>>::read(_r, _pair.second)
|
||||
return Parser<R, W, std::remove_cvref_t<ValueType>>::read(_r, _pair.second)
|
||||
.and_then(to_pair)
|
||||
.value();
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ struct NamedTupleParser {
|
||||
using FieldType = typename std::tuple_element<
|
||||
i, typename NamedTuple<FieldTypes...>::Fields>::type;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
|
||||
|
||||
const auto& f = std::get<i>(_fields_arr);
|
||||
|
||||
@@ -129,7 +129,7 @@ struct NamedTupleParser {
|
||||
using FieldType = typename std::tuple_element<
|
||||
_i, typename NamedTuple<FieldTypes...>::Fields>::type;
|
||||
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
|
||||
|
||||
const auto& f = std::get<_i>(_fields_arr);
|
||||
|
||||
@@ -163,7 +163,7 @@ struct NamedTupleParser {
|
||||
} else {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
|
||||
const auto& value = rfl::get<_i>(_tup);
|
||||
const auto name = FieldType::name_.str();
|
||||
const auto new_parent = typename ParentType::Object{name, _ptr};
|
||||
@@ -206,7 +206,7 @@ struct NamedTupleParser {
|
||||
const auto key = FieldType::name_.str();
|
||||
return Error("Failed to parse field '" + key + "': " + _e.what());
|
||||
};
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
|
||||
return Parser<R, W, ValueType>::read(_r, _var).or_else(embellish_error);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ struct Parent {
|
||||
template <class ParentType>
|
||||
static OutputArrayType add_array(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
using Type = std::remove_cvref_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_array_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
@@ -47,7 +47,7 @@ struct Parent {
|
||||
template <class ParentType>
|
||||
static OutputObjectType add_object(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
using Type = std::remove_cvref_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_object_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
@@ -61,11 +61,11 @@ struct Parent {
|
||||
|
||||
template <class ParentType>
|
||||
static OutputVarType add_null(const W& _w, const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
using Type = std::remove_cvref_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_null_to_array(_parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
if constexpr (supports_attributes<std::remove_cvref_t<W>>) {
|
||||
return _w.add_null_to_object(_parent.name_, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
@@ -81,11 +81,11 @@ struct Parent {
|
||||
template <class ParentType, class T>
|
||||
static OutputVarType add_value(const W& _w, const T& _var,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
using Type = std::remove_cvref_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_value_to_array(_var, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
if constexpr (supports_attributes<std::remove_cvref_t<W>>) {
|
||||
return _w.add_value_to_object(_parent.name_, _var, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
|
||||
@@ -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"
|
||||
@@ -14,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"
|
||||
|
||||
@@ -51,8 +51,8 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, _size, _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (auto it = _arr.begin(); it != _arr.end(); ++it) {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *it, new_parent);
|
||||
for (const auto& e : _arr) {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, e, new_parent);
|
||||
}
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
@@ -65,7 +65,7 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
AlreadyExtracted&&... _already_extracted) noexcept {
|
||||
constexpr size_t i = sizeof...(AlreadyExtracted);
|
||||
if constexpr (i == _size) {
|
||||
return std::array<T, _size>({std::move(_already_extracted)...});
|
||||
return std::array<T, _size>{std::move(_already_extracted)...};
|
||||
} else {
|
||||
const auto extract_next = [&](auto&& new_entry) {
|
||||
return extract_field_by_field(_r, std::move(_vec),
|
||||
|
||||
@@ -18,13 +18,14 @@ struct Parser<R, W, Box<T>> {
|
||||
|
||||
static Result<Box<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_box = [](auto&& _t) { return Box<T>::make(std::move(_t)); };
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_box);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_box);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const Box<T>& _box,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_box, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_box, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#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 "../internal/Array.hpp"
|
||||
#include "../internal/to_std_array.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_array.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 ParentType = Parent<W>;
|
||||
using CArray = T[_size];
|
||||
|
||||
static Result<internal::Array<CArray>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
using StdArray = internal::to_std_array_t<CArray>;
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const CArray& _arr,
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, _size, _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (const auto& e : _arr) {
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, e, new_parent);
|
||||
}
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -34,7 +34,7 @@ struct Parser {
|
||||
return _r.template use_custom_constructor<T>(_var);
|
||||
} else {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
using ReflectionType = std::remove_cvref_t<typename T::ReflectionType>;
|
||||
const auto wrap_in_t = [](auto _named_tuple) -> Result<T> {
|
||||
try {
|
||||
return T(_named_tuple);
|
||||
@@ -58,7 +58,7 @@ struct Parser {
|
||||
return _r.template to_basic_type<std::string>(_var).and_then(
|
||||
StringConverter::string_to_enum);
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _r.template to_basic_type<std::decay_t<T>>(_var);
|
||||
return _r.template to_basic_type<std::remove_cvref_t<T>>(_var);
|
||||
} else {
|
||||
static_assert(
|
||||
always_false_v<T>,
|
||||
@@ -72,7 +72,7 @@ struct Parser {
|
||||
template <class P>
|
||||
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
using ReflectionType = std::remove_cvref_t<typename T::ReflectionType>;
|
||||
if constexpr (internal::has_reflection_method_v<T>) {
|
||||
Parser<R, W, ReflectionType>::write(_w, _var.reflection(), _parent);
|
||||
} else {
|
||||
@@ -81,7 +81,7 @@ struct Parser {
|
||||
}
|
||||
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
|
||||
const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var);
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
using PtrNamedTupleType = std::remove_cvref_t<decltype(ptr_named_tuple)>;
|
||||
Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple, _parent);
|
||||
} else if constexpr (std::is_enum_v<T>) {
|
||||
using StringConverter = internal::enums::StringConverter<T>;
|
||||
|
||||
@@ -26,7 +26,8 @@ struct Parser<R, W, std::optional<T>> {
|
||||
return std::optional<T>();
|
||||
}
|
||||
const auto to_opt = [](auto&& _t) { return std::make_optional<T>(_t); };
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_opt);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_opt);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
@@ -36,7 +37,7 @@ struct Parser<R, W, std::optional<T>> {
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_o, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_o, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ struct Parser<R, W, T*> {
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ptr, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_ptr, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -19,13 +19,14 @@ struct Parser<R, W, Ref<T>> {
|
||||
|
||||
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ref);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_ref);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const Ref<T>& _ref,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ref, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_ref, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ struct Parser<R, W, std::reference_wrapper<T>> {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::reference_wrapper<T> _ref,
|
||||
const P& _p) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _ref.get(), _p);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, _ref.get(), _p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -23,13 +23,14 @@ struct Parser<R, W, Rename<_name, T>> {
|
||||
const auto to_rename = [](auto&& _t) {
|
||||
return Rename<_name, T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_rename);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const Rename<_name, T>& _rename,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _rename.value(), _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, _rename.value(), _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -18,16 +18,16 @@ struct Parser<R, W, Result<T>> {
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ErrorType = NamedTuple<Field<"error", std::string>>;
|
||||
using VariantType = std::variant<std::decay_t<T>, ErrorType>;
|
||||
using VariantType = std::variant<std::remove_cvref_t<T>, ErrorType>;
|
||||
|
||||
static Result<Result<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto handle = [](auto&& _t) -> Result<T> {
|
||||
using Type = std::decay_t<decltype(_t)>;
|
||||
using Type = std::remove_cvref_t<decltype(_t)>;
|
||||
if constexpr (std::is_same<Type, ErrorType>()) {
|
||||
return Error(_t.template get<"error">());
|
||||
} else {
|
||||
return std::forward<std::decay_t<T>>(_t);
|
||||
return std::forward<std::remove_cvref_t<T>>(_t);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ struct Parser<R, W, Result<T>> {
|
||||
static void write(const W& _w, const Result<T>& _r,
|
||||
const P& _parent) noexcept {
|
||||
const auto write_t = [&](const auto& _t) -> Nothing {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _t, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, _t, _parent);
|
||||
return Nothing{};
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
#define RFL_PARSING_PARSER_RFL_ARRAY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Array.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, internal::Array<T>>
|
||||
struct Parser<R, W, internal::Array<T>> {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using StdArray = internal::to_std_array_t<T>;
|
||||
|
||||
static Result<internal::Array<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
return Parser<R, W, StdArray>::read(_r, _var);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w, const internal::Array<T>& _arr,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, StdArray>::write(_w, _arr.arr_, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -28,7 +28,8 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
return std::make_shared<T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_ptr);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
@@ -38,7 +39,7 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ namespace parsing {
|
||||
|
||||
template <class R, class W, internal::StringLiteral _discriminator,
|
||||
class... AlternativeTypes>
|
||||
requires AreReaderAndWriter<R, W,
|
||||
TaggedUnion<_discriminator, AlternativeTypes...>>
|
||||
requires AreReaderAndWriter<R, W,
|
||||
TaggedUnion<_discriminator, AlternativeTypes...>>
|
||||
struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
using ResultType = Result<TaggedUnion<_discriminator, AlternativeTypes...>>;
|
||||
|
||||
@@ -58,7 +58,7 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
return Error("Could not parse tagged union, could not match " +
|
||||
_discriminator.str() + " '" + _disc_value + "'.");
|
||||
} else {
|
||||
using AlternativeType = std::decay_t<
|
||||
using AlternativeType = std::remove_cvref_t<
|
||||
std::variant_alternative_t<_i, std::variant<AlternativeTypes...>>>;
|
||||
|
||||
if (contains_disc_value<AlternativeType>(_disc_value)) {
|
||||
@@ -115,8 +115,8 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
static void write_wrapped(const W& _w, const T& _val,
|
||||
const P& _parent) noexcept {
|
||||
const auto tag = internal::make_tag<T>();
|
||||
using TagType = std::decay_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::decay_t<T>>()) {
|
||||
using TagType = std::remove_cvref_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::remove_cvref_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
|
||||
@@ -28,7 +28,8 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
const auto to_ptr = [](auto&& _t) {
|
||||
return std::make_unique<T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _var).transform(
|
||||
to_ptr);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
@@ -38,7 +39,7 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
|
||||
return read<_i + 1>(_r, _var, errors);
|
||||
};
|
||||
|
||||
using AltType = std::decay_t<
|
||||
using AltType = std::remove_cvref_t<
|
||||
std::variant_alternative_t<_i, std::variant<FieldTypes...>>>;
|
||||
|
||||
return Parser<R, W, AltType>::read(_r, _var)
|
||||
@@ -56,7 +56,7 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
|
||||
FieldVariantParser<R, W, FieldTypes...>::write(_w, _variant, _parent);
|
||||
} else {
|
||||
const auto handle = [&](const auto& _v) {
|
||||
using Type = std::decay_t<decltype(_v)>;
|
||||
using Type = std::remove_cvref_t<decltype(_v)>;
|
||||
Parser<R, W, Type>::write(_w, _v, _parent);
|
||||
};
|
||||
return std::visit(handle, _variant);
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R, class W, bool _ignore_empty_containers, class... Ts>
|
||||
requires AreReaderAndWriter<R, W, std::tuple<Ts...>>
|
||||
requires AreReaderAndWriter<R, W, std::tuple<Ts...>>
|
||||
struct TupleParser {
|
||||
public:
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
@@ -76,8 +76,8 @@ struct TupleParser {
|
||||
template <int _i>
|
||||
static auto extract_single_field(
|
||||
const R& _r, const std::vector<InputVarType>& _vec) noexcept {
|
||||
using NewFieldType =
|
||||
std::decay_t<typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
using NewFieldType = std::remove_cvref_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
|
||||
using ResultType = Result<NewFieldType>;
|
||||
|
||||
@@ -98,7 +98,7 @@ struct TupleParser {
|
||||
static void to_array(const W& _w, const std::tuple<Ts...>& _tup,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (_i < sizeof...(Ts)) {
|
||||
using NewFieldType = std::decay_t<
|
||||
using NewFieldType = std::remove_cvref_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
Parser<R, W, NewFieldType>::write(_w, std::get<_i>(_tup), _parent);
|
||||
to_array<_i + 1>(_w, _tup, _parent);
|
||||
|
||||
@@ -58,7 +58,7 @@ struct VectorParser {
|
||||
_w, std::distance(_vec.begin(), _vec.end()), _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (const auto& v : _vec) {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, v, new_parent);
|
||||
Parser<R, W, std::remove_cvref_t<T>>::write(_w, v, new_parent);
|
||||
}
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
@@ -66,13 +66,14 @@ struct VectorParser {
|
||||
|
||||
private:
|
||||
static auto get_elem(const R& _r, auto& _v) {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _v).value();
|
||||
return Parser<R, W, std::remove_cvref_t<T>>::read(_r, _v).value();
|
||||
};
|
||||
|
||||
static auto get_pair(const R& _r, auto& _v) {
|
||||
using K = std::decay_t<typename T::first_type>;
|
||||
using V = std::decay_t<typename T::second_type>;
|
||||
return Parser<R, W, std::decay_t<std::pair<K, V>>>::read(_r, _v).value();
|
||||
using K = std::remove_cvref_t<typename T::first_type>;
|
||||
using V = std::remove_cvref_t<typename T::second_type>;
|
||||
return Parser<R, W, std::remove_cvref_t<std::pair<K, V>>>::read(_r, _v)
|
||||
.value();
|
||||
}
|
||||
|
||||
static Result<VecType> to_container(const R& _r, InputArrayType&& _arr) {
|
||||
@@ -109,7 +110,7 @@ struct VectorParser {
|
||||
static constexpr bool treat_as_map() {
|
||||
if constexpr (is_map_like_not_multimap<VecType>()) {
|
||||
if constexpr (internal::has_reflection_type_v<typename T::first_type>) {
|
||||
using U = std::decay_t<typename T::first_type::ReflectionType>;
|
||||
using U = std::remove_cvref_t<typename T::first_type::ReflectionType>;
|
||||
return std::is_same<U, std::string>() || std::is_integral_v<U> ||
|
||||
std::is_floating_point_v<U>;
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace parsing {
|
||||
|
||||
template <class T>
|
||||
static const bool is_empty(const T& _var) {
|
||||
using Type = std::decay_t<T>;
|
||||
using Type = std::remove_cvref_t<T>;
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
return !_var || is_empty(*_var);
|
||||
} else if constexpr (internal::has_reflection_type_v<Type>) {
|
||||
|
||||
@@ -28,7 +28,7 @@ class is_map_like<std::unordered_multimap<K, V>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_map_like_v =
|
||||
is_map_like<std::decay_t<std::remove_pointer_t<T>>>::value;
|
||||
is_map_like<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
@@ -36,7 +36,7 @@ constexpr bool is_never_required_v = is_never_required<T>::value;
|
||||
|
||||
template <class T, bool _ignore_empty_containers>
|
||||
consteval bool is_required() {
|
||||
using Type = std::decay_t<std::remove_pointer_t<T>>;
|
||||
using Type = std::remove_cvref_t<std::remove_pointer_t<T>>;
|
||||
if constexpr (internal::has_reflection_type_v<Type>) {
|
||||
return is_required<typename Type::ReflectionType,
|
||||
_ignore_empty_containers>();
|
||||
|
||||
@@ -45,7 +45,7 @@ class is_vector_like<std::vector<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_vector_like_v =
|
||||
is_vector_like<std::decay_t<std::remove_pointer_t<T>>>::value;
|
||||
is_vector_like<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace rfl {
|
||||
/// NamedTupleType.
|
||||
template <class NamedTupleType, internal::StringLiteral... _names>
|
||||
using remove_fields_t =
|
||||
typename internal::remove_fields<std::decay_t<NamedTupleType>,
|
||||
typename internal::remove_fields<std::remove_cvref_t<NamedTupleType>,
|
||||
_names...>::type;
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -21,9 +21,9 @@ namespace rfl {
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
auto to_named_tuple(T&& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
if constexpr (internal::is_named_tuple_v<std::remove_cvref_t<T>>) {
|
||||
return _t;
|
||||
} else if constexpr (internal::is_field_v<std::decay_t<T>>) {
|
||||
} else if constexpr (internal::is_field_v<std::remove_cvref_t<T>>) {
|
||||
return make_named_tuple(std::forward<T>(_t));
|
||||
} else if constexpr (std::is_lvalue_reference<T>{}) {
|
||||
auto field_tuple = internal::copy_to_field_tuple(_t);
|
||||
@@ -39,9 +39,9 @@ auto to_named_tuple(T&& _t) {
|
||||
/// All fields of the struct must be an rfl::Field.
|
||||
template <class T>
|
||||
auto to_named_tuple(const T& _t) {
|
||||
if constexpr (internal::is_named_tuple_v<std::decay_t<T>>) {
|
||||
if constexpr (internal::is_named_tuple_v<std::remove_cvref_t<T>>) {
|
||||
return _t;
|
||||
} else if constexpr (internal::is_field_v<std::decay_t<T>>) {
|
||||
} else if constexpr (internal::is_field_v<std::remove_cvref_t<T>>) {
|
||||
return make_named_tuple(_t);
|
||||
} else {
|
||||
auto field_tuple = internal::copy_to_field_tuple(_t);
|
||||
|
||||
@@ -53,7 +53,7 @@ struct Reader {
|
||||
const std::variant<pugi::xml_node, pugi::xml_attribute>&
|
||||
_node_or_attribute) {
|
||||
const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
|
||||
using Type = std::decay_t<decltype(_n)>;
|
||||
using Type = std::remove_cvref_t<decltype(_n)>;
|
||||
if constexpr (std::is_same<Type, pugi::xml_node>()) {
|
||||
return _n;
|
||||
} else {
|
||||
@@ -82,7 +82,7 @@ struct Reader {
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
|
||||
const auto get_value = [](const auto& _n) -> std::string {
|
||||
using Type = std::decay_t<decltype(_n)>;
|
||||
using Type = std::remove_cvref_t<decltype(_n)>;
|
||||
if constexpr (std::is_same<Type, pugi::xml_node>()) {
|
||||
return std::string(_n.child_value());
|
||||
} else {
|
||||
@@ -90,11 +90,11 @@ struct Reader {
|
||||
}
|
||||
};
|
||||
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return std::visit(get_value, _var.node_or_attribute_);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return std::visit(get_value, _var.node_or_attribute_) == "true";
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
const auto str = std::visit(get_value, _var.node_or_attribute_);
|
||||
try {
|
||||
return static_cast<T>(std::stod(str));
|
||||
@@ -102,7 +102,7 @@ struct Reader {
|
||||
return Error("Could not cast '" + std::string(str) +
|
||||
"' to floating point value.");
|
||||
}
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
const auto str = std::visit(get_value, _var.node_or_attribute_);
|
||||
try {
|
||||
return static_cast<T>(std::stoi(str));
|
||||
|
||||
@@ -156,12 +156,12 @@ struct Writer {
|
||||
private:
|
||||
template <class T>
|
||||
std::string to_string(const T& _val) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
return _val;
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return _val ? "true" : "false";
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return std::to_string(_val);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
|
||||
@@ -17,7 +17,7 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a XML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ Result<T> read(const std::string& _xml_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto xml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(xml_str);
|
||||
|
||||
@@ -22,7 +22,7 @@ consteval auto get_root_name() {
|
||||
return _root;
|
||||
} else {
|
||||
return internal::remove_namespaces<
|
||||
internal::get_type_name<std::decay_t<T>>()>();
|
||||
internal::get_type_name<std::remove_cvref_t<T>>()>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -65,11 +65,11 @@ struct Reader {
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>() ||
|
||||
std::is_same<std::decay_t<T>, bool>() ||
|
||||
std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
return _var.node_.as<std::decay_t<T>>();
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>() ||
|
||||
std::is_same<std::remove_cvref_t<T>, bool>() ||
|
||||
std::is_floating_point<std::remove_cvref_t<T>>() ||
|
||||
std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return _var.node_.as<std::remove_cvref_t<T>>();
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "../internal/wrap_in_rfl_array_t.hpp"
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
@@ -16,14 +17,14 @@ using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
auto read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _yaml_str) {
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T>(var);
|
||||
@@ -34,7 +35,7 @@ Result<T> read(const std::string& _yaml_str) {
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
auto read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(yaml_str);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "test_c_array_class1.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "rfl.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_c_array_class1 {
|
||||
|
||||
struct Test1 {
|
||||
std::vector<std::string> classes[3];
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Test1 test1 = {.classes = {{"Little A", "Little B", "Little C"},
|
||||
{"BIG A", "BIG B", "BIG C"},
|
||||
{"??", "$%", "#@"}}};
|
||||
write_and_read(
|
||||
test1,
|
||||
R"({"classes":[["Little A","Little B","Little C"],["BIG A","BIG B","BIG C"],["??","$%","#@"]]})");
|
||||
}
|
||||
|
||||
} // namespace test_c_array_class1
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace test_c_array_class1 {
|
||||
|
||||
void test();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "test_c_array_class2.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "rfl.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_c_array_class2 {
|
||||
|
||||
struct Test2 {
|
||||
int multi_dimension_arr[2][2][2][2];
|
||||
std::string s;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Test2 test2;
|
||||
for (int i = 0; i < 1 << 4; i++) {
|
||||
test2.multi_dimension_arr[i >> 3 & 1][i >> 2 & 1][i >> 1 & 1][i >> 0 & 1] = i;
|
||||
}
|
||||
test2.s = "Hello, world!";
|
||||
|
||||
write_and_read(
|
||||
test2,
|
||||
R"({"multi_dimension_arr":[[[[0,1],[2,3]],[[4,5],[6,7]]],[[[8,9],[10,11]],[[12,13],[14,15]]]],"s":"Hello, world!"})");
|
||||
}
|
||||
|
||||
} // namespace test_c_array
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace test_c_array_class2 {
|
||||
|
||||
void test();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "test_c_array_class3.hpp"
|
||||
|
||||
#include <source_location>
|
||||
#include <array>
|
||||
|
||||
#include "rfl.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_c_array_class3 {
|
||||
|
||||
using Test3 = std::array<int, 3>[3];
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Test3 test3 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
write_and_read(test3, "[[1,2,3],[4,5,6],[7,8,9]]");
|
||||
}
|
||||
|
||||
} // namespace test_c_array
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace test_c_array_class3 {
|
||||
|
||||
void test();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "test_c_array_class4.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <source_location>
|
||||
|
||||
#include "rfl.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_c_array_class4 {
|
||||
|
||||
struct Test4 {
|
||||
int a[3];
|
||||
int b[3];
|
||||
int c[2][2];
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Test4 test4 = {.a = {1, 2, 3}, .b = {4, 5, 6}, .c = {{7, 8}, {9, 10}}};
|
||||
|
||||
write_and_read(test4, R"({"a":[1,2,3],"b":[4,5,6],"c":[[7,8],[9,10]]})");
|
||||
}
|
||||
|
||||
} // namespace test_c_array_class4
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace test_c_array_class4 {
|
||||
|
||||
void test();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "test_c_array_class5.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <source_location>
|
||||
|
||||
#include "rfl.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_c_array_class5 {
|
||||
|
||||
struct Test5 {
|
||||
int a[3];
|
||||
int b[3];
|
||||
int c[2][2];
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Test5 t1 = {.a = {1, 2, 3}, .b = {4, 5, 6}, .c = {{7, 8}, {9, 10}}};
|
||||
|
||||
const auto t2 = rfl::replace(t1, rfl::make_field<"b", int[3]>({1, 2, 3}));
|
||||
|
||||
write_and_read(t2, R"({"a":[1,2,3],"b":[1,2,3],"c":[[7,8],[9,10]]})");
|
||||
}
|
||||
|
||||
} // namespace test_c_array_class5
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace test_c_array_class5 {
|
||||
|
||||
void test();
|
||||
|
||||
}
|
||||
@@ -8,6 +8,11 @@
|
||||
#include "test_as2.hpp"
|
||||
#include "test_as_flatten.hpp"
|
||||
#include "test_box.hpp"
|
||||
#include "test_c_array_class1.hpp"
|
||||
#include "test_c_array_class2.hpp"
|
||||
#include "test_c_array_class3.hpp"
|
||||
#include "test_c_array_class4.hpp"
|
||||
#include "test_c_array_class5.hpp"
|
||||
#include "test_custom_class1.hpp"
|
||||
#include "test_custom_class2.hpp"
|
||||
#include "test_custom_class3.hpp"
|
||||
@@ -112,6 +117,12 @@ int main() {
|
||||
test_custom_class3::test();
|
||||
test_custom_class4::test();
|
||||
|
||||
test_c_array_class1::test();
|
||||
test_c_array_class2::test();
|
||||
test_c_array_class3::test();
|
||||
test_c_array_class4::test();
|
||||
test_c_array_class5::test();
|
||||
|
||||
test_replace::test();
|
||||
test_replace2::test();
|
||||
test_replace_flatten::test();
|
||||
|
||||
Reference in New Issue
Block a user