OutPipe

OutPipe is a class for writing output to an arbitrary destination such as a file, network socket, or encryption/compression codec. It's often used as the underlying output destination of an OutStream.

OutPipe is a lower-level class than OutStream and is more akin to a Unix file descriptor. When the OutPipe writes to a file or network socket, each call to write() invokes a system call to the OS kernel. Therefore, when writing directly to an OutPipe, it's best to write medium-to-large quantities of data (say, several KB) at a time in order to minimize system call overhead.

If writing small amounts of data (say, a few bytes) at a time -- for example, to parse text or binary formats -- it's better to work with OutStream or StringWriter instead.

Header File

#include <ply-runtime/io/Pipe.h>

Also included from <ply-runtime/Base.h>.

Member Functions

template <typename T>
T* cast() [code]

Cast this OutPipe to a derived type such as OutPipe_Win32, OutPipe_FD or OutPipe_Winsock in order to access additional functions or data members. A runtime check is performed to ensure the cast is valid.

bool write(ConstBufferView buf) [code]

Attempts to write the entire contents of buf to the output destination. This call may block depending on the state of the OutPipe. For example, if the OutPipe is a network socket or interprocess pipe, and the process on the other end is not reading quickly enough, write() may block. Returns true if successful. Return false if the write fails such as when attempting to write to a network socket that was closed prematurely.

bool flush(bool toDevice = true) [code]

Flushes any application-level memory buffers in the same manner as flushMem(), then performs an implementation-specific device flush if toDevice is true. For example, if toDevice is true, this will call fsync() on when writing to a POSIX file descriptor or FlushFileBuffers() when writing to a Windows file handle. Returns true unless there's an error in the implementation-specific device flush.

void flushMem() [code]

Flushes any application-level memory buffers maintained by the OutPipe will be flushed to their destination. Appropriate to use when the OutPipe is a line feed filter or any kind of application-level conversion, compression or encryption filter.

u64 seek(s64 pos, SeekDir seekDir) [code]

If the output destination is seekable, seeks to the specified file offset and returns the current file offset. To get the current file offset without changing it, call seek(0, SeekDir::Cur).