Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ustl::ostream Class Reference
[Binary Streams]

Helper class to write packed binary streams. More...

#include <ustl.h>

Inheritance diagram for ustl::ostream:

ustl::memlink ustl::cmemlink ustl::ostringstream ustl::fdostringstream List of all members.

Public Member Functions

 ostream (void)
 Constructs a stream attached to nothing. A stream attached to nothing is not usable. Call Link() functions inherited from memlink to attach to some memory block.
 ostream (void *p, size_type n)
 Attaches the stream to a block at p of size n.
 ostream (const memlink &source)
 Attaches to the block pointed to by source.
void seek (uoff_t newPos)
 Move the write pointer to newPos.
void seek (const_iterator newPos)
 Sets the current read position to newPos.
void skip (size_type nBytes)
 Skips nBytes without writing anything.
uoff_t pos (void) const
 Returns the current write position. Usually this is also the number of bytes written.
iterator ipos (void)
 Returns the current write position.
const_iterator ipos (void) const
 Returns the current write position.
size_type remaining (void) const
 Returns number of bytes remaining in the write buffer.
bool aligned (size_type grain=c_DefaultAlignment) const
 Returns true if the write pointer is aligned on grain.
size_type align_size (size_type grain=c_DefaultAlignment) const
 Returns the number of bytes to skip to be aligned on grain.
void align (size_type grain=c_DefaultAlignment)
 Aligns the write pointer on grain. Nothing is written to the skipped bytes.
void write (const void *buffer, size_type size)
 Writes n bytes from buffer.
void write (const cmemlink &buf)
 Writes the contents of buf into the stream as a raw dump.
void write_strz (const char *str)
 Writes str as a null-terminated string.
void read (istream &is)
 Equivalent to istream::write(os).
void write (ostream &os) const
 Copy currently written bytes into os.
void insert (iterator start, size_type size)
 Inserts an empty area of size, at start.
void erase (iterator start, size_type size)
 Erases an area of size, at start.
void swap (ostream &os)
 Swaps with os.
size_t stream_size (void) const
 Returns number of bytes written.
template<typename T>
void iwrite (const T &v)
 Writes type T into the stream via a direct pointer cast.
virtual void unlink (void)
 Links to p of size n.
void link (void *p, size_type n)
 Initializes both links to point to p, n.
void link (memlink &l)
 Links to l.
void link (void *f, void *l)
 Links to iterator range first - last.

Detailed Description

Helper class to write packed binary streams.

This class contains a set of functions to write integral types into an unstructured memory block. Packing binary file data can be done this way, for instance. aligning the data is your responsibility, and can be accomplished by proper ordering of writes and by calling the align() function. Unaligned access is usually slower by orders of magnitude and, on some architectures, such as PowerPC, can cause your program to crash. Therefore, all write functions have asserts to check alignment. Overwriting the end of the stream will also cause a crash (an assert in debug builds). Oh, and don't be intimidated by the size of the inlines here. In the assembly code the compiler will usually chop everything down to five instructions each.

Example code:

        memblock b;
        ostream os (b);
        os << boolVar;
        os.align (sizeof(int));
        os << intVar << floatVar;
        os.write (binaryData, binaryDataSize);
        os.align ();
        b.resize (os.pos());
        write (fd, b, b.size());


The documentation for this class was generated from the following files:
Generated on Mon Jan 17 14:35:02 2005 for uSTL by 1.3.9 Doxygen Hosted on SourceForge.net