Flattenable Class Reference

This class is an interface representing an object that knows how to save itself into an array of bytes, and recover its state from an array of bytes. More...

#include <Flattenable.h>

Inheritance diagram for Flattenable:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 Flattenable ()
 Constructor.
virtual ~Flattenable ()
 Destructor.
virtual bool IsFixedSize () const=0
 Should return true iff every object of this type has a size that is known at compile time.
virtual uint32 TypeCode () const=0
 Should return the type code identifying this type of object.
virtual uint32 FlattenedSize () const=0
 Should return the number of bytes needed to store this object in its current state.
virtual void Flatten (uint8 *buffer) const =0
 Should store this object's state into (buffer).
virtual bool AllowsTypeCode (uint32 code) const
 Should return true iff a buffer with uint32 (code) can be used to reconstruct this object's state.
virtual status_t Unflatten (const uint8 *buf, uint32 size)=0
 Should attempt to restore this object's state from the given buffer.
status_t CopyTo (Flattenable &copyTo) const
 Causes (copyTo)'s state to set from this Flattenable, if possible.
status_t CopyFrom (const Flattenable &copyFrom)
 Causes our state to be set from (copyFrom)'s state, if possible.
status_t FlattenToByteBuffer (ByteBuffer &outBuf) const
 Convenience method.
status_t UnflattenFromByteBuffer (const ByteBuffer &buf)
 Convenience method.
Ref< ByteBufferFlattenToByteBuffer () const
 Convenience method.
status_t FlattenToDataIO (DataIO &outputStream, bool addSizeHeader) const
 Convenience method.
status_t UnflattenFromDataIO (DataIO &inputStream, int32 optReadSize, uint32 optMaxReadSize=MUSCLE_NO_LIMIT)
 Convenience method.

Static Public Member Functions

static void WriteData (uint8 *outBuf, uint32 *writeOffset, const void *copyFrom, uint32 blockSize)
 Convenience method for writing data into a byte buffer.
static status_t ReadData (const uint8 *inBuf, uint32 inputBufferBytes, uint32 *readOffset, void *copyTo, uint32 blockSize)
 Convenience method for safely reading bytes from a byte buffer.

Protected Member Functions

virtual status_t CopyFromImplementation (const Flattenable &copyFrom)
 Called by CopyFrom() and CopyTo().

Detailed Description

This class is an interface representing an object that knows how to save itself into an array of bytes, and recover its state from an array of bytes.

Definition at line 26 of file Flattenable.h.


Member Function Documentation

virtual void Flattenable::Flatten ( uint8 *  buffer  )  const [pure virtual]

Should store this object's state into (buffer).

Parameters:
buffer The bytes to write this object's stat into. Buffer must be at least FlattenedSize() bytes long.

Implemented in Message, Point, Rect, ByteBuffer, and String.

virtual bool Flattenable::AllowsTypeCode ( uint32  code  )  const [inline, virtual]

Should return true iff a buffer with uint32 (code) can be used to reconstruct this object's state.

Defaults implementation returns true iff (code) equals TypeCode() or B_RAW_DATA.

Parameters:
code A type code constant, e.g. B_RAW_TYPE or B_STRING_TYPE, or something custom.
Returns:
True iff this object can Unflatten from a buffer of the given type, false otherwise.

Reimplemented in ByteBuffer.

Definition at line 56 of file Flattenable.h.

References TypeCode().

Referenced by CopyFrom(), and CopyTo().

virtual status_t Flattenable::Unflatten ( const uint8 *  buf,
uint32  size 
) [pure virtual]

Should attempt to restore this object's state from the given buffer.

Parameters:
buf The buffer of bytes to unflatten from.
size Number of bytes in the buffer.
Returns:
B_NO_ERROR if the Unflattening was successful, else B_ERROR.

Implemented in Message, Point, Rect, ByteBuffer, and String.

status_t Flattenable::CopyTo ( Flattenable copyTo  )  const [inline]

Causes (copyTo)'s state to set from this Flattenable, if possible.

Default implementation is not very efficient, since it has to flatten this object into a byte buffer, and then unflatten the bytes back into (copyTo). However, you can override CopyFromImplementation() to provide a more efficient implementation when possible.

Parameters:
copyTo Object to make into the equivalent of this object. (copyTo) May be any subclass of Flattenable.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (typecode mismatch, out-of-memory, etc)

Definition at line 76 of file Flattenable.h.

References AllowsTypeCode(), CopyFromImplementation(), and TypeCode().

status_t Flattenable::CopyFrom ( const Flattenable copyFrom  )  [inline]

Causes our state to be set from (copyFrom)'s state, if possible.

Default implementation is not very efficient, since it has to flatten (copyFrom) into a byte buffer, and then unflatten the bytes back into (this). However, you can override CopyFromImplementation() to provide a more efficient implementation when possible.

Parameters:
copyFrom Object to read from to set the state of this object. (copyFrom) may be any subclass of Flattenable.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (typecode mismatch, out-of-memory, etc)

