All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::BinaryData Class Reference

OSC-like binary data storage. More...

#include <Radiant/BinaryData.hpp>

Public Member Functions

void append (const BinaryData &that)
 Appends another BinaryData object to this. More...
 
 BinaryData ()
 Constructs empty data container.
 
 BinaryData (const BinaryData &)
 Copy constructor.
 
void clear ()
 Rewind the buffer and fill it with zeroes.
 
char * data ()
 Returns a pointer to the buffer. More...
 
const char * data () const
 Returns a pointer to the buffer. More...
 
void ensure (size_t bytes)
 Ensure that at least required amount of bytes is available. More...
 
void linkTo (void *data, int capacity)
 Makes the buffer point to existing memory. More...
 
bool loadFromFile (const char *filename, size_t maxSize=10000000)
 Reads data from a file. More...
 
BinaryDataoperator= (const BinaryData &that)
 Copy a buffer object. More...
 
int32_t peekMarker (bool *ok=0) const
 Extracts the next marker code from the stream. More...
 
int pos () const
 Tells the current position of the read/write pointer. More...
 
template<class T >
read (bool *ok=0)
 Read a value from the data. More...
 
bool read (Radiant::BinaryStream &stream, int timeoutMs=-1)
 Reads the buffer from a stream. More...
 
template<>
float read (bool *ok)
 
template<>
double read (bool *ok)
 
template<>
int read (bool *ok)
 
template<>
unsigned int read (bool *ok)
 
template<>
bool read (bool *ok)
 
template<>
int64_t read (bool *ok)
 
template<>
TimeStamp read (bool *ok)
 
template<>
uint64_t read (bool *ok)
 
template<>
Nimble::Vector2f read (bool *ok)
 
template<>
Nimble::Vector3f read (bool *ok)
 
template<>
Nimble::Vector4f read (bool *ok)
 
template<>
Nimble::Frame4f read (bool *ok)
 
template<>
Nimble::Vector2i read (bool *ok)
 
template<>
Nimble::Vector3i read (bool *ok)
 
template<>
Nimble::Vector4i read (bool *ok)
 
template<>
Color read (bool *ok)
 
template<>
QString read (bool *ok)
 
template<>
QByteArray read (bool *ok)
 
bool readBlob (void *ptr, int n)
 Reads a blob of expected size. More...
 
bool readBlob (std::vector< uint8_t > &buf)
 Reads a blob of expected size. More...
 
float readFloat32 (bool *ok=0)
 Read a value from the buffer Tries to read the matching value type from the buffer. More...
 
double readFloat64 (bool *ok=0)
 Read a value from the buffer Tries to read the matching value type from the buffer. More...
 
int32_t readInt32 (bool *ok=0)
 Read a value from the buffer Tries to read the matching value type from the buffer. More...
 
int64_t readInt64 (bool *ok=0)
 Read a value from the buffer Tries to read the matching value type from the buffer. More...
 
bool readString (char *str, size_t maxbytes)
 Read a null-terminated string from the buffer. More...
 
bool readString (QString &str)
 Read a string from the buffer. More...
 
bool readString (QByteArray &str)
 Read a string from the buffer. More...
 
TimeStamp readTimeStamp (bool *ok=0)
 Read a value from the buffer Tries to read the matching value type from the buffer. More...
 
Nimble::Vector2f readVector2Float32 (bool *ok=0)
 Reads a 2D 32-bit floating point vector from the buffer. More...
 
Nimble::Vector2i readVector2Int32 (bool *ok=0)
 Reads a 2D 32-bit integer vector from the buffer. More...
 
Nimble::Vector3f readVector3Float32 (bool *ok=0)
 Reads a 3D 32-bit floating point vector from the buffer. More...
 
Nimble::Vector3i readVector3Int32 (bool *ok=0)
 Reads a 3D 32-bit integer vector from the buffer. More...
 
Nimble::Vector4f readVector4Float32 (bool *ok=0)
 Reads a 4D 32-bit float vector from the buffer. More...
 
Nimble::Vector4i readVector4Int32 (bool *ok=0)
 Reads a 4D 32-bit integer vector from the buffer. More...
 
