Added generic elements; resolves #96 (#125)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2024-06-27 23:05:48 +02:00
committed by GitHub
parent c3db8321b3
commit 28b89ae0df
23 changed files with 1012 additions and 39 deletions
+3
View File
@@ -13,12 +13,15 @@
#include "rfl/Attribute.hpp"
#include "rfl/Box.hpp"
#include "rfl/Description.hpp"
#include "rfl/ExtraFields.hpp"
#include "rfl/Field.hpp"
#include "rfl/Flatten.hpp"
#include "rfl/Generic.hpp"
#include "rfl/Literal.hpp"
#include "rfl/NamedTuple.hpp"
#include "rfl/NoFieldNames.hpp"
#include "rfl/NoOptionals.hpp"
#include "rfl/Object.hpp"
#include "rfl/OneOf.hpp"
#include "rfl/Pattern.hpp"
#include "rfl/PatternValidator.hpp"
+15
View File
@@ -0,0 +1,15 @@
#ifndef RFL_EXTRAFIELDS_HPP_
#define RFL_EXTRAFIELDS_HPP_
#include "Object.hpp"
namespace rfl {
/// Used to embed additional fields for which the names cannot be known in
/// advance and can therefore not be encoded in the struct.
template <class T>
class ExtraFields : public Object<T> {};
} // namespace rfl
#endif
+181
View File
@@ -0,0 +1,181 @@
#ifndef RFL_GENERIC_HPP_
#define RFL_GENERIC_HPP_
#include <string>
#include <variant>
#include <vector>
#include "Object.hpp"
#include "Result.hpp"
namespace rfl {
class Generic {
public:
using Array = std::vector<Generic>;
using Object = rfl::Object<Generic>;
using ReflectionType =
std::variant<bool, int, double, std::string, Object, Array>;
Generic() : value_(false) {}
Generic(Generic&& _other) noexcept = default;
Generic(const Generic& _other) = default;
Generic(const ReflectionType& _value) : value_(_value) {}
Generic(ReflectionType&& _value) noexcept : value_(std::move(_value)) {}
template <class T,
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
bool>::type = true>
Generic(const T& _value) : value_(_value) {}
template <class T,
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
bool>::type = true>
Generic(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
~Generic() = default;
/// Returns the underlying object.
const ReflectionType& get() const { return value_; }
/// Assigns the underlying object.
auto& operator=(const ReflectionType& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
auto& operator=(ReflectionType&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
/// Assigns the underlying object.
template <class T,
typename std::enable_if<std::is_convertible_v<T, ReflectionType>,
bool>::type = true>
auto& operator=(const T& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object.
Generic& operator=(const Generic& _other) = default;
/// Assigns the underlying object.
Generic& operator=(Generic&& _other) = default;
/// Returns the underlying object, necessary for the serialization to work.
const ReflectionType& reflection() const { return value_; };
/// Casts the underlying value to an rfl::Generic::Array or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Array.
Result<Array> to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Array.");
}
}
/// Casts the underlying value to a boolean or returns an rfl::Error, if the
/// underlying value is not a boolean.
Result<bool> to_bool() const noexcept {
if (const auto* ptr = std::get_if<bool>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a boolean.");
}
}
/// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double.
Result<double> to_double() const noexcept {
if (const auto* ptr = std::get_if<double>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a double.");
}
}
/// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer.
Result<int> to_int() const noexcept {
if (const auto* ptr = std::get_if<int>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an integer.");
}
}
/// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
Result<Object> to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to an "
"rfl::Generic::Object.");
}
}
/// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string.
Result<std::string> to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) {
return *ptr;
} else {
return Error(
"rfl::Generic: Could not cast the underlying value to a string.");
}
}
private:
ReflectionType value_;
};
/// Casts the underlying value to an rfl::Generic::Array or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Array.
inline Result<Generic::Array> to_array(const Generic& _g) noexcept {
return _g.to_array();
}
/// Casts the underlying value to a boolean or returns an rfl::Error, if the
/// underlying value is not a boolean.
inline Result<bool> to_bool(const Generic& _g) noexcept { return _g.to_bool(); }
/// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double.
inline Result<double> to_double(const Generic& _g) noexcept {
return _g.to_double();
}
/// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer.
inline Result<int> to_int(const Generic& _g) noexcept { return _g.to_int(); }
/// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object.
inline Result<Generic::Object> to_object(const Generic& _g) noexcept {
return _g.to_object();
}
/// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string.
inline Result<std::string> to_string(const Generic& _g) noexcept {
return _g.to_string();
}
} // namespace rfl
#endif
+59 -1
View File
@@ -12,6 +12,7 @@
#include "get.hpp"
#include "internal/StringLiteral.hpp"
#include "internal/find_index.hpp"
#include "internal/is_extra_fields.hpp"
#include "internal/no_duplicate_field_names.hpp"
namespace rfl {
@@ -282,6 +283,22 @@ class NamedTuple {
inline auto operator!=(const rfl::NamedTuple<FieldTypes...>& _other) const {
return !(*this == _other);
}
/// Returns the number of fields. Note that this is not necessary the same
/// thing as .size(), because there might be rfl::ExtraFields, which are
/// simply counted as one entry by .size(), but are counted by individually by
/// .num_fields().
size_t num_fields() const {
if constexpr (pos_extra_fields() == -1) {
return size();
} else {
return calc_num_fields<pos_extra_fields()>();
}
}
/// The position of the extra fields, or -1 if there aren't any.
constexpr static int pos_extra_fields() { return pos_extra_fields_; }
/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
template <internal::StringLiteral _name, class FType, class... OtherRFields>
@@ -409,6 +426,38 @@ class NamedTuple {
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
}
/// Unfortunately, MSVC forces us to do this...
template <int _pos>
size_t calc_num_fields() const {
const auto& extra_fields = get<_pos>();
if constexpr (std::is_pointer_v<
std::remove_cvref_t<decltype(extra_fields)>>) {
return size() + extra_fields->size() - 1;
} else {
return size() + extra_fields.size() - 1;
}
}
/// Finds the position of the extra fields, or -1 if there aren't any.
template <int _i = 0, int _idx = -1>
constexpr static int find_extra_fields() {
if constexpr (_i == size()) {
return _idx;
} else {
using FieldType = typename std::tuple_element_t<_i, Fields>;
constexpr bool is_extra_fields =
internal::is_extra_fields_v<typename FieldType::Type>;
static_assert(_idx == -1 || !is_extra_fields,
"There can only be one rfl::ExtraFields in any struct or "
"named tuple.");
if constexpr (is_extra_fields) {
return find_extra_fields<_i + 1, _i>();
} else {
return find_extra_fields<_i + 1, _idx>();
}
}
}
/// Generates the fields.
template <int num_additional_fields = 0, class... Args>
auto make_fields(Args&&... _args) {
@@ -423,7 +472,7 @@ class NamedTuple {
} else {
// When we add additional fields, it is more intuitive to add
// them to the end, that is why we do it like this.
using FieldType = typename std::tuple_element<i, Fields>::type;
using FieldType = typename std::tuple_element_t<i, Fields>;
using T = std::remove_cvref_t<typename FieldType::Type>;
return make_fields<num_additional_fields>(
FieldType(std::forward<T>(std::get<i>(values_))),
@@ -554,6 +603,9 @@ class NamedTuple {
/// everything else is resolved at compile time. It should have no
/// runtime overhead over a normal std::tuple.
Values values_;
/// The position of rfl::ExtraFields, or -1 if there aren't any.
constexpr static int pos_extra_fields_ = find_extra_fields();
};
// ----------------------------------------------------------------------------
@@ -611,6 +663,12 @@ class NamedTuple<> {
/// Returns an empty tuple.
auto fields() const { return std::tuple(); }
/// Must always be 0.
size_t num_fields() const { return 0; }
/// Must always be -1.
constexpr static int pos_extra_fields() { return -1; }
/// Must always be 0.
static constexpr size_t size() { return 0; }
+231
View File
@@ -0,0 +1,231 @@
#ifndef RFL_OBJECT_HPP_
#define RFL_OBJECT_HPP_
#include <algorithm>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "Result.hpp"
namespace rfl {
/// Used to embed additional fields for which the names cannot be known in
/// advance and can therefore not be encoded in the struct.
template <class T>
class Object {
public:
using DataType = std::vector<std::pair<std::string, T>>;
using Type = T;
/// We want this to behave as similarly to C++ standard containers as
/// possible.
using key_type = std::string;
using mapped_type = T;
using value_type = std::pair<std::string, T>;
using size_type = typename DataType::size_type;
using difference_type = typename DataType::size_type;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename DataType::pointer;
using const_pointer = typename DataType::const_pointer;
using iterator = typename DataType::iterator;
using const_iterator = typename DataType::const_iterator;
using reverse_iterator = typename DataType::reverse_iterator;
using const_reverse_iterator = typename DataType::const_reverse_iterator;
Object() : data_(), it_(data_.begin()) {}
Object(const Object<T>& _f) = default;
Object(Object<T>&& _f) noexcept = default;
~Object() = default;
/// Iterator to the beginning.
auto begin() { return data_.begin(); }
/// Iterator to the beginning.
auto begin() const { return data_.begin(); }
/// Const iterator to the beginning.
auto cbegin() const { return data_.cbegin(); }
/// Iterator to the end.
auto end() { return data_.end(); }
/// Iterator to the end.
auto end() const { return data_.end(); }
/// Const iterator to the end.
auto cend() const { return data_.cend(); }
/// Reverse iterator.
auto rbegin() { return data_.rbegin(); }
/// Reverse iterator.
auto rbegin() const { return data_.rbegin(); }
/// Const reverse iterator.
auto crbegin() const { return data_.crbegin(); }
/// Reverse iterator.
auto rend() { return data_.rend(); }
/// Reverse iterator.
auto rend() const { return data_.rend(); }
/// Const reverse iterator.
auto crend() const { return data_.crend(); }
Object<T>& operator=(const Object<T>& _f) = default;
Object<T>& operator=(Object<T>&& _f) = default;
/// Whether the object is empty.
auto empty() const { return data_.size() == 0; }
/// The number of elements currently inside the object.
auto size() const { return data_.size(); }
/// The maximum possible size.
auto max_size() const { return data_.max_size(); }
/// Inserts a new element at the end.
void insert(const value_type& _value) {
data_.push_back(_value);
it_ = data_.begin();
}
/// Inserts a new element at the end.
void insert(value_type&& _value) {
data_.emplace_back(std::move(_value));
it_ = data_.begin();
}
/// Inserts several new elements at the end.
template <class InputIt>
void insert(InputIt _first, InputIt _last) {
for (auto it = _first; it != _last; ++it) {
insert(*it);
}
}
/// Inserts a new element at the end.
void insert(const std::string& _k, const T& _v) {
insert(std::make_pair(_k, _v));
}
/// Inserts a new element at the end.
void insert(const std::string& _k, T&& _v) {
insert(std::make_pair(_k, std::move(_v)));
}
/// Inserts a new element at the end.
void insert(std::string&& _k, T&& _v) {
insert(std::make_pair(std::move(_k), std::move(_v)));
}
/// Alias for insert that primarily exists for compatability with standard
/// containers.
template <class... Args>
void emplace(Args&&... _args) {
insert(std::forward<Args>(_args)...);
}
/// Alias for insert that primarily exists for compatability with standard
/// containers.
template <class... Args>
void emplace(const Args&... _args) {
insert(_args...);
}
/// Returns the element signified by the key or creates a new one.
T& operator[](const std::string& _key) {
for (auto it = it_; it != end(); ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
for (auto it = begin(); it != it_; ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
data_.emplace_back(std::make_pair(_key, T()));
it_ = data_.begin();
return data_.back().second;
}
/// Returns the element signified by the key or creates a new one.
T& operator[](std::string&& _key) {
for (auto it = it_; it != end(); ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
for (auto it = begin(); it != it_; ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
data_.emplace_back(std::make_pair(std::move(_key), T()));
it_ = data_.begin();
return data_.back().second;
}
/// Deletes all elements.
void clear() {
data_.clear();
it_ = data_.begin();
}
/// Returns the element signified by the key or throws an exception.
T& at(const std::string& _key) {
for (auto it = it_; it != end(); ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
throw std::runtime_error("Key named '" + _key + "' not found.");
}
/// Returns the element signified by the key or throws an exception.
const T& at(const std::string& _key) const {
for (auto it = it_; it != end(); ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
throw std::runtime_error("Key named '" + _key + "' not found.");
}
/// Returns a result wrapping the element signified by the key.
Result<T> get(const std::string& _key) const noexcept {
for (auto it = it_; it != end(); ++it) {
if (it->first == _key) {
it_ = it + 1;
return it->second;
}
}
return Error("Key named '" + _key + "' not found.");
}
private:
DataType data_;
/// Allows faster access
iterator it_;
};
} // namespace rfl
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef RFL_INTERNAL_ISEXTRAFIELDS_HPP_
#define RFL_INTERNAL_ISEXTRAFIELDS_HPP_
#include <type_traits>
#include "../ExtraFields.hpp"
namespace rfl::internal {
template <class T>
class is_extra_fields;
template <class T>
class is_extra_fields : public std::false_type {};
template <class T>
class is_extra_fields<ExtraFields<T>> : public std::true_type {};
template <class T>
constexpr bool is_extra_fields_v =
is_extra_fields<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;
} // namespace rfl::internal
#endif
+1
View File
@@ -88,6 +88,7 @@ struct Type {
std::optional<std::string> description;
std::map<std::string, Type> properties;
std::vector<std::string> required;
std::shared_ptr<Type> additionalProperties;
};
struct OneOf {
+8 -1
View File
@@ -193,8 +193,15 @@ inline schema::Type type_to_json_schema_type(
required.push_back(k);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(
type_to_json_schema_type(*_t.additional_properties_))
: std::shared_ptr<schema::Type>();
return schema::Type{.value = schema::Type::Object{
.properties = properties, .required = required}};
.properties = properties,
.required = required,
.additionalProperties = additional_properties}};
} else if constexpr (std::is_same<T, Type::Optional>()) {
return schema::Type{.value = schema::Type::AnyOf{
+36 -11
View File
@@ -15,6 +15,7 @@
#include "../internal/is_array.hpp"
#include "../internal/is_attribute.hpp"
#include "../internal/is_basic_type.hpp"
#include "../internal/is_extra_fields.hpp"
#include "../internal/is_skip.hpp"
#include "../internal/strings/replace_all.hpp"
#include "../to_view.hpp"
@@ -59,6 +60,10 @@ struct NamedTupleParser {
static constexpr size_t size_ = NamedTupleType::size();
static_assert(NamedTupleType::pos_extra_fields() == -1 || !_no_field_names,
"You cannot use the rfl::NoFieldNames processor if you are "
"including rfl::ExtraFields.");
public:
/// The way this works is that we allocate space on the stack in this size of
/// the named tuple in which we then write the individual fields using
@@ -102,11 +107,11 @@ struct NamedTupleParser {
static void write(const W& _w, const NamedTuple<FieldTypes...>& _tup,
const P& _parent) noexcept {
if constexpr (_no_field_names) {
auto arr = ParentType::add_array(_w, _tup.size(), _parent);
auto arr = ParentType::add_array(_w, _tup.num_fields(), _parent);
build_object(_w, _tup, &arr, std::make_integer_sequence<int, size_>());
_w.end_array(&arr);
} else {
auto obj = ParentType::add_object(_w, _tup.size(), _parent);
auto obj = ParentType::add_object(_w, _tup.num_fields(), _parent);
build_object(_w, _tup, &obj, std::make_integer_sequence<int, size_>());
_w.end_object(&obj);
}
@@ -128,11 +133,17 @@ struct NamedTupleParser {
using FieldType =
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
const auto& value = rfl::get<_i>(_tup);
constexpr auto name = FieldType::name_.string_view();
const auto new_parent = make_parent(name, _ptr);
if constexpr (!_all_required && !_no_field_names &&
!is_required<ValueType, _ignore_empty_containers>()) {
const auto value = rfl::get<_i>(_tup);
if constexpr (internal::is_extra_fields_v<ValueType>) {
for (const auto& [k, v] : *value) {
const auto new_parent = make_parent(k, _ptr);
Parser<R, W, std::remove_cvref_t<decltype(v)>, ProcessorsType>::write(
_w, v, new_parent);
}
} else if constexpr (!_all_required && !_no_field_names &&
!is_required<ValueType, _ignore_empty_containers>()) {
constexpr auto name = FieldType::name_.string_view();
const auto new_parent = make_parent(name, _ptr);
if (!is_empty(value)) {
if constexpr (internal::is_attribute_v<ValueType>) {
Parser<R, W, ValueType, ProcessorsType>::write(
@@ -142,6 +153,8 @@ struct NamedTupleParser {
}
}
} else {
constexpr auto name = FieldType::name_.string_view();
const auto new_parent = make_parent(name, _ptr);
if constexpr (internal::is_attribute_v<ValueType>) {
Parser<R, W, ValueType, ProcessorsType>::write(
_w, value, new_parent.as_attribute());
@@ -157,8 +170,8 @@ struct NamedTupleParser {
SchemaType* _schema) noexcept {
using F =
std::tuple_element_t<_i, typename NamedTuple<FieldTypes...>::Fields>;
using U = typename F::Type;
if constexpr (!internal::is_skip_v<U>) {
using U = std::remove_cvref_t<typename F::Type>;
if constexpr (!internal::is_skip_v<U> && !internal::is_extra_fields_v<U>) {
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
@@ -180,6 +193,16 @@ struct NamedTupleParser {
SchemaType* _schema,
std::integer_sequence<int, _is...>) noexcept {
(add_field_to_schema<_is>(_definitions, _schema), ...);
if constexpr (NamedTupleType::pos_extra_fields() != -1) {
using F =
std::tuple_element_t<NamedTupleType::pos_extra_fields(),
typename NamedTuple<FieldTypes...>::Fields>;
using ExtraFieldsType = std::remove_cvref_t<typename F::Type>;
using U = std::remove_cvref_t<typename ExtraFieldsType::Type>;
_schema->additional_properties_ = std::make_shared<schema::Type>(
Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
}
}
/// Generates error messages for when fields are missing.
@@ -193,8 +216,10 @@ struct NamedTupleParser {
std::remove_pointer_t<typename FieldType::Type>>;
if (!std::get<_i>(_found)) {
if constexpr (_all_required ||
is_required<ValueType, _ignore_empty_containers>()) {
constexpr bool is_required_field =
!internal::is_extra_fields_v<ValueType> &&
(_all_required || is_required<ValueType, _ignore_empty_containers>());
if constexpr (is_required_field) {
constexpr auto current_name =
std::tuple_element_t<_i, typename NamedTupleType::Fields>::name();
_errors->emplace_back(Error(
+11 -5
View File
@@ -140,8 +140,7 @@ struct Parser {
return make_validated<U>(_definitions);
} else if constexpr (internal::has_reflection_type_v<U>) {
return Parser<R, W, typename U::ReflectionType,
ProcessorsType>::to_schema(_definitions);
return make_reference<U>(_definitions);
} else {
static_assert(rfl::always_false_v<U>, "Unsupported type.");
@@ -181,9 +180,16 @@ struct Parser {
if (_definitions->find(name) == _definitions->end()) {
(*_definitions)[name] =
Type{Type::Integer{}}; // Placeholder to avoid infinite loop.
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
(*_definitions)[name] =
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(_definitions);
if constexpr (internal::has_reflection_type_v<U>) {
(*_definitions)[name] =
Parser<R, W, typename U::ReflectionType, ProcessorsType>::to_schema(
_definitions);
} else {
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
(*_definitions)[name] =
Parser<R, W, NamedTupleType, ProcessorsType>::to_schema(
_definitions);
}
}
return Type{Type::Reference{name}};
}
+14
View File
@@ -7,6 +7,8 @@
#include <type_traits>
#include <unordered_map>
#include "../ExtraFields.hpp"
#include "../Object.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
#include "MapParser.hpp"
@@ -28,6 +30,18 @@ struct Parser<R, W, std::unordered_map<std::string, T>, ProcessorsType>
ProcessorsType> {
};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Object<T>>
struct Parser<R, W, Object<T>, ProcessorsType>
: public MapParser<R, W, Object<T>, ProcessorsType> {
};
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, ExtraFields<T>>
struct Parser<R, W, ExtraFields<T>, ProcessorsType>
: public MapParser<R, W, ExtraFields<T>, ProcessorsType> {
};
} // namespace parsing
} // namespace rfl
+34
View File
@@ -73,6 +73,31 @@ class ViewReader {
}
}
template <int _pos>
static void assign_to_extra_fields(const R& _r,
const std::string_view& _current_name,
const auto& _var, auto* _view,
auto* _errors, auto* _found, auto* _set) {
auto* extra_fields = _view->template get<_pos>();
using ExtraFieldsType =
std::remove_cvref_t<std::remove_pointer_t<decltype(extra_fields)>>;
using T = std::remove_cvref_t<
std::remove_pointer_t<typename ExtraFieldsType::Type>>;
if (!std::get<_pos>(*_set)) {
::new (extra_fields) ExtraFieldsType();
std::get<_pos>(*_set) = true;
std::get<_pos>(*_found) = true;
}
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) {
_errors->emplace_back(Error("Failed to parse field '" +
std::string(_current_name) +
"': " + std::move(res.error()->what())));
return;
}
extra_fields->emplace(std::string(_current_name), std::move(*res));
}
template <int... is>
static void assign_to_matching_field(const R& _r,
const std::string_view& _current_name,
@@ -80,9 +105,18 @@ class ViewReader {
auto* _errors, auto* _found, auto* _set,
std::integer_sequence<int, is...>) {
bool already_assigned = false;
(assign_if_field_matches<is>(_r, _current_name, _var, _view, _errors,
_found, _set, &already_assigned),
...);
if constexpr (ViewType::pos_extra_fields() != -1) {
constexpr int pos = ViewType::pos_extra_fields();
if (!already_assigned) {
assign_to_extra_fields<pos>(_r, _current_name, _var, _view, _errors,
_found, _set);
}
}
}
template <class T>
+2
View File
@@ -3,6 +3,7 @@
#include <cstddef>
#include <map>
#include <memory>
#include <string>
#include <variant>
#include <vector>
@@ -51,6 +52,7 @@ struct Type {
struct Object {
std::map<std::string, Type> types_;
std::shared_ptr<Type> additional_properties_;
};
/// All values are assumed to be required unless explicitly stated otherwise