Definition at line 91 of file Flattenable.h.

References AllowsTypeCode(), CopyFromImplementation(), and TypeCode().

static void Flattenable::WriteData ( uint8 *  outBuf,
uint32 *  writeOffset,
const void *  copyFrom,
uint32  blockSize 
) [inline, static]

Convenience method for writing data into a byte buffer.

Writes data consecutively into a byte buffer. The output buffer is assumed to be large enough to hold the data.

Parameters:
outBuf Flat buffer to write to
writeOffset Offset into buffer to write to. Incremented by (blockSize) on success.
copyFrom memory location to copy bytes from
blockSize number of bytes to copy

Definition at line 105 of file Flattenable.h.

static status_t Flattenable::ReadData ( const uint8 *  inBuf,
uint32  inputBufferBytes,
uint32 *  readOffset,
void *  copyTo,
uint32  blockSize 
) [inline, static]

Convenience method for safely reading bytes from a byte buffer.

(Checks to avoid buffer overrun problems)

Parameters:
inBuf Flat buffer to read bytes from
inputBufferBytes total size of the input buffer
readOffset Offset into buffer to read from. Incremented by (blockSize) on success.
copyTo memory location to copy bytes to
blockSize number of bytes to copy
Returns:
B_NO_ERROR if the data was successfully read, B_ERROR if the data couldn't be read (because the buffer wasn't large enough)

Definition at line 120 of file Flattenable.h.

status_t Flattenable::FlattenToByteBuffer ( ByteBuffer outBuf  )  const

Convenience method.

Flattens this object into the supplied ByteBuffer object.

Parameters:
outBuf the ByteBuffer to dump our flattened bytes into.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (out of memory?)

status_t Flattenable::UnflattenFromByteBuffer ( const ByteBuffer buf  ) 

Convenience method.

Unflattens this object from the supplied ByteBuffer object.

Parameters:
buf The ByteBuffer to unflatten from.
Returns:
B_NO_ERROR on success, or B_ERROR on failure.

Ref<ByteBuffer> Flattenable::FlattenToByteBuffer (  )  const

Convenience method.

Allocated an appropriately sized ByteBuffer object via GetByteBufferFromPool(), Flatten()s this object into the byte buffer, and returns the resulting ByteBufferRef. Returns a NULL reference on failure (out of memory?)

status_t Flattenable::FlattenToDataIO ( DataIO outputStream,
bool  addSizeHeader 
) const

Convenience method.

Flattens this object to the given DataIO object.

Parameters:
outputStream The DataIO object to send our flattened data to. This DataIO should be in blocking I/O mode; this method won't work reliably if used with non-blocking I/O.
addSizeHeader If true, we will prefix our flattened data with a four-byte little-endian uint32 indicating the number of bytes of flattened data that we are going to write. If false, then the buffer size will need to be determined by the reading code by some other means.
Returns:
B_NO_ERROR on success, or B_ERROR on failure.

status_t Flattenable::UnflattenFromDataIO ( DataIO inputStream,
int32  optReadSize,
uint32  optMaxReadSize = MUSCLE_NO_LIMIT 
)

Convenience method.

Flattens this object from the given DataIO object.

Parameters:
inputStream the DataIO object to read data from. This DataIO should be in blocking I/O mode; this method won't work reliably if used with non-blocking I/O.
optReadSize If non-negative, this many bytes will be read from (inputStream). If set to -1, then the first four bytes read from the stream will be used to determine how many more bytes should be read from the stream. If the data was created with Flatten(io, true), then set this parameter to -1.
optMaxReadSize An optional value indicating the largest number of bytes that should be read by this call. This value is only used if (optReadSize) is negative. If (optReadSize) is negative and the first four bytes read from (inputStream) are greater than this value, then this method will return B_ERROR instead of trying to read that many bytes. This can be useful if you want to prevent the possibility of huge buffers being allocated due to malicious or corrupted size headers. Defaults to MUSCLE_NO_LIMIT, meaning that no size limit is enforced.
Returns:
B_NO_ERROR if the data was correctly read, or B_ERROR otherwise.

virtual status_t Flattenable::CopyFromImplementation ( const Flattenable copyFrom  )  [protected, virtual]

Called by CopyFrom() and CopyTo().

Sets our state from (copyFrom) if possible. Default implementation is not very efficient, since it has to flatten (copyFrom) into a byte buffer, and then unflatten the bytes back into (this). However, you can override CopyFromImplementation() to provide a more efficient implementation when possible.

Parameters:
copyFrom Object to set this object's state from. May be any subclass of Flattenable, but it has been pre-screened by CopyFrom() (or CopyTo()) to make sure it's not (*this), and that we allow its type code.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (out-of-memory, etc)

Reimplemented in Message, and ByteBuffer.

Referenced by CopyFrom(), and CopyTo().


The documentation for this class was generated from the following file:
Generated on Thu Jun 5 17:48:07 2008 for MUSCLE by  doxygen 1.5.1