This commit is contained in:
Mars 2024-06-16 00:13:15 -04:00
parent f668a2eb4c
commit 50083503cf
Signed by: pupbrained
GPG key ID: 0FF5B8826803F895
224 changed files with 16150 additions and 19488 deletions

View file

@ -5,18 +5,20 @@ AlignConsecutiveDeclarations: true
AllowShortBlocksOnASingleLine: Always
AllowShortCompoundRequirementOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLoopsOnASingleLine: true
BasedOnStyle: Chromium
BinPackArguments: false
BinPackParameters: false
ColumnLimit: 100
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false
FixNamespaceComments: false
IndentAccessModifiers: false
IndentExternBlock: Indent
IndentPPDirectives: BeforeHash
IndentWidth: 2
NamespaceIndentation: All
SkipMacroDefinitionBody: true
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1

View file

@ -1,3 +1,4 @@
# noinspection SpellCheckingInspection
Checks: >
*,
-abseil-*,

File diff suppressed because it is too large Load diff

View file

@ -17,13 +17,11 @@ namespace rfl {
/// Adds the name of the struct as a new field.
template <class StructType>
static auto process(auto&& _view) {
using LiteralType = Literal<
internal::remove_namespaces<internal::get_type_name<StructType>()>()>;
using LiteralType =
Literal<internal::remove_namespaces<internal::get_type_name<StructType>()>()>;
using FieldType = Field<field_name_, LiteralType>;
const auto add_new_field = [](auto&&... _fields) {
return make_named_tuple(
FieldType(LiteralType()), std::move(_fields)...
);
return make_named_tuple(FieldType(LiteralType()), std::move(_fields)...);
};
return std::apply(add_new_field, std::move(_view.fields()));
}

View file

@ -19,9 +19,8 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto types = std::vector<ValidationType>(
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
);
const auto types =
std::vector<ValidationType>({ C::template to_schema<T>(), Cs::template to_schema<T>()... });
return ValidationType { ValidationType::AllOf { .types_ = types } };
}

View file

