DataEncoder Class Reference

An abstract class with basic functionality that is common to all data decoders (readers) and data encoders (writers). More...

#include <DataEncoder.h++>

Inheritance diagram for DataEncoder:
Inheritance graph
[legend]

List of all members.

Classes

class  Align
 A manipulator that skips past enough bytes to re-align the data stream at a specified boundary. More...
struct  Context
 A DataEncoder context. More...
class  Manipulator
 A base class for DataEncoder manipulators. More...
class  SetEndianness
 A manipulator that specifies the endianness of integer values to be subsequently encoded/decoded. More...
class  SetLength
 A manipulator that specifies the length of String, Blob, and array values to be subsequently encoded/decoded. More...
class  SetLimit
 A manipulator that specifies the maximum number of bytes that can be read/written. More...
class  SetStringPadding
 A manipulator that enables or disables string padding. More...
class  Skip
 A manipulator that skips past bytes in the stream. More...

Public Member Functions

virtual void reset () throw (IOException)
 Reset the encoder.
void pushContext ()
 Push a new encoder context.
void popContext ()
 Pop the active encoder context.
bool isTopContext () const throw ()
 Determine if the active context is the topmost (and only) context.
uint_t getContextDepth () const throw ()
 Get the current context depth.
void setEndianness (Endianness endianness) throw ()
 Specify the byte-endianness for reading/writing primitive integer types in the active context.
Endianness getEndianness () const throw ()
 Get the byte-endianness in the active context.
void setLength (size_t length) throw ()
 Specify the length of String, Blob, and array values for subsequent reading/writing in the active context.
size_t getLength () const throw ()
 Get the current length for string values.
void setLimit (int64_t limit) throw (IOException)
 Specify the maximum number of bytes that can be read/written in the active context.
int64_t getLimit () const throw ()
 Get the current limit.
void setStringPadding (bool stringPadding) throw ()
 Enables or disable string padding in the active context.
bool getStringPadding () const throw ()
 Get the current string padding status.
int64_t getOffset () const throw ()
 Get the current encoding/decoding offset.
virtual void setOffset (int64_t offset)=0 throw (IOException)
 Set the current reading/writing offset.
int64_t getCumulativeOffset () const throw ()
 Get the cumulative reading/writing offset.
int64_t getRemaining () const throw ()
 Get the number of bytes between the current offset and the limit, i.e., the number of bytes remaining to be read or written.
void align (size_t size) throw (IOException)
 Skip past enough bytes to re-align the data stream at a specified boundary.
virtual void skip (size_t count)=0 throw (IOException)
 Skip past the given number of bytes in the data stream.
virtual ~DataEncoder () throw ()
 Destructor.

Protected Member Functions

 DataEncoder () throw ()
 Constructor.
bool isSameEndianness () const throw ()
 Test if the endianness of the encoder is the same as the endianness of the host system.
ContextcurrentContext ()
 Get a reference to the current context.
const ContextcurrentContext () const
 Get a reference to the current context.
void checkRemaining (const Context &ctx, size_t count) const throw (IOException)
 Check if at least the given number of bytes are available to be read or written.

Detailed Description

An abstract class with basic functionality that is common to all data decoders (readers) and data encoders (writers).

A DataEncoder operates on an input source or an output destination, such as a stream or buffer. Subclasses provide specific input/output functionality, such as reading and writing primitive data types.

The class supports nested encoding contexts. Initially, there is a top-level context, which represents the entire range of bytes being read or written. A nested context represents a sub-range of bytes within its parent context. Each context has an offset and a limit; the offset is the position, relative to the beginning of the context, of the next byte to be read or written, and the limit is the maximum offset within the context beyond which further reading or writing is not allowed.

A new context is created by calling pushContext(). The new context inherits the encoder attributes—such as byte endianness, value length, and string padding mode—of its parent context, and its offset is initially 0. It represents the range of bytes between the parent context's current offset and limit.

A context is destroyed by calling popContext(). The parent context then becomes the current context, and its offset is incremented by the offset of the popped context.

Though data is typically read or written sequentially, random access is also possible. The offset in the current context can be changed insofar as it remains within the bounds of that context. When reading, the offset can be set to any value between 0 and the current context's limit. When writing, the offset can be set to any value between 0 and the maximum offset to which data has already been written within the current context.