void rewind ()
 Rewind the index pointer to the beginning.
 
bool saveToFile (const char *filename) const
 Saves this buffer to the given file. More...
 
void setPos (int index)
 Sets the position of the read/write pointer. More...
 
void setTotal (unsigned bytes)
 Sets the total number of bytes used by this buffer. More...
 
int total () const
 Returns the total number of bytes used by this buffer. More...
 
void write (float v)
 Writes a 32-bit floating point number to the data buffer. More...
 
void write (double v)
 Writes a 32-bit floating point number to the data buffer. More...
 
void write (bool v)
 Writes a bool to the data buffer. More...
 
void write (int32_t v)
 Writes a 32-bit integer to the data buffer. More...
 
void write (int64_t v)
 Writes a 64-bit integer to the data buffer. More...
 
void write (const Radiant::TimeStamp &ts)
 Write a 64-bit time-stamp to the data buffer The timestamp uses Radiant::TimeStamp internal structure (40+24 bit fixed-point value). More...
 
void write (const char *str)
 Write a null-terminated string to the buffer. More...
 
void write (const QString &str)
 Write a string to the buffer. More...
 
void write (const QByteArray &str)
 Write a string to the buffer. More...
 
void write (Nimble::Vector2f v)
 Writes a 2D 32-bit floating point vector to the data buffer. More...
 
void write (Nimble::Vector3f v)
 Writes a 3D 32-bit floating point vector to the data buffer. More...
 
void write (Nimble::Vector2i v)
 Writes a 2D 32-bit integer vector to the data buffer. More...
 
void write (Nimble::Vector3i v)
 Writes a 3D 32-bit integer vector to the data buffer. More...
 
void write (const Nimble::Vector4i &v)
 Writes a 4D 32-bit integer vector to the data buffer. More...
 
void write (const Nimble::Vector4f &v)
 Writes a 4D 32-bit float vector to the data buffer. More...
 
bool write (Radiant::BinaryStream &stream) const
 Writes the buffer into a stream. More...
 
void writeBlob (const void *ptr, int n)
 Writes binary blob to the buffer. More...
 
void writeFloat32 (float v)
 Writes a 32-bit floating point number to the data buffer. More...
 
void writeFloat64 (double v)
 Writes a 32-bit floating point number to the data buffer. More...
 
void writeInt32 (int32_t v)
 Writes a 32-bit integer to the data buffer. More...
 
void writeInt64 (int64_t v)
 Writes a 64-bit integer to the data buffer. More...
 
void writePointer (void *ptr)
 Writes a 64-bit integer to the data buffer. More...
 
void writeString (const char *str)
 Write a null-terminated string to the buffer. More...
 
void writeString (const QString &str)
 Write a string to the buffer. More...
 
void writeString (const QByteArray &str)
 Write a string to the buffer. More...
 
void writeTimeStamp (TimeStamp v)
 Write a 64-bit time-stamp to the data buffer The timestamp uses Radiant::TimeStamp internal structure (40+24 bit fixed-point value). More...
 
void writeVector2Float32 (Nimble::Vector2f v)
 Writes a 2D 32-bit floating point vector to the data buffer. More...
 
void writeVector2Int32 (Nimble::Vector2i v)
 Writes a 2D 32-bit integer vector to the data buffer. More...
 
void writeVector3Float32 (Nimble::Vector3f v)
 Writes a 3D 32-bit floating point vector to the data buffer. More...
 
void writeVector3Int32 (Nimble::Vector3i v)
 Writes a 3D 32-bit integer vector to the data buffer. More...
 
void writeVector4Float32 (const Nimble::Vector4f &v)
 Writes a 4D 32-bit float vector to the data buffer. More...
 
void writeVector4Int32 (const Nimble::Vector4i &v)
 Writes a 4D 32-bit integer vector to the data buffer. More...
 
 ~BinaryData ()
 Destructor.
 

Static Public Attributes

static const int32_t BLOB_MARKER
 
static const int32_t DOUBLE_MARKER
 
static const int32_t FLOAT_MARKER
 
static const int32_t INT32_MARKER
 
