Joshua
open source statistical hierarchical phrase-based machine translation system
|
00001 #ifndef UTIL_STREAM_IO_H 00002 #define UTIL_STREAM_IO_H 00003 00004 #include "util/exception.hh" 00005 #include "util/file.hh" 00006 00007 namespace util { 00008 namespace stream { 00009 00010 class ChainPosition; 00011 00012 class ReadSizeException : public util::Exception { 00013 public: 00014 ReadSizeException() throw(); 00015 ~ReadSizeException() throw(); 00016 }; 00017 00018 class Read { 00019 public: 00020 explicit Read(int fd) : file_(fd) {} 00021 void Run(const ChainPosition &position); 00022 private: 00023 int file_; 00024 }; 00025 00026 // Like read but uses pread so that the file can be accessed from multiple threads. 00027 class PRead { 00028 public: 00029 explicit PRead(int fd, bool take_own = false) : file_(fd), own_(take_own) {} 00030 void Run(const ChainPosition &position); 00031 private: 00032 int file_; 00033 bool own_; 00034 }; 00035 00036 class Write { 00037 public: 00038 explicit Write(int fd) : file_(fd) {} 00039 void Run(const ChainPosition &position); 00040 private: 00041 int file_; 00042 }; 00043 00044 // It's a common case that stuff is written and then recycled. So rather than 00045 // spawn another thread to Recycle, this combines the two roles. 00046 class WriteAndRecycle { 00047 public: 00048 explicit WriteAndRecycle(int fd) : file_(fd) {} 00049 void Run(const ChainPosition &position); 00050 private: 00051 int file_; 00052 }; 00053 00054 class PWriteAndRecycle { 00055 public: 00056 explicit PWriteAndRecycle(int fd) : file_(fd) {} 00057 void Run(const ChainPosition &position); 00058 private: 00059 int file_; 00060 }; 00061 00062 00063 // Reuse the same file over and over again to buffer output. 00064 class FileBuffer { 00065 public: 00066 explicit FileBuffer(int fd) : file_(fd) {} 00067 00068 PWriteAndRecycle Sink() const { 00069 util::SeekOrThrow(file_.get(), 0); 00070 return PWriteAndRecycle(file_.get()); 00071 } 00072 00073 PRead Source(bool discard = false) { 00074 return PRead(discard ? file_.release() : file_.get(), discard); 00075 } 00076 00077 uint64_t Size() const { 00078 return SizeOrThrow(file_.get()); 00079 } 00080 00081 private: 00082 scoped_fd file_; 00083 }; 00084 00085 } // namespace stream 00086 } // namespace util 00087 #endif // UTIL_STREAM_IO_H