Nested contexts can be used to implement an encoder that reads or writes hierarchical data structures, such as "chunks" in a RIFF file.

Author:
Mark Lindner

Constructor & Destructor Documentation

~DataEncoder (  )  throw () [virtual]

Destructor.

DataEncoder (  )  throw () [protected]

Constructor.


Member Function Documentation

void align ( size_t  size  )  throw (IOException)

Skip past enough bytes to re-align the data stream at a specified boundary.

When reading, the bytes are simply skipped over; when writing, the bytes are filled with NULs.

Parameters:
size The alignment size. Typical values include 2, 4, or 8. Other values are allowed but are of limited usefulness.
Exceptions:
IOException If an I/O error occurs.
void checkRemaining ( const Context ctx,
size_t  count 
) const throw (IOException) [protected]

Check if at least the given number of bytes are available to be read or written.

Parameters:
ctx The context to check.
count The number of bytes.
Exceptions:
IOException If there are not enough bytes remaining.
const DataEncoder::Context & currentContext (  )  const [protected]

Get a reference to the current context.

DataEncoder::Context & currentContext (  )  [protected]

Get a reference to the current context.

uint_t getContextDepth (  )  const throw ()

Get the current context depth.

int64_t getCumulativeOffset (  )  const throw ()

Get the cumulative reading/writing offset.

This is a sum of the offsets across all contexts.

Endianness getEndianness (  )  const throw ()

Get the byte-endianness in the active context.

size_t getLength (  )  const throw ()

Get the current length for string values.

int64_t getLimit (  )  const throw ()

Get the current limit.

int64_t getOffset (  )  const throw ()

Get the current encoding/decoding offset.

int64_t getRemaining (  )  const throw ()

Get the number of bytes between the current offset and the limit, i.e., the number of bytes remaining to be read or written.

bool getStringPadding (  )  const throw ()

Get the current string padding status.

bool isSameEndianness (  )  const throw () [protected]

Test if the endianness of the encoder is the same as the endianness of the host system.

bool isTopContext (  )  const throw ()

Determine if the active context is the topmost (and only) context.

void popContext (  ) 

Pop the active encoder context.

The previous context becomes the active context, and the offset in the current context is incremented by the offset of the popped context.

void pushContext (  ) 

Push a new encoder context.

void reset (  )  throw (IOException) [virtual]

Reset the encoder.

Discards all but the topmost context, and resets the topmost context to default values.

Reimplemented in ByteArrayDataReader, ByteArrayDataWriter, ByteBufferDataReader, ByteBufferDataWriter, and StreamDataReader.

void setEndianness ( Endianness  endianness  )  throw ()

Specify the byte-endianness for reading/writing primitive integer types in the active context.

void setLength ( size_t  length  )  throw ()

Specify the length of String, Blob, and array values for subsequent reading/writing in the active context.

Depending on whether string padding is enabled or disabled, written strings will be padded with either space characters or NUL bytes, respectively.

void setLimit ( int64_t  limit  )  throw (IOException)

Specify the maximum number of bytes that can be read/written in the active context.

Attempts to read/write past this limit will raise an IOException.

virtual void setOffset ( int64_t  offset  )  throw (IOException) [pure virtual]
void setStringPadding ( bool  stringPadding  )  throw ()

Enables or disable string padding in the active context.

When enabled, strings are padded with spaces during writing; otherwise they are padded with NUL bytes.

Parameters:
stringPadding A flag indicating the type of padding; true for spaces, false for NUL bytes.
virtual void skip ( size_t  count  )  throw (IOException) [pure virtual]

Skip past the given number of bytes in the data stream.

When reading, the bytes are simply skipped over; when writing, the bytes are filled with NULs.

Parameters:
count The number of bytes to skip.
Exceptions:
IOException If an I/O error occurs.

Implemented in ByteArrayDataReader, ByteArrayDataWriter, ByteBufferDataReader, ByteBufferDataWriter, DataWriter, StreamDataReader, and StreamDataWriter.


The documentation for this class was generated from the following files:
Generated on Sat Nov 26 16:49:08 2011 for libcommonc++ by  doxygen 1.6.3