static const int32_t INT64_MARKER
 
static const int32_t STRING_MARKER
 
static const int32_t TS_MARKER
 
static const int32_t VECTOR2F_MARKER
 
static const int32_t VECTOR2I_MARKER
 
static const int32_t VECTOR3F_MARKER
 
static const int32_t VECTOR3I_MARKER
 
static const int32_t VECTOR4F_MARKER
 
static const int32_t VECTOR4I_MARKER
 

Detailed Description

OSC-like binary data storage.

This class encapsulates control messages in a binary buffer. The data is stored in pretty much the same way as in Open Sound Control (OSC). This is done to ensure easy control data conversion between Radiant and OSC-compliant applications.

Differences between Radiant and OSC:

  • In ControlData the byte order is machine native byte order, for performance reasons.

  • Time-stamp for is slightly different. In Radiant the the timestamps are 40+24 bits fixed point while in OSC they are 32+32 bit fixed point.

  • In Radiant, there is no address matching

  • In Radiant, the type of each parameter is stored right before the parameter, to make writing/reading the stream easier.

  • In Radiant strings are padded to 4-byte margin (like OSC), but the padding does not need to be zeros.

OSC basic types are well known, so automatic conversion of all the differences should be easy enough.

Wrting functions always put the type marker before the actual value.

Reading functions set the optional bool argument "ok" to true if the operation is successful and to false if it fails.

Examples:
EventsExample.cpp, MixedJavaScriptExample.js, PostProcessingExample.cpp, and SamplePlayerExample.cpp.

Member Function Documentation

void Radiant::BinaryData::append ( const BinaryData that)

Appends another BinaryData object to this.

Parameters
thatBinaryData to append
char* Radiant::BinaryData::data ( )
inline

Returns a pointer to the buffer.

Returns
Pointer to the raw data used by BinaryData
const char* Radiant::BinaryData::data ( ) const
inline

Returns a pointer to the buffer.

Returns
Pointer to the raw data used by BinaryData
void Radiant::BinaryData::ensure ( size_t  bytes)

Ensure that at least required amount of bytes is available.

Parameters
bytes
void Radiant::BinaryData::linkTo ( void *  data,
int  capacity 
)

Makes the buffer point to existing memory.

The shared memory will not be freed when the BinaryData object is destroyed.

Parameters
dataPointer to data that is being shared
capacitySize of the BinaryData
bool Radiant::BinaryData::loadFromFile ( const char *  filename,
size_t  maxSize = 10000000 
)

Reads data from a file.

Parameters
filenameName of the file where data is loaded
maxSizeMaximum size of the read in bytes
Returns
Did we manage to read the whole file succesfully?
BinaryData& Radiant::BinaryData::operator= ( const BinaryData that)
inline

Copy a buffer object.

Parameters
thatBuffer to assign
Returns
Reference to this
int32_t Radiant::BinaryData::peekMarker ( bool *  ok = 0) const

Extracts the next marker code from the stream.

Parameters
[out]okcheck if the read was successful
int Radiant::BinaryData::pos ( ) const
inline

Tells the current position of the read/write pointer.

Returns
Position of read write pointer in bytes from the begininning.
Examples:
MixedJavaScriptExample.js.
template<class T >
T Radiant::BinaryData::read ( bool *  ok = 0)
inline

Read a value from the data.

Parameters
[out]okIf read was succesful true is writtten to this, otherwise false.
Template Parameters
TType of value to read
bool Radiant::BinaryData::read ( Radiant::BinaryStream stream,
int  timeoutMs = -1 
)

Reads the buffer from a stream.

Parameters
streamStream to read from
timeoutMsHow many milliseconds the data is waited for. -1 if waiting indefinitively.
Returns
False if the data could not completely read, true otherwise
bool Radiant::BinaryData::readBlob ( void *  ptr,
int  n 
)

Reads a blob of expected size.

Parameters
[out]ptrbuffer to write to
nbytes to read
Returns
true on success
bool Radiant::BinaryData::readBlob ( std::vector< uint8_t > &  buf)

Reads a blob of expected size.

