cartographer源码分析(26)-io-file_writer.h
2017-07-28 21:24
435 查看
源码可在https://github.com/learnmoreonce/SLAM 下载
本文发于:
* http://www.jianshu.com/u/9e38d2febec1
* https://zhuanlan.zhihu.com/learnmoreonce
* http://blog.csdn.net/learnmoreonce
* slam源码分析微信公众号:slamcode
文件:io/file_writer.h #ifndef CARTOGRAPHER_IO_FILE_WRITER_H_ #define CARTOGRAPHER_IO_FILE_WRITER_H_ #include <fstream> #include <functional> #include <memory> #include "cartographer/common/port.h" namespace cartographer { namespace io { /* FileWriter负责文件写入的虚基类,不可拷贝/赋值 没有数据成员.只提供一系列抽象接口. 1),WriteHeader(),写入文件head 2),Write(),写入数据 3),Close(),关闭文件 */ // Simple abstraction for a file. class FileWriter { public: FileWriter() {} FileWriter(const FileWriter&) = delete; FileWriter& operator=(const FileWriter&) = delete; virtual ~FileWriter() {} // Write 'data' to the beginning of the file. This is required to overwrite // fixed size headers which contain the number of points once we actually know // how many points there are. virtual bool WriteHeader(const char* data, size_t len) = 0; virtual bool Write(const char* data, size_t len) = 0; virtual bool Close() = 0; }; /* StreamFileWriter 文件流写入类,继承自FileWriter 数据成员只有一个std::ofstream out_负责将文件写入磁盘 */ // An Implementation of file using std::ofstream. class StreamFileWriter : public FileWriter { public: ~StreamFileWriter() override; StreamFileWriter(const string& filename); bool Write(const char* data, size_t len) override; bool WriteHeader(const char* data, size_t len) override; //从文件开始处写入,覆盖旧数据 bool Close() override; private: std::ofstream out_; }; /*工厂模式, 创建一个FileWriter对象,由智能指针管理生命期, 返回值是std::uniq 4000 ue_ptr<FileWriter>, 函数参数是string. */ using FileWriterFactory = std::function<std::unique_ptr<FileWriter>(const string& filename)>; } // namespace io } // namespace cartographer #endif // CARTOGRAPHER_IO_FILE_WRITER_H_
io/file_writer.cc #include "cartographer/io/file_writer.h" namespace cartographer { namespace io { //out:File open for writing: the internal stream buffer supports output operations. //binary:Operations are performed in binary mode rather than text. StreamFileWriter::StreamFileWriter(const string& filename) : out_(filename, std::ios::out | std::ios::binary) {}//打开,并以2进制方式写入 StreamFileWriter::~StreamFileWriter() {} //写入len个char bool StreamFileWriter::Write(const char* const data, const size_t len) { if (out_.bad()) { return false; } out_.write(data, len); return !out_.bad(); } bool StreamFileWriter::Close() { //关闭文件 if (out_.bad()) { return false; } out_.close(); return !out_.bad(); } bool StreamFileWriter::WriteHeader(const char* const data, const size_t len) { if (out_.bad()) { return false; } out_.flush(); //清空buffer out_.seekp(0); //偏移量为0 return Write(data, len); } } // namespace io } // namespace cartographer
本文发于:
* http://www.jianshu.com/u/9e38d2febec1
* https://zhuanlan.zhihu.com/learnmoreonce
* http://blog.csdn.net/learnmoreonce
* slam源码分析微信公众号:slamcode
相关文章推荐
- cartographer源码分析(37)-io-pcd_writing_points_processor.h
- Java IO 之 FileInputStream & FileOutputStream源码分析
- cartographer源码分析(32)-io-intensity_to_color_points_processor.h
- cartographer源码分析(38)-io-null_points_processor.h
- 【Java部分源码分析之io篇】7.FileReader
- cartographer源码分析(33)-io-ply_writing_points_processor.h
- cartographer源码分析(34)-io-coloring_points_processor.h
- [置顶] 40-总结-【cartographer源码分析】系列的第四部分【io源码分析】
- Alluxio源码分析读数据:打开文件选项OpenFileOptions
- JavaIO(3):Writer,OutputStreamWriter和FileWriter源码分析
- cartographer源码分析(35)-io-fixed_ratio_sampling_points_processor.h
- Java IO:CharArrayWriter使用及源码分析
- FileReader FileWriter 源码分析
- cartographer源码分析(28)-io-points_processor.h
- cartographer源码分析(36)-io- outlier_removing_points_processor.h
- cartographer源码分析(29)-io-min_max_range_filtering_points_processor.h
- cartographer源码分析(30)-io-counting_points_processor.h
- rt-thread的IO设备管理系统源码分析
- Java IO 之 FileReader FileWriter 实现字符文件的复制(处理FileWriter新建文件失败的问题)
- Java IO:ByteArrayInputStream使用详解及源码分析