mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
More efficient way of parsing array-like data structures. (#98)
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "ArrayReader.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "schema/Type.hpp"
|
||||
@@ -29,24 +30,27 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
|
||||
|
||||
static Result<std::array<T, _size>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_vec = [&](const auto& _arr) { return _r.to_vec(_arr); };
|
||||
|
||||
const auto check_size = [](auto _vec) -> Result<std::vector<InputVarType>> {
|
||||
if (_vec.size() != _size) {
|
||||
return Error("Expected " + std::to_string(_size) + " fields, got " +
|
||||
std::to_string(_vec.size()) + ".");
|
||||
const auto parse =
|
||||
[&](const InputArrayType& _arr) -> Result<std::array<T, _size>> {
|
||||
alignas(
|
||||
std::array<T, _size>) unsigned char buf[sizeof(std::array<T, _size>)];
|
||||
auto ptr = reinterpret_cast<std::array<T, _size>*>(buf);
|
||||
const auto array_reader =
|
||||
ArrayReader<R, W, ProcessorsType, T, _size>(&_r, ptr);
|
||||
auto err = _r.read_array(array_reader, _arr);
|
||||
if (err) {
|
||||
array_reader.call_destructors_where_necessary();
|
||||
return *err;
|
||||
}
|
||||
return std::move(_vec);
|
||||
err = array_reader.check_size();
|
||||
if (err) {
|
||||
array_reader.call_destructors_where_necessary();
|
||||
return *err;
|
||||
}
|
||||
return std::move(*ptr);
|
||||
};
|
||||
|
||||
const auto extract = [&_r](auto _vec) {
|
||||
return extract_field_by_field(_r, std::move(_vec));
|
||||
};
|
||||
|
||||
return _r.to_array(_var)
|
||||
.transform(to_vec)
|
||||
.and_then(check_size)
|
||||
.and_then(extract);
|
||||
return _r.to_array(_var).and_then(parse);
|
||||
}
|
||||
|
||||
template <class P>
|
||||
@@ -69,32 +73,6 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
|
||||
.type_ = Ref<schema::Type>::make(
|
||||
Parser<R, W, U, ProcessorsType>::to_schema(_definitions))}};
|
||||
}
|
||||
|
||||
private:
|
||||
/// Extracts values from the array, field by field.
|
||||
template <class... AlreadyExtracted>
|
||||
static Result<std::array<T, _size>> extract_field_by_field(
|
||||
const R& _r, std::vector<InputVarType> _vec,
|
||||
AlreadyExtracted&&... _already_extracted) noexcept {
|
||||
constexpr size_t i = sizeof...(AlreadyExtracted);
|
||||
if constexpr (i == _size) {
|
||||
return std::array<T, _size>{std::move(_already_extracted)...};
|
||||
} else {
|
||||
const auto extract_next = [&](auto&& new_entry) {
|
||||
return extract_field_by_field(_r, std::move(_vec),
|
||||
std::move(_already_extracted)...,
|
||||
std::move(new_entry));
|
||||
};
|
||||
return extract_single_field<i>(_r, _vec).and_then(extract_next);
|
||||
}
|
||||
}
|
||||
|
||||
/// Extracts a single field from a JSON.
|
||||
template <int _i>
|
||||
static auto extract_single_field(
|
||||
const R& _r, const std::vector<InputVarType>& _vec) noexcept {
|
||||
return Parser<R, W, T, ProcessorsType>::read(_r, _vec[_i]);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
Reference in New Issue
Block a user