Public Member Functions | Protected Member Functions | List of all members
ustl::ostream Class Reference

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

#include <ustl.h>

Inheritance diagram for ustl::ostream:
ustl::memlink ustl::ios_base ustl::cmemlink ustl::ostringstream ustl::ofstream

Public Member Functions

 ostream (void *p, streamsize n)
 
 ostream (const memlink &source)
 
iterator end (void)
 
const_iterator end (void) const
 
void seek (uoff_t newPos)
 Move the write pointer to newPos.
 
void iseek (const_iterator newPos)
 Sets the current write position to newPos.
 
void skip (streamsize nBytes)
 Skips nBytes without writing anything.
 
uoff_t pos (void) const
 
iterator ipos (void)
 
const_iterator ipos (void) const
 
streamsize remaining (void) const
 Returns number of bytes remaining in the write buffer.
 
bool aligned (streamsize grain=c_DefaultAlignment) const
 Returns true if the write pointer is aligned on grain.
 
bool verify_remaining (const char *op, const char *type, size_t n)
 Checks that n bytes are available in the stream, or else throws.
 
streamsize align_size (streamsize grain=c_DefaultAlignment) const
 Returns the number of bytes to skip to be aligned on grain.
 
void align (streamsize grain=c_DefaultAlignment)
 Aligns the write pointer on grain. The skipped bytes are zeroed.
 
void write (const void *buffer, streamsize 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)
 Writes all available data from is.
 
void write (ostream &os) const
 
void text_write (ostringstream &os) const
 Writes all written data to os.
 
size_t stream_size (void) const
 
void insert (iterator start, streamsize size)
 Inserts an empty area of size, at start.
 
void erase (iterator start, streamsize size)
 Erases an area of size, at start.
 
void swap (ostream &os)
 Swaps with os.
 
template<typename T >
void iwrite (const T &v)
 Writes type T into the stream via a direct pointer cast.
 
virtual streamsize overflow (streamsize=1)
 
virtual void unlink (void) noexcept override
 
void link (void *p, streamsize n)
 
void link (memlink &l)
 
void link (void *f, void *l)
 
void relink (void *p, streamsize n)
 
void relink (memlink &l)
 
void seekp (off_t p, seekdir d=beg)
 Sets the current write position to p based on d.
 
off_t tellp (void) const
 
- Public Member Functions inherited from ustl::ios_base
iostate rdstate (void) const
 
bool bad (void) const
 
bool good (void) const
 
bool fail (void) const
 
bool eof (void) const
 
bool operator! (void) const
 
 operator void * (void) const
 
void clear (iostate v=goodbit)
 
void setstate (iostate v)
 
iostate exceptions (void) const
 
iostate exceptions (iostate v)
 

Protected Member Functions

void SetPos (uoff_t newPos)
 
- Protected Member Functions inherited from ustl::ios_base
bool set_and_throw (iostate v)
 
void overrun (const char *op, const char *type, uint32_t n, uint32_t p, uint32_t rem)
 Checks that n bytes are available in the stream, or else throws.
 

Additional Inherited Members

- Public Types inherited from ustl::ios_base
enum  fmtflags {
  boolalpha = (1 << 0),
  dec = (1 << 1),
  fixed = (1 << 2),
  hex = (1 << 3),
  internal = (1 << 4),
  left = (1 << 5),
  oct = (1 << 6),
  right = (1 << 7),
  scientific = (1 << 8),
  showbase = (1 << 9),
  showpoint = (1 << 10),
  showpos = (1 << 11),
  skipws = (1 << 12),
  unitbuf = (1 << 13),
  uppercase = (1 << 14),
  adjustfield = (1 << 15),
  basefield = (1 << 16),
  floatfield = (1 << 17)
}
 Used to set parameters for stringstreams. More...
 
enum  openmode_bits {
  in = (1 << 0),
  out = (1 << 1),
  app = (1 << 2),
  ate = (1 << 3),
  binary = (1 << 4),
  trunc = (1 << 5)
}
 For file-based streams, specifies fd mode.
 
enum  seekdir {
  beg,
  cur,
  end
}
 Seek directions, equivalent to SEEK_SET, SEEK_CUR, and SEEK_END.
 
enum  iostate_bits {
  goodbit = 0,
  badbit = (1 << 0),
  eofbit = (1 << 1),
  failbit = (1 << 2)
}
 I/O state bitmasks.
 
typedef uint32_t openmode
 Holds openmode_bits.
 
typedef uint32_t iostate
 Holds iostate_bits for a file stream.
 
typedef file_exception failure
 Thrown by fstream on errors.
 
- Static Public Attributes inherited from ustl::ios_base
static const char c_DefaultDelimiters [16] = DEFAULT_DELIMITERS
 Default word delimiters for stringstreams.
 

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 align. 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. See istream documentation for rules on designing your data format. 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 << ios::talign<int>();
os << intVar << floatVar;
os.write (binaryData, binaryDataSize);
os.align();
b.resize (os.pos());
b.write_file ("test.file");

The documentation for this class was generated from the following files:

Generated on Mon Sep 28 2015 17:58:50 for uSTL by Doxygen 1.8.10