diff --git a/cartographer/io/in_memory_proto_stream.cc b/cartographer/io/in_memory_proto_stream.cc new file mode 100644 index 0000000..0773472 --- /dev/null +++ b/cartographer/io/in_memory_proto_stream.cc @@ -0,0 +1,30 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cartographer/io/in_memory_proto_stream.h" + +namespace cartographer { +namespace io { + +bool InMemoryProtoStreamReader::ReadProto(google::protobuf::Message* proto) { + if (eof()) return false; + proto->CopyFrom(*map_chunks_.front()); + map_chunks_.pop(); + return true; +} + +} // namespace io +} // namespace cartographer diff --git a/cartographer/io/in_memory_proto_stream.h b/cartographer/io/in_memory_proto_stream.h new file mode 100644 index 0000000..ced89d3 --- /dev/null +++ b/cartographer/io/in_memory_proto_stream.h @@ -0,0 +1,58 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CARTOGRAPHER_IO_IN_MEMORY_PROTO_STREAM_H_ +#define CARTOGRAPHER_IO_IN_MEMORY_PROTO_STREAM_H_ + +#include + +#include "cartographer/common/make_unique.h" +#include "cartographer/common/port.h" +#include "cartographer/io/proto_stream_interface.h" +#include "google/protobuf/message.h" + +namespace cartographer { +namespace io { + +class InMemoryProtoStreamReader + : public cartographer::io::ProtoStreamReaderInterface { + public: + explicit InMemoryProtoStreamReader( + std::queue>&& map_chunks) + : map_chunks_(std::move(map_chunks)){}; + InMemoryProtoStreamReader() = default; + ~InMemoryProtoStreamReader() = default; + + InMemoryProtoStreamReader(const InMemoryProtoStreamReader&) = delete; + InMemoryProtoStreamReader& operator=(const InMemoryProtoStreamReader&) = + delete; + + template + void AddProto(const MessageType& proto) { + map_chunks_.push(common::make_unique(proto)); + } + + bool ReadProto(google::protobuf::Message* proto) override; + bool eof() const override { return map_chunks_.empty(); } + + private: + std::queue> map_chunks_; +}; + +} // namespace io +} // namespace cartographer + +#endif // CARTOGRAPHER_IO_IN_MEMORY_PROTO_STREAM_H_ diff --git a/cartographer/io/in_memory_proto_stream_test.cc b/cartographer/io/in_memory_proto_stream_test.cc new file mode 100644 index 0000000..f2ef8dc --- /dev/null +++ b/cartographer/io/in_memory_proto_stream_test.cc @@ -0,0 +1,83 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cartographer/io/in_memory_proto_stream.h" +#include "cartographer/mapping/proto/pose_graph.pb.h" +#include "cartographer/mapping/proto/serialization.pb.h" + +#include "gtest/gtest.h" + +namespace cartographer { +namespace io { +namespace { + +using common::make_unique; +using google::protobuf::Message; +using mapping::proto::PoseGraph; +using mapping::proto::SerializedData; + +class InMemoryProtoStreamTest : public ::testing::Test { + protected: + void SetUp() override { + pose_graph_.add_trajectory()->set_trajectory_id(1); + serialized_data_.mutable_odometry_data()->set_trajectory_id(2); + } + + PoseGraph pose_graph_; + SerializedData serialized_data_; +}; + +TEST_F(InMemoryProtoStreamTest, ReadStreamInitializedFromQueue) { + std::queue> proto_queue; + proto_queue.push(make_unique(pose_graph_)); + proto_queue.push(make_unique(serialized_data_)); + + InMemoryProtoStreamReader reader(std::move(proto_queue)); + + PoseGraph actual_pose_graph; + EXPECT_FALSE(reader.eof()); + EXPECT_TRUE(reader.ReadProto(&actual_pose_graph)); + EXPECT_EQ(1, actual_pose_graph.trajectory(0).trajectory_id()); + + SerializedData actual_serialized_data; + EXPECT_FALSE(reader.eof()); + EXPECT_TRUE(reader.ReadProto(&actual_serialized_data)); + EXPECT_EQ(2, actual_serialized_data.odometry_data().trajectory_id()); + + EXPECT_TRUE(reader.eof()); +} + +TEST_F(InMemoryProtoStreamTest, ReadStreamInitializedIncrementally) { + InMemoryProtoStreamReader reader; + reader.AddProto(pose_graph_); + reader.AddProto(serialized_data_); + + PoseGraph actual_pose_graph; + EXPECT_FALSE(reader.eof()); + EXPECT_TRUE(reader.ReadProto(&actual_pose_graph)); + EXPECT_EQ(1, actual_pose_graph.trajectory(0).trajectory_id()); + + SerializedData actual_serialized_data; + EXPECT_FALSE(reader.eof()); + EXPECT_TRUE(reader.ReadProto(&actual_serialized_data)); + EXPECT_EQ(2, actual_serialized_data.odometry_data().trajectory_id()); + + EXPECT_TRUE(reader.eof()); +} + +} // namespace +} // namespace io +} // namespace cartographer