Parameters
[out]bufbuffer to write to. The buffer will be resized to fit the data
Returns
true on success
float Radiant::BinaryData::readFloat32 ( bool *  ok = 0)

Read a value from the buffer Tries to read the matching value type from the buffer.

If successful the value is returned and the buffer read position is incremented. If the read fails, zero is returned and the optional ok flag is set to false.

Parameters
[out]okcheck if the read was successful
Returns
requested value or zero on failure
Examples:
MixedJavaScriptExample.js, and SamplePlayerExample.cpp.
double Radiant::BinaryData::readFloat64 ( bool *  ok = 0)

Read a value from the buffer Tries to read the matching value type from the buffer.

If successful the value is returned and the buffer read position is incremented. If the read fails, zero is returned and the optional ok flag is set to false.

Parameters
[out]okcheck if the read was successful
Returns
requested value or zero on failure
int32_t Radiant::BinaryData::readInt32 ( bool *  ok = 0)

Read a value from the buffer Tries to read the matching value type from the buffer.

If successful the value is returned and the buffer read position is incremented. If the read fails, zero is returned and the optional ok flag is set to false.

Parameters
[out]okcheck if the read was successful
Returns
requested value or zero on failure
Examples:
MixedJavaScriptExample.js.
int64_t Radiant::BinaryData::readInt64 ( bool *  ok = 0)

Read a value from the buffer Tries to read the matching value type from the buffer.

If successful the value is returned and the buffer read position is incremented. If the read fails, zero is returned and the optional ok flag is set to false.

Parameters
[out]okcheck if the read was successful
Returns
requested value or zero on failure
bool Radiant::BinaryData::readString ( char *  str,
size_t  maxbytes 
)

Read a null-terminated string from the buffer.

Parameters
[out]strstring buffer to write to
maxbytesmaximum number of bytes to read
Returns
true on success
Examples:
MixedJavaScriptExample.js.
bool Radiant::BinaryData::readString ( QString &  str)

Read a string from the buffer.

Parameters
[out]strstring to write to
Returns
true on success
bool Radiant::BinaryData::readString ( QByteArray &  str)

Read a string from the buffer.

Parameters
[out]strstring to write to
Returns
true on success
TimeStamp Radiant::BinaryData::readTimeStamp ( bool *  ok = 0)

Read a value from the buffer Tries to read the matching value type from the buffer.

If successful the value is returned and the buffer read position is incremented. If the read fails, zero is returned and the optional ok flag is set to false.

Parameters
[out]okcheck if the read was successful
Returns
requested value or zero on failure
Nimble::Vector2f Radiant::BinaryData::readVector2Float32 ( bool *  ok = 0)

Reads a 2D 32-bit floating point vector from the buffer.

Parameters
[out]okcheck if the read was successful
Nimble::Vector2i Radiant::BinaryData::readVector2Int32 ( bool *  ok = 0)

Reads a 2D 32-bit integer vector from the buffer.

Parameters
[out]okcheck if the read was successful
Nimble::Vector3f Radiant::BinaryData::readVector3Float32 ( bool *  ok = 0)

Reads a 3D 32-bit floating point vector from the buffer.

Parameters
[out]okcheck if the read was successful
Nimble::Vector3i Radiant::BinaryData::readVector3Int32 ( bool *  ok = 0)

Reads a 3D 32-bit integer vector from the buffer.

Parameters
[out]okcheck if the read was successful
Nimble::Vector4f Radiant::BinaryData::readVector4Float32 ( bool *  ok = 0)

Reads a 4D 32-bit float vector from the buffer.

Parameters
[out]okcheck if the read was successful
Nimble::Vector4i Radiant::BinaryData::readVector4Int32 ( bool *  ok = 0)

Reads a 4D 32-bit integer vector from the buffer.

Parameters
[out]okcheck if the read was successful
bool Radiant::BinaryData::saveToFile ( const char *  filename) const

Saves this buffer to the given file.

Parameters
filenameName of the target file
Returns
True if write was succesful, false otherwise
void Radiant::BinaryData::setPos ( int  index)
inline

Sets the position of the read/write pointer.

