2024-05-31 22:59:00 -04:00
|
|
|
#ifndef RFL_PARSING_VIEWREADER_HPP_
|
|
|
|
#define RFL_PARSING_VIEWREADER_HPP_
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <string_view>
|
|
|
|
#include <type_traits>
|
2024-06-05 19:04:53 -04:00
|
|
|
#include <utility>
|
2024-05-31 22:59:00 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../internal/is_array.hpp"
|
|
|
|
|
|
|
|
namespace rfl::parsing {
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class R, class W, class ViewType, class ProcessorsType>
|
|
|
|
class ViewReader {
|
|
|
|
private:
|
|
|
|
using InputVarType = typename R::InputVarType;
|
|
|
|
static constexpr size_t size_ = ViewType::size();
|
|
|
|
|
|
|
|
public:
|
2024-06-08 15:53:06 -04:00
|
|
|
ViewReader(
|
2024-06-16 00:13:15 -04:00
|
|
|
const R* _r,
|
|
|
|
ViewType* _view,
|
|
|
|
std::array<bool, size_>* _found,
|
|
|
|
std::array<bool, size_>* _set,
|
|
|
|
std::vector<Error>* _errors
|
2024-06-08 15:53:06 -04:00
|
|
|
)
|
2024-06-16 00:13:15 -04:00
|
|
|
: r_(_r), view_(_view), found_(_found), set_(_set), errors_(_errors) {}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
~ViewReader() = default;
|
|
|
|
|
|
|
|
/// Assigns the parsed version of _var to the field signified by _name, if
|
|
|
|
/// such a field exists in the underlying view.
|
|
|
|
void read(const std::string_view& _name, const InputVarType& _var) const {
|
2024-06-08 15:53:06 -04:00
|
|
|
assign_to_matching_field(
|
2024-06-16 00:13:15 -04:00
|
|
|
*r_, _name, _var, view_, errors_, found_, set_, std::make_integer_sequence<int, size_>()
|
2024-06-08 15:53:06 -04:00
|
|
|
);
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
/// Because of the way we have allocated the fields, we need to manually
|
|
|
|
/// trigger the destructors.
|
|
|
|
void call_destructors_where_necessary() const {
|
|
|
|
[&]<int... is>(std::integer_sequence<int, is...>) {
|
|
|
|
(call_destructor_on_one_if_necessary<is>(), ...);
|
|
|
|
}(std::make_integer_sequence<int, size_>());
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <int i>
|
2024-06-08 15:53:06 -04:00
|
|
|
static void assign_if_field_matches(
|
2024-06-16 00:13:15 -04:00
|
|
|
const R& _r,
|
|
|
|
const std::string_view& _current_name,
|
|
|
|
const auto& _var,
|
|
|
|
auto* _view,
|
|
|
|
auto* _errors,
|
|
|
|
auto* _found,
|
|
|
|
auto* _set,
|
|
|
|
bool* _already_assigned
|
2024-06-08 15:53:06 -04:00
|
|
|
) {
|
2024-06-16 00:13:15 -04:00
|
|
|
using FieldType = std::tuple_element_t<i, typename ViewType::Fields>;
|
|
|
|
using OriginalType = typename FieldType::Type;
|
|
|
|
using T = std::remove_cvref_t<std::remove_pointer_t<typename FieldType::Type>>;
|
2024-06-08 14:10:59 -04:00
|
|
|
constexpr auto name = FieldType::name();
|
2024-06-16 00:13:15 -04:00
|
|
|
if (!(*_already_assigned) && !std::get<i>(*_found) && _current_name == name) {
|
2024-06-08 14:10:59 -04:00
|
|
|
std::get<i>(*_found) = true;
|
|
|
|
*_already_assigned = true;
|
|
|
|
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
|
|
|
|
if (!res) {
|
2024-06-08 15:53:06 -04:00
|
|
|
_errors->emplace_back(Error(
|
2024-06-16 00:13:15 -04:00
|
|
|
"Failed to parse field '" + std::string(name) + "': " + std::move(res.error()->what())
|
2024-06-08 15:53:06 -04:00
|
|
|
));
|
2024-06-08 14:10:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if constexpr (std::is_pointer_v<OriginalType>) {
|
|
|
|
move_to(rfl::get<i>(*_view), &(*res));
|
|
|
|
} else {
|
|
|
|
rfl::get<i>(*_view) = std::move(*res);
|
|
|
|
}
|
|
|
|
std::get<i>(*_set) = true;
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <int... is>
|
2024-06-08 15:53:06 -04:00
|
|
|
static void
|
|
|
|
assign_to_matching_field(const R& _r, const std::string_view& _current_name, const auto& _var, auto* _view, auto* _errors, auto* _found, auto* _set, std::integer_sequence<int, is...>) {
|
2024-06-08 14:10:59 -04:00
|
|
|
bool already_assigned = false;
|
2024-06-08 15:53:06 -04:00
|
|
|
(assign_if_field_matches<is>(
|
2024-06-16 00:13:15 -04:00
|
|
|
_r, _current_name, _var, _view, _errors, _found, _set, &already_assigned
|
2024-06-08 15:53:06 -04:00
|
|
|
),
|
2024-06-08 14:10:59 -04:00
|
|
|
...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void call_destructor_on_array(const size_t _size, T* _ptr) {
|
|
|
|
for (size_t i = 0; i < _size; ++i) {
|
|
|
|
if constexpr (std::is_array_v<T>) {
|
|
|
|
call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i));
|
|
|
|
} else if constexpr (std::is_destructible_v<T>) {
|
|
|
|
(_ptr + i)->~T();
|
|
|
|
}
|
2024-06-05 19:04:53 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
template <int _i>
|
|
|
|
void call_destructor_on_one_if_necessary() const {
|
|
|
|
using FieldType = std::tuple_element_t<_i, typename ViewType::Fields>;
|
|
|
|
using OriginalType = std::remove_cvref_t<typename FieldType::Type>;
|
2024-06-16 00:13:15 -04:00
|
|
|
using ValueType = std::remove_cvref_t<std::remove_pointer_t<typename FieldType::Type>>;
|
|
|
|
if constexpr (!std::is_array_v<ValueType> && std::is_pointer_v<OriginalType> &&
|
2024-06-08 14:10:59 -04:00
|
|
|
std::is_destructible_v<ValueType>) {
|
2024-06-09 18:55:00 -04:00
|
|
|
if (std::get<_i>(*set_)) {
|
|
|
|
rfl::get<_i>(*view_)->~ValueType();
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
} else if constexpr (std::is_array_v<ValueType>) {
|
|
|
|
if (std::get<_i>(*set_)) {
|
|
|
|
auto ptr = rfl::get<_i>(*view_);
|
|
|
|
call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr);
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Target, class Source>
|
|
|
|
static void move_to(Target* _t, Source* _s) {
|
|
|
|
if constexpr (std::is_const_v<Target>) {
|
|
|
|
return move_to(const_cast<std::remove_const_t<Target>*>(_t), _s);
|
2024-06-16 00:13:15 -04:00
|
|
|
} else if constexpr (!rfl::internal::is_array_v<Source> && !std::is_array_v<Target>) {
|
2024-06-08 14:10:59 -04:00
|
|
|
::new (_t) Target(std::move(*_s));
|
|
|
|
} else if constexpr (rfl::internal::is_array_v<Source>) {
|
2024-06-16 00:13:15 -04:00
|
|
|
static_assert(std::is_array_v<Target>, "Expected target to be a c-array.");
|
|
|
|
for (size_t i = 0; i < _s->arr_.size(); ++i) { move_to(&((*_t)[i]), &(_s->arr_[i])); }
|
2024-06-08 14:10:59 -04:00
|
|
|
} else {
|
2024-06-16 00:13:15 -04:00
|
|
|
for (size_t i = 0; i < _s->size(); ++i) { move_to(&((*_t)[i]), &((*_s)[i])); }
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
private:
|
|
|
|
/// The underlying reader.
|
|
|
|
const R* r_;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// The underlying view.
|
|
|
|
ViewType* view_;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Indicates that a certain field has been found.
|
|
|
|
std::array<bool, size_>* found_;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Indicates that a certain field has been successfully set - necessary,
|
|
|
|
/// because we have to trigger the destructors manually.
|
|
|
|
std::array<bool, size_>* set_;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Collects any errors we may have come across.
|
|
|
|
std::vector<Error>* errors_;
|
|
|
|
};
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
} // namespace rfl::parsing
|
2024-05-31 22:59:00 -04:00
|
|
|
|
|
|
|
#endif
|