Skip to content

Commit

Permalink
Make sure flatbuf::read compiles (but doesn't run yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 26, 2025
1 parent 4e4b9ee commit d97999e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion include/rfl/flatbuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "flatbuf/Schema.hpp"
#include "flatbuf/Writer.hpp"
// #include "flatbuf/load.hpp"
// #include "flatbuf/read.hpp"
#include "flatbuf/read.hpp"
// #include "flatbuf/save.hpp"
#include "flatbuf/to_schema.hpp"
#include "flatbuf/write.hpp"
Expand Down
7 changes: 2 additions & 5 deletions include/rfl/flatbuf/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Reader {
const size_t size = static_cast<size_t>(_arr.val_->size()) / elem_size;
for (size_t i = 0; i < size; ++i) {
const auto err = _array_reader.read(
InputVarType{flatbuffers::GetAnyVectorElemAddressOf<const uint8_t*>(
InputVarType{flatbuffers::GetAnyVectorElemAddressOf<const uint8_t>(
_arr.val_, i, elem_size)});
if (err) {
return err;
Expand All @@ -127,11 +127,8 @@ class Reader {
const InputObjectType& _obj) const noexcept {
constexpr size_t size = ObjectReader::size();
for (size_t i = 0; i < size; ++i) {
const auto err = _object_reader.read(
_object_reader.read(
i, InputVarType{_obj.val_->GetAddressOf(calc_vtable_offset(i))});
if (err) {
return err;
}
}
return std::nullopt;
}
Expand Down
25 changes: 3 additions & 22 deletions include/rfl/flatbuf/read.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#ifndef RFL_FLATBUF_READ_HPP_
#define RFL_FLATBUF_READ_HPP_

#include <capnp/dynamic.h>
#include <capnp/serialize-packed.h>
#include <kj/io.h>
#include <flatbuffers/flatbuffers.h>

#include <bit>
#include <istream>
#include <string>
#include <type_traits>

#include "../Processors.hpp"
#include "../SnakeCaseToCamelCase.hpp"
#include "../internal/strings/strings.hpp"
#include "../internal/wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
#include "Schema.hpp"
#include "get_root_name.hpp"
#include "to_schema.hpp"

namespace rfl::flatbuf {

Expand All @@ -36,24 +30,11 @@ auto read(const InputVarType& _obj) {
template <class T, class... Ps>
Result<internal::wrap_in_rfl_array_t<T>> read(const char* _bytes,
const size_t _size) {
const auto array_ptr = kj::ArrayPtr<const kj::byte>(
internal::ptr_cast<const kj::byte*>(_bytes), _size);
auto input_stream = kj::ArrayInputStream(array_ptr);
auto message_reader = capnp::PackedMessageReader(input_stream);
const auto root_name = get_root_name<std::remove_cv_t<T>, Ps...>();
const auto root_schema = _schema.value().getNested(root_name.c_str());
const auto input_var = InputVarType{
message_reader.getRoot<capnp::DynamicStruct>(root_schema.asStruct())};
const auto input_var = InputVarType{flatbuffers::GetRoot<const uint8_t>(
internal::ptr_cast<const uint8_t*>(_bytes))};
return read<T, Ps...>(input_var);
}

/// Parses an object from flatbuffers using reflection.
template <class T, class... Ps>
auto read(const char* _bytes, const size_t _size) {
const auto schema = to_schema<std::remove_cvref_t<T>, Ps...>();
return read<T, Ps...>(_bytes, _size, schema);
}

/// Parses an object from flatbuffers using reflection.
template <class T, class... Ps>
auto read(const std::vector<char>& _bytes) {
Expand Down
4 changes: 2 additions & 2 deletions tests/flatbuffers/test_person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string>
#include <vector>

// #include "write_and_read.hpp"
#include "write_and_read.hpp"

namespace test_tutorial_example {

Expand All @@ -26,6 +26,6 @@ TEST(flatbuffers, test_person) {
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};

rfl::flatbuf::write(homer, std::cout) << std::endl << std::endl;
write_and_read(homer);
}
} // namespace test_tutorial_example
20 changes: 20 additions & 0 deletions tests/flatbuffers/write_and_read.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef WRITE_AND_READ_
#define WRITE_AND_READ_

#include <gtest/gtest.h>

#include <iostream>
#include <rfl/flatbuf.hpp>
#include <string>

template <class... Ps>
void write_and_read(const auto& _struct) {
using T = std::remove_cvref_t<decltype(_struct)>;
const auto serialized1 = rfl::flatbuf::write<Ps...>(_struct);
const auto res = rfl::flatbuf::read<T, Ps...>(serialized1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().value().what();
const auto serialized2 = rfl::flatbuf::write<Ps...>(res.value());
EXPECT_EQ(serialized1, serialized2);
}
#endif

0 comments on commit d97999e

Please sign in to comment.