From 06ed35ae41a8c63af47b327cd0be0e2fb90166e8 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Thu, 26 Dec 2024 12:45:12 +0100 Subject: [PATCH] Implemented rfl::capnproto::read --- include/rfl/capnproto.hpp | 4 ++-- include/rfl/capnproto/read.hpp | 27 ++++++++++++--------------- tests/capnproto/test_person.cpp | 7 +++++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/rfl/capnproto.hpp b/include/rfl/capnproto.hpp index d3d74ca0..52a35c06 100644 --- a/include/rfl/capnproto.hpp +++ b/include/rfl/capnproto.hpp @@ -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" diff --git a/include/rfl/capnproto/read.hpp b/include/rfl/capnproto/read.hpp index 01e8c0bd..f8d95efb 100644 --- a/include/rfl/capnproto/read.hpp +++ b/include/rfl/capnproto/read.hpp @@ -1,7 +1,9 @@ #ifndef RFL_CAPNPROTO_READ_HPP_ #define RFL_CAPNPROTO_READ_HPP_ -#include +#include +#include +#include #include #include @@ -31,20 +33,15 @@ auto read(const InputVarType& _obj) { template Result> read( const char* _bytes, const size_t _size, const Schema& _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(InputVarType{&root}); - capnproto_value_decref(&root); - capnproto_reader_free(capnproto_reader); - return result; + const auto array_ptr = kj::ArrayPtr( + internal::ptr_cast(_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(root_schema.asStruct())}; + return read(input_var); } /// Parses an object from CAPNPROTO using reflection. diff --git a/tests/capnproto/test_person.cpp b/tests/capnproto/test_person.cpp index 67b8dbe8..f3e16383 100644 --- a/tests/capnproto/test_person.cpp +++ b/tests/capnproto/test_person.cpp @@ -27,7 +27,10 @@ struct Person { TEST(capnproto, test_person) { const auto schema = rfl::capnproto::Schema::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(serialized1, schema).value(); + const auto serialized2 = rfl::capnproto::write(homer2, schema); + EXPECT_EQ(serialized1, serialized2); } } // namespace test_tutorial_example