file_writer.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_FILE_WRITER_H_
18 #define CARTOGRAPHER_IO_FILE_WRITER_H_
19 
20 #include <fstream>
21 #include <functional>
22 #include <memory>
23 
25 
26 namespace cartographer {
27 namespace io {
28 
29 // Simple abstraction for a file.
30 class FileWriter {
31  public:
33  FileWriter(const FileWriter&) = delete;
34  FileWriter& operator=(const FileWriter&) = delete;
35 
36  virtual ~FileWriter() {}
37 
38  // Write 'data' to the beginning of the file. This is required to overwrite
39  // fixed size headers which contain the number of points once we actually know
40  // how many points there are.
41  virtual bool WriteHeader(const char* data, size_t len) = 0;
42 
43  virtual bool Write(const char* data, size_t len) = 0;
44  virtual bool Close() = 0;
45  virtual std::string GetFilename() = 0;
46 };
47 
48 // An Implementation of file using std::ofstream.
49 class StreamFileWriter : public FileWriter {
50  public:
51  ~StreamFileWriter() override;
52 
53  StreamFileWriter(const std::string& filename);
54 
55  bool Write(const char* data, size_t len) override;
56  bool WriteHeader(const char* data, size_t len) override;
57  bool Close() override;
58  std::string GetFilename() override;
59 
60  private:
61  const std::string filename_;
62  std::ofstream out_;
63 };
64 
65 using FileWriterFactory =
66  std::function<std::unique_ptr<FileWriter>(const std::string& filename)>;
67 
68 } // namespace io
69 } // namespace cartographer
70 
71 #endif // CARTOGRAPHER_IO_FILE_WRITER_H_
std::function< std::unique_ptr< FileWriter >(const std::string &filename)> FileWriterFactory
Definition: file_writer.h:66
FileWriter & operator=(const FileWriter &)=delete
virtual std::string GetFilename()=0
virtual bool WriteHeader(const char *data, size_t len)=0
virtual bool Write(const char *data, size_t len)=0


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58