Skip to content

Commit

Permalink
Implemented rfl::capnproto::read
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Dec 26, 2024
1 parent 0376a31 commit 06ed35a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/rfl/capnproto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#define RFL_CAPNPROTO_HPP_

#include "../rfl.hpp"
// #include "capnproto/Parser.hpp"
#include "capnproto/Parser.hpp"
#include "capnproto/Reader.hpp"
#include "capnproto/Schema.hpp"
#include "capnproto/Writer.hpp"
// #include "capnproto/load.hpp"
// #include "capnproto/read.hpp"
#include "capnproto/read.hpp"
// #include "capnproto/save.hpp"
// #include "capnproto/to_schema.hpp"
#include "capnproto/write.hpp"
Expand Down
27 changes: 12 additions & 15 deletions include/rfl/capnproto/read.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef RFL_CAPNPROTO_READ_HPP_
#define RFL_CAPNPROTO_READ_HPP_

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

#include <bit>
#include <istream>
Expand Down Expand Up @@ -31,20 +33,15 @@ 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 Schema<T>& _schema) noexcept {
capnproto_reader_t capnproto_reader = capnproto_reader_memory(_bytes, _size);
capnproto_value_t root;
capnproto_generic_value_new(_schema.iface(), &root);
auto err = capnproto_value_read(capnproto_reader, &root);
if (err) {
capnproto_value_decref(&root);
capnproto_reader_free(capnproto_reader);
return Error(std::string("Could not read root value: ") +
capnproto_strerror());
}
auto result = read<T, Ps...>(InputVarType{&root});
capnproto_value_decref(&root);
capnproto_reader_free(capnproto_reader);
return result;
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);
// TODO: Person is hardcoded.
const auto root_schema = _schema.value().getNested("Person");
const auto input_var = InputVarType{
message_reader.getRoot<capnp::DynamicStruct>(root_schema.asStruct())};
return read<T, Ps...>(input_var);
}

/// Parses an object from CAPNPROTO using reflection.
Expand Down
7 changes: 5 additions & 2 deletions tests/capnproto/test_person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ struct Person {
TEST(capnproto, test_person) {
const auto schema =
rfl::capnproto::Schema<Person>::from_string(PERSON_SCHEMA).value();
const auto homer = Person{.firstName = "Homer", .lastName = "Simpson"};
std::cout << rfl::capnproto::write(homer, schema).data() << std::endl;
const auto homer1 = Person{.firstName = "Homer", .lastName = "Simpson"};
const auto serialized1 = rfl::capnproto::write(homer1, schema);
const auto homer2 = rfl::capnproto::read<Person>(serialized1, schema).value();
const auto serialized2 = rfl::capnproto::write(homer2, schema);
EXPECT_EQ(serialized1, serialized2);
}
} // namespace test_tutorial_example

0 comments on commit 06ed35a

Please sign in to comment.