Parameters
indexBytes from the beginning
void Radiant::BinaryData::setTotal ( unsigned  bytes)
inline

Sets the total number of bytes used by this buffer.

Parameters
bytesUse at most this many bytes
int Radiant::BinaryData::total ( ) const
inline

Returns the total number of bytes used by this buffer.

Returns
Total bytes used
Examples:
MixedJavaScriptExample.js.
void Radiant::BinaryData::write ( float  v)
inline

Writes a 32-bit floating point number to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( double  v)
inline

Writes a 32-bit floating point number to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( bool  v)
inline

Writes a bool to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( int32_t  v)
inline

Writes a 32-bit integer to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( int64_t  v)
inline

Writes a 64-bit integer to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( const Radiant::TimeStamp ts)
inline

Write a 64-bit time-stamp to the data buffer The timestamp uses Radiant::TimeStamp internal structure (40+24 bit fixed-point value).

Parameters
tstime-stamp to write
void Radiant::BinaryData::write ( const char *  str)
inline

Write a null-terminated string to the buffer.

Parameters
strString to write
void Radiant::BinaryData::write ( const QString &  str)
inline

Write a string to the buffer.

Parameters
strstring to write
void Radiant::BinaryData::write ( const QByteArray &  str)
inline

Write a string to the buffer.

Parameters
strstring to write
void Radiant::BinaryData::write ( Nimble::Vector2f  v)
inline

Writes a 2D 32-bit floating point vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( Nimble::Vector3f  v)
inline

Writes a 3D 32-bit floating point vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( Nimble::Vector2i  v)
inline

Writes a 2D 32-bit integer vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( Nimble::Vector3i  v)
inline

Writes a 3D 32-bit integer vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( const Nimble::Vector4i v)
inline

Writes a 4D 32-bit integer vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::write ( const Nimble::Vector4f v)
inline

Writes a 4D 32-bit float vector to the data buffer.

Parameters
vValue to write
bool Radiant::BinaryData::write ( Radiant::BinaryStream stream) const

Writes the buffer into a stream.

Parameters
streamStream to write
Returns
True if the whole data was written to the stream. false otherwise
void Radiant::BinaryData::writeBlob ( const void *  ptr,
int  n 
)

Writes binary blob to the buffer.

Parameters
ptrPointer to blob
nBytes to write
void Radiant::BinaryData::writeFloat32 ( float  v)

Writes a 32-bit floating point number to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeFloat64 ( double  v)

Writes a 32-bit floating point number to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeInt32 ( int32_t  v)

Writes a 32-bit integer to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeInt64 ( int64_t  v)

Writes a 64-bit integer to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writePointer ( void *  ptr)
inline

Writes a 64-bit integer to the data buffer.

Parameters
ptrValue to write
void Radiant::BinaryData::writeString ( const char *  str)

Write a null-terminated string to the buffer.

Parameters
strString to write
void Radiant::BinaryData::writeString ( const QString &  str)
inline

Write a string to the buffer.

Parameters
strstring to write
void Radiant::BinaryData::writeString ( const QByteArray &  str)
inline

Write a string to the buffer.

Parameters
strstring to write
void Radiant::BinaryData::writeTimeStamp ( TimeStamp  v)

Write a 64-bit time-stamp to the data buffer The timestamp uses Radiant::TimeStamp internal structure (40+24 bit fixed-point value).

Parameters
vtime-stamp to write
void Radiant::BinaryData::writeVector2Float32 ( Nimble::Vector2f  v)

Writes a 2D 32-bit floating point vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeVector2Int32 ( Nimble::Vector2i  v)

Writes a 2D 32-bit integer vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeVector3Float32 ( Nimble::Vector3f  v)

Writes a 3D 32-bit floating point vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeVector3Int32 ( Nimble::Vector3i  v)

Writes a 3D 32-bit integer vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeVector4Float32 ( const Nimble::Vector4f v)

Writes a 4D 32-bit float vector to the data buffer.

Parameters
vValue to write
void Radiant::BinaryData::writeVector4Int32 ( const Nimble::Vector4i v)

Writes a 4D 32-bit integer vector to the data buffer.

Parameters
vValue to write