FlatCountable Class Reference

This class is used simply to more succinctly designate any object that derives from both Flattenable and RefCountable. More...

#include <FlatCountable.h>

Inheritance diagram for FlatCountable:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 FlatCountable ()
 Default ctor.
virtual ~FlatCountable ()
 Dtor.
void IncrementRefCount () const
 Increments the counter and returns true iff the new value is zero.
bool DecrementRefCount () const
 Decrements the counter and returns true iff the new value is zero.
void SetManager (AbstractObjectManager *manager)
 Sets the recycle-pointer for this object.
AbstractObjectManagerGetManager () const
 Returns this object's current recyler pointer.
uint32 GetRefCount () const
 Returns this object's current reference count.
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.
Ref< ByteBufferFlattenToByteBuffer () const
 Convenience method.
status_t UnflattenFromByteBuffer (const ByteBuffer &buf)
 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 used simply to more succinctly designate any object that derives from both Flattenable and RefCountable.

That is, it is an interface for objects that can be both flattened and reference-counted.

Definition at line 16 of file FlatCountable.h.


Member Function Documentation

void RefCountable::IncrementRefCount (  )  const [inline, inherited]

Increments the counter and returns true iff the new value is zero.

Thread safe.

Definition at line 32 of file RefCount.h.

References AtomicCounter::AtomicIncrement().

bool RefCountable::DecrementRefCount (  )  const [inline, inherited]

Decrements the counter and returns true iff the new value is zero.

Thread safe.

Definition at line 35 of file RefCount.h.

References AtomicCounter::AtomicDecrement().

void RefCountable::SetManager ( AbstractObjectManager manager  )  [inline, inherited]

Sets the recycle-pointer for this object.

If set to non-NULL, this pointer is used by the ObjectPool class to recycle this object when it is no longer in use, so as to avoid the overhead of having to delete it and re-create it later on. The RefCountable class itself does nothing with this pointer. Default value is NULL.

Parameters:
manager Pointer to the new manager object to use, or NULL to use no manager.

Definition at line 44 of file RefCount.h.

uint32 RefCountable::GetRefCount (  )  const [inline, inherited]

Returns this object's current reference count.

Note that the value returned by this method is volatile in multithreaded environments, so it may already be wrong by the time it is returned. Be careful!

Definition at line 54 of file RefCount.h.

References AtomicCounter::GetCount().

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

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, inherited]

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 Flattenable::TypeCode().

Referenced by Flattenable::CopyFrom(), and Flattenable::CopyTo().

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

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, inherited]

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 Flattenable::AllowsTypeCode(), Flattenable::CopyFromImplementation(), and Flattenable::TypeCode().

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

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 Flattenable::AllowsTypeCode(), Flattenable::CopyFromImplementation(), and Flattenable::TypeCode().

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

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, inherited]

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 [inherited]

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?)

Ref<ByteBuffer> Flattenable::FlattenToByteBuffer (  )  const [inherited]

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::UnflattenFromByteBuffer ( const ByteBuffer buf  )  [inherited]

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.

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

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 
) [inherited]

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, inherited]

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 Flattenable::CopyFrom(), and Flattenable::CopyTo().


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