From edb18231b66b98bc6697f16056a5e4d33ea56b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCtte?= Date: Thu, 25 Jan 2018 15:27:50 +0100 Subject: [PATCH] Add ProtoStreamWriterInterface and implement forwarding writer. (#849) --- cartographer/io/in_memory_proto_stream.cc | 9 +++++++++ cartographer/io/in_memory_proto_stream.h | 20 +++++++++++++++++++ cartographer/io/proto_stream.h | 9 +++------ cartographer/io/proto_stream_interface.h | 12 +++++++++++ cartographer/mapping/map_builder.cc | 2 +- cartographer/mapping/map_builder.h | 2 +- cartographer/mapping/map_builder_interface.h | 2 +- cartographer_grpc/client_server_test.cc | 3 ++- cartographer_grpc/mapping/map_builder_stub.cc | 2 +- cartographer_grpc/mapping/map_builder_stub.h | 3 ++- 10 files changed, 52 insertions(+), 12 deletions(-) diff --git a/cartographer/io/in_memory_proto_stream.cc b/cartographer/io/in_memory_proto_stream.cc index 0773472..f1ca0cc 100644 --- a/cartographer/io/in_memory_proto_stream.cc +++ b/cartographer/io/in_memory_proto_stream.cc @@ -16,9 +16,18 @@ #include "cartographer/io/in_memory_proto_stream.h" +#include "glog/logging.h" + namespace cartographer { namespace io { +void ForwardingProtoStreamWriter::WriteProto( + const google::protobuf::Message& proto) { + CHECK(writer_callback_(&proto)); +} + +bool ForwardingProtoStreamWriter::Close() { return writer_callback_(nullptr); } + bool InMemoryProtoStreamReader::ReadProto(google::protobuf::Message* proto) { if (eof()) return false; proto->CopyFrom(*map_chunks_.front()); diff --git a/cartographer/io/in_memory_proto_stream.h b/cartographer/io/in_memory_proto_stream.h index ced89d3..4c368f1 100644 --- a/cartographer/io/in_memory_proto_stream.h +++ b/cartographer/io/in_memory_proto_stream.h @@ -27,6 +27,26 @@ namespace cartographer { namespace io { +class ForwardingProtoStreamWriter + : public cartographer::io::ProtoStreamWriterInterface { + public: + // A callback that is invoked anytime 'WriteProto()' is called on the + // 'ForwardingProtoStreamWriter'. When 'Close()' is called on the + // 'ForwardingProtoStreamWriter' the callback is invoked with a 'nullptr'. + using WriterCallback = + std::function; + + explicit ForwardingProtoStreamWriter(WriterCallback writer_callback) + : writer_callback_(writer_callback) {} + ~ForwardingProtoStreamWriter() = default; + + void WriteProto(const google::protobuf::Message& proto) override; + bool Close() override; + + private: + WriterCallback writer_callback_; +}; + class InMemoryProtoStreamReader : public cartographer::io::ProtoStreamReaderInterface { public: diff --git a/cartographer/io/proto_stream.h b/cartographer/io/proto_stream.h index f5c0cbe..fa27056 100644 --- a/cartographer/io/proto_stream.h +++ b/cartographer/io/proto_stream.h @@ -32,7 +32,7 @@ namespace io { // // TODO(whess): Compress the file instead of individual messages for better // compression performance? Should we use LZ4? -class ProtoStreamWriter { +class ProtoStreamWriter : public ProtoStreamWriterInterface { public: ProtoStreamWriter(const std::string& filename); ~ProtoStreamWriter() = default; @@ -40,11 +40,8 @@ class ProtoStreamWriter { ProtoStreamWriter(const ProtoStreamWriter&) = delete; ProtoStreamWriter& operator=(const ProtoStreamWriter&) = delete; - // Serializes, compressed and writes the 'proto' to the file. - void WriteProto(const google::protobuf::Message& proto); - - // This should be called to check whether writing was successful. - bool Close(); + void WriteProto(const google::protobuf::Message& proto) override; + bool Close() override; private: void Write(const std::string& uncompressed_data); diff --git a/cartographer/io/proto_stream_interface.h b/cartographer/io/proto_stream_interface.h index 62b6be1..6b39c2a 100644 --- a/cartographer/io/proto_stream_interface.h +++ b/cartographer/io/proto_stream_interface.h @@ -23,6 +23,18 @@ namespace cartographer { namespace io { +// A writer for writing proto messages to a pbstream. +class ProtoStreamWriterInterface { + public: + virtual ~ProtoStreamWriterInterface(){}; + + // Serializes, compressed and writes the 'proto' to the file. + virtual void WriteProto(const google::protobuf::Message& proto) = 0; + + // This should be called to check whether writing was successful. + virtual bool Close() = 0; +}; + // A reader of the format produced by ProtoStreamWriter. class ProtoStreamReaderInterface { public: diff --git a/cartographer/mapping/map_builder.cc b/cartographer/mapping/map_builder.cc index c5c1d42..a6ee1d1 100644 --- a/cartographer/mapping/map_builder.cc +++ b/cartographer/mapping/map_builder.cc @@ -168,7 +168,7 @@ std::string MapBuilder::SubmapToProto( return ""; } -void MapBuilder::SerializeState(io::ProtoStreamWriter* const writer) { +void MapBuilder::SerializeState(io::ProtoStreamWriterInterface* const writer) { // We serialize the pose graph followed by all the data referenced in it. writer->WriteProto(pose_graph_->ToProto()); // Next we serialize all submap data. diff --git a/cartographer/mapping/map_builder.h b/cartographer/mapping/map_builder.h index 3d717f4..de17686 100644 --- a/cartographer/mapping/map_builder.h +++ b/cartographer/mapping/map_builder.h @@ -60,7 +60,7 @@ class MapBuilder : public MapBuilderInterface { std::string SubmapToProto(const SubmapId& submap_id, proto::SubmapQuery::Response* response) override; - void SerializeState(io::ProtoStreamWriter* writer) override; + void SerializeState(io::ProtoStreamWriterInterface* writer) override; void LoadMap(io::ProtoStreamReaderInterface* reader) override; diff --git a/cartographer/mapping/map_builder_interface.h b/cartographer/mapping/map_builder_interface.h index e59a19f..b4b0ba6 100644 --- a/cartographer/mapping/map_builder_interface.h +++ b/cartographer/mapping/map_builder_interface.h @@ -74,7 +74,7 @@ class MapBuilderInterface { proto::SubmapQuery::Response* response) = 0; // Serializes the current state to a proto stream. - virtual void SerializeState(io::ProtoStreamWriter* writer) = 0; + virtual void SerializeState(io::ProtoStreamWriterInterface* writer) = 0; // Loads submaps from a proto stream into a new frozen trajectory. virtual void LoadMap(io::ProtoStreamReaderInterface* reader) = 0; diff --git a/cartographer_grpc/client_server_test.cc b/cartographer_grpc/client_server_test.cc index 2271038..8ddb6d5 100644 --- a/cartographer_grpc/client_server_test.cc +++ b/cartographer_grpc/client_server_test.cc @@ -56,7 +56,8 @@ class MockMapBuilder : public cartographer::mapping::MapBuilderInterface { SubmapToProto, std::string(const cartographer::mapping::SubmapId&, cartographer::mapping::proto::SubmapQuery::Response*)); - MOCK_METHOD1(SerializeState, void(cartographer::io::ProtoStreamWriter*)); + MOCK_METHOD1(SerializeState, + void(cartographer::io::ProtoStreamWriterInterface*)); MOCK_METHOD1(LoadMap, void(cartographer::io::ProtoStreamReaderInterface*)); MOCK_CONST_METHOD0(num_trajectory_builders, int()); MOCK_METHOD0(pose_graph, PoseGraphInterface*()); diff --git a/cartographer_grpc/mapping/map_builder_stub.cc b/cartographer_grpc/mapping/map_builder_stub.cc index 9280f0d..61efb16 100644 --- a/cartographer_grpc/mapping/map_builder_stub.cc +++ b/cartographer_grpc/mapping/map_builder_stub.cc @@ -88,7 +88,7 @@ std::string MapBuilderStub::SubmapToProto( } void MapBuilderStub::SerializeState( - cartographer::io::ProtoStreamWriter* writer) { + cartographer::io::ProtoStreamWriterInterface* writer) { LOG(FATAL) << "Not implemented"; } diff --git a/cartographer_grpc/mapping/map_builder_stub.h b/cartographer_grpc/mapping/map_builder_stub.h index 5b06ab5..e375a2b 100644 --- a/cartographer_grpc/mapping/map_builder_stub.h +++ b/cartographer_grpc/mapping/map_builder_stub.h @@ -47,7 +47,8 @@ class MapBuilderStub : public cartographer::mapping::MapBuilderInterface { std::string SubmapToProto( const cartographer::mapping::SubmapId& submap_id, cartographer::mapping::proto::SubmapQuery::Response* response) override; - void SerializeState(cartographer::io::ProtoStreamWriter* writer) override; + void SerializeState( + cartographer::io::ProtoStreamWriterInterface* writer) override; void LoadMap(cartographer::io::ProtoStreamReaderInterface* reader) override; int num_trajectory_builders() const override; cartographer::mapping::PoseGraphInterface* pose_graph() override;