weh
This commit is contained in:
parent
bd402f57f5
commit
3ea546fe08
|
@ -1,5 +1,8 @@
|
|||
---
|
||||
AlignAfterOpenBracket: BlockIndent
|
||||
AlignArrayOfStructures: Right
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignConsecutiveDeclarations: true
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortCompoundRequirementOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
|
|
5782
include/ctre.hpp
5782
include/ctre.hpp
File diff suppressed because it is too large
Load diff
|
@ -21,8 +21,9 @@ namespace rfl {
|
|||
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()));
|
||||
}
|
||||
|
|
|
@ -20,7 +20,8 @@ namespace rfl {
|
|||
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>()...});
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
|
||||
);
|
||||
return ValidationType {ValidationType::AllOf {.types_ = types}};
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ namespace rfl {
|
|||
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>()...});
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
|
||||
);
|
||||
return ValidationType {ValidationType::AnyOf {.types_ = types}};
|
||||
}
|
||||
|
||||
|
@ -39,15 +40,16 @@ 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));
|
||||
_value, std::forward<std::vector<Error>>(_errors)
|
||||
);
|
||||
}
|
||||
};
|
||||
return Head::validate(_value).or_else(handle_err);
|
||||
|
|
|
@ -33,25 +33,29 @@ 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
Attribute(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Attribute() = default;
|
||||
|
@ -78,18 +82,20 @@ 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;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
|
|
|
@ -96,9 +96,8 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class CharT, class Traits, class T>
|
||||
inline std::basic_ostream<CharT, Traits>& operator<<(
|
||||
std::basic_ostream<CharT, Traits>& _os,
|
||||
const Box<T>& _b) {
|
||||
inline std::basic_ostream<CharT, Traits>&
|
||||
operator<<(std::basic_ostream<CharT, Traits>& _os, const Box<T>& _b) {
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
}
|
||||
|
|
|
@ -42,26 +42,30 @@ namespace rfl {
|
|||
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>
|
||||
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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
Description(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Description() = default;
|
||||
|
@ -91,18 +95,20 @@ 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;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
|
@ -110,11 +116,13 @@ namespace rfl {
|
|||
|
||||
/// Assigns the underlying object.
|
||||
Description<_description, T>& operator=(
|
||||
const Description<_description, T>& _field) = default;
|
||||
const Description<_description, T>& _field
|
||||
) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Description<_description, T>& operator=(
|
||||
Description<_description, T>&& _field) = default;
|
||||
Description<_description, T>&& _field
|
||||
) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
|
|
|
@ -39,25 +39,29 @@ 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
Field(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Field() = default;
|
||||
|
@ -90,18 +94,20 @@ 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;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
|
|
|
@ -29,14 +29,16 @@ 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;
|
||||
|
@ -66,9 +68,10 @@ 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;
|
||||
|
|
|
@ -25,9 +25,8 @@ namespace rfl {
|
|||
using FieldsType = std::tuple<LiteralHelper<fields_>...>;
|
||||
|
||||
public:
|
||||
using ValueType =
|
||||
std::conditional_t<sizeof...(fields_) <=
|
||||
std::numeric_limits<std::uint8_t>::max(),
|
||||
using ValueType = std::conditional_t<
|
||||
sizeof...(fields_) <= std::numeric_limits<std::uint8_t>::max(),
|
||||
std::uint8_t,
|
||||
std::uint16_t>;
|
||||
|
||||
|
@ -47,7 +46,8 @@ namespace rfl {
|
|||
|
||||
/// A single-field literal is special because it
|
||||
/// can also have a default constructor.
|
||||
template <ValueType num_fields = num_fields_,
|
||||
template <
|
||||
ValueType num_fields = num_fields_,
|
||||
typename = std::enable_if_t<num_fields <= 1>>
|
||||
Literal() : value_(0) {}
|
||||
|
||||
|
@ -69,8 +69,9 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -155,8 +156,8 @@ 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) {
|
||||
|
@ -233,7 +234,8 @@ 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 = {}) {
|
||||
std::vector<std::string> _values = {}
|
||||
) {
|
||||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
const auto head = FieldType::field_.str();
|
||||
_values.push_back(head);
|
||||
|
@ -297,9 +299,10 @@ namespace rfl {
|
|||
using FieldType = typename std::tuple_element<_i, FieldsType>::type;
|
||||
if (FieldType::field_.str() == _str) { return _i; }
|
||||
if constexpr (_i + 1 == num_fields_) {
|
||||
return Error("Literal does not support string '" + _str +
|
||||
"'. The following strings are supported: " +
|
||||
allowed_strings() + ".");
|
||||
return Error(
|
||||
"Literal does not support string '" + _str +
|
||||
"'. The following strings are supported: " + allowed_strings() + "."
|
||||
);
|
||||
} else {
|
||||
return find_value<_i + 1>(_str);
|
||||
}
|
||||
|
@ -336,11 +339,15 @@ 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(),
|
||||
"Duplicate strings are not allowed in a Literal.");
|
||||
static_assert(
|
||||
sizeof...(fields_) <= 1 || !has_duplicates(),
|
||||
"Duplicate strings are not allowed in a Literal."
|
||||
);
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
|
|
|
@ -39,46 +39,56 @@ namespace rfl {
|
|||
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
|
||||
)...
|
||||
) {
|
||||
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)
|
||||
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");
|
||||
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");
|
||||
static_assert(
|
||||
no_duplicate_field_names(), "Duplicate field names are not allowed"
|
||||
);
|
||||
}
|
||||
|
||||
/// Construct from a tuple containing fields.
|
||||
NamedTuple(std::tuple<FieldTypes...>&& _tup)
|
||||
: 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");
|
||||
std::forward<std::tuple<FieldTypes...>>(_tup)
|
||||
)) {
|
||||
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.
|
||||
|
@ -91,16 +101,18 @@ 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;
|
||||
|
@ -111,11 +123,13 @@ namespace rfl {
|
|||
using Head = Field<_name, FType>;
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)))
|
||||
make_fields<1, Head>(std::forward<Head>(_head))
|
||||
)
|
||||
.add(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)));
|
||||
make_fields<1, Head>(std::forward<Head>(_head))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,11 +139,13 @@ namespace rfl {
|
|||
using Head = Field<_name, FType>;
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(_head))
|
||||
make_fields<1, Head>(_head)
|
||||
)
|
||||
.add(_tail...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::remove_cvref_t<Head>>(
|
||||
make_fields<1, Head>(_head));
|
||||
make_fields<1, Head>(_head)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,15 +176,17 @@ namespace rfl {
|
|||
/// named tuples.
|
||||
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)...);
|
||||
return add(
|
||||
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...);
|
||||
}
|
||||
|
||||
|
@ -179,10 +197,8 @@ 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));
|
||||
}
|
||||
|
@ -194,10 +210,8 @@ 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));
|
||||
}
|
||||
|
@ -205,8 +219,8 @@ 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());
|
||||
|
@ -270,12 +284,12 @@ 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 {
|
||||
|
@ -289,8 +303,8 @@ 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) {
|
||||
|
@ -304,8 +318,10 @@ 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) {
|
||||
|
@ -345,14 +361,15 @@ namespace rfl {
|
|||
auto replace(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
|
||||
return replace(
|
||||
std::forward<NamedTuple<TupContent...>>(_named_tuple).fields(),
|
||||
std::forward<Tail>(_tail)...);
|
||||
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...);
|
||||
}
|
||||
|
||||
|
@ -366,10 +383,8 @@ 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));
|
||||
}
|
||||
|
@ -381,10 +396,8 @@ 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));
|
||||
}
|
||||
|
@ -434,7 +447,8 @@ namespace rfl {
|
|||
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)...);
|
||||
std::forward<Args>(_args)...
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -454,7 +468,8 @@ namespace rfl {
|
|||
// them to the end, that is why we do it like this.
|
||||
using FieldType = typename std::tuple_element<i, Fields>::type;
|
||||
return make_fields<num_additional_fields>(
|
||||
FieldType(std::get<i>(values_)), std::move(_args)...);
|
||||
FieldType(std::get<i>(values_)), std::move(_args)...
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -473,13 +488,15 @@ namespace rfl {
|
|||
if constexpr (size == _index) {
|
||||
return make_replaced<_index, V, T>(
|
||||
std::forward<V>(_values), std::forward<T>(_val),
|
||||
std::forward<Args>(_args)..., FieldType(std::forward<T>(_val)));
|
||||
std::forward<Args>(_args)..., FieldType(std::forward<T>(_val))
|
||||
);
|
||||
} else {
|
||||
using U = typename FieldType::Type;
|
||||
return make_replaced<_index, V, T>(
|
||||
std::forward<V>(_values), std::forward<T>(_val),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(std::forward<U>(std::get<size>(_values))));
|
||||
FieldType(std::forward<U>(std::get<size>(_values)))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -494,8 +511,9 @@ 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.
|
||||
|
@ -504,8 +522,9 @@ 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,
|
||||
|
@ -532,7 +551,8 @@ namespace rfl {
|
|||
template <class... OtherFieldTypes, class... Args>
|
||||
constexpr static Fields retrieve_fields(
|
||||
std::tuple<OtherFieldTypes...>&& _other_fields,
|
||||
Args&&... _args) {
|
||||
Args&&... _args
|
||||
) {
|
||||
constexpr auto size = sizeof...(Args);
|
||||
|
||||
constexpr bool retrieved_all_fields = size == std::tuple_size_v<Fields>;
|
||||
|
@ -553,7 +573,8 @@ namespace rfl {
|
|||
return retrieve_fields(
|
||||
std::forward<std::tuple<OtherFieldTypes...>>(_other_fields),
|
||||
std::forward<Args>(_args)...,
|
||||
FieldType(std::forward<T>(std::get<index>(_other_fields).value_)));
|
||||
FieldType(std::forward<T>(std::get<index>(_other_fields).value_))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -603,8 +624,8 @@ 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...);
|
||||
}
|
||||
|
||||
|
@ -636,59 +657,73 @@ namespace rfl {
|
|||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <internal::StringLiteral _name1,
|
||||
template <
|
||||
internal::StringLiteral _name1,
|
||||
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);
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name1,
|
||||
template <
|
||||
internal::StringLiteral _name1,
|
||||
class Type1,
|
||||
internal::StringLiteral _name2,
|
||||
class Type2>
|
||||
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));
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ namespace rfl {
|
|||
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>()...});
|
||||
{C::template to_schema<T>(), Cs::template to_schema<T>()...}
|
||||
);
|
||||
return ValidationType {ValidationType::OneOf {.types_ = types}};
|
||||
}
|
||||
|
||||
|
@ -40,8 +41,8 @@ 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;
|
||||
|
@ -55,7 +56,8 @@ namespace rfl {
|
|||
return make_error_message(_errors);
|
||||
} else {
|
||||
return validate_impl<T, Tail...>(
|
||||
_value, std::forward<std::vector<Error>>(_errors));
|
||||
_value, std::forward<std::vector<Error>>(_errors)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -20,8 +20,10 @@ namespace rfl {
|
|||
if (ctre::match<_regex.arr_>(_str)) {
|
||||
return _str;
|
||||
} else {
|
||||
return rfl::Error("String '" + _str + "' did not match format '" +
|
||||
_name.str() + "': '" + _regex.str() + "'.");
|
||||
return rfl::Error(
|
||||
"String '" + _str + "' did not match format '" + _name.str() +
|
||||
"': '" + _regex.str() + "'."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,14 +22,15 @@ namespace rfl {
|
|||
|
||||
template <class Head, class... Tail>
|
||||
struct Processors<Head, Tail...> {
|
||||
static constexpr bool all_required_ =
|
||||
std::disjunction_v<internal::is_no_optionals<Head>,
|
||||
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) {
|
||||
return Processors<Tail...>::template process<T>(
|
||||
Head::template process<T>(std::move(_named_tuple)));
|
||||
Head::template process<T>(std::move(_named_tuple))
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -116,9 +116,8 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class CharT, class Traits, class T>
|
||||
inline std::basic_ostream<CharT, Traits>& operator<<(
|
||||
std::basic_ostream<CharT, Traits>& _os,
|
||||
const Ref<T>& _b) {
|
||||
inline std::basic_ostream<CharT, Traits>&
|
||||
operator<<(std::basic_ostream<CharT, Traits>& _os, const Ref<T>& _b) {
|
||||
_os << _b.get();
|
||||
return _os;
|
||||
}
|
||||
|
|
|
@ -39,25 +39,29 @@ 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
Rename(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Rename() = default;
|
||||
|
@ -87,18 +91,20 @@ 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;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
|
|
|
@ -40,8 +40,10 @@ 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))>;
|
||||
|
@ -75,8 +77,9 @@ namespace rfl {
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,8 +25,9 @@ namespace rfl {
|
|||
/// Applies the logic to a single field.
|
||||
template <class FieldType>
|
||||
static auto handle_one_field(FieldType&& _f) {
|
||||
using NewFieldType =
|
||||
Field<internal::transform_snake_case<FieldType::name_,
|
||||
using NewFieldType = Field<
|
||||
internal::transform_snake_case<
|
||||
FieldType::name_,
|
||||
/*capitalize=*/false>(),
|
||||
typename FieldType::Type>;
|
||||
return NewFieldType(_f.value());
|
||||
|
|
|
@ -25,8 +25,9 @@ namespace rfl {
|
|||
/// Applies the logic to a single field.
|
||||
template <class FieldType>
|
||||
static auto handle_one_field(FieldType&& _f) {
|
||||
using NewFieldType =
|
||||
Field<internal::transform_snake_case<FieldType::name_,
|
||||
using NewFieldType = Field<
|
||||
internal::transform_snake_case<
|
||||
FieldType::name_,
|
||||
/*capitalize=*/true>(),
|
||||
typename FieldType::Type>;
|
||||
return NewFieldType(_f.value());
|
||||
|
|
|
@ -27,20 +27,22 @@ namespace rfl {
|
|||
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>
|
||||
template <
|
||||
class T,
|
||||
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>
|
||||
template <
|
||||
class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
|
||||
type = true>
|
||||
TaggedUnion(T&& _t) noexcept : variant_(std::forward<T>(_t)) {}
|
||||
|
||||
~TaggedUnion() = default;
|
||||
|
@ -58,18 +60,20 @@ namespace rfl {
|
|||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
template <
|
||||
class T,
|
||||
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;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
template <
|
||||
class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
|
||||
type = true>
|
||||
TaggedUnion<_discriminator, Ts...>& operator=(const T& _variant) {
|
||||
variant_ = _variant;
|
||||
return *this;
|
||||
|
@ -77,11 +81,13 @@ namespace rfl {
|
|||
|
||||
/// Assigns the underlying object.
|
||||
TaggedUnion<_discriminator, Ts...>& operator=(
|
||||
const TaggedUnion<_discriminator, Ts...>& _other) = default;
|
||||
const TaggedUnion<_discriminator, Ts...>& _other
|
||||
) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
TaggedUnion<_discriminator, Ts...>& operator=(
|
||||
TaggedUnion<_discriminator, Ts...>&& _other) = default;
|
||||
TaggedUnion<_discriminator, Ts...>&& _other
|
||||
) = default;
|
||||
|
||||
/// Returns the underlying variant.
|
||||
VariantType& variant() { return variant_; }
|
||||
|
@ -89,8 +95,10 @@ namespace rfl {
|
|||
/// Returns the underlying variant.
|
||||
const VariantType& variant() const { return variant_; }
|
||||
|
||||
static_assert(!PossibleTags::has_duplicates(),
|
||||
"Duplicate tags are not allowed inside tagged unions.");
|
||||
static_assert(
|
||||
!PossibleTags::has_duplicates(),
|
||||
"Duplicate tags are not allowed inside tagged unions."
|
||||
);
|
||||
|
||||
/// The underlying variant - a TaggedUnion is a thin wrapper
|
||||
/// around a variant that is mainly used for parsing.
|
||||
|
@ -105,15 +113,18 @@ namespace rfl {
|
|||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<_index>(
|
||||
_tu.variant_);
|
||||
_tu.variant_
|
||||
);
|
||||
}
|
||||
|
||||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
|
@ -121,15 +132,18 @@ namespace rfl {
|
|||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline auto& get(
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get<Field>(
|
||||
_tu.variant_);
|
||||
_tu.variant_
|
||||
);
|
||||
}
|
||||
|
||||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_index>(_tu.variant_);
|
||||
}
|
||||
|
@ -137,7 +151,8 @@ namespace rfl {
|
|||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
_field_name>(_tu.variant_);
|
||||
}
|
||||
|
@ -145,7 +160,8 @@ namespace rfl {
|
|||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu) {
|
||||
const TaggedUnion<_discriminator, NamedTupleTypes...>& _tu
|
||||
) {
|
||||
return Getter<std::variant<NamedTupleTypes...>>::template get_const<
|
||||
Field>(_tu.variant_);
|
||||
}
|
||||
|
|
|
@ -28,9 +28,10 @@ namespace rfl {
|
|||
Timestamp(const char* _str) : tm_(std::tm {}) {
|
||||
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() +
|
||||
"'.");
|
||||
throw std::runtime_error(
|
||||
"String '" + std::string(_str) + "' did not match format '" +
|
||||
Format().str() + "'."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -70,12 +70,12 @@ 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 <
|
||||
|
@ -115,8 +115,10 @@ 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();
|
||||
}
|
||||
|
||||
|
|
|
@ -18,14 +18,16 @@ namespace rfl {
|
|||
|
||||
Variant(Variant<AlternativeTypes...>&& _variant) noexcept = default;
|
||||
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
template <
|
||||
class T,
|
||||
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>
|
||||
template <
|
||||
class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
|
||||
type = true>
|
||||
Variant(T&& _t) noexcept : variant_(std::forward<T>(_t)) {}
|
||||
|
||||
~Variant() = default;
|
||||
|
@ -43,18 +45,20 @@ namespace rfl {
|
|||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
template <
|
||||
class T,
|
||||
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;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
template <
|
||||
class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
|
||||
type = true>
|
||||
Variant<AlternativeTypes...>& operator=(const T& _variant) {
|
||||
variant_ = _variant;
|
||||
return *this;
|
||||
|
@ -62,11 +66,13 @@ namespace rfl {
|
|||
|
||||
/// Assigns the underlying object.
|
||||
Variant<AlternativeTypes...>& operator=(
|
||||
const Variant<AlternativeTypes...>& _other) = default;
|
||||
const Variant<AlternativeTypes...>& _other
|
||||
) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Variant<AlternativeTypes...>& operator=(
|
||||
Variant<AlternativeTypes...>&& _other) = default;
|
||||
Variant<AlternativeTypes...>&& _other
|
||||
) = default;
|
||||
|
||||
/// Returns the underlying variant.
|
||||
VariantType& variant() { return variant_; }
|
||||
|
|
|
@ -15,7 +15,8 @@ namespace rfl {
|
|||
} else {
|
||||
return from_named_tuple<T>(
|
||||
to_named_tuple(std::forward<Head>(_head))
|
||||
.add(to_named_tuple(std::forward<Tail>(_tail))...));
|
||||
.add(to_named_tuple(std::forward<Tail>(_tail))...)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,8 +26,8 @@ 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
|
||||
)...));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,22 +19,24 @@ 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.");
|
||||
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 {}};
|
||||
}
|
||||
};
|
||||
|
|
|
@ -51,7 +51,8 @@ namespace rfl {
|
|||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _obj.val_->val_.value.v_doc;
|
||||
|
@ -84,7 +85,8 @@ namespace rfl {
|
|||
|
||||
default:
|
||||
return rfl::Error(
|
||||
"Could not cast to string. The type must be UTF8 or symbol.");
|
||||
"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) {
|
||||
|
@ -109,10 +111,11 @@ namespace rfl {
|
|||
default:
|
||||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be double, "
|
||||
"int32, int64 or date_time.");
|
||||
"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.");
|
||||
}
|
||||
|
@ -122,8 +125,8 @@ 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.");
|
||||
|
@ -134,7 +137,8 @@ namespace rfl {
|
|||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
const InputArrayType& _arr
|
||||
) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _arr.val_->val_.value.v_doc;
|
||||
|
@ -152,7 +156,8 @@ namespace rfl {
|
|||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _obj.val_->val_.value.v_doc;
|
||||
|
@ -167,8 +172,8 @@ 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.");
|
||||
|
@ -177,8 +182,8 @@ 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()); }
|
||||
|
|
|
@ -79,14 +79,17 @@ 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* _parent
|
||||
) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
|
||||
return OutputArrayType(val, IsArray {_parent->val_});
|
||||
|
@ -95,51 +98,62 @@ namespace rfl {
|
|||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_append_array_builder_begin(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), &val);
|
||||
bson_append_array_builder_begin(
|
||||
_parent->val_, _name.data(), static_cast<int>(_name.size()), &val
|
||||
);
|
||||
return OutputArrayType(val, IsObject {_parent->val_});
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
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(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) 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_});
|
||||
bson_append_document_begin(
|
||||
_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 {
|
||||
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()));
|
||||
bson_array_builder_append_utf8(
|
||||
_parent->val_, _var.c_str(), static_cast<int>(_var.size())
|
||||
);
|
||||
} 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.");
|
||||
|
@ -151,26 +165,32 @@ namespace rfl {
|
|||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
||||
bson_append_utf8(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), _var.c_str(),
|
||||
static_cast<int>(_var.size()));
|
||||
bson_append_utf8(
|
||||
_parent->val_, _name.data(), static_cast<int>(_name.size()),
|
||||
_var.c_str(), 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));
|
||||
bson_append_double(
|
||||
_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(_parent->val_, _name.data(),
|
||||
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);
|
||||
bson_append_int64(
|
||||
_parent->val_, _name.data(), 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 {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
@ -184,9 +204,11 @@ namespace rfl {
|
|||
|
||||
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()));
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
bson_append_null(
|
||||
_parent->val_, _name.data(), static_cast<int>(_name.size())
|
||||
);
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
||||
|
@ -194,8 +216,9 @@ 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>()) {
|
||||
|
|
|
@ -27,8 +27,9 @@ namespace rfl {
|
|||
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);
|
||||
|
@ -39,8 +40,9 @@ 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;
|
||||
}
|
||||
|
|
|
@ -12,14 +12,17 @@ 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,
|
||||
requires AreReaderAndWriter<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<cbor::Reader,
|
||||
struct Parser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
NamedTuple<FieldTypes...>,
|
||||
ProcessorsType>
|
||||
: public NamedTupleParser<cbor::Reader,
|
||||
: public NamedTupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
|
@ -29,7 +32,8 @@ namespace rfl {
|
|||
template <class ProcessorsType, class... Ts>
|
||||
requires AreReaderAndWriter<cbor::Reader, cbor::Writer, std::tuple<Ts...>>
|
||||
struct Parser<cbor::Reader, cbor::Writer, std::tuple<Ts...>, ProcessorsType>
|
||||
: public TupleParser<cbor::Reader,
|
||||
: public TupleParser<
|
||||
cbor::Reader,
|
||||
cbor::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
|
|
|
@ -47,7 +47,8 @@ namespace rfl {
|
|||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_obj.val_, &val);
|
||||
|
@ -113,23 +114,24 @@ namespace rfl {
|
|||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double.");
|
||||
"or double."
|
||||
);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
@ -139,7 +141,8 @@ namespace rfl {
|
|||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
const InputArrayType& _arr
|
||||
) const noexcept {
|
||||
CborValue val;
|
||||
auto buffer = std::vector<char>();
|
||||
auto err = cbor_value_enter_container(_arr.val_, &val);
|
||||
|
@ -165,7 +168,8 @@ namespace rfl {
|
|||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_map_length(_obj.val_, &length);
|
||||
if (err != CborNoError) { return Error(cbor_error_string(err)); }
|
||||
|
@ -190,23 +194,24 @@ 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) { return err; }
|
||||
_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 {
|
||||
|
|
|
@ -62,28 +62,32 @@ namespace rfl {
|
|||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
@ -98,7 +102,8 @@ namespace rfl {
|
|||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
@ -110,7 +115,8 @@ namespace rfl {
|
|||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType {};
|
||||
|
@ -125,23 +131,23 @@ 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>()) {
|
||||
|
|
|
@ -25,12 +25,13 @@ 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;
|
||||
|
|
|
@ -15,16 +15,21 @@ namespace rfl {
|
|||
namespace cbor {
|
||||
|
||||
template <class... Ps>
|
||||
void write_into_buffer(const auto& _obj,
|
||||
void write_into_buffer(
|
||||
const auto& _obj,
|
||||
CborEncoder* _encoder,
|
||||
std::vector<char>* _buffer) noexcept {
|
||||
std::vector<char>* _buffer
|
||||
) 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,7 +45,8 @@ namespace rfl {
|
|||
write_into_buffer<Ps...>(_obj, &encoder, &buffer);
|
||||
}
|
||||
const auto length = cbor_encoder_get_buffer_size(
|
||||
&encoder, reinterpret_cast<uint8_t*>(buffer.data()));
|
||||
&encoder, reinterpret_cast<uint8_t*>(buffer.data())
|
||||
);
|
||||
buffer.resize(length);
|
||||
return buffer;
|
||||
}
|
||||
|
|
|
@ -14,9 +14,10 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to be equal to " + std::to_string(threshold) +
|
||||
", but got " + std::to_string(_value) + "."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
@ -38,9 +39,11 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to be greater than or equal to " +
|
||||
std::to_string(threshold) + ", but got " + std::to_string(_value) +
|
||||
"."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
@ -62,9 +65,10 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to be greater than " + std::to_string(threshold) +
|
||||
", but got " + std::to_string(_value) + "."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
@ -76,8 +80,8 @@ namespace rfl {
|
|||
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}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -87,9 +91,11 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to be less than or equal to " +
|
||||
std::to_string(threshold) + ", but got " + std::to_string(_value) +
|
||||
"."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
@ -111,9 +117,10 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to be less than " + std::to_string(threshold) +
|
||||
", but got " + std::to_string(_value) + "."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
@ -125,8 +132,8 @@ namespace rfl {
|
|||
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}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -136,9 +143,10 @@ namespace rfl {
|
|||
static Result<T> validate(T _value) noexcept {
|
||||
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) + ".");
|
||||
return Error(
|
||||
"Value expected to not be equal to " + std::to_string(threshold) +
|
||||
", but got " + std::to_string(_value) + "."
|
||||
);
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
|
|
@ -8,9 +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
|
||||
|
||||
|
|
|
@ -14,15 +14,15 @@ 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
|
||||
|
|
|
@ -28,31 +28,32 @@ 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>,
|
||||
std::enable_if_t<std::is_invocable_r<
|
||||
rfl::Result<T>,
|
||||
decltype(T::from_flexbuf),
|
||||
InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
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 {
|
||||
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 {
|
||||
|
@ -89,7 +90,8 @@ namespace rfl {
|
|||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
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]));
|
||||
|
@ -101,36 +103,38 @@ namespace rfl {
|
|||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
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!"); }
|
||||
return _var.AsMap();
|
||||
}
|
||||
|
||||
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()); }
|
||||
|
|
|
@ -59,27 +59,31 @@ namespace rfl {
|
|||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
|
@ -93,7 +97,8 @@ namespace rfl {
|
|||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
|
@ -104,7 +109,8 @@ namespace rfl {
|
|||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
fbb_->Null(_name.data());
|
||||
return OutputVarType {};
|
||||
}
|
||||
|
@ -119,8 +125,8 @@ namespace rfl {
|
|||
|
||||
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>()) {
|
||||
|
@ -161,8 +167,8 @@ 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};
|
||||
}
|
||||
|
|
|
@ -21,8 +21,9 @@ 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();
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@ 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);
|
||||
|
@ -39,8 +39,8 @@ 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);
|
||||
|
|
|
@ -34,7 +34,8 @@ namespace rfl {
|
|||
template <internal::StringLiteral _field_name, class NamedTupleType>
|
||||
inline const auto& get(const NamedTupleType& _tup) {
|
||||
return internal::Getter<NamedTupleType>::template get_const<_field_name>(
|
||||
_tup);
|
||||
_tup
|
||||
);
|
||||
}
|
||||
|
||||
/// Gets a field by the field type.
|
||||
|
|
|
@ -40,12 +40,14 @@ namespace rfl::internal {
|
|||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
"well."
|
||||
);
|
||||
return Getter<NamedTupleType>::template get<index>(_tup);
|
||||
}
|
||||
|
||||
|
@ -69,12 +71,14 @@ namespace rfl::internal {
|
|||
constexpr auto index =
|
||||
find_index<Field::name_, typename NamedTupleType::Fields>();
|
||||
static_assert(
|
||||
std::is_same<typename std::tuple_element<
|
||||
std::is_same<
|
||||
typename std::tuple_element<
|
||||
index, typename NamedTupleType::Fields>::type::Type,
|
||||
typename Field::Type>(),
|
||||
"If two fields have the same name, "
|
||||
"their type must be the same as "
|
||||
"well.");
|
||||
"well."
|
||||
);
|
||||
return Getter<NamedTupleType>::template get_const<index>(_tup);
|
||||
}
|
||||
};
|
||||
|
@ -118,7 +122,8 @@ namespace rfl::internal {
|
|||
/// Retrieves the indicated value from the tuple.
|
||||
template <int _index>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
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);
|
||||
|
@ -129,7 +134,8 @@ namespace rfl::internal {
|
|||
/// Gets a field by name.
|
||||
template <StringLiteral _field_name>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
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);
|
||||
|
@ -140,7 +146,8 @@ namespace rfl::internal {
|
|||
/// Gets a field by the field type.
|
||||
template <class Field>
|
||||
static inline const auto& get_const(
|
||||
const std::variant<NamedTupleTypes...>& _tup) {
|
||||
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);
|
||||
|
|
|
@ -39,27 +39,31 @@ 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,
|
||||
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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
Skip(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Skip() = default;
|
||||
|
@ -89,18 +93,20 @@ 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;
|
||||
}
|
||||
|
||||
/// 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>
|
||||
template <
|
||||
class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>, bool>::
|
||||
type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
|
|
|
@ -34,15 +34,19 @@ 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; }
|
||||
return _first.string_view() == _second.string_view();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,9 +7,8 @@ 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...);
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -13,8 +13,10 @@ namespace rfl {
|
|||
namespace internal {
|
||||
|
||||
template <class FieldNames, class... Fields>
|
||||
auto copy_flattened_tuple_to_named_tuple(const auto& _flattened_tuple,
|
||||
Fields&&... _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)>>;
|
||||
constexpr auto i = sizeof...(_fields);
|
||||
|
@ -24,9 +26,11 @@ namespace rfl {
|
|||
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)>>>(
|
||||
std::get<i>(_flattened_tuple));
|
||||
std::get<i>(_flattened_tuple)
|
||||
);
|
||||
return copy_flattened_tuple_to_named_tuple<FieldNames>(
|
||||
_flattened_tuple, std::move(_fields)..., std::move(new_field));
|
||||
_flattened_tuple, std::move(_fields)..., std::move(new_field)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,13 +11,16 @@ namespace rfl {
|
|||
struct define_literal;
|
||||
|
||||
/// General case
|
||||
template <StringLiteral... _content1,
|
||||
template <
|
||||
StringLiteral... _content1,
|
||||
StringLiteral... _content2,
|
||||
class... Tail>
|
||||
struct define_literal<Literal<_content1...>,
|
||||
struct define_literal<
|
||||
Literal<_content1...>,
|
||||
Literal<_content2...>,
|
||||
Tail...> {
|
||||
using type = typename define_literal<Literal<_content1..., _content2...>,
|
||||
using type = typename define_literal<
|
||||
Literal<_content1..., _content2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
|
|
|
@ -20,7 +20,8 @@ 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>,
|
||||
using type = typename define_named_tuple<
|
||||
NamedTuple<TupContent..., Head>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
|
@ -28,11 +29,12 @@ namespace rfl {
|
|||
/// Recursive case - first type is NamedTuple, second type is also
|
||||
/// NamedTuple.
|
||||
template <class... TupContent, class... TupContent2, class... Tail>
|
||||
struct define_named_tuple<NamedTuple<TupContent...>,
|
||||
struct define_named_tuple<
|
||||
NamedTuple<TupContent...>,
|
||||
NamedTuple<TupContent2...>,
|
||||
Tail...> {
|
||||
using type =
|
||||
typename define_named_tuple<NamedTuple<TupContent..., TupContent2...>,
|
||||
using type = typename define_named_tuple<
|
||||
NamedTuple<TupContent..., TupContent2...>,
|
||||
Tail...>::type;
|
||||
};
|
||||
|
||||
|
|
|
@ -12,11 +12,13 @@ namespace rfl {
|
|||
struct define_tagged_union;
|
||||
|
||||
/// Recursive case - both tagged union.
|
||||
template <StringLiteral _discriminator,
|
||||
template <
|
||||
StringLiteral _discriminator,
|
||||
class... NamedTupleTypes1,
|
||||
class... NamedTupleTypes2,
|
||||
class... Tail>
|
||||
struct define_tagged_union<_discriminator,
|
||||
struct define_tagged_union<
|
||||
_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes1...>,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes2...>,
|
||||
Tail...> {
|
||||
|
@ -27,17 +29,20 @@ namespace rfl {
|
|||
};
|
||||
|
||||
/// Recursive case - tagged union plus named tuple.
|
||||
template <StringLiteral _discriminator,
|
||||
template <
|
||||
StringLiteral _discriminator,
|
||||
class... NamedTupleTypes,
|
||||
class... FieldTypes,
|
||||
class... Tail>
|
||||
struct define_tagged_union<_discriminator,
|
||||
struct define_tagged_union<
|
||||
_discriminator,
|
||||
TaggedUnion<_discriminator, NamedTupleTypes...>,
|
||||
NamedTuple<FieldTypes...>,
|
||||
Tail...> {
|
||||
using type =
|
||||
typename define_tagged_union<_discriminator,
|
||||
TaggedUnion<_discriminator,
|
||||
using type = typename define_tagged_union<
|
||||
_discriminator,
|
||||
TaggedUnion<
|
||||
_discriminator,
|
||||
NamedTupleTypes...,
|
||||
NamedTuple<FieldTypes...>>,
|
||||
Tail...>::type;
|
||||
|
@ -45,7 +50,8 @@ namespace rfl {
|
|||
|
||||
/// Recursive case - named tuple.
|
||||
template <StringLiteral _discriminator, class... FieldTypes, class... Tail>
|
||||
struct define_tagged_union<_discriminator,
|
||||
struct define_tagged_union<
|
||||
_discriminator,
|
||||
NamedTuple<FieldTypes...>,
|
||||
Tail...> {
|
||||
using type = typename define_tagged_union<
|
||||
|
|
|
@ -12,11 +12,13 @@ namespace rfl {
|
|||
|
||||
/// Recursive case - both variants.
|
||||
template <class... Vars1, class... Vars2, class... Tail>
|
||||
struct define_variant<std::variant<Vars1...>,
|
||||
struct define_variant<
|
||||
std::variant<Vars1...>,
|
||||
std::variant<Vars2...>,
|
||||
Tail...> {
|
||||
using type = typename define_variant<std::variant<Vars1..., Vars2...>,
|
||||
Tail...>::type;
|
||||
using type =
|
||||
typename define_variant<std::variant<Vars1..., Vars2...>, Tail...>::
|
||||
type;
|
||||
};
|
||||
|
||||
/// Recursive case - variant plus other type.
|
||||
|
|
|
@ -29,63 +29,86 @@ namespace rfl {
|
|||
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,
|
||||
using AddOneType = std::conditional_t<
|
||||
N == 0,
|
||||
Names<EnumType, NewLiteral, 1, _new_enum>,
|
||||
Names<EnumType,
|
||||
Names<
|
||||
EnumType,
|
||||
define_literal_t<LiteralType, NewLiteral>,
|
||||
N + 1,
|
||||
_enums...,
|
||||
_new_enum>>;
|
||||
};
|
||||
|
||||
template <class EnumType,
|
||||
template <
|
||||
class EnumType,
|
||||
size_t N,
|
||||
StringLiteral... _names,
|
||||
auto... _enums>
|
||||
auto names_to_enumerator_named_tuple(
|
||||
Names<EnumType, Literal<_names...>, N, _enums...>) {
|
||||
auto names_to_enumerator_named_tuple(Names<
|
||||
EnumType,
|
||||
Literal<_names...>,
|
||||
N,
|
||||
_enums...>) {
|
||||
return make_named_tuple(Field<_names, EnumType> {_enums}...);
|
||||
}
|
||||
|
||||
template <class EnumType,
|
||||
template <
|
||||
class EnumType,
|
||||
size_t N,
|
||||
StringLiteral... _names,
|
||||
auto... _enums>
|
||||
auto names_to_underlying_enumerator_named_tuple(
|
||||
Names<EnumType, Literal<_names...>, N, _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)}...);
|
||||
static_cast<std::underlying_type_t<EnumType>>(_enums)
|
||||
}...
|
||||
);
|
||||
}
|
||||
|
||||
template <class EnumType,
|
||||
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,
|
||||
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...>) {
|
||||
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))...};
|
||||
static_cast<std::underlying_type_t<EnumType>>(_enums)
|
||||
)...};
|
||||
}
|
||||
|
||||
} // namespace enums
|
||||
|
|
|
@ -43,7 +43,8 @@ namespace rfl {
|
|||
static_assert(
|
||||
names_.size != 0,
|
||||
"No enum could be identified. Please choose enum values "
|
||||
"between 0 to 127 or for flag enums choose 1,2,4,8,16,...");
|
||||
"between 0 to 127 or for flag enums choose 1,2,4,8,16,..."
|
||||
);
|
||||
if constexpr (is_flag_enum_) {
|
||||
return string_to_flag_enum(_str);
|
||||
} else {
|
||||
|
@ -63,7 +64,8 @@ namespace rfl {
|
|||
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));
|
||||
static_cast<EnumType>(static_cast<T>(1) << i)
|
||||
);
|
||||
flags.emplace_back(std::move(str));
|
||||
}
|
||||
++i;
|
||||
|
@ -80,14 +82,16 @@ 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))
|
||||
static_cast<typename NamesLiteral::ValueType>(i)
|
||||
)
|
||||
.transform(to_str)
|
||||
.value();
|
||||
}
|
||||
}
|
||||
|
||||
return std::to_string(
|
||||
static_cast<std::underlying_type_t<EnumType>>(_enum));
|
||||
static_cast<std::underlying_type_t<EnumType>>(_enum)
|
||||
);
|
||||
}
|
||||
|
||||
/// Finds the enum matching the literal.
|
||||
|
@ -111,8 +115,8 @@ 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);
|
||||
|
|
|
@ -63,7 +63,8 @@ namespace rfl {
|
|||
static_assert(
|
||||
false,
|
||||
"You are using an unsupported compiler. Please use GCC, Clang "
|
||||
"or MSVC or use rfl::Literal.");
|
||||
"or MSVC or use rfl::Literal."
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -105,7 +106,8 @@ namespace rfl {
|
|||
}
|
||||
}
|
||||
|
||||
template <class EnumType,
|
||||
template <
|
||||
class EnumType,
|
||||
class NamesType,
|
||||
auto _max,
|
||||
bool _is_flag,
|
||||
|
@ -121,8 +123,8 @@ 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<
|
||||
|
@ -131,8 +133,8 @@ namespace rfl {
|
|||
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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,10 +144,13 @@ namespace rfl {
|
|||
static_assert(
|
||||
is_scoped_enum<EnumType>,
|
||||
"You must use scoped enums (using class or struct) for the "
|
||||
"parsing to work!");
|
||||
"parsing to work!"
|
||||
);
|
||||
|
||||
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
|
||||
"The underlying type of any Enum must be integral!");
|
||||
static_assert(
|
||||
std::is_integral_v<std::underlying_type_t<EnumType>>,
|
||||
"The underlying type of any Enum must be integral!"
|
||||
);
|
||||
|
||||
constexpr auto max =
|
||||
get_max<std::underlying_type_t<EnumType>, _is_flag>();
|
||||
|
|
|
@ -35,7 +35,8 @@ namespace rfl {
|
|||
|
||||
/// For variants - in this case the FieldType returned by all options must
|
||||
/// be the same.
|
||||
template <StringLiteral _field_name,
|
||||
template <
|
||||
StringLiteral _field_name,
|
||||
class FirstAlternativeType,
|
||||
class... OtherAlternativeTypes>
|
||||
struct FieldType<
|
||||
|
@ -51,15 +52,17 @@ namespace rfl {
|
|||
};
|
||||
|
||||
/// For tagged union - just defers to the variant.
|
||||
template <StringLiteral _field_name,
|
||||
template <
|
||||
StringLiteral _field_name,
|
||||
StringLiteral _discriminator_name,
|
||||
class... VarTypes>
|
||||
struct FieldType<_field_name,
|
||||
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
|
||||
|
|
|
@ -10,9 +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
|
||||
|
|
|
@ -64,7 +64,8 @@ namespace rfl::internal {
|
|||
static_assert(
|
||||
false,
|
||||
"You are using an unsupported compiler. Please use GCC, Clang "
|
||||
"or MSVC or switch to the rfl::Field-syntax.");
|
||||
"or MSVC or switch to the rfl::Field-syntax."
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -99,8 +100,10 @@ namespace rfl::internal {
|
|||
}
|
||||
|
||||
template <StringLiteral... _names1, StringLiteral... _names2>
|
||||
auto concat_two_literals(const rfl::Literal<_names1...>& _lit1,
|
||||
const rfl::Literal<_names2...>& _lit2) {
|
||||
auto concat_two_literals(
|
||||
const rfl::Literal<_names1...>& _lit1,
|
||||
const rfl::Literal<_names2...>& _lit2
|
||||
) {
|
||||
return rfl::Literal<_names1..., _names2...>::template from_value<0>();
|
||||
}
|
||||
|
||||
|
@ -135,14 +138,16 @@ 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>())>()...);
|
||||
get_field_name<Type, std::get<Is>(bind_fake_object_to_tuple<T>())>(
|
||||
)...
|
||||
);
|
||||
};
|
||||
#endif
|
||||
return get(std::make_index_sequence<num_fields<T>>());
|
||||
|
|
|
@ -23,7 +23,8 @@ namespace rfl {
|
|||
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)));
|
||||
MetaField(std::move(name), std::move(type))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,14 +35,17 @@ namespace rfl {
|
|||
static_assert(
|
||||
false,
|
||||
"You are using an unsupported compiler. Please use GCC, Clang "
|
||||
"or MSVC or explicitly tag your structs using 'Tag' or 'Name'.");
|
||||
"or MSVC or explicitly tag your structs using 'Tag' or 'Name'."
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
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]...};
|
||||
|
|
|
@ -57,7 +57,8 @@ namespace rfl {
|
|||
"If some of your fields are annotated using rfl::Field<...>, "
|
||||
"then you must annotate all of your fields. "
|
||||
"Also, you cannot combine annotated and "
|
||||
"unannotated fields using rfl::Flatten<...>.");
|
||||
"unannotated fields using rfl::Flatten<...>."
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -20,11 +20,14 @@ 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_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::make_tuple(std::move(std::get<i>(_t)))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,23 +29,25 @@ namespace rfl {
|
|||
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());
|
||||
_n, _args..., SubPtrNamedTupleType(_n).fields()
|
||||
);
|
||||
|
||||
} else if constexpr (is_flatten_field<Field>::value) {
|
||||
using SubPtrFieldTupleType =
|
||||
std::remove_cvref_t<ptr_field_tuple_t<T>>;
|
||||
|
||||
return make_ptr_fields<PtrFieldTupleType>(
|
||||
_n, _args..., make_ptr_fields<SubPtrFieldTupleType>(_n));
|
||||
_n, _args..., make_ptr_fields<SubPtrFieldTupleType>(_n)
|
||||
);
|
||||
|
||||
} else {
|
||||
return make_ptr_fields<PtrFieldTupleType>(
|
||||
_n, _args..., _n.template get_field<Field::name_>());
|
||||
_n, _args..., _n.template get_field<Field::name_>()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +65,9 @@ namespace rfl {
|
|||
return move_from_ptr_fields<T>(
|
||||
_ptrs, std::move(_args)...,
|
||||
rfl::make_field<FieldType::name_>(
|
||||
std::move(*std::get<i>(_ptrs).value())));
|
||||
std::move(*std::get<i>(_ptrs).value())
|
||||
)
|
||||
);
|
||||
|
||||
} else {
|
||||
using PtrFieldTupleType = std::remove_cvref_t<ptr_field_tuple_t<T>>;
|
||||
|
@ -73,7 +77,8 @@ namespace rfl {
|
|||
|
||||
return move_from_ptr_fields<T>(
|
||||
_ptrs, std::move(_args)...,
|
||||
move_from_ptr_fields<U>(std::get<i>(_ptrs)));
|
||||
move_from_ptr_fields<U>(std::get<i>(_ptrs))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +92,8 @@ 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>,
|
||||
} else if constexpr (std::is_same<
|
||||
std::remove_cvref_t<NamedTupleType>,
|
||||
RequiredType>()) {
|
||||
auto ptr_named_tuple = nt_to_ptr_named_tuple(_n);
|
||||
|
||||
|
@ -98,8 +104,8 @@ 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)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,8 @@ namespace rfl {
|
|||
}
|
||||
}
|
||||
|
||||
template <class TargetTupleType,
|
||||
template <
|
||||
class TargetTupleType,
|
||||
class PtrTupleType,
|
||||
int _j = 0,
|
||||
class... Args>
|
||||
|
@ -52,14 +53,16 @@ namespace rfl {
|
|||
constexpr int flattened_size =
|
||||
calc_flattened_size<SubTargetTupleType>();
|
||||
|
||||
return unflatten_ptr_tuple<TargetTupleType, PtrTupleType,
|
||||
_j + flattened_size>(
|
||||
return unflatten_ptr_tuple<
|
||||
TargetTupleType, PtrTupleType, _j + flattened_size>(
|
||||
_t, _args...,
|
||||
unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t));
|
||||
unflatten_ptr_tuple<SubTargetTupleType, PtrTupleType, _j>(_t)
|
||||
);
|
||||
|
||||
} else {
|
||||
return unflatten_ptr_tuple<TargetTupleType, PtrTupleType, _j + 1>(
|
||||
_t, _args..., std::get<_j>(_t));
|
||||
_t, _args..., std::get<_j>(_t)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,8 +77,9 @@ namespace rfl {
|
|||
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>>;
|
||||
|
@ -85,7 +89,8 @@ namespace rfl {
|
|||
|
||||
return move_from_pointers<T>(
|
||||
_ptrs, std::move(_args)...,
|
||||
move_from_pointers<U>(std::get<i>(_ptrs)));
|
||||
move_from_pointers<U>(std::get<i>(_ptrs))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,16 +24,16 @@ 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_, T*>(&std::get<i>(_nt.values())));
|
||||
_nt, _a..., Field<FieldType::name_, T*>(&std::get<i>(_nt.values()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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);
|
||||
|
@ -46,7 +46,8 @@ namespace rfl {
|
|||
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())));
|
||||
Field<FieldType::name_, const T*>(&std::get<i>(_nt.values()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -53,11 +53,7 @@ namespace rfl {
|
|||
struct any_empty_base {
|
||||
any_empty_base(std::size_t);
|
||||
template <class Base>
|
||||
requires(std::is_empty_v<std::remove_cvref_t<Base>> &&
|
||||
std::is_base_of_v<std::remove_cvref_t<Base>,
|
||||
std::remove_cv_t<Derived>> &&
|
||||
!std::is_same_v<std::remove_cvref_t<Base>,
|
||||
std::remove_cv_t<Derived>>)
|
||||
requires(std::is_empty_v<std::remove_cvref_t<Base>> && std::is_base_of_v<std::remove_cvref_t<Base>, std::remove_cv_t<Derived>> && !std::is_same_v<std::remove_cvref_t<Base>, std::remove_cv_t<Derived>>)
|
||||
constexpr operator Base&() const noexcept;
|
||||
};
|
||||
|
||||
|
@ -65,10 +61,7 @@ namespace rfl {
|
|||
struct any_base {
|
||||
any_base(std::size_t);
|
||||
template <class Base>
|
||||
requires(std::is_base_of_v<std::remove_cvref_t<Base>,
|
||||
std::remove_cv_t<Derived>> &&
|
||||
!std::is_same_v<std::remove_cvref_t<Base>,
|
||||
std::remove_cv_t<Derived>>)
|
||||
requires(std::is_base_of_v<std::remove_cvref_t<Base>, std::remove_cv_t<Derived>> && !std::is_same_v<std::remove_cvref_t<Base>, std::remove_cv_t<Derived>>)
|
||||
constexpr operator Base&() const noexcept;
|
||||
};
|
||||
|
||||
|
@ -89,9 +82,10 @@ 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... j, std::size_t... k>(
|
||||
std::index_sequence<i...>, std::index_sequence<j...>,
|
||||
std::index_sequence<k...>) {
|
||||
return
|
||||
[]<std::size_t... i, std::size_t... j,
|
||||
std::
|
||||
size_t... k>(std::index_sequence<i...>, std::index_sequence<j...>, std::index_sequence<k...>) {
|
||||
return requires { T {any(i)..., {any(j)...}, any(k)...}; };
|
||||
}(std::make_index_sequence<l>(), std::make_index_sequence<nested>(),
|
||||
std::make_index_sequence<r>());
|
||||
|
@ -112,8 +106,8 @@ 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>();
|
||||
|
@ -123,17 +117,20 @@ namespace rfl {
|
|||
template <std::size_t max_args, std::size_t index = 0>
|
||||
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...>) {
|
||||
constexpr auto check =
|
||||
[]<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)...};
|
||||
};
|
||||
};
|
||||
|
||||
if constexpr (check(std::make_index_sequence<index>(),
|
||||
std::make_index_sequence<max_args - index - 1>())) {
|
||||
if constexpr (check(
|
||||
std::make_index_sequence<index>(),
|
||||
std::make_index_sequence<max_args - index - 1>()
|
||||
)) {
|
||||
return index;
|
||||
} else {
|
||||
return find_the_sole_non_empty_base_index<max_args, index + 1>();
|
||||
|
@ -154,10 +151,13 @@ 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>
|
||||
|
|
|
@ -15,8 +15,8 @@ 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;
|
||||
};
|
||||
|
||||
|
|
|
@ -14,17 +14,20 @@ namespace rfl {
|
|||
|
||||
/// Recursively builds a new NamedTuple type from the FieldTypes, leaving
|
||||
/// out the field signified by _name.
|
||||
template <class _OldNamedTupleType,
|
||||
template <
|
||||
class _OldNamedTupleType,
|
||||
StringLiteral _name,
|
||||
class _NewNamedTupleType,
|
||||
int _i>
|
||||
struct remove_single_field;
|
||||
|
||||
/// Special case - _i == 0
|
||||
template <class _OldNamedTupleType,
|
||||
template <
|
||||
class _OldNamedTupleType,
|
||||
StringLiteral _name,
|
||||
class _NewNamedTupleType>
|
||||
struct remove_single_field<_OldNamedTupleType,
|
||||
struct remove_single_field<
|
||||
_OldNamedTupleType,
|
||||
_name,
|
||||
_NewNamedTupleType,
|
||||
0> {
|
||||
|
@ -32,7 +35,8 @@ namespace rfl {
|
|||
};
|
||||
|
||||
/// General case.
|
||||
template <class _OldNamedTupleType,
|
||||
template <
|
||||
class _OldNamedTupleType,
|
||||
StringLiteral _name,
|
||||
class _NewNamedTupleType,
|
||||
int _i>
|
||||
|
@ -51,7 +55,8 @@ namespace rfl {
|
|||
_NewNamedTupleType,
|
||||
define_named_tuple_t<_NewNamedTupleType, FieldType>>;
|
||||
|
||||
using type = typename remove_single_field<OldNamedTupleType,
|
||||
using type = typename remove_single_field<
|
||||
OldNamedTupleType,
|
||||
_name,
|
||||
NewNamedTupleType,
|
||||
_i - 1>::type;
|
||||
|
@ -59,7 +64,8 @@ namespace rfl {
|
|||
|
||||
/// Recursively removes all of the fields signified by _head and _tail from
|
||||
/// the NamedTupleType.
|
||||
template <class _NamedTupleType,
|
||||
template <
|
||||
class _NamedTupleType,
|
||||
StringLiteral _head,
|
||||
StringLiteral... _tail>
|
||||
struct remove_fields;
|
||||
|
@ -72,14 +78,16 @@ namespace rfl {
|
|||
constexpr static int num_fields =
|
||||
std::tuple_size_v<typename NamedTupleType::Fields>;
|
||||
|
||||
using type = typename remove_single_field<NamedTupleType,
|
||||
using type = typename remove_single_field<
|
||||
NamedTupleType,
|
||||
_head,
|
||||
NamedTuple<>,
|
||||
num_fields>::type;
|
||||
};
|
||||
|
||||
/// General case.
|
||||
template <class _NamedTupleType,
|
||||
template <
|
||||
class _NamedTupleType,
|
||||
StringLiteral _head,
|
||||
StringLiteral... _tail>
|
||||
struct remove_fields {
|
||||
|
@ -88,7 +96,8 @@ namespace rfl {
|
|||
constexpr static int num_fields =
|
||||
std::tuple_size_v<typename NamedTupleType::Fields>;
|
||||
|
||||
using NewNamedTupleType = typename remove_single_field<NamedTupleType,
|
||||
using NewNamedTupleType = typename remove_single_field<
|
||||
NamedTupleType,
|
||||
_head,
|
||||
NamedTuple<>,
|
||||
num_fields>::type;
|
||||
|
|
|
@ -9,8 +9,10 @@ namespace rfl {
|
|||
namespace strings {
|
||||
|
||||
/// Joins a string using the delimiter
|
||||
inline std::string join(const std::string& _delimiter,
|
||||
const std::vector<std::string>& _strings) {
|
||||
inline std::string join(
|
||||
const std::string& _delimiter,
|
||||
const std::vector<std::string>& _strings
|
||||
) {
|
||||
if (_strings.size() == 0) { return ""; }
|
||||
auto res = _strings[0];
|
||||
for (size_t i = 1; i < _strings.size(); ++i) {
|
||||
|
|
|
@ -8,9 +8,11 @@ namespace rfl {
|
|||
namespace internal {
|
||||
namespace strings {
|
||||
|
||||
inline std::string replace_all(const std::string& _str,
|
||||
inline std::string replace_all(
|
||||
const std::string& _str,
|
||||
const std::string& _from,
|
||||
const std::string& _to) {
|
||||
const std::string& _to
|
||||
) {
|
||||
auto str = _str;
|
||||
|
||||
size_t pos = 0;
|
||||
|
|
|
@ -9,8 +9,8 @@ namespace rfl {
|
|||
namespace strings {
|
||||
|
||||
/// Splits a string alongside the delimiter
|
||||
inline std::vector<std::string> split(const std::string& _str,
|
||||
const std::string& _delimiter) {
|
||||
inline std::vector<std::string>
|
||||
split(const std::string& _str, const std::string& _delimiter) {
|
||||
auto str = _str;
|
||||
size_t pos = 0;
|
||||
std::vector<std::string> result;
|
||||
|
|
|
@ -9,9 +9,8 @@
|
|||
namespace rfl::internal {
|
||||
|
||||
template <internal::StringLiteral _discriminator, class T>
|
||||
using tag_t =
|
||||
typename std::invoke_result<decltype(make_tag<_discriminator, T>),
|
||||
T>::type;
|
||||
using tag_t = typename std::
|
||||
invoke_result<decltype(make_tag<_discriminator, T>), T>::type;
|
||||
|
||||
} // namespace rfl::internal
|
||||
|
||||
|
|
|
@ -23,11 +23,13 @@ namespace rfl {
|
|||
if constexpr (is_flatten_field_v<T>) {
|
||||
return flatten_ptr_tuple(
|
||||
std::forward<PtrTuple>(_t), std::forward<Args>(_args)...,
|
||||
flatten_ptr_tuple(to_ptr_tuple(std::get<i>(_t)->get())));
|
||||
flatten_ptr_tuple(to_ptr_tuple(std::get<i>(_t)->get()))
|
||||
);
|
||||
} else {
|
||||
return flatten_ptr_tuple(std::forward<PtrTuple>(_t),
|
||||
std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
return flatten_ptr_tuple(
|
||||
std::forward<PtrTuple>(_t), std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,11 +29,14 @@ namespace rfl {
|
|||
using T = std::tuple_element_t<i, std::remove_cvref_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
auto subtuple = internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple));
|
||||
return flatten_ptr_field_tuple(
|
||||
_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple)
|
||||
);
|
||||
} else {
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
return flatten_ptr_field_tuple(
|
||||
_t, std::forward<Args>(_args)..., std::make_tuple(std::get<i>(_t))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +74,8 @@ namespace rfl {
|
|||
using FieldNames = rfl::field_names_t<T>;
|
||||
auto flattened_ptr_tuple = to_flattened_ptr_tuple(_t);
|
||||
return copy_flattened_tuple_to_named_tuple<FieldNames>(
|
||||
flattened_ptr_tuple);
|
||||
flattened_ptr_tuple
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,8 @@ namespace rfl::internal {
|
|||
constexpr size_t n = StdArrayType<Type>::size;
|
||||
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
|
||||
return to_std_array_t<Type>({to_std_array(
|
||||
std::forward<typename StdArrayType<Type>::ValueType>(_t[_i]))...});
|
||||
std::forward<typename StdArrayType<Type>::ValueType>(_t[_i])
|
||||
)...});
|
||||
};
|
||||
return fct(std::make_index_sequence<n>());
|
||||
} else {
|
||||
|
|
|
@ -16,7 +16,8 @@ namespace rfl::internal {
|
|||
}
|
||||
|
||||
/// Transforms the field name from snake case to camel case.
|
||||
template <internal::StringLiteral _name,
|
||||
template <
|
||||
internal::StringLiteral _name,
|
||||
bool _capitalize,
|
||||
size_t _i = 0,
|
||||
char... chars>
|
||||
|
@ -28,11 +29,11 @@ namespace rfl::internal {
|
|||
} else if constexpr (_name.arr_[_i] == '\0') {
|
||||
return transform_snake_case<_name, false, _name.arr_.size(), chars...>();
|
||||
} else if constexpr (_capitalize) {
|
||||
return transform_snake_case<_name, false, _i + 1, chars...,
|
||||
to_upper<_name.arr_[_i]>()>();
|
||||
return transform_snake_case<
|
||||
_name, false, _i + 1, chars..., to_upper<_name.arr_[_i]>()>();
|
||||
} else {
|
||||
return transform_snake_case<_name, false, _i + 1, chars...,
|
||||
_name.arr_[_i]>();
|
||||
return transform_snake_case<
|
||||
_name, false, _i + 1, chars..., _name.arr_[_i]>();
|
||||
}
|
||||
}
|
||||
} // namespace rfl::internal
|
||||
|
|
|
@ -28,14 +28,17 @@ namespace rfl {
|
|||
constexpr auto n_skip = std::tuple_size_v<
|
||||
std::remove_cvref_t<flattened_ptr_tuple_t<typename Type::Type>>>;
|
||||
return wrap_in_fields<FieldNames, j + n_skip>(
|
||||
std::move(_tuple), std::move(_fields)..., std::move(value));
|
||||
std::move(_tuple), std::move(_fields)..., std::move(value)
|
||||
);
|
||||
} else {
|
||||
const auto name_literal = FieldNames::template name_of<j>();
|
||||
auto new_field = rfl::make_field<
|
||||
lit_name_v<std::remove_cvref_t<decltype(name_literal)>>>(
|
||||
std::move(value));
|
||||
std::move(value)
|
||||
);
|
||||
return wrap_in_fields<FieldNames, j + 1>(
|
||||
std::move(_tuple), std::move(_fields)..., std::move(new_field));
|
||||
std::move(_tuple), std::move(_fields)..., std::move(new_field)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,13 +13,16 @@ namespace rfl {
|
|||
inline Result<std::string> load_string(const std::string& _fname) {
|
||||
std::ifstream infile(_fname);
|
||||
if (infile.is_open()) {
|
||||
auto r = std::string(std::istreambuf_iterator<char>(infile),
|
||||
std::istreambuf_iterator<char>());
|
||||
auto r = std::string(
|
||||
std::istreambuf_iterator<char>(infile),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
infile.close();
|
||||
return r;
|
||||
} else {
|
||||
return Error("Unable to open file '" + _fname +
|
||||
"' or file could not be found.");
|
||||
return Error(
|
||||
"Unable to open file '" + _fname + "' or file could not be found."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,9 +12,11 @@ namespace rfl {
|
|||
namespace io {
|
||||
|
||||
template <class T, class WriteFunction>
|
||||
Result<Nothing> save_bytes(const std::string& _fname,
|
||||
Result<Nothing> save_bytes(
|
||||
const std::string& _fname,
|
||||
const T& _obj,
|
||||
const WriteFunction& _write) {
|
||||
const WriteFunction& _write
|
||||
) {
|
||||
try {
|
||||
std::ofstream output(_fname, std::ios::out | std::ios::binary);
|
||||
_write(_obj, output);
|
||||
|
|
|
@ -11,9 +11,11 @@ namespace rfl {
|
|||
namespace io {
|
||||
|
||||
template <class T, class WriteFunction>
|
||||
Result<Nothing> save_string(const std::string& _fname,
|
||||
Result<Nothing> save_string(
|
||||
const std::string& _fname,
|
||||
const T& _obj,
|
||||
const WriteFunction& _write) {
|
||||
const WriteFunction& _write
|
||||
) {
|
||||
try {
|
||||
std::ofstream outfile;
|
||||
outfile.open(_fname);
|
||||
|
|
|
@ -48,7 +48,8 @@ namespace rfl {
|
|||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType _obj) const noexcept {
|
||||
const InputObjectType _obj
|
||||
) const noexcept {
|
||||
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
|
@ -63,7 +64,8 @@ namespace rfl {
|
|||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
const InputArrayType& _arr
|
||||
) const noexcept {
|
||||
yyjson_val* val;
|
||||
yyjson_arr_iter iter;
|
||||
yyjson_arr_iter_init(_arr.val_, &iter);
|
||||
|
@ -77,7 +79,8 @@ namespace rfl {
|
|||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
yyjson_obj_iter iter;
|
||||
yyjson_obj_iter_init(_obj.val_, &iter);
|
||||
yyjson_val* key;
|
||||
|
@ -119,16 +122,16 @@ namespace rfl {
|
|||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType _var) const noexcept {
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var
|
||||
) const noexcept {
|
||||
if (!yyjson_is_arr(_var.val_)) {
|
||||
return rfl::Error("Could not cast to 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 (!yyjson_is_obj(_var.val_)) {
|
||||
return rfl::Error("Could not cast to object!");
|
||||
}
|
||||
|
@ -136,8 +139,8 @@ 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_json_obj(_var);
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
|
|
|
@ -74,7 +74,8 @@ namespace rfl {
|
|||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, arr);
|
||||
return OutputArrayType(arr);
|
||||
|
@ -83,16 +84,19 @@ namespace rfl {
|
|||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
arr);
|
||||
yyjson_mut_obj_add(
|
||||
_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), arr
|
||||
);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, obj);
|
||||
return OutputObjectType(obj);
|
||||
|
@ -101,10 +105,12 @@ namespace rfl {
|
|||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
obj);
|
||||
yyjson_mut_obj_add(
|
||||
_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), obj
|
||||
);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
|
@ -120,10 +126,12 @@ namespace rfl {
|
|||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
val.val_);
|
||||
yyjson_mut_obj_add(
|
||||
_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), val.val_
|
||||
);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
|
@ -135,10 +143,12 @@ namespace rfl {
|
|||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
null);
|
||||
yyjson_mut_obj_add(
|
||||
_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), null
|
||||
);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
|
@ -154,14 +164,15 @@ namespace rfl {
|
|||
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var))
|
||||
);
|
||||
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_uint(doc_, static_cast<uint64_t>(_var)));
|
||||
yyjson_mut_uint(doc_, static_cast<uint64_t>(_var))
|
||||
);
|
||||
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_int(doc_, static_cast<int64_t>(_var)));
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int64_t>(_var))
|
||||
);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
|
|
|
@ -25,8 +25,8 @@ namespace rfl {
|
|||
|
||||
/// Parses an object from JSON using reflection.
|
||||
template <class T, class... Ps>
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(
|
||||
const std::string& _json_str) {
|
||||
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _json_str
|
||||
) {
|
||||
yyjson_doc* doc = yyjson_read(_json_str.c_str(), _json_str.size(), 0);
|
||||
if (!doc) { return Error("Could not parse document"); }
|
||||
yyjson_val* root = yyjson_doc_get_root(doc);
|
||||
|
@ -39,8 +39,10 @@ namespace rfl {
|
|||
/// Parses an object from a stringstream.
|
||||
template <class T, class... Ps>
|
||||
auto read(std::istream& _stream) {
|
||||
const auto json_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
const auto json_str = std::string(
|
||||
std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
return read<T, Ps...>(json_str);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,11 +14,13 @@ namespace rfl {
|
|||
namespace json {
|
||||
|
||||
template <class... Ps>
|
||||
Result<Nothing> save(const std::string& _fname,
|
||||
Result<Nothing> save(
|
||||
const std::string& _fname,
|
||||
const auto& _obj,
|
||||
const yyjson_write_flag _flag = 0) {
|
||||
const auto write_func = [_flag](const auto& _obj,
|
||||
auto& _stream) -> auto& {
|
||||
const yyjson_write_flag _flag = 0
|
||||
) {
|
||||
const auto write_func =
|
||||
[_flag](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write<Ps...>(_obj, _stream, _flag);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
|
|
|
@ -131,7 +131,8 @@ namespace rfl::json::schema {
|
|||
rfl::Ref<Type> items;
|
||||
};
|
||||
|
||||
using ReflectionType = std::variant<AllOf,
|
||||
using ReflectionType = std::variant<
|
||||
AllOf,
|
||||
AnyOf,
|
||||
Boolean,
|
||||
ExclusiveMaximum,
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
namespace rfl::json {
|
||||
|
||||
inline schema::Type type_to_json_schema_type(
|
||||
const parsing::schema::Type& _type);
|
||||
const parsing::schema::Type& _type
|
||||
);
|
||||
|
||||
inline bool is_optional(const parsing::schema::Type& _t) {
|
||||
const auto handle = [](const auto& _v) -> bool {
|
||||
|
@ -31,8 +32,8 @@ namespace rfl::json {
|
|||
return std::visit(handle, _t.variant_);
|
||||
}
|
||||
|
||||
inline std::string numeric_type_to_string(
|
||||
const parsing::schema::Type& _type) {
|
||||
inline std::string numeric_type_to_string(const parsing::schema::Type& _type
|
||||
) {
|
||||
const auto handle_variant = [](const auto& _t) -> std::string {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
|
@ -51,7 +52,8 @@ namespace rfl::json {
|
|||
|
||||
inline schema::Type handle_validation_type(
|
||||
const parsing::schema::Type& _type,
|
||||
const parsing::schema::ValidationType& _validation_type) {
|
||||
const parsing::schema::ValidationType& _validation_type
|
||||
) {
|
||||
const auto handle_variant = [&](const auto& _v) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
|
@ -77,55 +79,84 @@ namespace rfl::json {
|
|||
return schema::Type {.value = schema::Type::OneOf {.oneOf = one_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
|
||||
return schema::Type {.value =
|
||||
schema::Type::Regex {.pattern = _v.pattern_}};
|
||||
return schema::Type {
|
||||
.value = schema::Type::Regex {.pattern = _v.pattern_}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
|
||||
return type_to_json_schema_type(_type);
|
||||
|
||||
} else if constexpr (std::is_same<T,
|
||||
ValidationType::ExclusiveMaximum>()) {
|
||||
return schema::Type {.value = schema::Type::ExclusiveMaximum {
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>(
|
||||
)) {
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::ExclusiveMaximum {
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
.type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T,
|
||||
ValidationType::ExclusiveMinimum>()) {
|
||||
return schema::Type {.value = schema::Type::ExclusiveMinimum {
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>(
|
||||
)) {
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::ExclusiveMinimum {
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
.type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
|
||||
return schema::Type {
|
||||
.value = schema::Type::Maximum {
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
.value =
|
||||
schema::Type::Maximum {
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
|
||||
return schema::Type {
|
||||
.value = schema::Type::Minimum {
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
.value =
|
||||
schema::Type::Minimum {
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
|
||||
const auto maximum = schema::Type {
|
||||
.value = schema::Type::Maximum {
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
.value =
|
||||
schema::Type::Maximum {
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
const auto minimum = schema::Type {
|
||||
.value = schema::Type::Minimum {
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
.value =
|
||||
schema::Type::Minimum {
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
return schema::Type {
|
||||
.value = schema::Type::AllOf {.allOf = {maximum, minimum}}};
|
||||
.value = schema::Type::AllOf {.allOf = {maximum, minimum}}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
|
||||
const auto excl_maximum =
|
||||
schema::Type {.value = schema::Type::ExclusiveMaximum {
|
||||
const auto excl_maximum = schema::Type {
|
||||
.value =
|
||||
schema::Type::ExclusiveMaximum {
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
const auto excl_minimum =
|
||||
schema::Type {.value = schema::Type::ExclusiveMinimum {
|
||||
.type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
const auto excl_minimum = schema::Type {
|
||||
.value =
|
||||
schema::Type::ExclusiveMinimum {
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
return schema::Type {.value = schema::Type::AnyOf {
|
||||
.anyOf = {excl_maximum, excl_minimum}}};
|
||||
.type = numeric_type_to_string(_type)
|
||||
}
|
||||
};
|
||||
return schema::Type {
|
||||
.value = schema::Type::AnyOf {.anyOf = {excl_maximum, excl_minimum}}
|
||||
};
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
|
@ -136,7 +167,8 @@ namespace rfl::json {
|
|||
}
|
||||
|
||||
inline schema::Type type_to_json_schema_type(
|
||||
const parsing::schema::Type& _type) {
|
||||
const parsing::schema::Type& _type
|
||||
) {
|
||||
const auto handle_variant = [](const auto& _t) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
|
@ -173,15 +205,20 @@ namespace rfl::json {
|
|||
return std::visit(update_prediction, res.value);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
|
||||
return schema::Type {.value = schema::Type::FixedSizeTypedArray {
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::FixedSizeTypedArray {
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_)),
|
||||
.minContains = _t.size_,
|
||||
.maxContains = _t.size_}};
|
||||
type_to_json_schema_type(*_t.type_)
|
||||
), .minContains = _t.size_,
|
||||
.maxContains = _t.size_
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type {
|
||||
.value = schema::Type::StringEnum {.values = _t.values_}};
|
||||
.value = schema::Type::StringEnum {.values = _t.values_}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto properties = std::map<std::string, schema::Type>();
|
||||
|
@ -190,38 +227,57 @@ namespace rfl::json {
|
|||
properties[k] = type_to_json_schema_type(v);
|
||||
if (!is_optional(v)) { required.push_back(k); }
|
||||
}
|
||||
return schema::Type {.value =
|
||||
schema::Type::Object {.properties = properties,
|
||||
.required = required}};
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::Object {
|
||||
.properties = properties, .required = required
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Optional>()) {
|
||||
return schema::Type {
|
||||
.value = schema::Type::AnyOf {
|
||||
.anyOf = {type_to_json_schema_type(*_t.type_),
|
||||
schema::Type {schema::Type::Null {}}}}};
|
||||
.value =
|
||||
schema::Type::AnyOf {
|
||||
.anyOf =
|
||||
{type_to_json_schema_type(*_t.type_),
|
||||
schema::Type {schema::Type::Null {}}}
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Reference>()) {
|
||||
return schema::Type {.value = schema::Type::Reference {
|
||||
.ref = "#/definitions/" + _t.name_}};
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::Reference {.ref = "#/definitions/" + _t.name_}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::StringMap>()) {
|
||||
return schema::Type {
|
||||
.value = schema::Type::StringMap {
|
||||
.value =
|
||||
schema::Type::StringMap {
|
||||
.additionalProperties = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.value_type_))}};
|
||||
type_to_json_schema_type(*_t.value_type_)
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Tuple>()) {
|
||||
auto items = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
items.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type {.value =
|
||||
schema::Type::Tuple {.prefixItems = items}};
|
||||
return schema::Type {
|
||||
.value = schema::Type::Tuple {.prefixItems = items}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
|
||||
return schema::Type {.value = schema::Type::TypedArray {
|
||||
return schema::Type {
|
||||
.value =
|
||||
schema::Type::TypedArray {
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_))}};
|
||||
type_to_json_schema_type(*_t.type_)
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Validated>()) {
|
||||
return handle_validation_type(*_t.type_, _t.validation_);
|
||||
|
@ -255,8 +311,9 @@ namespace rfl::json {
|
|||
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
|
||||
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
|
||||
using U = std::decay_t<decltype(_root)>;
|
||||
return schema::JSONSchema<U> {.root = std::move(_root),
|
||||
.definitions = definitions};
|
||||
return schema::JSONSchema<U> {
|
||||
.root = std::move(_root), .definitions = definitions
|
||||
};
|
||||
};
|
||||
auto root = type_to_json_schema_type(internal_schema.root_);
|
||||
const auto json_schema = std::visit(to_schema, std::move(root.value));
|
||||
|
|
|
@ -22,8 +22,9 @@ namespace rfl {
|
|||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj,
|
||||
typename ParentType::Root {});
|
||||
Parser<T, Processors<Ps...>>::write(
|
||||
w, _obj, typename ParentType::Root {}
|
||||
);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, _flag, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
|
@ -33,14 +34,17 @@ namespace rfl {
|
|||
|
||||
/// Writes a JSON into an ostream.
|
||||
template <class... Ps>
|
||||
std::ostream& write(const auto& _obj,
|
||||
std::ostream& write(
|
||||
const auto& _obj,
|
||||
std::ostream& _stream,
|
||||
const yyjson_write_flag _flag = 0) {
|
||||
const yyjson_write_flag _flag = 0
|
||||
) {
|
||||
using T = std::remove_cvref_t<decltype(_obj)>;
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
Parser<T, Processors<Ps...>>::write(w, _obj,
|
||||
typename ParentType::Root {});
|
||||
Parser<T, Processors<Ps...>>::write(
|
||||
w, _obj, typename ParentType::Root {}
|
||||
);
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, _flag, NULL);
|
||||
_stream << json_c_str;
|
||||
free((void*)json_c_str);
|
||||
|
|
|
@ -12,7 +12,8 @@ namespace rfl {
|
|||
template <class... FieldTypes>
|
||||
inline auto make_named_tuple(FieldTypes&&... _args) {
|
||||
return NamedTuple<std::remove_cvref_t<FieldTypes>...>(
|
||||
std::forward<FieldTypes>(_args)...);
|
||||
std::forward<FieldTypes>(_args)...
|
||||
);
|
||||
}
|
||||
|
||||
/// Convenience constructor that doesn't require you
|
||||
|
|
|
@ -12,14 +12,17 @@ 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<msgpack::Reader,
|
||||
requires AreReaderAndWriter<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
NamedTuple<FieldTypes...>>
|
||||
struct Parser<msgpack::Reader,
|
||||
struct Parser<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
NamedTuple<FieldTypes...>,
|
||||
ProcessorsType>
|
||||
: public NamedTupleParser<msgpack::Reader,
|
||||
: public NamedTupleParser<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
|
@ -27,14 +30,17 @@ namespace rfl {
|
|||
FieldTypes...> {};
|
||||
|
||||
template <class ProcessorsType, class... Ts>
|
||||
requires AreReaderAndWriter<msgpack::Reader,
|
||||
requires AreReaderAndWriter<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
std::tuple<Ts...>>
|
||||
struct Parser<msgpack::Reader,
|
||||
struct Parser<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
std::tuple<Ts...>,
|
||||
ProcessorsType>
|
||||
: public TupleParser<msgpack::Reader,
|
||||
: public TupleParser<
|
||||
msgpack::Reader,
|
||||
msgpack::Writer,
|
||||
/*_ignore_empty_containers=*/false,
|
||||
/*_all_required=*/true,
|
||||
|
|
|
@ -34,12 +34,14 @@ namespace rfl {
|
|||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
return Error(
|
||||
"Key in element " + std::to_string(i) + " was not a string."
|
||||
);
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
|
@ -79,22 +81,23 @@ namespace rfl {
|
|||
return rfl::Error(
|
||||
"Could not cast to numeric value. The type must be integral, "
|
||||
"float "
|
||||
"or double.");
|
||||
"or double."
|
||||
);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var
|
||||
) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
rfl::Result<InputObjectType> to_object(const InputVarType& _var
|
||||
) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
|
@ -104,7 +107,8 @@ namespace rfl {
|
|||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(
|
||||
const ArrayReader& _array_reader,
|
||||
const InputArrayType& _arr) const noexcept {
|
||||
const InputArrayType& _arr
|
||||
) const noexcept {
|
||||
for (uint32_t i = 0; i < _arr.size; ++i) {
|
||||
const auto err = _array_reader.read(_arr.ptr[i]);
|
||||
if (err) { return err; }
|
||||
|
@ -115,13 +119,15 @@ namespace rfl {
|
|||
template <class ObjectReader>
|
||||
std::optional<Error> read_object(
|
||||
const ObjectReader& _object_reader,
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
const InputObjectType& _obj
|
||||
) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
const auto& val = _obj.ptr[i].val;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
return Error(
|
||||
"Key in element " + std::to_string(i) + " was not a string."
|
||||
);
|
||||
}
|
||||
const auto name = std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
_object_reader.read(name, val);
|
||||
|
@ -130,8 +136,8 @@ 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_msgpack_obj(_var);
|
||||
} catch (std::exception& e) { return rfl::Error(e.what()); }
|
||||
|
|
|
@ -55,14 +55,16 @@ namespace rfl::msgpack {
|
|||
|
||||
OutputArrayType add_array_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
|
@ -70,22 +72,24 @@ namespace rfl::msgpack {
|
|||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
OutputArrayType* _parent
|
||||
) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -93,7 +97,8 @@ namespace rfl::msgpack {
|
|||
OutputVarType add_value_to_object(
|
||||
const std::string_view& _name,
|
||||
const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
OutputObjectType* _parent
|
||||
) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_value(_var);
|
||||
|
@ -104,8 +109,10 @@ namespace rfl::msgpack {
|
|||
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 {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
|
|
|
@ -25,8 +25,8 @@ namespace rfl {
|
|||
|
||||
/// Parses an object from MSGPACK 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) {
|
||||
msgpack_zone mempool;
|
||||
msgpack_zone_init(&mempool, 2048);
|
||||
msgpack_object deserialized;
|
||||
|
|
|
@ -18,7 +18,8 @@ namespace rfl {
|
|||
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct remove_ptr<Field<_name, T>> {
|
||||
using FieldType = Field<_name,
|
||||
using FieldType = Field<
|
||||
_name,
|
||||
internal::wrap_in_rfl_array_t<
|
||||
std::remove_cvref_t<std::remove_pointer_t<T>>>>;
|
||||
};
|
||||
|
|
|
@ -27,8 +27,10 @@ namespace rfl::parsing {
|
|||
|
||||
std::optional<Error> check_size() const {
|
||||
if (i_ != size_) {
|
||||
return Error("Expected " + std::to_string(size_) + " elements, got " +
|
||||
std::to_string(i_) + ".");
|
||||
return Error(
|
||||
"Expected " + std::to_string(size_) + " elements, got " +
|
||||
std::to_string(i_) + "."
|
||||
);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
@ -39,8 +41,10 @@ namespace rfl::parsing {
|
|||
if (res) {
|
||||
move_to(&((*array_)[i_]), &(*res));
|
||||
} else {
|
||||
return Error("Failed to parse element " + std::to_string(i_) + ": " +
|
||||
res.error()->what());
|
||||
return Error(
|
||||
"Failed to parse element " + std::to_string(i_) + ": " +
|
||||
res.error()->what()
|
||||
);
|
||||
}
|
||||
++i_;
|
||||
return std::nullopt;
|
||||
|
|
|
@ -12,14 +12,15 @@
|
|||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class R,
|
||||
template <
|
||||
class R,
|
||||
class W,
|
||||
class ProcessorsType,
|
||||
class OriginalClass,
|
||||
class HelperStruct>
|
||||
struct CustomParser {
|
||||
static Result<OriginalClass> read(const R& _r,
|
||||
const auto& _var) noexcept {
|
||||
static Result<OriginalClass>
|
||||
read(const R& _r, const auto& _var) noexcept {
|
||||
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
|
||||
try {
|
||||
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
|
||||
|
@ -38,17 +39,18 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class P>
|
||||
static auto write(const W& _w,
|
||||
const OriginalClass& _p,
|
||||
const P& _parent) noexcept {
|
||||
static auto
|
||||
write(const W& _w, const OriginalClass& _p, const P& _parent) noexcept {
|
||||
Parser<R, W, HelperStruct, ProcessorsType>::write(
|
||||
_w, HelperStruct::from_class(_p), _parent);
|
||||
_w, HelperStruct::from_class(_p), _parent
|
||||
);
|
||||
}
|
||||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions) {
|
||||
return Parser<R, W, std::remove_cvref_t<HelperStruct>,
|
||||
ProcessorsType>::to_schema(_definitions);
|
||||
std::map<std::string, schema::Type>* _definitions
|
||||
) {
|
||||
return Parser<R, W, std::remove_cvref_t<HelperStruct>, ProcessorsType>::
|
||||
to_schema(_definitions);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -35,19 +35,22 @@ namespace rfl {
|
|||
static_assert(
|
||||
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
|
||||
"Externally tagged variants cannot have duplicate field "
|
||||
"names.");
|
||||
"names."
|
||||
);
|
||||
|
||||
const auto to_result = [&](const auto _obj) -> ResultType {
|
||||
auto field_variant = std::optional<Result<FieldVariantType>>();
|
||||
const auto reader =
|
||||
FieldVariantReader<R, W, ProcessorsType, FieldTypes...>(
|
||||
&_r, &field_variant);
|
||||
&_r, &field_variant
|
||||
);
|
||||
auto err = _r.read_object(reader, _obj);
|
||||
if (err) { return *err; }
|
||||
if (!field_variant) {
|
||||
return Error(
|
||||
"Could not parse: Expected the object to have "
|
||||
"exactly one field, but found more than one.");
|
||||
"exactly one field, but found more than one."
|
||||
);
|
||||
}
|
||||
return std::move(*field_variant);
|
||||
};
|
||||
|
@ -56,20 +59,24 @@ namespace rfl {
|
|||
}
|
||||
|
||||
template <class P>
|
||||
static void write(const W& _w,
|
||||
static void write(
|
||||
const W& _w,
|
||||
const std::variant<FieldTypes...>& _v,
|
||||
const P& _parent) noexcept {
|
||||
const P& _parent
|
||||
) noexcept {
|
||||
static_assert(
|
||||
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
|
||||
"Externally tagged variants cannot have duplicate field "
|
||||
"names.");
|
||||
"names."
|
||||
);
|
||||
|
||||
const auto handle = [&](const auto& _field) {
|
||||
const auto named_tuple =
|
||||
make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::remove_cvref_t<decltype(named_tuple)>;
|
||||
Parser<R, W, NamedTupleType, ProcessorsType>::write(_w, named_tuple,
|
||||
_parent);
|
||||
Parser<R, W, NamedTupleType, ProcessorsType>::write(
|
||||
_w, named_tuple, _parent
|
||||
);
|
||||
};
|
||||
|
||||
std::visit(handle, _v);
|
||||
|
@ -77,10 +84,11 @@ namespace rfl {
|
|||
|
||||
static schema::Type to_schema(
|
||||
std::map<std::string, schema::Type>* _definitions,
|
||||
std::vector<schema::Type> _types = {}) {
|
||||
std::vector<schema::Type> _types = {}
|
||||
) {
|
||||
using VariantType = std::variant<NamedTuple<FieldTypes>...>;
|
||||
return Parser<R, W, VariantType, ProcessorsType>::to_schema(
|
||||
_definitions);
|
||||
return Parser<R, W, VariantType, ProcessorsType>::to_schema(_definitions
|
||||
);
|
||||
}
|
||||
};
|
||||
} // namespace parsing
|
||||
|
|
|
@ -21,21 +21,24 @@ namespace rfl::parsing {
|
|||
using ResultType = Result<FieldVariantType>;
|
||||
|
||||
public:
|
||||
FieldVariantReader(const R* _r,
|
||||
std::optional<Result<FieldVariantType>>* _field_variant)
|
||||
FieldVariantReader(
|
||||
const R* _r,
|
||||
std::optional<Result<FieldVariantType>>* _field_variant
|
||||
)
|
||||
: r_(_r), field_variant_(_field_variant) {}
|
||||
|
||||
~FieldVariantReader() = default;
|
||||
|
||||
template <size_t _i = 0>
|
||||
void read(const std::string_view& _disc_value,
|
||||
const InputVarType& _var) const noexcept {
|
||||
void read(const std::string_view& _disc_value, const InputVarType& _var)
|
||||
const noexcept {
|
||||
if constexpr (_i < sizeof...(FieldTypes)) {
|
||||
if constexpr (_i == 0) {
|
||||
if (*field_variant_) {
|
||||
*field_variant_ = Error(
|
||||
"Could not parse: Expected the object to have "
|
||||
"exactly one field, but found more than one.");
|
||||
"exactly one field, but found more than one."
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -52,8 +55,10 @@ namespace rfl::parsing {
|
|||
return std::variant<FieldTypes...>(FieldType(std::move(_val)));
|
||||
};
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Could not parse std::variant with field '" +
|
||||
std::string(_disc_value) + "': " + _e.what());
|
||||
return Error(
|
||||
"Could not parse std::variant with field '" +
|
||||
std::string(_disc_value) + "': " + _e.what()
|
||||
);
|
||||
};
|
||||
*field_variant_ =
|
||||
Parser<R, W, ValueType, ProcessorsType>::read(*r_, _var)
|
||||
|
@ -67,7 +72,8 @@ namespace rfl::parsing {
|
|||
*field_variant_ = Error(
|
||||
"Could not parse std::variant, could not match field named "
|
||||
"'" +
|
||||
std::string(_disc_value) + "'.");
|
||||
std::string(_disc_value) + "'."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,8 +25,8 @@ namespace rfl {
|
|||
|
||||
template <class R>
|
||||
struct MockObjectReader {
|
||||
void read(const std::string_view& _name,
|
||||
typename R::InputVarType& _var) const {}
|
||||
void read(const std::string_view& _name, typename R::InputVarType& _var)
|
||||
const {}
|
||||
};
|
||||
|
||||
template <class R, class T>
|
||||
|
@ -38,7 +38,8 @@ namespace rfl {
|
|||
MockObjectReader<R> object_reader,
|
||||
typename R::InputArrayType arr,
|
||||
typename R::InputObjectType obj,
|
||||
typename R::InputVarType var) {
|
||||
typename R::InputVarType var
|
||||
) {
|
||||
/// Any Reader needs to define the following:
|
||||
///
|
||||
/// 1) An InputArrayType, which must be an array-like data structure.
|
||||
|
|
|
@ -11,14 +11,16 @@ namespace rfl {
|
|||
namespace parsing {
|
||||
|
||||
template <class W, class T>
|
||||
concept IsWriter = requires(W w,
|
||||
concept IsWriter = requires(
|
||||
W w,
|
||||
T t,
|
||||
std::string_view name,
|
||||
std::string basic_value,
|
||||
typename W::OutputArrayType arr,
|
||||
typename W::OutputObjectType obj,
|
||||
typename W::OutputVarType var,
|
||||
size_t size) {
|
||||
size_t size
|
||||
) {
|
||||
/// Sets an empty array as the root element of the document.
|
||||
/// Some serialization formats require you to pass the expected size in
|
||||
/// advance. If you are not working with such a format, you can ignore the
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue