file_writer.h
Go to the documentation of this file.
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_FILE_WRITER_H_
00018 #define CARTOGRAPHER_IO_FILE_WRITER_H_
00019 
00020 #include <fstream>
00021 #include <functional>
00022 #include <memory>
00023 
00024 #include "cartographer/common/port.h"
00025 
00026 namespace cartographer {
00027 namespace io {
00028 
00029 // Simple abstraction for a file.
00030 class FileWriter {
00031  public:
00032   FileWriter() {}
00033   FileWriter(const FileWriter&) = delete;
00034   FileWriter& operator=(const FileWriter&) = delete;
00035 
00036   virtual ~FileWriter() {}
00037 
00038   // Write 'data' to the beginning of the file. This is required to overwrite
00039   // fixed size headers which contain the number of points once we actually know
00040   // how many points there are.
00041   virtual bool WriteHeader(const char* data, size_t len) = 0;
00042 
00043   virtual bool Write(const char* data, size_t len) = 0;
00044   virtual bool Close() = 0;
00045   virtual std::string GetFilename() = 0;
00046 };
00047 
00048 // An Implementation of file using std::ofstream.
00049 class StreamFileWriter : public FileWriter {
00050  public:
00051   ~StreamFileWriter() override;
00052 
00053   StreamFileWriter(const std::string& filename);
00054 
00055   bool Write(const char* data, size_t len) override;
00056   bool WriteHeader(const char* data, size_t len) override;
00057   bool Close() override;
00058   std::string GetFilename() override;
00059 
00060  private:
00061   const std::string filename_;
00062   std::ofstream out_;
00063 };
00064 
00065 using FileWriterFactory =
00066     std::function<std::unique_ptr<FileWriter>(const std::string& filename)>;
00067 
00068 }  // namespace io
00069 }  // namespace cartographer
00070 
00071 #endif  // CARTOGRAPHER_IO_FILE_WRITER_H_


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35