proto_stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_IO_PROTO_STREAM_H_
18 #define CARTOGRAPHER_IO_PROTO_STREAM_H_
19 
20 #include <fstream>
21 
23 
24 namespace cartographer {
25 namespace io {
26 
27 // A simple writer of a compressed sequence of protocol buffer messages to a
28 // file. The format is not intended to be compatible with any other format used
29 // outside of Cartographer.
30 //
31 // TODO(whess): Compress the file instead of individual messages for better
32 // compression performance? Should we use LZ4?
34  public:
35  ProtoStreamWriter(const string& filename);
37 
38  ProtoStreamWriter(const ProtoStreamWriter&) = delete;
40 
41  // Serializes, compressed and writes the 'proto' to the file.
42  template <typename MessageType>
43  void WriteProto(const MessageType& proto) {
44  string uncompressed_data;
45  proto.SerializeToString(&uncompressed_data);
46  Write(uncompressed_data);
47  }
48 
49  // This should be called to check whether writing was successful.
50  bool Close();
51 
52  private:
53  void Write(const string& uncompressed_data);
54 
55  std::ofstream out_;
56 };
57 
58 // A reader of the format produced by ProtoStreamWriter.
60  public:
61  ProtoStreamReader(const string& filename);
63 
64  ProtoStreamReader(const ProtoStreamReader&) = delete;
66 
67  template <typename MessageType>
68  bool ReadProto(MessageType* proto) {
69  string decompressed_data;
70  return Read(&decompressed_data) &&
71  proto->ParseFromString(decompressed_data);
72  }
73 
74  private:
75  bool Read(string* decompressed_data);
76 
77  std::ifstream in_;
78 };
79 
80 } // namespace io
81 } // namespace cartographer
82 
83 #endif // CARTOGRAPHER_IO_PROTO_STREAM_H_
void WriteProto(const MessageType &proto)
Definition: proto_stream.h:43
ProtoStreamWriter(const string &filename)
Definition: proto_stream.cc:45
ProtoStreamWriter & operator=(const ProtoStreamWriter &)=delete
bool ReadProto(MessageType *proto)
Definition: proto_stream.h:68
void Write(const string &uncompressed_data)
Definition: proto_stream.cc:52


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:39