Skip to content

Commit bf60ca0

Browse files
Merge branch 'hazby2002-main'
2 parents 5c81e40 + 5e9a660 commit bf60ca0

File tree

90 files changed

+1565
-1495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1565
-1495
lines changed

include/rfl/Field.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99

1010
#include "Literal.hpp"
1111
#include "default.hpp"
12+
#include "internal/Array.hpp"
1213
#include "internal/StringLiteral.hpp"
14+
#include "internal/to_std_array.hpp"
15+
#include "internal/wrap_in_rfl_array_t.hpp"
1316

1417
namespace rfl {
1518

1619
/// Used to define a field in the NamedTuple.
1720
template <internal::StringLiteral _name, class T>
1821
struct Field {
1922
/// The underlying type.
20-
using Type = T;
23+
using Type = internal::wrap_in_rfl_array_t<T>;
2124

2225
/// The name of the field.
2326
using Name = rfl::Literal<_name>;
@@ -28,7 +31,7 @@ struct Field {
2831

2932
Field(Field<_name, T>&& _field) noexcept = default;
3033

31-
Field(const Field<_name, Type>& _field) = default;
34+
Field(const Field<_name, T>& _field) = default;
3235

3336
template <class U>
3437
Field(const Field<_name, U>& _field) : value_(_field.get()) {}
@@ -135,12 +138,12 @@ struct Field {
135138

136139
template <internal::StringLiteral _name, class T>
137140
inline auto make_field(T&& _value) {
138-
return Field<_name, T>(std::forward<T>(_value));
139-
}
140-
141-
template <internal::StringLiteral _name, class T>
142-
inline auto make_field(const T& _value) {
143-
return Field<_name, T>(_value);
141+
using T0 = std::remove_cvref_t<T>;
142+
if constexpr (std::is_array_v<T0>) {
143+
return Field<_name, T0>(internal::Array<T0>(std::forward<T>(_value)));
144+
} else {
145+
return Field<_name, T0>(std::forward<T>(_value));
146+
}
144147
}
145148

146149
} // namespace rfl

include/rfl/Flatten.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace rfl {
1313
template <class T>
1414
struct Flatten {
1515
/// The underlying type.
16-
using Type = std::decay_t<T>;
16+
using Type = std::remove_cvref_t<T>;
1717

1818
Flatten(const Type& _value) : value_(_value) {}
1919

include/rfl/NamedTuple.hpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@ namespace rfl {
2121
template <class... FieldTypes>
2222
class NamedTuple {
2323
public:
24-
using Fields = std::tuple<std::decay_t<FieldTypes>...>;
25-
using Values = std::tuple<typename std::decay<FieldTypes>::type::Type...>;
24+
using Fields = std::tuple<std::remove_cvref_t<FieldTypes>...>;
25+
using Values =
26+
std::tuple<typename std::remove_cvref<FieldTypes>::type::Type...>;
2627

2728
public:
2829
/// Construct from the values.
29-
NamedTuple(typename std::decay<FieldTypes>::type::Type&&... _values)
30-
: values_(std::forward<typename std::decay<FieldTypes>::type::Type>(
31-
_values)...) {
30+
NamedTuple(typename std::remove_cvref<FieldTypes>::type::Type&&... _values)
31+
: values_(
32+
std::forward<typename std::remove_cvref<FieldTypes>::type::Type>(
33+
_values)...) {
3234
static_assert(no_duplicate_field_names(),
3335
"Duplicate field names are not allowed");
3436
}
3537

3638
/// Construct from the values.
37-
NamedTuple(const typename std::decay<FieldTypes>::type::Type&... _values)
39+
NamedTuple(
40+
const typename std::remove_cvref<FieldTypes>::type::Type&... _values)
3841
: values_(std::make_tuple(_values...)) {
3942
static_assert(no_duplicate_field_names(),
4043
"Duplicate field names are not allowed");
@@ -97,11 +100,11 @@ class NamedTuple {
97100
template <class Head, class... Tail>
98101
auto add(Head&& _head, Tail&&... _tail) {
99102
if constexpr (sizeof...(Tail) > 0) {
100-
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
103+
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
101104
make_fields<1, Head>(std::forward<Head>(_head)))
102105
.add(std::forward<Tail>(_tail)...);
103106
} else {
104-
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
107+
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
105108
make_fields<1, Head>(std::forward<Head>(_head)));
106109
}
107110
}
@@ -110,11 +113,11 @@ class NamedTuple {
110113
template <class Head, class... Tail>
111114
auto add(Head&& _head, Tail&&... _tail) const {
112115
if constexpr (sizeof...(Tail) > 0) {
113-
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
116+
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
114117
make_fields<1, Head>(std::forward<Head>(_head)))
115118
.add(std::forward<Tail>(_tail)...);
116119
} else {
117-
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
120+
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
118121
make_fields<1, Head>(std::forward<Head>(_head)));
119122
}
120123
}
@@ -331,7 +334,7 @@ class NamedTuple {
331334
// When we add additional fields, it is more intuitive to add
332335
// them to the end, that is why we do it like this.
333336
using FieldType = typename std::tuple_element<i, Fields>::type;
334-
using T = std::decay_t<typename FieldType::Type>;
337+
using T = std::remove_cvref_t<typename FieldType::Type>;
335338
return make_fields<num_additional_fields>(
336339
FieldType(std::forward<T>(std::get<i>(values_))),
337340
std::forward<Args>(_args)...);
@@ -392,7 +395,7 @@ class NamedTuple {
392395
/// Replaced the field signified by the field type.
393396
template <class Field, class T>
394397
NamedTuple<FieldTypes...> replace_value(T&& _val) {
395-
using FieldType = std::decay_t<Field>;
398+
using FieldType = std::remove_cvref_t<Field>;
396399
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
397400
return make_replaced<index, Values, T>(std::forward<Values>(values_),
398401
std::forward<T>(_val));
@@ -401,7 +404,7 @@ class NamedTuple {
401404
/// Replaced the field signified by the field type.
402405
template <class Field, class T>
403406
NamedTuple<FieldTypes...> replace_value(T&& _val) const {
404-
using FieldType = std::decay_t<Field>;
407+
using FieldType = std::remove_cvref_t<Field>;
405408
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
406409
auto values = values_;
407410
return make_replaced<index, Values, T>(std::move(values),
@@ -446,7 +449,7 @@ class NamedTuple {
446449

447450
using FieldType = typename std::tuple_element<size, Fields>::type;
448451

449-
using T = std::decay_t<typename FieldType::Type>;
452+
using T = std::remove_cvref_t<typename FieldType::Type>;
450453

451454
return retrieve_fields(
452455
std::forward<std::tuple<OtherFieldTypes...>>(_other_fields),

include/rfl/Result.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
#include <optional>
55
#include <ranges>
6+
#include <span>
67
#include <stdexcept>
78
#include <tuple>
89
#include <type_traits>
910
#include <variant>
1011
#include <vector>
1112

13+
#include "internal/to_std_array.hpp"
14+
1215
namespace rfl {
1316

1417
/// To be returned
@@ -79,7 +82,7 @@ class Result {
7982

8083
const auto handle_variant =
8184
[&]<class TOrError>(TOrError&& _t_or_err) -> Result_U {
82-
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
85+
if constexpr (!std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
8386
return _f(std::forward<TOrError>(_t_or_err));
8487
} else {
8588
return std::forward<TOrError>(_t_or_err);
@@ -212,7 +215,7 @@ class Result {
212215
Result<T> or_else(const F& _f) {
213216
const auto handle_variant =
214217
[&]<class TOrError>(TOrError&& _t_or_err) -> Result<T> {
215-
if constexpr (std::is_same<std::decay_t<TOrError>, Error>()) {
218+
if constexpr (std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
216219
return _f(std::forward<Error>(_t_or_err));
217220
} else {
218221
return std::forward<T>(_t_or_err);
@@ -252,7 +255,7 @@ class Result {
252255

253256
const auto handle_variant =
254257
[&]<class TOrError>(TOrError&& _t_or_err) -> rfl::Result<U> {
255-
if constexpr (!std::is_same<std::decay_t<TOrError>, Error>()) {
258+
if constexpr (!std::is_same<std::remove_cvref_t<TOrError>, Error>()) {
256259
return _f(std::forward<TOrError>(_t_or_err));
257260
} else {
258261
return std::forward<TOrError>(_t_or_err);
@@ -311,7 +314,7 @@ class Result {
311314
/// Returns the value or a default.
312315
T value_or(T&& _default) noexcept {
313316
const auto handle_variant = [&]<class TOrError>(TOrError&& _t_or_err) -> T {
314-
using Type = std::decay_t<TOrError>;
317+
using Type = std::remove_cvref_t<TOrError>;
315318
if constexpr (!std::is_same<Type, Error>()) {
316319
return std::forward<T>(_t_or_err);
317320
} else {
@@ -342,4 +345,3 @@ class Result {
342345
} // namespace rfl
343346

344347
#endif
345-

include/rfl/field_names_t.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace rfl {
1111
/// Returns a rfl::Literal containing the field names of struct T.
1212
template <class T>
1313
using field_names_t = typename std::invoke_result<
14-
decltype(internal::get_field_names<std::decay_t<T>>)>::type;
14+
decltype(internal::get_field_names<std::remove_cvref_t<T>>)>::type;
1515

1616
} // namespace rfl
1717

include/rfl/flexbuf/Reader.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,22 @@ struct Reader {
5959

6060
template <class T>
6161
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
62-
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
62+
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
6363
if (!_var.IsString()) {
6464
return rfl::Error("Could not cast to string.");
6565
}
6666
return std::string(_var.AsString().c_str());
67-
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
67+
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
6868
if (!_var.IsBool()) {
6969
return rfl::Error("Could not cast to boolean.");
7070
}
7171
return _var.AsBool();
72-
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
72+
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
7373
if (!_var.IsNumeric()) {
7474
return rfl::Error("Could not cast to double.");
7575
}
7676
return static_cast<T>(_var.AsDouble());
77-
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
77+
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
7878
if (!_var.IsNumeric()) {
7979
return rfl::Error("Could not cast to int.");
8080
}

include/rfl/flexbuf/Writer.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ struct Writer {
115115
template <class T>
116116
OutputVarType insert_value(const std::string& _name,
117117
const T& _var) const noexcept {
118-
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
118+
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
119119
fbb_->String(_name.c_str(), _var);
120-
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
120+
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
121121
fbb_->Bool(_name.c_str(), _var);
122-
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
122+
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
123123
fbb_->Double(_name.c_str(), _var);
124-
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
124+
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
125125
fbb_->Int(_name.c_str(), _var);
126126
} else {
127127
static_assert(always_false_v<T>, "Unsupported type");
@@ -131,13 +131,13 @@ struct Writer {
131131

132132
template <class T>
133133
OutputVarType insert_value(const T& _var) const noexcept {
134-
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
134+
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
135135
fbb_->String(_var);
136-
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
136+
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
137137
fbb_->Bool(_var);
138-
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
138+
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
139139
fbb_->Double(_var);
140-
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
140+
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
141141
fbb_->Int(_var);
142142
} else {
143143
static_assert(always_false_v<T>, "Unsupported type");

include/rfl/flexbuf/read.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ using InputVarType = typename Reader::InputVarType;
1616

1717
/// Parses an object from flexbuf var.
1818
template <class T>
19-
Result<T> read(const InputVarType& _obj) {
19+
auto read(const InputVarType& _obj) {
2020
const auto r = Reader();
2121
return Parser<T>::read(r, _obj);
2222
}
2323

2424
/// Parses an object from flexbuf using reflection.
2525
template <class T>
26-
Result<T> read(const char* _bytes, const size_t _size) {
26+
auto read(const char* _bytes, const size_t _size) {
2727
const InputVarType root =
2828
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
2929
return read<T>(root);
3030
}
3131

3232
/// Parses an object from flexbuf using reflection.
3333
template <class T>
34-
Result<T> read(const std::vector<char>& _bytes) {
34+
auto read(const std::vector<char>& _bytes) {
3535
return read<T>(_bytes.data(), _bytes.size());
3636
}
3737

3838
/// Parses an object directly from a stream.
3939
template <class T>
40-
Result<T> read(std::istream& _stream) {
40+
auto read(std::istream& _stream) {
4141
std::istreambuf_iterator<char> begin(_stream), end;
4242
const auto bytes = std::vector<char>(begin, end);
4343
return read<T>(bytes.data(), bytes.size());

include/rfl/from_named_tuple.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ namespace rfl {
1414

1515
/// Generates the struct T from a named tuple.
1616
template <class T, class NamedTupleType>
17-
T from_named_tuple(NamedTupleType&& _n) {
18-
using RequiredType = std::decay_t<rfl::named_tuple_t<T>>;
19-
if constexpr (!std::is_same<std::decay_t<NamedTupleType>, RequiredType>()) {
17+
auto from_named_tuple(NamedTupleType&& _n) {
18+
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
19+
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
20+
RequiredType>()) {
2021
return from_named_tuple<T>(RequiredType(std::forward<NamedTupleType>(_n)));
2122
} else if constexpr (internal::has_fields<T>()) {
2223
if constexpr (std::is_lvalue_reference<NamedTupleType>{}) {
@@ -35,9 +36,10 @@ T from_named_tuple(NamedTupleType&& _n) {
3536

3637
/// Generates the struct T from a named tuple.
3738
template <class T, class NamedTupleType>
38-
T from_named_tuple(const NamedTupleType& _n) {
39-
using RequiredType = std::decay_t<rfl::named_tuple_t<T>>;
40-
if constexpr (!std::is_same<std::decay_t<NamedTupleType>, RequiredType>()) {
39+
auto from_named_tuple(const NamedTupleType& _n) {
40+
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
41+
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
42+
RequiredType>()) {
4143
return from_named_tuple<T>(RequiredType(_n));
4244
} else if constexpr (internal::has_fields<T>()) {
4345
return internal::copy_from_named_tuple<T>(_n);

include/rfl/internal/Array.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef RFL_INTERNAL_ARRAY_HPP_
2+
#define RFL_INTERNAL_ARRAY_HPP_
3+
4+
#include <array>
5+
#include <type_traits>
6+
7+
#include "to_std_array.hpp"
8+
9+
namespace rfl {
10+
namespace internal {
11+
12+
template <class T>
13+
requires std::is_array_v<T>
14+
struct Array {
15+
using Type = T;
16+
using StdArrayType = to_std_array_t<T>;
17+
18+
Array() = default;
19+
Array(const StdArrayType &_arr) : arr_(_arr) {}
20+
Array(StdArrayType &&_arr) : arr_(std::move(_arr)) {}
21+
Array(const T &_arr) : arr_(to_std_array(_arr)) {}
22+
Array(T &&_arr) : arr_(to_std_array(_arr)) {}
23+
24+
~Array() = default;
25+
26+
StdArrayType arr_;
27+
};
28+
29+
} // namespace internal
30+
} // namespace rfl
31+
32+
#endif

0 commit comments

Comments
 (0)