Skip to content

Commit

Permalink
Added calc_vtable_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 19, 2025
1 parent 0dd56d0 commit 277a8ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
10 changes: 4 additions & 6 deletions include/rfl/flatbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/ptr_cast.hpp"
#include "calc_vtable_offset.hpp"

namespace rfl::flatbuf {

Expand Down Expand Up @@ -124,13 +125,10 @@ class Reader {
template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader,
const InputObjectType& _obj) const noexcept {
// TODO
// constexpr auto offset_array =
// offset_array<typename ObjectReader::ViewType>;
for (size_t i = 0; i < 0 /*TODO offset_array.size()*/; ++i) {
constexpr size_t size = ObjectReader::size();
for (size_t i = 0; i < size; ++i) {
_object_reader.read(
i,
InputVarType{_obj.val_->GetAddressOf(0 /* TODO offset_array[i]*/)});
i, InputVarType{_obj.val_->GetAddressOf(calc_vtable_offset(i))});
}
}

Expand Down
6 changes: 3 additions & 3 deletions include/rfl/flatbuf/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../always_false.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/ptr_cast.hpp"
#include "calc_vtable_offset.hpp"

namespace rfl::flatbuf {

Expand All @@ -39,7 +40,6 @@ class Writer {
};

struct FlatbufOutputObject {
flatbuffers::voffset_t* field_offsets_ = nullptr;
flatbuffers::uoffset_t offset_ = 0;
size_t ix_ = 0;
};
Expand Down Expand Up @@ -179,7 +179,7 @@ class Writer {
OutputObjectType* _parent) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto str = fbb_->CreateString(_var.c_str(), _var.size());
fbb_->AddOffset(_parent->field_offsets_[_parent->ix_++], str);
fbb_->AddOffset(calc_vtable_offset(_parent->ix_++), str);

// TODO
// } else if constexpr (std::is_same<std::remove_cvref_t<T>,
Expand All @@ -188,7 +188,7 @@ class Writer {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
fbb_->AddElement<T>(_parent->field_offsets_[_parent->ix_++], _var);
fbb_->AddElement<T>(calc_vtable_offset(_parent->ix_++), _var);

// TODO
//} else if constexpr (internal::is_literal_v<T>) {
Expand Down
17 changes: 17 additions & 0 deletions include/rfl/flatbuf/calc_vtable_offset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef RFL_FLATBUF_CALCVTABLEOFFSET_HPP_
#define RFL_FLATBUF_CALCVTABLEOFFSET_HPP_

#include <flatbuffers/flatbuffers.h>

namespace rfl::flatbuf {

inline flatbuffers::voffset_t calc_vtable_offset(const size_t _i) {
return 2 * sizeof(flatbuffers::voffset_t) +
static_cast<flatbuffers::voffset_t>(_i) *
sizeof(flatbuffers::voffset_t);
}

} // namespace rfl::flatbuf

#endif

3 changes: 3 additions & 0 deletions include/rfl/parsing/ViewReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class ViewReader {
std::make_integer_sequence<int, size_>());
}

/// Returns the size of the underlying view.
static constexpr size_t size() { return size_; }

private:
template <int i, class FieldType>
static bool is_matching(const int _current_index) {
Expand Down

0 comments on commit 277a8ac

Please sign in to comment.