@ -21,9 +21,8 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto types = std::vector<ValidationType>(
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
);
const auto types =
std::vector<ValidationType>({ C::template to_schema<T>(), Cs::template to_schema<T>()... });
return ValidationType { ValidationType::AnyOf { .types_ = types } };
}
@ -40,16 +39,13 @@ namespace rfl {
}
template <class T, class Head, class... Tail>
static rfl::Result<T>
validate_impl(const T& _value, std::vector<Error> _errors) {
static rfl::Result<T> validate_impl(const T& _value, std::vector<Error> _errors) {
const auto handle_err = [&](Error&& _err) {
_errors.push_back(std::forward<Error>(_err));
if constexpr (sizeof...(Tail) == 0) {
return make_error_message(_errors);
} else {
return validate_impl<T, Tail...>(
_value, std::forward<std::vector<Error>>(_errors)
);
return validate_impl<T, Tail...>(_value, std::forward<std::vector<Error>>(_errors));
}
};
return Head::validate(_value).or_else(handle_err);

View file

@ -33,29 +33,19 @@ namespace rfl {
template <class U>
Attribute(Attribute<U>&& _attr) : value_(_attr.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Attribute(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Attribute(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Attribute(const Attribute<U>& _attr) : value_(_attr.value()) {}
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
Attribute(const Default& _default) : value_(Type()) {}
~Attribute() = default;
@ -82,10 +72,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
@ -94,8 +81,7 @@ namespace rfl {
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;

View file

@ -38,8 +38,7 @@ namespace rfl {
Box(Box<T>&& _other) = default;
template <class U>
Box(Box<U>&& _other) noexcept
: ptr_(std::forward<std::unique_ptr<U>>(_other.ptr())) {}
Box(Box<U>&& _other) noexcept : ptr_(std::forward<std::unique_ptr<U>>(_other.ptr())) {}
~Box() = default;
@ -110,9 +109,7 @@ namespace std {
template <class T>
struct hash<rfl::Box<T>> {
size_t operator()(const rfl::Box<T>& _b) const {
return hash<unique_ptr<T>>()(_b.ptr());
}
size_t operator()(const rfl::Box<T>& _b) const { return hash<unique_ptr<T>>()(_b.ptr()); }
};
template <class T>

View file

@ -36,36 +36,24 @@ namespace rfl {
Description(const Description<_description, Type>& _field) = default;
template <class U>
Description(const Description<_description, U>& _field)
: value_(_field.get()) {}
Description(const Description<_description, U>& _field) : value_(_field.get()) {}
template <class U>
Description(Description<_description, U>&& _field) : value_(_field.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Description(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Description(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
Description(const Description<_description, U>& _field)
: value_(_field.value()) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Description(const Description<_description, U>& _field) : value_(_field.value()) {}
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
Description(const Default& _default) : value_(Type()) {}
~Description() = default;
@ -95,10 +83,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
@ -107,22 +92,17 @@ namespace rfl {
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;
}
/// Assigns the underlying object.
Description<_description, T>& operator=(
const Description<_description, T>& _field
) = default;
Description<_description, T>& operator=(const Description<_description, T>& _field) = default;
/// Assigns the underlying object.
Description<_description, T>& operator=(
Description<_description, T>&& _field
) = default;
Description<_description, T>& operator=(Description<_description, T>&& _field) = default;
/// Assigns the underlying object.
template <class U>

View file

@ -39,29 +39,19 @@ namespace rfl {
template <class U>
Field(Field<_name, U>&& _field) : value_(_field.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Field(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Field(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Field(const Field<_name, U>& _field) : value_(_field.value()) {}
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
Field(const Default& _default) : value_(Type()) {}
~Field() = default;
@ -94,10 +84,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
@ -106,8 +93,7 @@ namespace rfl {
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;

View file

@ -29,16 +29,10 @@ namespace rfl {
template <class U>
Flatten(Flatten<U>&& _f) : value_(_f.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Flatten(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Flatten(U&& _value) : value_(_value) {}
~Flatten() = default;
@ -68,10 +62,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Flatten<T>& operator=(const U& _value) {
value_ = _value;
return *this;

View file

@ -46,9 +46,7 @@ namespace rfl {
/// A single-field literal is special because it
/// can also have a default constructor.
template <
ValueType num_fields = num_fields_,
typename = std::enable_if_t<num_fields <= 1>>
template <ValueType num_fields = num_fields_, typename = std::enable_if_t<num_fields <= 1>>
Literal() : value_(0) {}
~Literal() = default;
@ -69,9 +67,7 @@ namespace rfl {
/// Constructs a new Literal.
template <ValueType _value>
static Literal<fields_...> from_value() {
static_assert(
_value < num_fields_, "Value cannot exceed number of fields."
);
static_assert(_value < num_fields_, "Value cannot exceed number of fields.");
return Literal<fields_...>(_value);
}
@ -132,9 +128,7 @@ namespace rfl {
/// Constructs a Literal from a string. Returns an error if the string
/// cannot be found.
static Result<Literal> from_string(const std::string& _str) {
const auto to_literal = [](const auto& _v) {
return Literal<fields_...>(_v);
};
const auto to_literal = [](const auto& _v) { return Literal<fields_...>(_v); };
return find_value(_str).transform(to_literal);
};
@ -156,8 +150,7 @@ namespace rfl {
Literal<fields_...>& operator=(const Literal<fields_...>& _other) = default;
/// Assigns from another literal.
Literal<fields_...>& operator=(Literal<fields_...>&& _other
) noexcept = default;
Literal<fields_...>& operator=(Literal<fields_...>&& _other) noexcept = default;
/// Assigns the literal from a string
Literal<fields_...>& operator=(const std::string& _str) {
@ -166,9 +159,7 @@ namespace rfl {
}
/// <=> for other Literals with the same fields.
auto operator<=>(const Literal<fields_...>& _other) const {
return value() <=> _other.value();
}
auto operator<=>(const Literal<fields_...>& _other) const { return value() <=> _other.value(); }
/// <=> for other Literals with different fields.
template <internal::StringLiteral... _fields>
@ -177,9 +168,7 @@ namespace rfl {
}
/// <=> for strings.
inline auto operator<=>(const std::string& _str) const {
return name() <=> _str;
}
inline auto operator<=>(const std::string& _str) const { return name() <=> _str; }
/// <=> for const char*.
template <internal::StringLiteral... other_fields>
@ -233,9 +222,7 @@ namespace rfl {
/// Returns all of the allowed fields.
template <int _i = 0>
static std::vector<std::string> allowed_strings_vec(
std::vector<std::string> _values = {}
) {
static std::vector<std::string> allowed_strings_vec(std::vector<std::string> _values = {}) {
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
const auto head = FieldType::field_.str();
_values.push_back(head);
@ -341,10 +328,7 @@ namespace rfl {
}
}
static_assert(
sizeof...(fields_) <= std::numeric_limits<ValueType>::max(),
"Too many fields."
);
static_assert(sizeof...(fields_) <= std::numeric_limits<ValueType>::max(), "Too many fields.");
static_assert(
sizeof...(fields_) <= 1 || !has_duplicates(),

View file

@ -8,8 +8,7 @@ namespace rfl {
/// Contains meta-information about a field in a struct.
class MetaField {
public:
MetaField(const std::string& _name, const std::string& _type)
: name_(_name), type_(_type) {}
MetaField(const std::string& _name, const std::string& _type) : name_(_name), type_(_type) {}
~MetaField() = default;

View file

@ -31,46 +31,29 @@ namespace rfl {
public:
using Fields = std::tuple<std::remove_cvref_t<FieldTypes>...>;
using Names = Literal<std::remove_cvref_t<FieldTypes>::name_...>;
using Values =
std::tuple<typename std::remove_cvref_t<FieldTypes>::Type...>;
using Values = std::tuple<typename std::remove_cvref_t<FieldTypes>::Type...>;
public:
/// Construct from the 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"
);
: 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::remove_cvref<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"
);
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Construct from the fields.
NamedTuple(FieldTypes&&... _fields)
: values_(std::make_tuple(std::move(_fields.value_)...)) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
NamedTuple(FieldTypes&&... _fields) : values_(std::make_tuple(std::move(_fields.value_)...)) {
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Construct from the fields.
NamedTuple(const FieldTypes&... _fields)
: values_(std::make_tuple(_fields.value_...)) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
NamedTuple(const FieldTypes&... _fields) : values_(std::make_tuple(_fields.value_...)) {
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Construct from a tuple containing fields.
@ -78,17 +61,13 @@ namespace rfl {
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(
std::forward<std::tuple<FieldTypes...>>(_tup)
)) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Construct from a tuple containing fields.
NamedTuple(const std::tuple<FieldTypes...>& _tup)
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(_tup)) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Copy constructor.
@ -101,18 +80,14 @@ namespace rfl {
template <class... OtherFieldTypes>
NamedTuple(const NamedTuple<OtherFieldTypes...>& _other)
: NamedTuple(retrieve_fields(_other.fields())) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
/// Move constructor.
template <class... OtherFieldTypes>
NamedTuple(NamedTuple<OtherFieldTypes...>&& _other)
: NamedTuple(retrieve_fields(_other.fields())) {
static_assert(
no_duplicate_field_names(), "Duplicate field names are not allowed"
);
static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed");
}
~NamedTuple() = default;
@ -138,14 +113,10 @@ namespace rfl {
auto add(Field<_name, FType> _head, const Tail&... _tail) const {
using Head = Field<_name, FType>;
if constexpr (sizeof...(Tail) > 0) {
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
make_fields<1, Head>(_head)
)
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(make_fields<1, Head>(_head))
.add(_tail...);
} else {
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
make_fields<1, Head>(_head)
);
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(make_fields<1, Head>(_head));
}
}
@ -177,16 +148,14 @@ namespace rfl {
template <class... TupContent, class... Tail>
auto add(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
return add(
std::forward<std::tuple<TupContent...>>(_named_tuple.fields()),
std::forward<Tail>(_tail)...
std::forward<std::tuple<TupContent...>>(_named_tuple.fields()), std::forward<Tail>(_tail)...
);
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail)
const {
auto add(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail) const {
return add(_named_tuple.fields(), _tail...);
}
@ -197,8 +166,9 @@ namespace rfl {
const auto transform_field = [&_f](auto... _fields) {
return std::tuple_cat(_f(std::move(_fields)).fields()...);
};
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup
) { return NamedTuple<NewFields...>(_tup); };
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup) {
return NamedTuple<NewFields...>(_tup);
};
auto new_fields = std::apply(transform_field, std::move(fields()));
return to_nt(std::move(new_fields));
}
@ -210,8 +180,9 @@ namespace rfl {
const auto transform_field = [&_f](auto... _fields) {
return std::tuple_cat(_f(std::move(_fields)).fields()...);
};
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup
) { return NamedTuple<NewFields...>(_tup); };
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup) {
return NamedTuple<NewFields...>(_tup);
};
auto new_fields = std::apply(transform_field, std::move(fields()));
return to_nt(std::move(new_fields));
}
@ -219,8 +190,7 @@ namespace rfl {
/// Invokes a callable object once for each field in order.
template <typename F>
void apply(F&& _f) {
const auto apply_to_field = [&_f]<typename... AFields>(AFields&&... fields
) {
const auto apply_to_field = [&_f]<typename... AFields>(AFields&&... fields) {
((_f(std::forward<AFields>(fields))), ...);
};
std::apply(apply_to_field, fields());
@ -229,9 +199,7 @@ namespace rfl {
/// Invokes a callable object once for each field in order.
template <typename F>
void apply(F&& _f) const {
const auto apply_to_field = [&_f](const auto&... fields) {
((_f(fields)), ...);
};
const auto apply_to_field = [&_f](const auto&... fields) { ((_f(fields)), ...); };
std::apply(apply_to_field, fields());
}
@ -284,12 +252,10 @@ namespace rfl {
}
/// Copy assignment operator.
NamedTuple<FieldTypes...>& operator=(const NamedTuple<FieldTypes...>& _other
) = default;
NamedTuple<FieldTypes...>& operator=(const NamedTuple<FieldTypes...>& _other) = default;
/// Move assignment operator.
NamedTuple<FieldTypes...>& operator=(NamedTuple<FieldTypes...>&& _other
) noexcept = default;
NamedTuple<FieldTypes...>& operator=(NamedTuple<FieldTypes...>&& _other) noexcept = default;
/// Equality operator
inline auto operator==(const rfl::NamedTuple<FieldTypes...>& _other) const {
@ -303,8 +269,7 @@ namespace rfl {
/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
template <internal::StringLiteral _name, class FType, class... OtherRFields>
auto
replace(Field<_name, FType>&& _field, OtherRFields&&... _other_fields) {
auto replace(Field<_name, FType>&& _field, OtherRFields&&... _other_fields) {
using RField = Field<_name, FType>;
constexpr auto num_other_fields = sizeof...(OtherRFields);
if constexpr (num_other_fields == 0) {
@ -318,17 +283,13 @@ namespace rfl {
/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
template <internal::StringLiteral _name, class FType, class... OtherRFields>
auto replace(
Field<_name, FType> _field,
const OtherRFields&... _other_fields
) const {
auto replace(Field<_name, FType> _field, const OtherRFields&... _other_fields) const {
using RField = Field<_name, FType>;
constexpr auto num_other_fields = sizeof...(OtherRFields);
if constexpr (num_other_fields == 0) {
return replace_value<RField>(std::move(_field.value_));
} else {
return replace_value<RField>(std::move(_field.value_))
.replace(_other_fields...);
return replace_value<RField>(std::move(_field.value_)).replace(_other_fields...);
}
}
@ -360,16 +321,14 @@ namespace rfl {
template <class... TupContent, class... Tail>
auto replace(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
return replace(
std::forward<NamedTuple<TupContent...>>(_named_tuple).fields(),
std::forward<Tail>(_tail)...
std::forward<NamedTuple<TupContent...>>(_named_tuple).fields(), std::forward<Tail>(_tail)...
);
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto replace(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail)
const {
auto replace(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail) const {
return replace(_named_tuple.fields(), _tail...);
}
@ -383,8 +342,9 @@ namespace rfl {
const auto transform_field = [&_f](auto... fields) {
return std::make_tuple(_f(std::move(fields))...);
};
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup
) { return NamedTuple<NewFields...>(_tup); };
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup) {
return NamedTuple<NewFields...>(_tup);
};
auto new_fields = std::apply(transform_field, std::move(fields()));
return to_nt(std::move(new_fields));
}
@ -396,8 +356,9 @@ namespace rfl {
const auto transform_field = [&_f](auto... fields) {
return std::make_tuple(_f(std::move(fields))...);
};
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup
) { return NamedTuple<NewFields...>(_tup); };
const auto to_nt = []<class... NewFields>(std::tuple<NewFields...>&& _tup) {
return NamedTuple<NewFields...>(_tup);
};
auto new_fields = std::apply(transform_field, std::move(fields()));
return to_nt(std::move(new_fields));
}
@ -446,8 +407,7 @@ namespace rfl {
using FieldType = typename std::tuple_element<i, Fields>::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)...
FieldType(std::forward<T>(std::get<i>(values_))), std::forward<Args>(_args)...
);
}
}
@ -514,9 +474,7 @@ namespace rfl {
NamedTuple<FieldTypes...> replace_value(T&& _val) {
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)
);
return make_replaced<index, Values, T>(std::forward<Values>(values_), std::forward<T>(_val));
}
/// Replaced the field signified by the field type.
@ -525,9 +483,7 @@ namespace rfl {
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), std::forward<T>(_val)
);
return make_replaced<index, Values, T>(std::move(values), std::forward<T>(_val));
}
/// Adds the elements of a tuple to a newly created named tuple,
@ -552,10 +508,8 @@ namespace rfl {
/// Retrieves the fields from another tuple.
template <class... OtherFieldTypes, class... Args>
constexpr static Fields retrieve_fields(
std::tuple<OtherFieldTypes...>&& _other_fields,
Args&&... _args
) {
constexpr static Fields
retrieve_fields(std::tuple<OtherFieldTypes...>&& _other_fields, Args&&... _args) {
constexpr auto size = sizeof...(Args);
constexpr bool retrieved_all_fields = size == std::tuple_size_v<Fields>;
@ -563,11 +517,9 @@ namespace rfl {
if constexpr (retrieved_all_fields) {
return std::make_tuple(std::forward<Args>(_args)...);
} else {
constexpr auto field_name =
std::tuple_element<size, Fields>::type::name_;
constexpr auto field_name = std::tuple_element<size, Fields>::type::name_;
constexpr auto index =
internal::find_index<field_name, std::tuple<OtherFieldTypes...>>();
constexpr auto index = internal::find_index<field_name, std::tuple<OtherFieldTypes...>>();
using FieldType = typename std::tuple_element<size, Fields>::type;
@ -627,8 +579,7 @@ namespace rfl {
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail)
const {
auto add(NamedTuple<TupContent...> _named_tuple, const Tail&... _tail) const {
return add(_named_tuple.fields(), _tail...);
}
@ -665,34 +616,24 @@ namespace rfl {
class Type1,
internal::StringLiteral _name2,
class Type2>
inline auto operator*(
const rfl::Field<_name1, Type1>& _f1,
const rfl::Field<_name2, Type2>& _f2
) {
inline auto
operator*(const rfl::Field<_name1, Type1>& _f1, const rfl::Field<_name2, Type2>& _f2) {
return NamedTuple(_f1, _f2);
}
template <internal::StringLiteral _name, class Type, class... FieldTypes>
inline auto operator*(
const NamedTuple<FieldTypes...>& _tup,
const rfl::Field<_name, Type>& _f
) {
inline auto operator*(const NamedTuple<FieldTypes...>& _tup, const rfl::Field<_name, Type>& _f) {
return _tup.add(_f);
}
template <internal::StringLiteral _name, class Type, class... FieldTypes>
inline auto operator*(
const rfl::Field<_name, Type>& _f,
const NamedTuple<FieldTypes...>& _tup
) {
inline auto operator*(const rfl::Field<_name, Type>& _f, const NamedTuple<FieldTypes...>& _tup) {
return NamedTuple(_f).add(_tup);
}
template <class... FieldTypes1, class... FieldTypes2>
inline auto operator*(
const NamedTuple<FieldTypes1...>& _tup1,
const NamedTuple<FieldTypes2...>& _tup2
) {
inline auto
operator*(const NamedTuple<FieldTypes1...>& _tup1, const NamedTuple<FieldTypes2...>& _tup2) {
return _tup1.add(_tup2);
}
@ -701,32 +642,25 @@ namespace rfl {
class Type1,
internal::StringLiteral _name2,
class Type2>
inline auto
operator*(rfl::Field<_name1, Type1>&& _f1, rfl::Field<_name2, Type2>&& _f2) {
inline auto operator*(rfl::Field<_name1, Type1>&& _f1, rfl::Field<_name2, Type2>&& _f2) {
return NamedTuple(
std::forward<Field<_name1, Type1>>(_f1),
std::forward<Field<_name2, Type2>>(_f2)
std::forward<Field<_name1, Type1>>(_f1), std::forward<Field<_name2, Type2>>(_f2)
);
}
template <internal::StringLiteral _name, class Type, class... FieldTypes>
inline auto
operator*(NamedTuple<FieldTypes...>&& _tup, rfl::Field<_name, Type>&& _f) {
inline auto operator*(NamedTuple<FieldTypes...>&& _tup, rfl::Field<_name, Type>&& _f) {
return _tup.add(std::forward<Field<_name, Type>>(_f));
}
template <internal::StringLiteral _name, class Type, class... FieldTypes>
inline auto
operator*(rfl::Field<_name, Type>&& _f, NamedTuple<FieldTypes...>&& _tup) {
inline auto operator*(rfl::Field<_name, Type>&& _f, NamedTuple<FieldTypes...>&& _tup) {
return NamedTuple(std::forward<Field<_name, Type>>(_f))
.add(std::forward<NamedTuple<FieldTypes...>>(_tup));
}
template <class... FieldTypes1, class... FieldTypes2>
inline auto operator*(
NamedTuple<FieldTypes1...>&& _tup1,
NamedTuple<FieldTypes2...>&& _tup2
) {
inline auto operator*(NamedTuple<FieldTypes1...>&& _tup1, NamedTuple<FieldTypes2...>&& _tup2) {
return _tup1.add(std::forward<NamedTuple<FieldTypes2...>>(_tup2));
}

View file

@ -21,16 +21,14 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto types = std::vector<ValidationType>(
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
);
const auto types =
std::vector<ValidationType>({ C::template to_schema<T>(), Cs::template to_schema<T>()... });
return ValidationType { ValidationType::OneOf { .types_ = types } };
}
private:
static Error make_error_message(const std::vector<Error>& _errors) {
std::string msg = "Expected exactly 1 out of " +
std::to_string(sizeof...(Cs) + 1) +
std::string msg = "Expected exactly 1 out of " + std::to_string(sizeof...(Cs) + 1) +
" validations to pass, but " +
std::to_string(sizeof...(Cs) + 1 - _errors.size()) +
" of them did. The following errors were generated: ";
@ -41,8 +39,7 @@ namespace rfl {
}
template <class T, class Head, class... Tail>
static rfl::Result<T>
validate_impl(const T& _value, std::vector<Error> _errors) {
static rfl::Result<T> validate_impl(const T& _value, std::vector<Error> _errors) {
const auto push_back = [&](Error&& _err) -> rfl::Result<T> {
_errors.emplace_back(std::forward<Error>(_err));
return _err;
@ -57,15 +54,11 @@ namespace rfl {
}
return make_error_message(_errors);
} else {
return validate_impl<T, Tail...>(
_value, std::forward<std::vector<Error>>(_errors)
);
return validate_impl<T, Tail...>(_value, std::forward<std::vector<Error>>(_errors));
}
};
return Head::validate(_value)
.and_then(next_validation)
.or_else(next_validation);
return Head::validate(_value).and_then(next_validation).or_else(next_validation);
}
};

View file

@ -21,8 +21,8 @@ namespace rfl {
return _str;
} else {
return rfl::Error(
"String '" + _str + "' did not match format '" + _name.str() +
"': '" + _regex.str() + "'."
"String '" + _str + "' did not match format '" + _name.str() + "': '" + _regex.str() +
"'."
);
}
}

View file

@ -22,9 +22,8 @@ namespace rfl {
template <class Head, class... Tail>
struct Processors<Head, Tail...> {
static constexpr bool all_required_ = std::disjunction_v<
internal::is_no_optionals<Head>,
internal::is_no_optionals<Tail>...>;
static constexpr bool all_required_ =
std::disjunction_v<internal::is_no_optionals<Head>, internal::is_no_optionals<Tail>...>;
template <class T, class NamedTupleType>
static auto process(NamedTupleType&& _named_tuple) {

View file

@ -50,8 +50,7 @@ namespace rfl {
Ref(const Ref<U>& _other) : ptr_(_other.ptr()) {}
template <class U>
Ref(Ref<U>&& _other) noexcept
: ptr_(std::forward<std::shared_ptr<U>>(_other.ptr())) {}
Ref(Ref<U>&& _other) noexcept : ptr_(std::forward<std::shared_ptr<U>>(_other.ptr())) {}
~Ref() = default;
@ -132,9 +131,7 @@ namespace std {
template <class T>
struct hash<rfl::Ref<T>> {
size_t operator()(const rfl::Ref<T>& _r) const {
return hash<shared_ptr<T>>()(_r.ptr());
}
size_t operator()(const rfl::Ref<T>& _r) const { return hash<shared_ptr<T>>()(_r.ptr()); }
};
template <class T>

View file

@ -39,29 +39,19 @@ namespace rfl {
template <class U>
Rename(Rename<_name, U>&& _field) : value_(_field.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Rename(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Rename(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Rename(const Rename<_name, U>& _field) : value_(_field.value()) {}
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
Rename(const Default& _default) : value_(Type()) {}
~Rename() = default;
@ -91,10 +81,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
@ -103,8 +90,7 @@ namespace rfl {
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;

View file

@ -40,52 +40,33 @@ namespace rfl {
/// The Result class is used for monadic error handling.
template <class T>
class Result {
static_assert(
!std::is_same<T, Error>(),
"The result type cannot be Error."
);
static_assert(!std::is_same<T, Error>(), "The result type cannot be Error.");
using TOrErr =
std::array<unsigned char, std::max(sizeof(T), sizeof(Error))>;
using TOrErr = std::array<unsigned char, std::max(sizeof(T), sizeof(Error))>;
public:
using Type = T;
Result(const T& _val) : success_(true) { new (&get_t()) T(_val); }
Result(T&& _val) noexcept : success_(true) {
new (&get_t()) T(std::move(_val));
}
Result(T&& _val) noexcept : success_(true) { new (&get_t()) T(std::move(_val)); }
Result(const Error& _err) : success_(false) {
new (&get_err()) Error(_err);
}
Result(const Error& _err) : success_(false) { new (&get_err()) Error(_err); }
Result(Error&& _err) noexcept : success_(false) {
new (&get_err()) Error(std::move(_err));
}
Result(Error&& _err) noexcept : success_(false) { new (&get_err()) Error(std::move(_err)); }
Result(Result<T>&& _other) noexcept : success_(_other.success_) {
move_from_other(_other);
}
Result(Result<T>&& _other) noexcept : success_(_other.success_) { move_from_other(_other); }
Result(const Result<T>& _other) : success_(_other.success_) {
copy_from_other(_other);
}
Result(const Result<T>& _other) : success_(_other.success_) { copy_from_other(_other); }
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Result(Result<U>&& _other) : success_(_other && true) {
auto temp = std::forward<Result<U>>(_other).transform([](U&& _u) {
return T(std::forward<U>(_u));
});
auto temp =
std::forward<Result<U>>(_other).transform([](U&& _u) { return T(std::forward<U>(_u)); });
move_from_other(temp);
}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Result(const Result<U>& _other) : success_(_other && true) {
auto temp = _other.transform([](const U& _u) { return T(_u); });
move_from_other(temp);
@ -209,9 +190,7 @@ namespace rfl {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
auto& operator=(const Result<U>& _other) {
const auto to_t = [](const U& _u) -> T { return _u; };
t_or_err_ = _other.transform(to_t).t_or_err_;
@ -320,8 +299,7 @@ namespace rfl {
void destroy() {
if (success_) {
if constexpr (std::is_destructible_v<
T> /*&& !internal::is_array_v<T>*/) {
if constexpr (std::is_destructible_v<T> /*&& !internal::is_array_v<T>*/) {
get_t().~T();
}
} else {
@ -331,13 +309,9 @@ namespace rfl {
T& get_t() noexcept { return *(reinterpret_cast<T*>(t_or_err_.data())); }
const T& get_t() const noexcept {
return *(reinterpret_cast<const T*>(t_or_err_.data()));
}
const T& get_t() const noexcept { return *(reinterpret_cast<const T*>(t_or_err_.data())); }
Error& get_err() noexcept {
return *(reinterpret_cast<Error*>(t_or_err_.data()));
}
Error& get_err() noexcept { return *(reinterpret_cast<Error*>(t_or_err_.data())); }
const Error& get_err() const noexcept {
return *(reinterpret_cast<const Error*>(t_or_err_.data()));

View file

@ -19,30 +19,24 @@ namespace rfl {
using VariantType = std::variant<Ts...>;
/// A literal containing all the tags that are possible
using PossibleTags =
define_literal_t<internal::tag_t<_discriminator, Ts>...>;
using PossibleTags = define_literal_t<internal::tag_t<_discriminator, Ts>...>;
TaggedUnion(const VariantType& _variant) : variant_(_variant) {}
TaggedUnion(VariantType&& _variant) noexcept
: variant_(std::move(_variant)) {}
TaggedUnion(VariantType&& _variant) noexcept : variant_(std::move(_variant)) {}
TaggedUnion(const TaggedUnion<_discriminator, Ts...>& _tagged_union
) = default;
TaggedUnion(const TaggedUnion<_discriminator, Ts...>& _tagged_union) = default;
TaggedUnion(TaggedUnion<_discriminator, Ts...>&& _tagged_union
) noexcept = default;
TaggedUnion(TaggedUnion<_discriminator, Ts...>&& _tagged_union) noexcept = default;
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
TaggedUnion(const T& _t) : variant_(_t) {}
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
TaggedUnion(T&& _t) noexcept : variant_(std::forward<T>(_t)) {}
~TaggedUnion() = default;
@ -62,8 +56,7 @@ namespace rfl {
/// Assigns the underlying object.
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
TaggedUnion<_discriminator, Ts...>& operator=(T&& _variant) {
variant_ = std::forward<T>(_variant);
return *this;
@ -72,21 +65,18 @@ namespace rfl {
/// Assigns the underlying object.
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
TaggedUnion<_discriminator, Ts...>& operator=(const T& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
TaggedUnion<_discriminator, Ts...>& operator=(
const TaggedUnion<_discriminator, Ts...>& _other
TaggedUnion<_discriminator, Ts...>& operator=(const TaggedUnion<_discriminator, Ts...>& _other
) = default;
/// Assigns the underlying object.
TaggedUnion<_discriminator, Ts...>& operator=(
TaggedUnion<_discriminator, Ts...>&& _other
TaggedUnion<_discriminator, Ts...>& operator=(TaggedUnion<_discriminator, Ts...>&& _other
) = default;
/// Returns the underlying variant.
@ -112,58 +102,43 @@ namespace rfl {
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_
);
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_);
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_
);
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
static inline const auto& get_const(const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
) {
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
_index>(_tu.variant_);
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
static inline const auto& get_const(const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
) {
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
_field_name>(_tu.variant_);
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
static inline const auto& get_const(const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
) {
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
Field>(_tu.variant_);
return Getter<std::variant<NamedTupleTypes...>>::template get_const<Field>(_tu.variant_);
}
};

View file

@ -29,8 +29,7 @@ namespace rfl {
const auto r = strptime(_str, _format.str().c_str(), &tm_);
if (r == NULL) {
throw std::runtime_error(
"String '" + std::string(_str) + "' did not match format '" +
Format().str() + "'."
"String '" + std::string(_str) + "' did not match format '" + Format().str() + "'."
);
}
}

View file

@ -21,8 +21,7 @@ namespace rfl {
struct Validator {
public:
using ReflectionType = T;
using ValidationType =
std::conditional_t<sizeof...(Vs) == 0, V, AllOf<V, Vs...>>;
using ValidationType = std::conditional_t<sizeof...(Vs) == 0, V, AllOf<V, Vs...>>;
/// Exception-free validation.
static Result<Validator<T, V, Vs...>> from_value(const T& _value) noexcept {
@ -39,21 +38,13 @@ namespace rfl {
Validator(T&& _value) : value_(ValidationType::validate(_value).value()) {}
Validator(const T& _value)
: value_(ValidationType::validate(_value).value()) {}
Validator(const T& _value) : value_(ValidationType::validate(_value).value()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Validator(U&& _value)
: value_(ValidationType::validate(T(std::forward<U>(_value))).value()) {
}
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Validator(U&& _value) : value_(ValidationType::validate(T(std::forward<U>(_value))).value()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Validator(const U& _value)
: value_(ValidationType::validate(T(_value)).value()) {}
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
Validator(const U& _value) : value_(ValidationType::validate(T(_value)).value()) {}
~Validator() = default;
@ -70,26 +61,20 @@ namespace rfl {
}
/// Assigns the underlying object.
Validator<T, V, Vs...>& operator=(const Validator<T, V, Vs...>& _other
) = default;
Validator<T, V, Vs...>& operator=(const Validator<T, V, Vs...>& _other) = default;
/// Assigns the underlying object.
Validator<T, V, Vs...>& operator=(Validator<T, V, Vs...>&& _other
) noexcept = default;
Validator<T, V, Vs...>& operator=(Validator<T, V, Vs...>&& _other) noexcept = default;
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
auto& operator=(U&& _value) noexcept {
value_ = ValidationType::validate(T(std::forward<U>(_value))).value();
return *this;
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
template <class U, typename std::enable_if<std::is_convertible_v<U, T>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = ValidationType::validate(T(_value)).value();
return *this;
@ -115,10 +100,7 @@ namespace rfl {
};
template <class T, class V, class... Vs>
inline auto operator<=>(
const Validator<T, V, Vs...>& _v1,
const Validator<T, V, Vs...>& _v2
) {
inline auto operator<=>(const Validator<T, V, Vs...>& _v1, const Validator<T, V, Vs...>& _v2) {
return _v1.value() <=> _v2.value();
}
@ -133,9 +115,7 @@ namespace std {
template <class T, class V, class... Vs>
struct hash<rfl::Validator<T, V, Vs...>> {
size_t operator()(const rfl::Validator<T, V, Vs...>& _v) const {
return hash<T>()(_v.value());
}
size_t operator()(const rfl::Validator<T, V, Vs...>& _v) const { return hash<T>()(_v.value()); }
};
} // namespace std

View file

@ -20,14 +20,12 @@ namespace rfl {
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
Variant(const T& _t) : variant_(_t) {}
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
Variant(T&& _t) noexcept : variant_(std::forward<T>(_t)) {}
~Variant() = default;
@ -47,8 +45,7 @@ namespace rfl {
/// Assigns the underlying object.
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
Variant<AlternativeTypes...>& operator=(T&& _variant) {
variant_ = std::forward<T>(_variant);
return *this;
@ -57,22 +54,17 @@ namespace rfl {
/// Assigns the underlying object.
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::type = true>
Variant<AlternativeTypes...>& operator=(const T& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(
const Variant<AlternativeTypes...>& _other
) = default;
Variant<AlternativeTypes...>& operator=(const Variant<AlternativeTypes...>& _other) = default;
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(
Variant<AlternativeTypes...>&& _other
) = default;
Variant<AlternativeTypes...>& operator=(Variant<AlternativeTypes...>&& _other) = default;
/// Returns the underlying variant.
VariantType& variant() { return variant_; }

View file

@ -14,8 +14,7 @@ namespace rfl {
return from_named_tuple<T>(to_named_tuple(std::forward<Head>(_head)));
} else {
return from_named_tuple<T>(
to_named_tuple(std::forward<Head>(_head))
.add(to_named_tuple(std::forward<Tail>(_tail))...)
to_named_tuple(std::forward<Head>(_head)).add(to_named_tuple(std::forward<Tail>(_tail))...)
);
}
}
@ -26,8 +25,7 @@ namespace rfl {
if constexpr (sizeof...(_tail) == 0) {
return from_named_tuple<T>(to_named_tuple(_head));
} else {
return from_named_tuple<T>(to_named_tuple(_head).add(to_named_tuple(_tail
)...));
return from_named_tuple<T>(to_named_tuple(_head).add(to_named_tuple(_tail)...));
}
}

View file

@ -19,24 +19,17 @@ namespace rfl::parsing {
using ParentType = Parent<W>;
static Result<bson_oid_t>
read(const R& _r, const InputVarType& _var) noexcept {
static Result<bson_oid_t> read(const R& _r, const InputVarType& _var) noexcept {
return _r.template to_basic_type<bson_oid_t>(_var);
}
template <class P>
static void
write(const W& _w, const bson_oid_t& _oid, const P& _parent) noexcept {
static void write(const W& _w, const bson_oid_t& _oid, const P& _parent) noexcept {
ParentType::add_value(_w, _oid, _parent);
}
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions
) {
static_assert(
rfl::always_false_v<R>,
"bson_oid_t cannot be expressed inside a JSON schema."
);
static schema::Type to_schema(std::map<std::string, schema::Type>* _definitions) {
static_assert(rfl::always_false_v<R>, "bson_oid_t cannot be expressed inside a JSON schema.");
return schema::Type { schema::Type::String {} };
}
};

View file

@ -49,10 +49,8 @@ namespace rfl {
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_bson_obj(var); });
rfl::Result<InputVarType> get_field(
const std::string& _name,
const InputObjectType& _obj
) const noexcept {
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _obj.val_->val_.value.v_doc;
@ -86,9 +84,7 @@ namespace rfl {
return std::string(value.v_symbol.symbol, value.v_symbol.len);
default:
return rfl::Error(
"Could not cast to string. The type must be UTF8 or symbol."
);
return rfl::Error("Could not cast to string. The type must be UTF8 or symbol.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != BSON_TYPE_BOOL) {
@ -116,8 +112,7 @@ namespace rfl {
"int32, int64 or date_time."
);
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>(
)) {
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
if (btype != BSON_TYPE_OID) {
return rfl::Error("Could not cast to OID.");
}
@ -127,8 +122,7 @@ namespace rfl {
}
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var
) const noexcept {
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
const auto btype = _var.val_->val_.value_type;
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to an array.");
@ -137,10 +131,8 @@ namespace rfl {
}
template <class ArrayReader>
std::optional<Error> read_array(
const ArrayReader& _array_reader,
const InputArrayType& _arr
) const noexcept {
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _arr.val_->val_.value.v_doc;
@ -158,10 +150,8 @@ namespace rfl {
}
template <class ObjectReader>
std::optional<Error> read_object(
const ObjectReader& _object_reader,
const InputObjectType& _obj
) const noexcept {
std::optional<Error>
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
bson_t b;
bson_iter_t iter;
const auto doc = _obj.val_->val_.value.v_doc;
@ -176,8 +166,7 @@ namespace rfl {
return std::nullopt;
}
rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept {
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
const auto btype = _var.val_->val_.value_type;
if (btype != BSON_TYPE_DOCUMENT) {
return Error("Could not cast to a document.");
@ -186,8 +175,7 @@ namespace rfl {
}
template <class T>
rfl::Result<T> use_custom_constructor(const InputVarType& _var
) const noexcept {
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
try {
return T::from_bson_obj(_var);
} catch (std::exception& e) { return rfl::Error(e.what()); }

View file

@ -47,8 +47,7 @@ namespace rfl {
};
struct BSONOutputObject {
BSONOutputObject(bson_t* _val, ParentType _parent)
: parent_(_parent), val_(_val) {}
BSONOutputObject(bson_t* _val, ParentType _parent) : parent_(_parent), val_(_val) {}
ParentType parent_;
bson_t* val_;
};
@ -79,17 +78,12 @@ namespace rfl {
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
static_assert(
rfl::always_false_v<T>,
"BSON only allows arrays or objects as its root."
);
static_assert(rfl::always_false_v<T>, "BSON only allows arrays or objects as its root.");
return OutputVarType {};
}
OutputArrayType add_array_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
bson_array_builder_t* val;
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
return OutputArrayType(val, IsArray { _parent->val_ });
@ -107,17 +101,11 @@ namespace rfl {
return OutputArrayType(val, IsObject { _parent->val_ });
}
OutputObjectType add_object_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_array_builder_append_document_begin(
_parent->val_, &(subdocs_->back()->val_)
);
return OutputObjectType(
&subdocs_->back()->val_, IsArray {_parent->val_}
);
bson_array_builder_append_document_begin(_parent->val_, &(subdocs_->back()->val_));
return OutputObjectType(&subdocs_->back()->val_, IsArray { _parent->val_ });
}
OutputObjectType add_object_to_object(
@ -127,19 +115,13 @@ namespace rfl {
) const noexcept {
subdocs_->emplace_back(rfl::Box<BSONType>());
bson_append_document_begin(
_parent->val_,
_name.data(),
static_cast<int>(_name.size()),
&(subdocs_->back()->val_)
);
return OutputObjectType(
&subdocs_->back()->val_, IsObject {_parent->val_}
_parent->val_, _name.data(), static_cast<int>(_name.size()), &(subdocs_->back()->val_)
);
return OutputObjectType(&subdocs_->back()->val_, IsObject { _parent->val_ });
}
template <class T>
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent)
const noexcept {
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
bson_array_builder_append_utf8(
_parent->val_, _var.c_str(), static_cast<int>(_var.size())
@ -147,15 +129,10 @@ namespace rfl {
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
bson_array_builder_append_bool(_parent->val_, _var);
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
bson_array_builder_append_double(
_parent->val_, static_cast<double>(_var)
);
bson_array_builder_append_double(_parent->val_, static_cast<double>(_var));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
bson_array_builder_append_int64(
_parent->val_, static_cast<std::int64_t>(_var)
);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>(
)) {
bson_array_builder_append_int64(_parent->val_, static_cast<std::int64_t>(_var));
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
bson_array_builder_append_oid(_parent->val_, &_var);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
@ -178,15 +155,10 @@ namespace rfl {
static_cast<int>(_var.size())
);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
bson_append_bool(
_parent->val_, _name.data(), static_cast<int>(_name.size()), _var
);
bson_append_bool(_parent->val_, _name.data(), static_cast<int>(_name.size()), _var);
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
bson_append_double(
_parent->val_,
_name.data(),
static_cast<int>(_name.size()),
static_cast<double>(_var)
_parent->val_, _name.data(), static_cast<int>(_name.size()), static_cast<double>(_var)
);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
bson_append_int64(
@ -195,11 +167,8 @@ namespace rfl {
static_cast<int>(_name.size()),
static_cast<std::int64_t>(_var)
);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>(
)) {
bson_append_oid(
_parent->val_, _name.data(), static_cast<int>(_name.size()), &_var
);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
bson_append_oid(_parent->val_, _name.data(), static_cast<int>(_name.size()), &_var);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
@ -211,13 +180,9 @@ namespace rfl {
return OutputVarType {};
}
OutputVarType add_null_to_object(
const std::string_view& _name,
OutputObjectType* _parent
) const noexcept {
bson_append_null(
_parent->val_, _name.data(), static_cast<int>(_name.size())
);
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
const noexcept {
bson_append_null(_parent->val_, _name.data(), static_cast<int>(_name.size()));
return OutputVarType {};
}
@ -225,9 +190,7 @@ namespace rfl {
const auto handle = [&](const auto _parent) {
using Type = std::remove_cvref_t<decltype(_parent)>;
if constexpr (std::is_same<Type, IsArray>()) {
bson_array_builder_append_array_builder_end(
_parent.ptr_, _arr->val_
);
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
} else if constexpr (std::is_same<Type, IsObject>()) {
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
} else if constexpr (std::is_same<Type, IsRoot>()) {

View file

@ -10,9 +10,7 @@ namespace rfl {
template <class T, class... Ps>
Result<T> load(const std::string& _fname) {
const auto read_bytes = [](const auto& _bytes) {
return read<T, Ps...>(_bytes);
};
const auto read_bytes = [](const auto& _bytes) { return read<T, Ps...>(_bytes); };
return rfl::io::load_bytes(_fname).and_then(read_bytes);
}

View file

@ -23,13 +23,10 @@ namespace rfl {
bson_t* doc = nullptr;
uint8_t* buf = nullptr;
size_t buflen = 0;
bson_writer_t* bson_writer =
bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL);
bson_writer_t* bson_writer = bson_writer_new(&buf, &buflen, 0, bson_realloc_ctx, NULL);
bson_writer_begin(bson_writer, &doc);
const auto rfl_writer = Writer(doc);
Parser<T, Processors<Ps...>>::write(
rfl_writer, _obj, typename ParentType::Root {}
);
Parser<T, Processors<Ps...>>::write(rfl_writer, _obj, typename ParentType::Root {});
bson_writer_end(bson_writer);
const auto len = bson_writer_get_length(bson_writer);
bson_writer_destroy(bson_writer);
@ -40,9 +37,8 @@ namespace rfl {
template <class... Ps>
std::vector<char> write(const auto& _obj) noexcept {
auto [buf, len] = to_buffer<Ps...>(_obj);
const auto result = std::vector<char>(
reinterpret_cast<char*>(buf), reinterpret_cast<char*>(buf) + len
);
const auto result =
std::vector<char>(reinterpret_cast<char*>(buf), reinterpret_cast<char*>(buf) + len);
bson_free(buf);
return result;
}

View file

@ -12,15 +12,8 @@ namespace rfl {
/// Because of that, we require all of the fields and then set them to
/// nullptr, if necessary.
template <class ProcessorsType, class... FieldTypes>
requires AreReaderAndWriter<
cbor::Reader,
cbor::Writer,
NamedTuple<FieldTypes...>>
struct Parser<
cbor::Reader,
cbor::Writer,
NamedTuple<FieldTypes...>,
ProcessorsType>
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>>
struct Parser<cbor::Reader, cbor::Writer, NamedTuple<FieldTypes...>, ProcessorsType>
: public NamedTupleParser<
cbor::Reader,
cbor::Writer,

View file

@ -45,10 +45,8 @@ namespace rfl {
static constexpr bool has_custom_constructor =
(requires(InputVarType var) { T::from_cbor_obj(var); });
rfl::Result<InputVarType> get_field(
const std::string& _name,
const InputObjectType& _obj
) const noexcept {
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
const noexcept {
CborValue val;
auto buffer = std::vector<char>();
auto err = cbor_value_enter_container(_obj.val_, &val);
@ -144,16 +142,14 @@ namespace rfl {
}
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var
) const noexcept {
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
if (!cbor_value_is_array(_var.val_)) {
return Error("Could not cast to an array.");
}
return InputArrayType { _var.val_ };
}
rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept {
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
if (!cbor_value_is_map(_var.val_)) {
return Error("Could not cast to an object.");
}
@ -161,10 +157,8 @@ namespace rfl {
}
template <class ArrayReader>
std::optional<Error> read_array(
const ArrayReader& _array_reader,
const InputArrayType& _arr
) const noexcept {
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
const noexcept {
CborValue val;
auto buffer = std::vector<char>();
auto err = cbor_value_enter_container(_arr.val_, &val);
@ -190,10 +184,8 @@ namespace rfl {
}
template <class ObjectReader>
std::optional<Error> read_object(
const ObjectReader& _object_reader,
const InputObjectType& _obj
) const noexcept {
std::optional<Error>
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
size_t length = 0;
auto err = cbor_value_get_map_length(_obj.val_, &length);
if (err != CborNoError) {
@ -226,16 +218,14 @@ namespace rfl {
}
template <class T>
rfl::Result<T> use_custom_constructor(const InputVarType& _var
) const noexcept {
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
try {
return T::from_cbor_obj(_var);
} catch (std::exception& e) { return rfl::Error(e.what()); }
}
private:
CborError get_string(const CborValue* _ptr, std::vector<char>* _buffer)
const noexcept {
CborError get_string(const CborValue* _ptr, std::vector<char>* _buffer) const noexcept {
size_t length = 0;
auto err = cbor_value_get_string_length(_ptr, &length);
if (err != CborNoError && err != CborErrorOutOfMemory) {
@ -243,9 +233,7 @@ namespace rfl {
}
_buffer->resize(length + 1);
(*_buffer)[length] = '\0';
return cbor_value_copy_text_string(
_ptr, _buffer->data(), &length, NULL
);
return cbor_value_copy_text_string(_ptr, _buffer->data(), &length, NULL);
}
InputVarType to_input_var(CborValue* _ptr) const noexcept {

View file

@ -60,10 +60,8 @@ namespace rfl {
return new_value(_var, encoder_);
}
OutputArrayType add_array_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
return new_array(_size, _parent->encoder_);
}
@ -76,10 +74,8 @@ namespace rfl {
return new_array(_size, _parent->encoder_);
}
OutputObjectType add_object_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
return new_object(_size, _parent->encoder_);
}
@ -93,8 +89,7 @@ namespace rfl {
}
template <class T>
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent)
const noexcept {
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
return new_value(_var, _parent->encoder_);
}
@ -113,10 +108,8 @@ namespace rfl {
return OutputVarType {};
}
OutputVarType add_null_to_object(
const std::string_view& _name,
OutputObjectType* _parent
) const noexcept {
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
const noexcept {
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
cbor_encode_null(_parent->encoder_);
return OutputVarType {};
@ -131,23 +124,20 @@ namespace rfl {
}
private:
OutputArrayType new_array(const size_t _size, CborEncoder* _parent)
const noexcept {
OutputArrayType new_array(const size_t _size, CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
return OutputArrayType { subencoders_->back().get(), _parent };
}
OutputObjectType new_object(const size_t _size, CborEncoder* _parent)
const noexcept {
OutputObjectType new_object(const size_t _size, CborEncoder* _parent) const noexcept {
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
return OutputObjectType { subencoders_->back().get(), _parent };
}
template <class T>
OutputVarType new_value(const T& _var, CborEncoder* _parent)
const noexcept {
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
cbor_encode_text_string(_parent, _var.c_str(), _var.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {

View file

@ -11,9 +11,7 @@ namespace rfl {
template <class T, class... Ps>
Result<T> load(const std::string& _fname) {
const auto read_bytes = [](const auto& _bytes) {
return read<T, Ps...>(_bytes);
};
const auto read_bytes = [](const auto& _bytes) { return read<T, Ps...>(_bytes); };
return rfl::io::load_bytes(_fname).and_then(read_bytes);
}

View file

@ -25,13 +25,10 @@ namespace rfl {
/// Parses an object from CBOR using reflection.
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>>
read(const char* _bytes, const size_t _size) {
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes, const size_t _size) {
CborParser parser;
CborValue value;
cbor_parser_init(
reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser, &value
);
cbor_parser_init(reinterpret_cast<const uint8_t*>(_bytes), _size, 0, &parser, &value);
auto doc = InputVarType { &value };
auto result = read<T, Ps...>(doc);
return result;

View file

@ -22,16 +22,9 @@ namespace rfl {
) noexcept {
using T = std::remove_cvref_t<decltype(_obj)>;
using ParentType = parsing::Parent<Writer>;
cbor_encoder_init(
_encoder,
reinterpret_cast<uint8_t*>(_buffer->data()),
_buffer->size(),
0
);
cbor_encoder_init(_encoder, reinterpret_cast<uint8_t*>(_buffer->data()), _buffer->size(), 0);
const auto writer = Writer(_encoder);
Parser<T, Processors<Ps...>>::write(
writer, _obj, typename ParentType::Root {}
);
Parser<T, Processors<Ps...>>::write(writer, _obj, typename ParentType::Root {});
}
/// Returns CBOR bytes.
@ -40,15 +33,13 @@ namespace rfl {
std::vector<char> buffer(4096);
CborEncoder encoder;
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
const auto total_bytes_needed =
buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
const auto total_bytes_needed = buffer.size() + cbor_encoder_get_extra_bytes_needed(&encoder);
if (total_bytes_needed != buffer.size()) {
buffer.resize(total_bytes_needed);
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
}
const auto length = cbor_encoder_get_buffer_size(
&encoder, reinterpret_cast<uint8_t*>(buffer.data())
);
const auto length =
cbor_encoder_get_buffer_size(&encoder, reinterpret_cast<uint8_t*>(buffer.data()));
buffer.resize(length);
return buffer;
}

View file

@ -15,8 +15,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value != threshold) {
return Error(
"Value expected to be equal to " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
"Value expected to be equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
);
}
return _value;
@ -25,8 +25,7 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::EqualTo { .value_ = value } };
@ -40,9 +39,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value < threshold) {
return Error(
"Value expected to be greater than or equal to " +
std::to_string(threshold) + ", but got " + std::to_string(_value) +
"."
"Value expected to be greater than or equal to " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
);
}
return _value;
@ -51,8 +49,7 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::Minimum { .value_ = value } };
@ -66,8 +63,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value <= threshold) {
return Error(
"Value expected to be greater than " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
"Value expected to be greater than " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
);
}
return _value;
@ -76,12 +73,10 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType {ValidationType::ExclusiveMinimum {.value_ = value}
};
return ValidationType { ValidationType::ExclusiveMinimum { .value_ = value } };
}
};
@ -92,9 +87,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value > threshold) {
return Error(
"Value expected to be less than or equal to " +
std::to_string(threshold) + ", but got " + std::to_string(_value) +
"."
"Value expected to be less than or equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
);
}
return _value;
@ -103,8 +97,7 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::Maximum { .value_ = value } };
@ -118,8 +111,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value >= threshold) {
return Error(
"Value expected to be less than " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
"Value expected to be less than " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
);
}
return _value;
@ -128,12 +121,10 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType {ValidationType::ExclusiveMaximum {.value_ = value}
};
return ValidationType { ValidationType::ExclusiveMaximum { .value_ = value } };
}
};
@ -144,8 +135,8 @@ namespace rfl {
constexpr auto threshold = static_cast<T>(_threshold);
if (_value == threshold) {
return Error(
"Value expected to not be equal to " + std::to_string(threshold) +
", but got " + std::to_string(_value) + "."
"Value expected to not be equal to " + std::to_string(threshold) + ", but got " +
std::to_string(_value) + "."
);
}
return _value;
@ -154,8 +145,7 @@ namespace rfl {
template <class T>
static parsing::schema::ValidationType to_schema() {
using ValidationType = parsing::schema::ValidationType;
const auto value =
std::is_floating_point_v<T>
const auto value = std::is_floating_point_v<T>
? std::variant<double, int>(static_cast<double>(_threshold))
: std::variant<double, int>(static_cast<int>(_threshold));
return ValidationType { ValidationType::NotEqualTo { .value_ = value } };

View file

@ -8,8 +8,7 @@ namespace rfl {
/// Allows you to combine several literal types.
template <class... LiteralTypes>
using define_literal_t =
typename internal::define_literal<LiteralTypes...>::type;
using define_literal_t = typename internal::define_literal<LiteralTypes...>::type;
} // namespace rfl

View file

@ -7,8 +7,7 @@
namespace rfl {
template <class... FieldTypes>
using define_named_tuple_t =
typename internal::define_named_tuple<FieldTypes...>::type;
using define_named_tuple_t = typename internal::define_named_tuple<FieldTypes...>::type;
} // namespace rfl

View file

@ -8,8 +8,8 @@
namespace rfl {
template <internal::StringLiteral _discriminator, class... TaggedUnionTypes>
using define_tagged_union_t = typename internal::
define_tagged_union<_discriminator, TaggedUnionTypes...>::type;
using define_tagged_union_t =
typename internal::define_tagged_union<_discriminator, TaggedUnionTypes...>::type;
} // namespace rfl

View file

@ -14,23 +14,21 @@ namespace rfl {
// Converts an enum value to a string.
template <internal::enums::is_scoped_enum EnumType>
std::string enum_to_string(EnumType _enum) {
return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum
);
return rfl::internal::enums::StringConverter<EnumType>::enum_to_string(_enum);
}
// Converts a string to a value of the given enum type.
template <internal::enums::is_scoped_enum EnumType>
rfl::Result<EnumType> string_to_enum(const std::string& _str) {
return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str
);
return rfl::internal::enums::StringConverter<EnumType>::string_to_enum(_str);
}
// Returns a named tuple mapping names of enumerators of the given enum type
// to their values.
template <internal::enums::is_scoped_enum EnumType>
auto get_enumerators() {
constexpr auto names = internal::enums::
get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_named_tuple(names);
}
@ -38,8 +36,8 @@ namespace rfl {
// to their underlying values.
template <internal::enums::is_scoped_enum EnumType>
auto get_underlying_enumerators() {
constexpr auto names = internal::enums::
get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_underlying_enumerator_named_tuple(names);
}
@ -47,8 +45,8 @@ namespace rfl {
// std::string_view) and values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_enumerator_array() {
constexpr auto names = internal::enums::
get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_enumerator_array(names);
}
@ -56,8 +54,8 @@ namespace rfl {
// std::string_view) and underlying values.
template <internal::enums::is_scoped_enum EnumType>
constexpr auto get_underlying_enumerator_array() {
constexpr auto names = internal::enums::
get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
constexpr auto names =
internal::enums::get_enum_names<EnumType, internal::enums::is_flag_enum<EnumType>>();
return internal::enums::names_to_underlying_enumerator_array(names);
}

View file

@ -13,8 +13,7 @@ namespace rfl {
/// Extracts a Literal containing all of the discriminators from a
/// TaggedUnion.
template <class TaggedUnionType>
using extract_discriminators_t =
typename internal::extract_discriminators<TaggedUnionType>::type;
using extract_discriminators_t = typename internal::extract_discriminators<TaggedUnionType>::type;
} // namespace rfl

View file

@ -10,8 +10,8 @@ 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::remove_cvref_t<T>>)>::type;
using field_names_t =
typename std::invoke_result<decltype(internal::get_field_names<std::remove_cvref_t<T>>)>::type;
} // namespace rfl

View file

@ -28,39 +28,31 @@ namespace rfl {
template <class T>
struct has_from_flexbuf<
T,
std::enable_if_t<
std::is_invocable_r<T, decltype(T::from_flexbuf), InputVarType>::
value>> : std::true_type {};
std::enable_if_t<std::is_invocable_r<T, decltype(T::from_flexbuf), InputVarType>::value>>
: std::true_type {};
template <class T>
struct has_from_flexbuf<
T,
std::enable_if_t<std::is_invocable_r<
rfl::Result<T>,
decltype(T::from_flexbuf),
InputVarType>::value>> : std::true_type {};
std::enable_if_t<
std::is_invocable_r<rfl::Result<T>, decltype(T::from_flexbuf), InputVarType>::value>>
: std::true_type {};
template <class T>
static constexpr bool has_custom_constructor = has_from_flexbuf<T>::value;
rfl::Result<InputVarType> get_field(
const std::string& _name,
const InputObjectType& _obj
) const noexcept {
rfl::Result<InputVarType> get_field(const std::string& _name, const InputObjectType& _obj)
const noexcept {
const auto keys = _obj.Keys();
for (size_t i = 0; i < keys.size(); ++i) {
if (_name == keys[i].AsString().c_str()) {
return _obj.Values()[i];
}
}
return rfl::Error(
"Map does not contain any element called '" + _name + "'."
);
return rfl::Error("Map does not contain any element called '" + _name + "'.");
}
bool is_empty(const InputVarType& _var) const noexcept {
return _var.IsNull();
}
bool is_empty(const InputVarType& _var) const noexcept { return _var.IsNull(); }
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
@ -90,10 +82,8 @@ namespace rfl {
}
template <class ArrayReader>
std::optional<Error> read_array(
const ArrayReader& _array_reader,
const InputArrayType& _arr
) const noexcept {
std::optional<Error> read_array(const ArrayReader& _array_reader, const InputArrayType& _arr)
const noexcept {
const auto size = _arr.size();
for (size_t i = 0; i < size; ++i) {
const auto err = _array_reader.read(InputVarType(_arr[i]));
@ -105,33 +95,27 @@ namespace rfl {
}
template <class ObjectReader>
std::optional<Error> read_object(
const ObjectReader& _object_reader,
const InputObjectType& _obj
) const noexcept {
std::optional<Error>
read_object(const ObjectReader& _object_reader, const InputObjectType& _obj) const noexcept {
const auto keys = _obj.Keys();
const auto values = _obj.Values();
const auto num_values = std::min(keys.size(), values.size());
for (size_t i = 0; i < num_values; ++i) {
_object_reader.read(
std::string_view(keys[i].AsString().c_str()), values[i]
);
_object_reader.read(std::string_view(keys[i].AsString().c_str()), values[i]);
}
return std::nullopt;
}
rfl::Result<InputArrayType> to_array(const InputVarType& _var
) const noexcept {
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {
if (!_var.IsVector()) {
return rfl::Error("Could not cast to Vector.");
}
return _var.AsVector();
}
rfl::Result<InputObjectType> to_object(const InputVarType& _var
) const noexcept {
rfl::Result<InputObjectType> to_object(const InputVarType& _var) const noexcept {
if (!_var.IsMap()) {
return rfl::Error("Could not cast to Map!");
}
@ -139,8 +123,7 @@ namespace rfl {
}
template <class T>
rfl::Result<T> use_custom_constructor(const InputVarType& _var
) const noexcept {
rfl::Result<T> use_custom_constructor(const InputVarType& _var) const noexcept {
try {
return T::from_flexbuf(_var);
} catch (std::exception& e) { return rfl::Error(e.what()); }

View file

@ -39,13 +39,9 @@ namespace rfl {
~Writer() = default;
OutputArrayType array_as_root(const size_t _size) const noexcept {
return new_array();
}
OutputArrayType array_as_root(const size_t _size) const noexcept { return new_array(); }
OutputObjectType object_as_root(const size_t _size) const noexcept {
return new_object();
}
OutputObjectType object_as_root(const size_t _size) const noexcept { return new_object(); }
OutputVarType null_as_root() const noexcept {
fbb_->Null();
@ -57,10 +53,8 @@ namespace rfl {
return insert_value(_var);
}
OutputArrayType add_array_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
return new_array();
}
@ -72,10 +66,8 @@ namespace rfl {
return new_array(_name);
}
OutputObjectType add_object_to_array(
const size_t _size,
OutputArrayType* _parent
) const noexcept {
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
const noexcept {
return new_object();
}
@ -88,8 +80,7 @@ namespace rfl {
}
template <class T>
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent)
const noexcept {
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
return insert_value(_var);
}
@ -107,10 +98,8 @@ namespace rfl {
return OutputVarType {};
}
OutputVarType add_null_to_object(
const std::string_view& _name,
OutputObjectType* _parent
) const noexcept {
OutputVarType add_null_to_object(const std::string_view& _name, OutputObjectType* _parent)
const noexcept {
fbb_->Null(_name.data());
return OutputVarType {};
}
@ -119,14 +108,11 @@ namespace rfl {
fbb_->EndVector(_arr->start_, false, false);
}
void end_object(OutputObjectType* _obj) const noexcept {
fbb_->EndMap(_obj->start_);
}
void end_object(OutputObjectType* _obj) const noexcept { fbb_->EndMap(_obj->start_); }
private:
template <class T>
OutputVarType insert_value(const std::string_view& _name, const T& _var)
const noexcept {
OutputVarType insert_value(const std::string_view& _name, const T& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
fbb_->String(_name.data(), _var);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
@ -167,8 +153,7 @@ namespace rfl {
return OutputArrayType { start };
}
OutputObjectType new_object(const std::string_view& _name
) const noexcept {
OutputObjectType new_object(const std::string_view& _name) const noexcept {
const auto start = fbb_->StartMap(_name.data());
return OutputObjectType { start };
}

View file

@ -10,9 +10,7 @@ namespace rfl {
template <class T, class... Ps>
Result<T> load(const std::string& _fname) {
const auto read_bytes = [](const auto& _bytes) {
return read<T, Ps...>(_bytes);
};
const auto read_bytes = [](const auto& _bytes) { return read<T, Ps...>(_bytes); };
return rfl::io::load_bytes(_fname).and_then(read_bytes);
}

View file

@ -21,9 +21,7 @@ namespace rfl {
using ParentType = parsing::Parent<Writer>;
const auto fbb = Ref<flexbuffers::Builder>::make();
auto w = Writer(fbb);
Parser<T, Processors<Ps...>>::write(
w, _obj, typename ParentType::Root {}
);
Parser<T, Processors<Ps...>>::write(w, _obj, typename ParentType::Root {});
fbb->Finish();
return fbb->GetBuffer();
}

View file

@ -16,11 +16,8 @@ namespace rfl {
template <class T, class NamedTupleType>
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))
);
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> {}) {
return internal::copy_from_named_tuple<T>(_n);
@ -40,9 +37,7 @@ namespace rfl {
template <class T, class NamedTupleType>
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>()) {
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);

View file

@ -33,9 +33,7 @@ namespace rfl {
/// Gets a field by name.
template <internal::StringLiteral _field_name, class NamedTupleType>
inline const auto& get(const NamedTupleType& _tup) {
return internal::Getter<NamedTupleType>::template get_const<_field_name>(
_tup
);
return internal::Getter<NamedTupleType>::template get_const<_field_name>(_tup);
}
/// Gets a field by the field type.

View file

@ -11,8 +11,7 @@ namespace rfl {
namespace internal {
template <class T>
using field_tuple_t =
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
using field_tuple_t = typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
}
} // namespace rfl

View file

@ -29,21 +29,17 @@ namespace rfl::internal {
/// 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>();
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>();
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 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 "
@ -61,21 +57,17 @@ namespace rfl::internal {
/// 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>();
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>();
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 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 "
@ -123,9 +115,7 @@ namespace rfl::internal {
/// Retrieves the indicated value from the tuple.
template <int _index>
static inline const auto& get_const(
const std::variant<NamedTupleTypes...>& _tup
) {
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);
@ -135,9 +125,7 @@ namespace rfl::internal {
/// Gets a field by name.
template <StringLiteral _field_name>
static inline const auto& get_const(
const std::variant<NamedTupleTypes...>& _tup
) {
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);
@ -147,9 +135,7 @@ namespace rfl::internal {
/// Gets a field by the field type.
template <class Field>
static inline const auto& get_const(
const std::variant<NamedTupleTypes...>& _tup
) {
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);

View file

@ -19,11 +19,15 @@ namespace rfl {
/// Returns the underlying value.
template <class F>
const T& value(const F& _f) {
if (flag_.test()) { return value_; }
if (flag_.test()) {
return value_;
}
std::lock_guard<std::mutex> guard(mtx_);
if (flag_.test()) { return value_; }
if (flag_.test()) {
return value_;
}
_f(&value_);

View file

@ -22,8 +22,7 @@ namespace rfl::internal {
Skip(const Type& _value) : value_(_value) {}
Skip(ReflectionType&& _value) noexcept
: value_(_value ? std::move(*_value) : Type()) {}
Skip(ReflectionType&& _value) noexcept : value_(_value ? std::move(*_value) : Type()) {}
Skip(const ReflectionType& _value) : value_(_value ? *_value : Type()) {}
@ -39,31 +38,23 @@ namespace rfl::internal {
template <class U, bool _skip_s, bool _skip_d>
Skip(Skip<U, _skip_s, _skip_d>&& _other) : value_(_other.get()) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Skip(const U& _value) : value_(_value) {}
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Skip(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <
class U,
bool _skip_s,
bool _skip_d,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
Skip(const Skip<U, _skip_s, _skip_d>& _skip) : value_(_skip.value()) {}
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
Skip(const Default& _default) : value_(Type()) {}
~Skip() = default;
@ -93,10 +84,7 @@ namespace rfl::internal {
}
/// Assigns the underlying object.
template <
class U,
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type =
true>
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
@ -105,8 +93,7 @@ namespace rfl::internal {
/// Assigns the underlying object to its default value.
template <
class U = Type,
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
type = true>
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
auto& operator=(const Default& _default) {
value_ = Type();
return *this;

View file

@ -18,9 +18,7 @@ namespace rfl {
constexpr StringLiteral(const std::array<char, N> _arr) : arr_(_arr) {}
constexpr StringLiteral(const char (&_str)[N]) {
std::copy_n(_str, N, std::data(arr_));
}
constexpr StringLiteral(const char (&_str)[N]) { std::copy_n(_str, N, std::data(arr_)); }
/// Returns the value as a string.
std::string str() const { return std::string(std::data(arr_), N - 1); }
@ -34,10 +32,8 @@ namespace rfl {
};
template <size_t N1, size_t N2>
constexpr inline bool operator==(
const StringLiteral<N1>& _first,
const StringLiteral<N2>& _second
) {
constexpr inline bool
operator==(const StringLiteral<N1>& _first, const StringLiteral<N2>& _second) {
if constexpr (N1 != N2) {
return false;
}
@ -45,10 +41,8 @@ namespace rfl {
}
template <size_t N1, size_t N2>
constexpr inline bool operator!=(
const StringLiteral<N1>& _first,
const StringLiteral<N2>& _second
) {
constexpr inline bool
operator!=(const StringLiteral<N1>& _first, const StringLiteral<N2>& _second) {
return !(_first == _second);
}

View file

@ -7,8 +7,7 @@ namespace rfl {
struct VisitTree {
/// Evaluates a visitor pattern using a tree-like structure.
template <int _begin, int _end, class Visitor, class... Args>
static inline auto
visit(const auto& _v, const int _i, const Args&... _args) {
static inline auto visit(const auto& _v, const int _i, const Args&... _args) {
static_assert(_end > _begin, "_end needs to be greater than _begin.");
if constexpr (_end - _begin == 1) {
return _v.template visit<_begin>(_args...);

View file

@ -44,9 +44,7 @@ namespace rfl {
static consteval auto tuple_view() { \
const auto& [__VA_ARGS__] = get_fake_object<std::remove_cvref_t<T>>(); \
const auto ref_tup = std::tie(__VA_ARGS__); \
const auto get_ptrs = [](const auto&... _refs) { \
return std::make_tuple(&_refs...); \
}; \
const auto get_ptrs = [](const auto&... _refs) { return std::make_tuple(&_refs...); }; \
return std::apply(get_ptrs, ref_tup); \
} \
}
@ -69,61 +67,11 @@ namespace rfl {
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(3, f0, f1, f2);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(4, f0, f1, f2, f3);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(5, f0, f1, f2, f3, f4);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
6,
f0,
f1,
f2,
f3,
f4,
f5
);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
7,
f0,
f1,
f2,
f3,
f4,
f5,
f6
);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
8,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7
);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
9,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8
);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
10,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9
);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(6, f0, f1, f2, f3, f4, f5);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(7, f0, f1, f2, f3, f4, f5, f6);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(8, f0, f1, f2, f3, f4, f5, f6, f7);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(9, f0, f1, f2, f3, f4, f5, f6, f7, f8);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(10, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9);
RFL_INTERNAL_DEFINE_FAKE_OBJECT_TUPLE_VIEW_HELPER(
11,
f0,

View file

@ -69,60 +69,10 @@ namespace rfl {
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(6, f0, f1, f2, f3, f4, f5);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(7, f0, f1, f2, f3, f4, f5, f6);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(8, f0, f1, f2, f3, f4, f5, f6, f7);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(
9,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8
);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(
10,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9
);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(
11,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10
);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(
12,
f0,
f1,
f2,
f3,
f4,
f5,
f6,
f7,
f8,
f9,
f10,
f11
);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(9, f0, f1, f2, f3, f4, f5, f6, f7, f8);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(10, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(11, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(12, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11);
RFL_INTERNAL_DEFINE_TUPLE_VIEW_HELPER(
13,
f0,

View file

@ -13,19 +13,14 @@ namespace rfl {
namespace internal {
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::remove_cvref_t<decltype(_flattened_tuple)>>;
auto copy_flattened_tuple_to_named_tuple(const auto& _flattened_tuple, Fields&&... _fields) {
constexpr auto size = 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::remove_cvref_t<decltype(name_literal)>>>(
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>(

View file

@ -11,17 +11,9 @@ namespace rfl {
struct define_literal;
/// General case
template <
StringLiteral... _content1,
StringLiteral... _content2,
class... Tail>
struct define_literal<
Literal<_content1...>,
Literal<_content2...>,
Tail...> {
using type = typename define_literal<
Literal<_content1..., _content2...>,
Tail...>::type;
template <StringLiteral... _content1, StringLiteral... _content2, class... Tail>
struct define_literal<Literal<_content1...>, Literal<_content2...>, Tail...> {
using type = typename define_literal<Literal<_content1..., _content2...>, Tail...>::type;
};
/// Special case - only a single literal is left

View file

@ -20,22 +20,16 @@ namespace rfl {
/// Recursive case - first type is NamedTuple, second type is field.
template <class Head, class... TupContent, class... Tail>
struct define_named_tuple<NamedTuple<TupContent...>, Head, Tail...> {
using type = typename define_named_tuple<
NamedTuple<TupContent..., Head>,
Tail...>::type;
using type = typename define_named_tuple<NamedTuple<TupContent..., Head>, Tail...>::type;
};
/// Allows you to combine several named tuples and/or additional fields.
/// Recursive case - first type is NamedTuple, second type is also
/// NamedTuple.
template <class... TupContent, class... TupContent2, class... Tail>
struct define_named_tuple<
NamedTuple<TupContent...>,
NamedTuple<TupContent2...>,
Tail...> {
using type = typename define_named_tuple<
NamedTuple<TupContent..., TupContent2...>,
Tail...>::type;
struct define_named_tuple<NamedTuple<TupContent...>, NamedTuple<TupContent2...>, Tail...> {
using type =
typename define_named_tuple<NamedTuple<TupContent..., TupContent2...>, Tail...>::type;
};
/// Allows you to combine several named tuples and/or additional fields.

View file

@ -41,19 +41,13 @@ namespace rfl {
Tail...> {
using type = typename define_tagged_union<
_discriminator,
TaggedUnion<
_discriminator,
NamedTupleTypes...,
NamedTuple<FieldTypes...>>,
TaggedUnion<_discriminator, NamedTupleTypes..., NamedTuple<FieldTypes...>>,
Tail...>::type;
};
/// Recursive case - named tuple.
template <StringLiteral _discriminator, class... FieldTypes, class... Tail>
struct define_tagged_union<
_discriminator,
NamedTuple<FieldTypes...>,
Tail...> {
struct define_tagged_union<_discriminator, NamedTuple<FieldTypes...>, Tail...> {
using type = typename define_tagged_union<
_discriminator,
TaggedUnion<_discriminator, NamedTuple<FieldTypes...>>,
@ -62,9 +56,7 @@ namespace rfl {
/// Special case - only a single TaggedUnion is left.
template <StringLiteral _discriminator, class... NamedTupleTypes>
struct define_tagged_union<
_discriminator,
TaggedUnion<_discriminator, NamedTupleTypes...>> {
struct define_tagged_union<_discriminator, TaggedUnion<_discriminator, NamedTupleTypes...>> {
using type = TaggedUnion<_discriminator, NamedTupleTypes...>;
};

View file

@ -12,20 +12,14 @@ namespace rfl {
/// Recursive case - both variants.
template <class... Vars1, class... Vars2, class... Tail>
struct define_variant<
std::variant<Vars1...>,
std::variant<Vars2...>,
Tail...> {
using type =
typename define_variant<std::variant<Vars1..., Vars2...>, Tail...>::
type;
struct define_variant<std::variant<Vars1...>, std::variant<Vars2...>, Tail...> {
using type = typename define_variant<std::variant<Vars1..., Vars2...>, Tail...>::type;
};
/// Recursive case - variant plus other type.
template <class... Vars, class Head, class... Tail>
struct define_variant<std::variant<Vars...>, Head, Tail...> {
using type =
typename define_variant<std::variant<Vars..., Head>, Tail...>::type;
using type = typename define_variant<std::variant<Vars..., Head>, Tail...>::type;
};
/// Recursive case - other type.

View file

@ -26,85 +26,41 @@ namespace rfl {
constexpr static size_t size = N;
/// A list of all the possible enums
constexpr static std::array<EnumType, N> enums_ =
std::array<EnumType, N> {_enums...};
constexpr static std::array<EnumType, N> enums_ = std::array<EnumType, N> { _enums... };
static_assert(
N == 0 || LiteralType::size() == N,
"Size of literal and enum do not match."
);
static_assert(N == 0 || LiteralType::size() == N, "Size of literal and enum do not match.");
template <class NewLiteral, auto _new_enum>
using AddOneType = std::conditional_t<
N == 0,
Names<EnumType, NewLiteral, 1, _new_enum>,
Names<
EnumType,
define_literal_t<LiteralType, NewLiteral>,
N + 1,
_enums...,
_new_enum>>;
Names<EnumType, define_literal_t<LiteralType, NewLiteral>, N + 1, _enums..., _new_enum>>;
};
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
auto names_to_enumerator_named_tuple(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_enumerator_named_tuple(Names<EnumType, Literal<_names...>, N, _enums...>) {
return make_named_tuple(Field<_names, EnumType> { _enums }...);
}
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_underlying_enumerator_named_tuple(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return make_named_tuple(
Field<_names, std::underlying_type_t<EnumType>> {
static_cast<std::underlying_type_t<EnumType>>(_enums)
}...
);
return make_named_tuple(Field<_names, std::underlying_type_t<EnumType>> {
static_cast<std::underlying_type_t<EnumType>>(_enums) }...);
}
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<std::pair<std::string_view, EnumType>, N>
names_to_enumerator_array(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
return {std::make_pair(
LiteralHelper<_names>::field_.string_view(), _enums
)...};
names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
return { std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)... };
}
template <
class EnumType,
size_t N,
StringLiteral... _names,
auto... _enums>
constexpr std::array<
std::pair<std::string_view, std::underlying_type_t<EnumType>>,
N>
names_to_underlying_enumerator_array(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
names_to_underlying_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
return { std::make_pair(
LiteralHelper<_names>::field_.string_view(),
static_cast<std::underlying_type_t<EnumType>>(_enums)

View file

@ -23,8 +23,7 @@ namespace rfl {
public:
static constexpr bool is_flag_enum_ = is_flag_enum<EnumType>;
static constexpr auto names_ =
get_enum_names<EnumType, is_flag_enum_>();
static constexpr auto names_ = get_enum_names<EnumType, is_flag_enum_>();
using NamesLiteral = typename decltype(names_)::Literal;
@ -63,9 +62,7 @@ namespace rfl {
while (val != 0) {
const auto bit = val & static_cast<T>(1);
if (bit == 1) {
auto str = enum_to_single_string(
static_cast<EnumType>(static_cast<T>(1) << i)
);
auto str = enum_to_single_string(static_cast<EnumType>(static_cast<T>(1) << i));
flags.emplace_back(std::move(str));
}
++i;
@ -81,17 +78,13 @@ namespace rfl {
for (size_t i = 0; i < names_.size; ++i) {
if (names_.enums_[i] == _enum) {
return NamesLiteral::from_value(
static_cast<typename NamesLiteral::ValueType>(i)
)
return NamesLiteral::from_value(static_cast<typename NamesLiteral::ValueType>(i))
.transform(to_str)
.value();
}
}
return std::to_string(
static_cast<std::underlying_type_t<EnumType>>(_enum)
);
return std::to_string(static_cast<std::underlying_type_t<EnumType>>(_enum));
}
/// Finds the enum matching the literal.
@ -102,8 +95,7 @@ namespace rfl {
/// This assumes that _enum can be exactly matched to one of the names
/// and does not have to be combined using |.
static Result<EnumType> single_string_to_enum(const std::string& _str) {
const auto r =
NamesLiteral::from_string(_str).transform(literal_to_enum);
const auto r = NamesLiteral::from_string(_str).transform(literal_to_enum);
if (r) {
return r;
} else {
@ -115,8 +107,7 @@ namespace rfl {
/// Only relevant if this is a flag enum - combines the different
/// matches using |.
static Result<EnumType> string_to_flag_enum(const std::string& _str
) noexcept {
static Result<EnumType> string_to_flag_enum(const std::string& _str) noexcept {
using T = std::underlying_type_t<EnumType>;
const auto split = strings::split(_str, "|");
auto res = static_cast<T>(0);

View file

@ -47,8 +47,7 @@ namespace rfl {
#if defined(__clang__) && defined(_MSC_VER)
const auto func_name = std::string_view { __PRETTY_FUNCTION__ };
#else
const auto func_name =
std::string_view {std::source_location::current().function_name()};
const auto func_name = std::string_view { std::source_location::current().function_name() };
#endif
#if defined(__clang__)
const auto split = func_name.substr(0, func_name.size() - 1);
@ -91,8 +90,7 @@ namespace rfl {
if constexpr (_is_flag) {
return calc_greatest_power_of_two<T>();
} else {
return std::numeric_limits<T>::max() > 127
? static_cast<T>(127)
return std::numeric_limits<T>::max() > 127 ? static_cast<T>(127)
: std::numeric_limits<T>::max();
}
}
@ -106,12 +104,7 @@ namespace rfl {
}
}
template <
class EnumType,
class NamesType,
auto _max,
bool _is_flag,
int _i>
template <class EnumType, class NamesType, auto _max, bool _is_flag, int _i>
consteval auto get_enum_names_impl() {
using T = std::underlying_type_t<EnumType>;
@ -123,27 +116,16 @@ namespace rfl {
if constexpr (j == _max) {
return NamesType {};
} else {
return get_enum_names_impl<
EnumType,
NamesType,
_max,
_is_flag,
_i + 1>();
return get_enum_names_impl<EnumType, NamesType, _max, _is_flag, _i + 1>();
}
} else {
using NewNames = typename NamesType::template AddOneType<
Literal<remove_namespaces<name>()>,
static_cast<EnumType>(j)>;
using NewNames = typename NamesType::
template AddOneType<Literal<remove_namespaces<name>()>, static_cast<EnumType>(j)>;
if constexpr (j == _max) {
return NewNames {};
} else {
return get_enum_names_impl<
EnumType,
NewNames,
_max,
_is_flag,
_i + 1>();
return get_enum_names_impl<EnumType, NewNames, _max, _is_flag, _i + 1>();
}
}
}
@ -161,8 +143,7 @@ namespace rfl {
"The underlying type of any Enum must be integral!"
);
constexpr auto max =
get_max<std::underlying_type_t<EnumType>, _is_flag>();
constexpr auto max = get_max<std::underlying_type_t<EnumType>, _is_flag>();
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0>;

View file

@ -10,8 +10,7 @@ namespace rfl {
namespace enums {
template <class EnumType>
concept is_flag_enum =
is_scoped_enum<EnumType> && requires(EnumType e1, EnumType e2) {
concept is_flag_enum = is_scoped_enum<EnumType> && requires(EnumType e1, EnumType e2) {
{ e1 | e2 } -> std::same_as<EnumType>;
};

View file

@ -9,8 +9,7 @@ namespace rfl {
namespace enums {
template <class EnumType>
concept is_scoped_enum =
std::is_enum_v<EnumType> &&
concept is_scoped_enum = std::is_enum_v<EnumType> &&
!std::is_convertible_v<EnumType, std::underlying_type_t<EnumType>>;
} // namespace enums

View file

@ -14,10 +14,9 @@ namespace rfl {
struct extract_discriminators;
template <StringLiteral _discriminator, class... NamedTupleType>
struct extract_discriminators<
TaggedUnion<_discriminator, NamedTupleType...>> {
using type = define_literal_t<
std::remove_cvref_t<field_type_t<_discriminator, NamedTupleType>>...>;
struct extract_discriminators<TaggedUnion<_discriminator, NamedTupleType...>> {
using type =
define_literal_t<std::remove_cvref_t<field_type_t<_discriminator, NamedTupleType>>...>;
};
} // namespace internal

View file

@ -11,8 +11,7 @@ namespace rfl {
namespace internal {
template <class T>
using field_tuple_t =
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
using field_tuple_t = typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
}
} // namespace rfl

View file

@ -29,19 +29,14 @@ namespace rfl {
static constexpr int field_ix_ =
internal::find_index<_field_name, typename NamedTupleType::Fields>();
using Type = typename std::
tuple_element<field_ix_, typename NamedTupleType::Fields>::type::Type;
using Type =
typename std::tuple_element<field_ix_, typename NamedTupleType::Fields>::type::Type;
};
/// For variants - in this case the FieldType returned by all options must
/// be the same.
template <
StringLiteral _field_name,
class FirstAlternativeType,
class... OtherAlternativeTypes>
struct FieldType<
_field_name,
std::variant<FirstAlternativeType, OtherAlternativeTypes...>> {
template <StringLiteral _field_name, class FirstAlternativeType, class... OtherAlternativeTypes>
struct FieldType<_field_name, std::variant<FirstAlternativeType, OtherAlternativeTypes...>> {
constexpr static bool all_types_match = std::conjunction_v<std::is_same<
typename FieldType<_field_name, FirstAlternativeType>::Type,
typename FieldType<_field_name, OtherAlternativeTypes>::Type>...>;
@ -52,17 +47,11 @@ namespace rfl {
};
/// For tagged union - just defers to the variant.
template <
StringLiteral _field_name,
StringLiteral _discriminator_name,
class... VarTypes>
struct FieldType<
_field_name,
TaggedUnion<_discriminator_name, VarTypes...>> {
template <StringLiteral _field_name, StringLiteral _discriminator_name, class... VarTypes>
struct FieldType<_field_name, TaggedUnion<_discriminator_name, VarTypes...>> {
using Type = typename FieldType<
_field_name,
typename TaggedUnion<_discriminator_name, VarTypes...>::VariantType>::
Type;
typename TaggedUnion<_discriminator_name, VarTypes...>::VariantType>::Type;
};
} // namespace internal

View file

@ -12,8 +12,7 @@ namespace rfl {
/// 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::remove_cvref_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);

View file

@ -10,8 +10,8 @@ namespace rfl {
namespace internal {
template <class T>
using flattened_ptr_tuple_t = typename std::
invoke_result<decltype(to_flattened_ptr_tuple<T>), T>::type;
using flattened_ptr_tuple_t =
typename std::invoke_result<decltype(to_flattened_ptr_tuple<T>), T>::type;
} // namespace internal
} // namespace rfl

View file

@ -13,8 +13,7 @@ namespace rfl {
namespace internal {
template <class T>
using flattened_tuple_t =
typename remove_ptrs_tup<flattened_ptr_tuple_t<T>>::TupleType;
using flattened_tuple_t = typename remove_ptrs_tup<flattened_ptr_tuple_t<T>>::TupleType;
} // namespace internal
} // namespace rfl

View file

@ -48,8 +48,7 @@ namespace rfl::internal {
#if defined(__clang__) && defined(_MSC_VER)
const auto func_name = std::string_view { __PRETTY_FUNCTION__ };
#else
const auto func_name =
std::string_view {std::source_location::current().function_name()};
const auto func_name = std::string_view { std::source_location::current().function_name() };
#endif
#if defined(__clang__)
const auto split = func_name.substr(0, func_name.size() - 2);
@ -84,8 +83,8 @@ namespace rfl::internal {
template <class T, auto ptr>
auto get_field_name() {
#if defined(__clang__)
using Type = std::remove_cvref_t<std::remove_pointer_t<
typename std::remove_pointer_t<decltype(ptr)>::Type>>;
using Type = std::remove_cvref_t<
std::remove_pointer_t<typename std::remove_pointer_t<decltype(ptr)>::Type>>;
#else
using Type = std::remove_cvref_t<std::remove_pointer_t<decltype(ptr)>>;
#endif
@ -138,17 +137,13 @@ namespace rfl::internal {
#if defined(__clang__)
const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) {
return concat_literals(
get_field_name<
Type,
wrap(std::get<Is>(bind_fake_object_to_tuple<T>()))>()...
get_field_name<Type, wrap(std::get<Is>(bind_fake_object_to_tuple<T>()))>()...
);
};
#else
const auto get = []<std::size_t... Is>(std::index_sequence<Is...>) {
return concat_literals(
get_field_name<Type, std::get<Is>(bind_fake_object_to_tuple<T>())>(
)...
);
return concat_literals(get_field_name<Type, std::get<Is>(bind_fake_object_to_tuple<T>())>(
)...);
};
#endif
return get(std::make_index_sequence<num_fields<T>>());

View file

@ -17,13 +17,11 @@ namespace rfl {
if constexpr (NamedTupleType::size() == i) {
return std::array<MetaField, i> { std::move(_already_extracted)... };
} else {
using FieldType =
std::tuple_element_t<i, typename NamedTupleType::Fields>;
using FieldType = std::tuple_element_t<i, typename NamedTupleType::Fields>;
auto name = typename FieldType::Name().str();
auto type = type_name_t<typename FieldType::Type>().str();
return get_meta_fields<NamedTupleType>(
std::move(_already_extracted)...,
MetaField(std::move(name), std::move(type))
std::move(_already_extracted)..., MetaField(std::move(name), std::move(type))
);
}
}

View file

@ -16,8 +16,7 @@ namespace rfl {
#if defined(__clang__) && defined(_MSC_VER)
const auto func_name = std::string_view { __PRETTY_FUNCTION__ };
#else
const auto func_name =
std::string_view {std::source_location::current().function_name()};
const auto func_name = std::string_view { std::source_location::current().function_name() };
#endif
#if defined(__clang__)
const auto split = func_name.substr(0, func_name.size() - 1);
@ -44,10 +43,7 @@ namespace rfl {
template <class T>
consteval auto get_type_name() {
static_assert(
get_type_name_str_view<int>() == "int",
"Expected 'int', got something else."
);
static_assert(get_type_name_str_view<int>() == "int", "Expected 'int', got something else.");
constexpr auto name = get_type_name_str_view<T>();
const auto to_str_lit = [&]<auto... Ns>(std::index_sequence<Ns...>) {
return StringLiteral<sizeof...(Ns) + 1> { name[Ns]... };

View file

@ -20,8 +20,7 @@ namespace rfl {
} else {
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>>() &&
return all_fields_or_flatten<ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>() &&
all_fields_or_flatten<TupleType, _i + 1>();
} else {
return is_field_v<T> && all_fields_or_flatten<TupleType, _i + 1>();
@ -36,8 +35,7 @@ namespace rfl {
} else {
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>>() ||
return some_fields_or_flatten<ptr_tuple_t<typename std::remove_pointer_t<T>::Type>>() ||
some_fields_or_flatten<TupleType, _i + 1>();
} else {
return is_field_v<T> || some_fields_or_flatten<TupleType, _i + 1>();

View file

@ -7,15 +7,13 @@ namespace rfl {
namespace internal {
template <typename Wrapper>
using reflection_method_t =
decltype(std::declval<const Wrapper>().reflection());
using reflection_method_t = decltype(std::declval<const Wrapper>().reflection());
template <typename Wrapper, typename = std::void_t<>>
struct has_refl_m : std::false_type {};
template <typename Wrapper>
struct has_refl_m<Wrapper, std::void_t<reflection_method_t<Wrapper>>>
: std::true_type {};
struct has_refl_m<Wrapper, std::void_t<reflection_method_t<Wrapper>>> : std::true_type {};
/// Utility parameter for named tuple parsing, can be used by the
/// parsers to determine whether a class or struct has a method

View file

@ -17,8 +17,7 @@ namespace rfl {
static std::int32_t foo(typename U::ReflectionType*);
public:
static constexpr bool value =
sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
static constexpr bool value = sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
};
/// Utility parameter for named tuple parsing, can be used by the

View file

@ -17,8 +17,7 @@ namespace rfl {
static std::int32_t foo(typename U::Tag*);
public:
static constexpr bool value =
sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
static constexpr bool value = sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
};
/// Used for tagged unions - determines whether a struct as a Tag.

View file

@ -7,15 +7,13 @@ namespace rfl {
namespace internal {
template <typename Wrapper>
using to_class_method_t =
decltype(std::declval<const Wrapper>().to_class());
using to_class_method_t = decltype(std::declval<const Wrapper>().to_class());
template <typename Wrapper, typename = std::void_t<>>
struct has_to_class_m : std::false_type {};
template <typename Wrapper>
struct has_to_class_m<Wrapper, std::void_t<to_class_method_t<Wrapper>>>
: std::true_type {};
struct has_to_class_m<Wrapper, std::void_t<to_class_method_t<Wrapper>>> : std::true_type {};
/// Utility parameter for named tuple parsing, can be used by the
/// parsers to determine whether a class or struct has a method

View file

@ -19,8 +19,7 @@ namespace rfl::internal {
class is_array<Array<Type>> : public std::true_type {};
template <class T>
constexpr bool is_array_v =
is_array<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_array_v = is_array<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace rfl::internal

View file

@ -8,8 +8,7 @@ namespace rfl {
namespace internal {
template <class T>
constexpr bool is_basic_type_v =
std::is_floating_point_v<std::remove_cvref_t<T>> ||
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>();

View file

@ -21,8 +21,7 @@ namespace rfl {
class is_field<Field<_name, Type>> : public std::true_type {};
template <class T>
constexpr bool is_field_v =
is_field<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_field_v = is_field<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl

View file

@ -19,8 +19,7 @@ namespace rfl {
class is_literal<Literal<_s...>> : public std::true_type {};
template <class T>
constexpr bool is_literal_v =
is_literal<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_literal_v = is_literal<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl

View file

@ -19,8 +19,7 @@ namespace rfl {
class is_pattern<Pattern<_regex, _name>> : public std::true_type {};
template <class T>
constexpr bool is_pattern_v =
is_pattern<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_pattern_v = is_pattern<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl

View file

@ -21,8 +21,7 @@ namespace rfl {
class is_rename<Rename<_name, Type>> : public std::true_type {};
template <class T>
constexpr bool is_rename_v =
is_rename<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_rename_v = is_rename<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl

View file

@ -14,12 +14,10 @@ namespace rfl::internal {
class is_skip : public std::false_type {};
template <class T, bool _skip_serialization, bool _skip_deserialization>
class is_skip<Skip<T, _skip_serialization, _skip_deserialization>>
: public std::true_type {};
class is_skip<Skip<T, _skip_serialization, _skip_deserialization>> : public std::true_type {};
template <class T>
constexpr bool is_skip_v =
is_skip<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_skip_v = is_skip<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace rfl::internal

View file

@ -17,8 +17,7 @@ namespace rfl {
class is_variant<std::variant<T...>> : public std::true_type {};
template <class T>
constexpr bool is_variant_v =
is_variant<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
constexpr bool is_variant_v = is_variant<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace internal
} // namespace rfl

View file

@ -17,15 +17,13 @@ namespace rfl::internal {
static inline auto make_tag(const T& _t) noexcept {
if constexpr (internal::has_reflection_type_v<T>) {
return make_tag<typename T::ReflectionType>();
} else if constexpr (named_tuple_t<T>::Names::template contains<
_discriminator>()) {
} else if constexpr (named_tuple_t<T>::Names::template contains<_discriminator>()) {
return *to_view(_t).template get<_discriminator>();
} else if constexpr (internal::has_tag_v<T>) {
using LiteralType = typename T::Tag;
return LiteralType::template name_of<0>();
} else {
return rfl::Literal<
internal::remove_namespaces<internal::get_type_name<T>()>()>();
return rfl::Literal<internal::remove_namespaces<internal::get_type_name<T>()>()>();
}
}

View file

@ -20,15 +20,11 @@ namespace rfl {
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_))
)
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)))
std::move(_t), std::move(_args)..., std::make_tuple(std::move(std::get<i>(_t)))
);
}
}

View file

@ -20,8 +20,7 @@ namespace rfl {
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));
auto flattened_tuple = move_and_flatten_field_tuple(std::move(_field_tuple));
return std::apply(ft_to_nt, std::move(flattened_tuple));
}
}

View file

@ -17,28 +17,24 @@ namespace rfl {
auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) {
constexpr auto i = sizeof...(Args);
constexpr auto size =
std::tuple_size_v<std::remove_cvref_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::remove_cvref_t<std::tuple_element_t<i, PtrFieldTupleType>>;
using T =
std::remove_cvref_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 = typename std::
invoke_result<decltype(nt_to_ptr_named_tuple<T>), T>::type;
using SubPtrNamedTupleType =
typename std::invoke_result<decltype(nt_to_ptr_named_tuple<T>), T>::type;
return make_ptr_fields<PtrFieldTupleType>(
_n, _args..., SubPtrNamedTupleType(_n).fields()
);
} else if constexpr (is_flatten_field<Field>::value) {
using SubPtrFieldTupleType =
std::remove_cvref_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)
@ -58,28 +54,23 @@ namespace rfl {
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::remove_cvref_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>(
_ptrs,
std::move(_args)...,
rfl::make_field<FieldType::name_>(
std::move(*std::get<i>(_ptrs).value())
)
rfl::make_field<FieldType::name_>(std::move(*std::get<i>(_ptrs).value()))
);
} else {
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
using U = std::remove_cvref_t<std::remove_pointer_t<
typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
using U = std::remove_cvref_t<
std::remove_pointer_t<typename std::tuple_element_t<i, PtrFieldTupleType>::Type>>;
return move_from_ptr_fields<T>(
_ptrs,
std::move(_args)...,
move_from_ptr_fields<U>(std::get<i>(_ptrs))
_ptrs, std::move(_args)..., move_from_ptr_fields<U>(std::get<i>(_ptrs))
);
}
}
@ -94,9 +85,7 @@ namespace rfl {
if constexpr (is_named_tuple_v<std::remove_cvref_t<T>>) {
return std::move(_n);
} else if constexpr (std::is_same<
std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
} 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::remove_cvref_t<ptr_field_tuple_t<T>>;
@ -106,8 +95,7 @@ namespace rfl {
return move_from_ptr_fields<T>(pointers);
} else {
return move_from_named_tuple<T, RequiredType>(RequiredType(std::move(_n)
));
return move_from_named_tuple<T, RequiredType>(RequiredType(std::move(_n)));
}
}

View file

@ -29,37 +29,25 @@ namespace rfl {
}
}
template <
class TargetTupleType,
class PtrTupleType,
int _j = 0,
class... Args>
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::remove_cvref_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::remove_cvref_t<
std::remove_pointer_t<std::tuple_element_t<i, TargetTupleType>>>;
using T =
std::remove_cvref_t<std::remove_pointer_t<std::tuple_element_t<i, TargetTupleType>>>;
if constexpr (is_flatten_field_v<T>) {
using SubTargetTupleType =
ptr_tuple_t<std::remove_pointer_t<typename T::Type>>;
using SubTargetTupleType = ptr_tuple_t<std::remove_pointer_t<typename T::Type>>;
constexpr int flattened_size =
calc_flattened_size<SubTargetTupleType>();
constexpr int flattened_size = calc_flattened_size<SubTargetTupleType>();
return unflatten_ptr_tuple<
TargetTupleType,
PtrTupleType,
_j + flattened_size>(
_t,
_args...,
unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t)
return unflatten_ptr_tuple<TargetTupleType, PtrTupleType, _j + flattened_size>(
_t, _args..., unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t)
);
} else {
@ -76,24 +64,19 @@ namespace rfl {
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::remove_cvref_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))
);
return move_from_pointers<T>(_ptrs, std::move(_args)..., std::move(*std::get<i>(_ptrs)));
} else {
using PtrTupleType = ptr_tuple_t<std::remove_cvref_t<T>>;
using U = std::remove_cvref_t<typename std::remove_pointer_t<
typename std::tuple_element_t<i, PtrTupleType>>::Type>;
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)...,
move_from_pointers<U>(std::get<i>(_ptrs))
_ptrs, std::move(_args)..., move_from_pointers<U>(std::get<i>(_ptrs))
);
}
}
@ -106,9 +89,7 @@ namespace rfl {
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)...);
};
const auto fct = [](auto&... _v) { return std::tuple_cat(flatten_array(&_v)...); };
return std::apply(fct, *_arr);
}
@ -123,9 +104,7 @@ namespace rfl {
}
auto flatten_c_arrays(const auto& _tup) {
const auto fct = [](auto... _v) {
return std::tuple_cat(make_tuple_from_element(_v)...);
};
const auto fct = [](auto... _v) { return std::tuple_cat(make_tuple_from_element(_v)...); };
return std::apply(fct, _tup);
}
@ -137,8 +116,7 @@ namespace rfl {
using TargetTupleType = tuple_t<std::remove_cvref_t<T>>;
auto pointers =
flatten_c_arrays(unflatten_ptr_tuple<TargetTupleType>(ptr_tuple));
auto pointers = flatten_c_arrays(unflatten_ptr_tuple<TargetTupleType>(ptr_tuple));
return move_from_pointers<T>(pointers);
}

View file

@ -18,10 +18,8 @@ namespace rfl {
} else if constexpr (_j == -1) {
return no_duplicate_field_names<Fields, _i + 1, _i>();
} else {
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>;
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_;

View file

@ -32,8 +32,7 @@ namespace rfl {
/// Generates a named tuple that contains pointers to the original values in
/// the struct from a named tuple.
template <class NamedTupleType, class... AlreadyExtracted>
auto
nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) {
auto nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) {
using Fields = typename NamedTupleType::Fields;
constexpr auto i = sizeof...(AlreadyExtracted);
@ -45,9 +44,7 @@ namespace rfl {
using FieldType = typename std::tuple_element<i, Fields>::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()))
_nt, _a..., Field<FieldType::name_, const T*>(&std::get<i>(_nt.values()))
);
}
}

View file

@ -83,7 +83,8 @@ namespace rfl {
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... i,
std::size_t... j,
std::
size_t... k>(std::index_sequence<i...>, std::index_sequence<j...>, std::index_sequence<k...>) {
@ -108,8 +109,7 @@ namespace rfl {
if constexpr (size < 1) {
return 1;
} else if constexpr (constructible_with_nested<index, size, rest>() &&
!constructible_with_nested<index, size, rest + 1>(
)) {
!constructible_with_nested<index, size, rest + 1>()) {
return size;
} else {
return get_nested_array_size<index, size - 1, rest + 1>();
@ -120,13 +120,11 @@ namespace rfl {
static consteval std::size_t find_the_sole_non_empty_base_index() {
static_assert(index < max_args);
constexpr auto check =
[]<std::size_t... l,
std::
size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) {
[]<
std::size_t... l,
std::size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) {
return requires {
T {any_empty_base<T>(l)...,
any_base<T>(0),
any_empty_base<T>(r)...};
T { any_empty_base<T>(l)..., any_base<T>(0), any_empty_base<T>(r)... };
};
};
@ -154,13 +152,11 @@ namespace rfl {
template <std::size_t n, std::size_t max_arg_num>
static consteval bool has_n_base_param() {
constexpr auto right_len = max_arg_num >= n ? max_arg_num - n : 0;
return
[]<std::size_t... l,
std::
size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) {
return []<
std::size_t... l,
std::size_t... r>(std::index_sequence<l...>, std::index_sequence<r...>) {
return requires { T { any_base<T>(l)..., any(r)... }; };
}(std::make_index_sequence<n>(),
std::make_index_sequence<right_len>());
}(std::make_index_sequence<n>(), std::make_index_sequence<right_len>());
}
template <std::size_t max_arg_num, std::size_t index = 0>
@ -188,8 +184,7 @@ namespace rfl {
constexpr std::size_t max_agg_args = count_max_args_in_agg_init();
constexpr std::size_t no_brace_ellison_args =
constructible_no_brace_elision<0, max_agg_args>();
constexpr std::size_t base_args =
base_param_num<no_brace_ellison_args>();
constexpr std::size_t base_args = base_param_num<no_brace_ellison_args>();
if constexpr (no_brace_ellison_args == 0 && base_args == 0) {
// Empty struct
return 0;
@ -197,8 +192,7 @@ namespace rfl {
// Special case when the derived class is empty.
// In such cases the filed number is the fields in base class.
// Note that there should be only one base class in this case.
return get_nested_base_field_count<
find_the_sole_non_empty_base_index<max_agg_args>()>();
return get_nested_base_field_count<find_the_sole_non_empty_base_index<max_agg_args>()>();
} else {
return no_brace_ellison_args - base_args;
}

View file

@ -15,8 +15,7 @@ namespace rfl::internal {
struct Processed<StructType, Processors<Ps...>> {
using NamedTupleType = named_tuple_t<StructType>;
using type = typename std::invoke_result<
decltype(Processors<
Ps...>::template process<StructType, NamedTupleType>),
decltype(Processors<Ps...>::template process<StructType, NamedTupleType>),
NamedTupleType>::type;
};

View file

@ -12,8 +12,7 @@ namespace rfl {
namespace internal {
template <class T>
using ptr_named_tuple_t =
typename std::invoke_result<decltype(to_ptr_named_tuple<T>), T>::type;
using ptr_named_tuple_t = typename std::invoke_result<decltype(to_ptr_named_tuple<T>), T>::type;
} // namespace internal
} // namespace rfl

View file

@ -14,60 +14,37 @@ namespace rfl {
/// Recursively builds a new NamedTuple type from the FieldTypes, leaving
/// out the field signified by _name.
template <
class _OldNamedTupleType,
StringLiteral _name,
class _NewNamedTupleType,
int _i>
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType, int _i>
struct remove_single_field;
/// Special case - _i == 0
template <
class _OldNamedTupleType,
StringLiteral _name,
class _NewNamedTupleType>
struct remove_single_field<
_OldNamedTupleType,
_name,
_NewNamedTupleType,
0> {
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType>
struct remove_single_field<_OldNamedTupleType, _name, _NewNamedTupleType, 0> {
using type = _NewNamedTupleType;
};
/// General case.
template <
class _OldNamedTupleType,
StringLiteral _name,
class _NewNamedTupleType,
int _i>
template <class _OldNamedTupleType, StringLiteral _name, class _NewNamedTupleType, int _i>
struct remove_single_field {
using OldNamedTupleType = std::remove_cvref_t<_OldNamedTupleType>;
constexpr static int num_fields =
std::tuple_size_v<typename OldNamedTupleType::Fields>;
constexpr static int num_fields = std::tuple_size_v<typename OldNamedTupleType::Fields>;
using FieldType = std::remove_cvref_t<typename std::tuple_element<
num_fields - _i,
typename OldNamedTupleType::Fields>::type>;
using FieldType = std::remove_cvref_t<
typename std::tuple_element<num_fields - _i, typename OldNamedTupleType::Fields>::type>;
using NewNamedTupleType = std::conditional_t<
_name == FieldType::name_,
_NewNamedTupleType,
define_named_tuple_t<_NewNamedTupleType, FieldType>>;
using type = typename remove_single_field<
OldNamedTupleType,
_name,
NewNamedTupleType,
_i - 1>::type;
using type =
typename remove_single_field<OldNamedTupleType, _name, NewNamedTupleType, _i - 1>::type;
};
/// Recursively removes all of the fields signified by _head and _tail from
/// the NamedTupleType.
template <
class _NamedTupleType,
StringLiteral _head,
StringLiteral... _tail>
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
struct remove_fields;
/// Special case - only head is left.
@ -75,32 +52,21 @@ namespace rfl {
struct remove_fields<_NamedTupleType, _head> {
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
constexpr static int num_fields =
std::tuple_size_v<typename NamedTupleType::Fields>;
constexpr static int num_fields = std::tuple_size_v<typename NamedTupleType::Fields>;
using type = typename remove_single_field<
NamedTupleType,
_head,
NamedTuple<>,
num_fields>::type;
using type =
typename remove_single_field<NamedTupleType, _head, NamedTuple<>, num_fields>::type;
};
/// General case.
template <
class _NamedTupleType,
StringLiteral _head,
StringLiteral... _tail>
template <class _NamedTupleType, StringLiteral _head, StringLiteral... _tail>
struct remove_fields {
using NamedTupleType = std::remove_cvref_t<_NamedTupleType>;
constexpr static int num_fields =
std::tuple_size_v<typename NamedTupleType::Fields>;
constexpr static int num_fields = std::tuple_size_v<typename NamedTupleType::Fields>;
using NewNamedTupleType = typename remove_single_field<
NamedTupleType,
_head,
NamedTuple<>,
num_fields>::type;
using NewNamedTupleType =
typename remove_single_field<NamedTupleType, _head, NamedTuple<>, num_fields>::type;
using type = typename remove_fields<NewNamedTupleType, _tail...>::type;
};

Some files were not shown because too many files have changed in this diff Show more