Introduce InMemoryProtoStreamReader. (#844)

* Introduce InMemoryProtoStreamReader.

* Move inmemory*.* to in_memory*.*.
master
Alexander Belyaev 2018-01-24 13:58:15 +01:00 committed by GitHub
parent 52527ec6d4
commit aee1bc46dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 171 additions and 0 deletions

View File

@ -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

View File

@ -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 <queue>
#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<std::unique_ptr<google::protobuf::Message>>&& map_chunks)
: map_chunks_(std::move(map_chunks)){};
InMemoryProtoStreamReader() = default;
~InMemoryProtoStreamReader() = default;
InMemoryProtoStreamReader(const InMemoryProtoStreamReader&) = delete;
InMemoryProtoStreamReader& operator=(const InMemoryProtoStreamReader&) =
delete;
template <typename MessageType>
void AddProto(const MessageType& proto) {
map_chunks_.push(common::make_unique<MessageType>(proto));
}
bool ReadProto(google::protobuf::Message* proto) override;
bool eof() const override { return map_chunks_.empty(); }
private:
std::queue<std::unique_ptr<google::protobuf::Message>> map_chunks_;
};
} // namespace io
} // namespace cartographer
#endif // CARTOGRAPHER_IO_IN_MEMORY_PROTO_STREAM_H_

View File

@ -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<std::unique_ptr<Message>> proto_queue;
proto_queue.push(make_unique<PoseGraph>(pose_graph_));
proto_queue.push(make_unique<SerializedData>(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