00001 /* 00002 * Copyright 2016 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_IO_PROTO_STREAM_H_ 00018 #define CARTOGRAPHER_IO_PROTO_STREAM_H_ 00019 00020 #include <fstream> 00021 00022 #include "cartographer/common/port.h" 00023 #include "cartographer/io/proto_stream_interface.h" 00024 #include "google/protobuf/message.h" 00025 00026 namespace cartographer { 00027 namespace io { 00028 00029 // A simple writer of a compressed sequence of protocol buffer messages to a 00030 // file. The format is not intended to be compatible with any other format used 00031 // outside of Cartographer. 00032 // 00033 // TODO(whess): Compress the file instead of individual messages for better 00034 // compression performance? Should we use LZ4? 00035 class ProtoStreamWriter : public ProtoStreamWriterInterface { 00036 public: 00037 ProtoStreamWriter(const std::string& filename); 00038 ~ProtoStreamWriter() = default; 00039 00040 ProtoStreamWriter(const ProtoStreamWriter&) = delete; 00041 ProtoStreamWriter& operator=(const ProtoStreamWriter&) = delete; 00042 00043 void WriteProto(const google::protobuf::Message& proto) override; 00044 bool Close() override; 00045 00046 private: 00047 void Write(const std::string& uncompressed_data); 00048 00049 std::ofstream out_; 00050 }; 00051 00052 // A reader of the format produced by ProtoStreamWriter. 00053 class ProtoStreamReader : public ProtoStreamReaderInterface { 00054 public: 00055 explicit ProtoStreamReader(const std::string& filename); 00056 ~ProtoStreamReader() = default; 00057 00058 ProtoStreamReader(const ProtoStreamReader&) = delete; 00059 ProtoStreamReader& operator=(const ProtoStreamReader&) = delete; 00060 00061 bool ReadProto(google::protobuf::Message* proto) override; 00062 bool eof() const override; 00063 00064 private: 00065 bool Read(std::string* decompressed_data); 00066 00067 std::ifstream in_; 00068 }; 00069 00070 } // namespace io 00071 } // namespace cartographer 00072 00073 #endif // CARTOGRAPHER_IO_PROTO_STREAM_H_