Optimizationg for the reader and writer; #68

This commit is contained in:
Patrick Urbanke
2024-03-16 09:58:26 +01:00
parent 54785ec893
commit 58d89e717c
27 changed files with 738 additions and 556 deletions
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_PARSING_STRUCTREADER_HPP_
#define RFL_PARSING_STRUCTREADER_HPP_
#include <array>
#include <string_view>
#include <type_traits>
#include <vector>
#include "../Result.hpp"
#include "../to_view.hpp"
#include "AreReaderAndWriter.hpp"
namespace rfl::parsing {
template <class R, class W, class StructType>
requires AreReaderAndWriter<R, W, StructType>
struct StructReader {
public:
using InputVarType = typename R::InputVarType;
/// The way this works is that we allocate space on the stack in this size of
/// the struct in which we then write the individual fields using
/// views and placement new. This is how we deal with the fact that some
/// fields might not be default-constructible.
static Result<StructType> read(const R& _r, const InputVarType& _var) {
alignas(StructType) unsigned char buf[sizeof(StructType)];
auto ptr = reinterpret_cast<StructType*>(buf);
const auto view = to_view(*ptr);
using ViewType = std::remove_cvref_t<decltype(view)>;
const auto err = Parser<R, W, ViewType>::read_view(_r, _var, view);
if (err) {
return *err;
}
return std::move(*ptr);
}
};
} // namespace rfl::parsing
#endif