Skip to content

Commit

Permalink
Merge branch 'hazby2002-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 4, 2024
2 parents 5c81e40 + 5e9a660 commit bf60ca0
Show file tree
Hide file tree
Showing 90 changed files with 1,565 additions and 1,495 deletions.
19 changes: 11 additions & 8 deletions include/rfl/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

#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 {

/// Used to define a field in the NamedTuple.
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>;
Expand All @@ -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()) {}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/rfl/Flatten.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
31 changes: 17 additions & 14 deletions include/rfl/NamedTuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)));
}
}
Expand All @@ -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)));
}
}
Expand Down Expand Up @@ -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)...);
Expand Down Expand Up @@ -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));
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 7 additions & 5 deletions include/rfl/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -342,4 +345,3 @@ class Result {
} // namespace rfl

#endif

2 changes: 1 addition & 1 deletion include/rfl/field_names_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions include/rfl/flexbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
16 changes: 8 additions & 8 deletions include/rfl/flexbuf/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions include/rfl/flexbuf/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ 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);
}

/// 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());
Expand Down
14 changes: 8 additions & 6 deletions include/rfl/from_named_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>{}) {
Expand All @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions include/rfl/internal/Array.hpp
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit bf60ca0

Please sign in to comment.