#ifndef RFL_CAPNPROTO_WRITE_HPP_ #define RFL_CAPNPROTO_WRITE_HPP_ #include #include #include #include #include #include #include #include #include "../SnakeCaseToCamelCase.hpp" #include "../internal/ptr_cast.hpp" //#include "../internal/strings/strings.hpp" #include "../parsing/Parent.hpp" #include "Parser.hpp" //#include "Schema.hpp" #include "Writer.hpp" #include "get_root_name.hpp" #include "to_schema.hpp" namespace rfl::capnproto { /// Returns CAPNPROTO bytes. template std::vector write(const auto& _obj, const auto& _schema) { using T = std::remove_cvref_t; using U = typename std::remove_cvref_t::Type; using ParentType = parsing::Parent; static_assert(std::is_same(), "The schema must be compatible with the type to write."); const auto root_name = get_root_name(); const auto root_schema = _schema.value().getNested(root_name.c_str()); capnp::MallocMessageBuilder message_builder; auto root = message_builder.initRoot(root_schema.asStruct()); const auto writer = Writer(&root); Parser>::write( writer, _obj, typename ParentType::Root{}); kj::VectorOutputStream output_stream; capnp::writePackedMessage(output_stream, message_builder); auto arr_ptr = output_stream.getArray(); return std::vector(internal::ptr_cast(arr_ptr.begin()), internal::ptr_cast(arr_ptr.end())); } /// Returns CAPNPROTO bytes. template std::vector write(const auto& _obj) { using T = std::remove_cvref_t; const auto schema = to_schema(); return write(_obj, schema); } /// Writes a CAPNPROTO into an ostream. template std::ostream& write(const auto& _obj, std::ostream& _stream) { auto buffer = write(_obj); _stream.write(buffer.data(), buffer.size()); return _stream; } template std::ostream& write(const auto& _obj, const auto& _schema, std::ostream& _stream) { auto buffer = write(_obj, _schema); _stream.write(buffer.data(), buffer.size()); return _stream; } } // namespace rfl::capnproto #endif