This class is used to convert data structures into a single sequence of bytes and to retrieve this data for the purpose of communication between a coordinator and workers. More...
#include <mwdata.h>
Public Member Functions | |
mwData () | |
The default constructor of empty data. | |
~mwData () | |
The destructor. | |
mwData (const mwData &x) | |
The copy constructor. | |
mwData & | operator= (const mwData &x) |
The assignment operator. | |
mwData & | Swap (mwData &x) |
Swaps the data with another data structure. | |
mwData & | Take (mwData &x) |
Takes the data from another data structure and makes the other data empty. | |
mwData & | Take (unsigned char *buffer, int length) |
Takes raw data that was allocated with the "new" operator. | |
mwData & | Take (char *buffer, int length) |
Takes raw data that was allocated with the "new" operator. | |
const char * | Buffer () const |
Returns a pointer to the data buffer. | |
int | Length () const |
Returns the length of all the data in the buffer. | |
mwData & | Rewind () |
Rewinds the buffer to the beginning for reading. | |
mwData & | Reset () |
Forgets the buffer and makes the data empty. | |
int | Position (int newpos) |
Sets the current reading position in the buffer. | |
int | Position () const |
Returns the current reading position in the buffer. | |
mwData & | Append (const char *buffer, int length) |
Appends raw data to the buffer. | |
mwData & | Append (const unsigned char *buffer, int length) |
Appends raw data to the buffer. | |
mwData & | Append (const mwData &x) |
Appends the entire data buffer to the data. | |
const char * | Current () const |
Returns the currently pointed data in the buffer. | |
mwData & | Append (const int &x) |
mwData & | Retrieve (int &x) |
mwData & | Append (const unsigned int &x) |
mwData & | Retrieve (unsigned int &x) |
mwData & | Append (const short &x) |
mwData & | Retrieve (short &x) |
mwData & | Append (const unsigned short &x) |
mwData & | Retrieve (unsigned short &x) |
mwData & | Append (const long &x) |
mwData & | Retrieve (long &x) |
mwData & | Append (const unsigned long &x) |
mwData & | Retrieve (unsigned long &x) |
mwData & | Append (const long long &x) |
mwData & | Retrieve (long long &x) |
mwData & | Append (const unsigned long long &x) |
mwData & | Retrieve (unsigned long long &x) |
mwData & | Append (const char &x) |
mwData & | Retrieve (char &x) |
mwData & | Append (const unsigned char &x) |
mwData & | Retrieve (unsigned char &x) |
mwData & | Append (const bool &x) |
mwData & | Retrieve (bool &x) |
mwData & | Append (const float &x) |
mwData & | Retrieve (float &x) |
mwData & | Append (const double &x) |
mwData & | Retrieve (double &x) |
mwData & | Append (const std::string &x) |
mwData & | Retrieve (std::string &x) |
mwData & | Append (const char *x) |
mwData & | Retrieve (char *x) |
mwData & | Append (const unsigned char *x) |
mwData & | Retrieve (unsigned char *x) |
mwData & | SkipString () |
Skips a zero-terminated string in the buffer. | |
Private Member Functions | |
void | IncreaseBuffer (int n) |
Increases the buffer by the given number of bytes (or more) beyond the current position. | |
mwData & | AppendBytes (const unsigned char *x, int n) |
Appends a data piece to the buffer and swaps bytes if necessary. | |
mwData & | RetrieveBytes (unsigned char *x, int n) |
Retrieve a data piece from the buffer and swaps bytes if necessary. | |
Private Attributes | |
unsigned char * | buf |
The data buffer. | |
int | len |
The length of the data in the buffer and the write position. | |
int | allocated |
The length of the allocated buffer memory space. | |
int | pos |
The current read position in the buffer. |
This class is used to convert data structures into a single sequence of bytes and to retrieve this data for the purpose of communication between a coordinator and workers.
It is assumed that 'int' has at least 32 bits and 'double' has 64 bits. For 'bool', 'long long' and 'string', see the corresponding constants. In the buffer: 'char' = 8-bit, 'short' = 16-bit, 'int' = 32-bit, 'long' and 'long long' = 64-bit, 'float' = 32-bit, 'double' = 64-bit (all big endian). NOTE: Until January 21, 2010 'long' was supposed to be 32 bits long.
Definition at line 67 of file mwdata.h.
chomp::multiwork::mwData::mwData | ( | ) | [inline] |
chomp::multiwork::mwData::~mwData | ( | ) | [inline] |
chomp::multiwork::mwData::mwData | ( | const mwData & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Append | ( | const char * | buffer, | |
int | length | |||
) | [inline] |
mwData & chomp::multiwork::mwData::Append | ( | const int & | x | ) | [inline] |
Definition at line 466 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
mwData & chomp::multiwork::mwData::Append | ( | const unsigned int & | x | ) | [inline] |
Definition at line 487 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
mwData & chomp::multiwork::mwData::Append | ( | const char & | x | ) | [inline] |
Definition at line 687 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (1); buf [len ++] = (unsigned char) x; return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const unsigned char & | x | ) | [inline] |
Definition at line 702 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (1); buf [len ++] = x; return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const bool & | x | ) | [inline] |
Definition at line 719 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (1); buf [len ++] = x ? '\1' : '\0'; return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const float & | x | ) | [inline] |
Definition at line 736 of file mwdata.h.
References AppendBytes(), and IncreaseBuffer().
{ IncreaseBuffer (4); return AppendBytes ((const unsigned char *) &x, 4); } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const short & | x | ) | [inline] |
Definition at line 508 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (2); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const double & | x | ) | [inline] |
Definition at line 749 of file mwdata.h.
References AppendBytes(), and IncreaseBuffer().
{ IncreaseBuffer (8); return AppendBytes ((const unsigned char *) &x, 8); } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const std::string & | x | ) | [inline] |
Definition at line 764 of file mwdata.h.
References Append(), and IncreaseBuffer().
{ const char *str = x. c_str (); int length = 0; while (str [length ++]); IncreaseBuffer (length); return Append (reinterpret_cast<const unsigned char *> (str), length); } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const unsigned short & | x | ) | [inline] |
Definition at line 525 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (2); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const char * | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Append | ( | const unsigned char * | x | ) | [inline] |
Definition at line 788 of file mwdata.h.
References Append(), and IncreaseBuffer().
{ int length = 0; while (x [length ++]); IncreaseBuffer (length); return Append (x, length); } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const unsigned char * | buffer, | |
int | length | |||
) | [inline] |
Appends raw data to the buffer.
Definition at line 394 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (n); for (int i = 0; i < n; ++ i) buf [len ++] = *(x ++); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const long & | x | ) | [inline] |
Definition at line 542 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (8); #if (__LONG_MAX__ > 2147483647) buf [len ++] = (unsigned char) (((unsigned long) x >> 56) & 0xFF); buf [len ++] = (unsigned char) ((x >> 48) & 0xFF); buf [len ++] = (unsigned char) ((x >> 40) & 0xFF); buf [len ++] = (unsigned char) ((x >> 32) & 0xFF); buf [len ++] = (unsigned char) ((x >> 24) & 0xFF); #else buf [len ++] = 0; buf [len ++] = 0; buf [len ++] = 0; buf [len ++] = 0; buf [len ++] = (unsigned char) (((unsigned long) x >> 24) & 0xFF); #endif buf [len ++] = (unsigned char) ((x >> 16) & 0xFF); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const unsigned long & | x | ) | [inline] |
Definition at line 584 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (8); #if (__LONG_MAX__ > 2147483647) buf [len ++] = (unsigned char) ((x >> 56) & 0xFF); buf [len ++] = (unsigned char) ((x >> 48) & 0xFF); buf [len ++] = (unsigned char) ((x >> 40) & 0xFF); buf [len ++] = (unsigned char) ((x >> 32) & 0xFF); #else buf [len ++] = 0; buf [len ++] = 0; buf [len ++] = 0; buf [len ++] = 0; #endif buf [len ++] = (unsigned char) ((x >> 24) & 0xFF); buf [len ++] = (unsigned char) ((x >> 16) & 0xFF); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const long long & | x | ) | [inline] |
Definition at line 627 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (8); buf [len ++] = (unsigned char) ((x >> 56) & 0xFF); buf [len ++] = (unsigned char) ((x >> 48) & 0xFF); buf [len ++] = (unsigned char) ((x >> 40) & 0xFF); buf [len ++] = (unsigned char) ((x >> 32) & 0xFF); buf [len ++] = (unsigned char) ((x >> 24) & 0xFF); buf [len ++] = (unsigned char) ((x >> 16) & 0xFF); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::Append | ( | const unsigned long long & | x | ) | [inline] |
Definition at line 656 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
{ IncreaseBuffer (8); buf [len ++] = (unsigned char) ((x >> 56) & 0xFF); buf [len ++] = (unsigned char) ((x >> 48) & 0xFF); buf [len ++] = (unsigned char) ((x >> 40) & 0xFF); buf [len ++] = (unsigned char) ((x >> 32) & 0xFF); buf [len ++] = (unsigned char) ((x >> 24) & 0xFF); buf [len ++] = (unsigned char) ((x >> 16) & 0xFF); buf [len ++] = (unsigned char) ((x >> 8) & 0xFF); buf [len ++] = (unsigned char) (x & 0xFF); return *this; } /* mwData::Append */
mwData & chomp::multiwork::mwData::AppendBytes | ( | const unsigned char * | x, | |
int | n | |||
) | [inline, private] |
Appends a data piece to the buffer and swaps bytes if necessary.
Definition at line 349 of file mwdata.h.
References buf, IncreaseBuffer(), and len.
Referenced by Append().
{ // increase the buffer if necessary IncreaseBuffer (n); // if the system is big-endian, the bytes must be copied directly const int testnumber = 1; if (!*((char *) &testnumber)) { for (int i = 0; i < n; ++ i) buf [len ++] = *(x ++); } // otherwise the bytes must be copied in the reverse order else { x += n; for (int i = 0; i < n; ++ i) buf [len ++] = *(-- x); } return *this; } /* mwData::AppendBytes */
const char * chomp::multiwork::mwData::Buffer | ( | ) | const [inline] |
const char * chomp::multiwork::mwData::Current | ( | ) | const [inline] |
void chomp::multiwork::mwData::IncreaseBuffer | ( | int | n | ) | [inline, private] |
Increases the buffer by the given number of bytes (or more) beyond the current position.
Definition at line 326 of file mwdata.h.
References allocated, buf, and len.
Referenced by Append(), and AppendBytes().
{ // if it is not necessary to increase the buffer, do nothing if (len + n <= allocated) return; // allocate a new buffer int allocated1 = allocated + allocated + n; unsigned char *buf1 = new unsigned char [allocated1]; if (!buf1) throw "Not enough memory to increase mwData buffer."; // copy the previous buffer to the new one for (int i = 0; i < len; ++ i) buf1 [i] = buf [i]; // replace the old buffer with the new one delete [] buf; buf = buf1; allocated = allocated1; return; } /* mwData::IncreaseBuffer */
int chomp::multiwork::mwData::Length | ( | ) | const [inline] |
The assignment operator.
Definition at line 223 of file mwdata.h.
References allocated, buf, len, and pos.
{ if (this == &x) return *this; if (buf) delete [] buf; len = x. len; allocated = x. allocated; pos = x. pos; if (allocated) { buf = new unsigned char [allocated]; if (!buf) throw "No memory for mwData assignment operator."; for (int i = 0; i < len; ++ i) buf [i] = x. buf [i]; } else buf = (unsigned char *) 0; return *this; } /* mwData::operator = */
int chomp::multiwork::mwData::Position | ( | int | newpos | ) | [inline] |
int chomp::multiwork::mwData::Position | ( | ) | const [inline] |
mwData & chomp::multiwork::mwData::Reset | ( | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | long & | x | ) | [inline] |
Definition at line 564 of file mwdata.h.
{ if (len - pos < 8) return *this; #if (__LONG_MAX__ > 2147483647) x = (long) (buf [pos ++]) << 56; x |= (long) (buf [pos ++]) << 48; x |= (long) (buf [pos ++]) << 40; x |= (long) (buf [pos ++]) << 32; x |= (long) (buf [pos ++]) << 24; #else pos += 4; x = (long) (buf [pos ++]) << 24; #endif x |= (long) (buf [pos ++]) << 16; x |= (long) (buf [pos ++]) << 8; x |= (long) (buf [pos ++]); return *this; } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | std::string & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | int & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned short & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | char & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned int & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned char & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned char * | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | bool & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned long long & | x | ) | [inline] |
Definition at line 670 of file mwdata.h.
{ if (len - pos < 8) return *this; x = (long long) (buf [pos ++]) << 56; x |= (long long) (buf [pos ++]) << 48; x |= (long long) (buf [pos ++]) << 40; x |= (long long) (buf [pos ++]) << 32; x |= (long long) (buf [pos ++]) << 24; x |= (long long) (buf [pos ++]) << 16; x |= (long long) (buf [pos ++]) << 8; x |= buf [pos ++]; return *this; } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | double & | x | ) | [inline] |
Definition at line 755 of file mwdata.h.
References len, pos, and RetrieveBytes().
{ if (len - pos < 8) return *this; return RetrieveBytes ((unsigned char *) &x, 8); } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | char * | x | ) | [inline] |
Definition at line 813 of file mwdata.h.
References Retrieve().
{ return Retrieve ((unsigned char *) x); } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | short & | x | ) | [inline] |
mwData & chomp::multiwork::mwData::Retrieve | ( | float & | x | ) | [inline] |
Definition at line 742 of file mwdata.h.
References len, pos, and RetrieveBytes().
{ if (len - pos < 4) return *this; return RetrieveBytes ((unsigned char *) &x, 4); } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | unsigned long & | x | ) | [inline] |
Definition at line 605 of file mwdata.h.
{ if (len - pos < 8) return *this; #if (__LONG_MAX__ > 2147483647) x = (long) (buf [pos ++]) << 56; x |= (long) (buf [pos ++]) << 48; x |= (long) (buf [pos ++]) << 40; x |= (long) (buf [pos ++]) << 32; x |= (long) (buf [pos ++]) << 24; #else pos += 4; x = (long) (buf [pos ++]) << 24; #endif x |= (long) (buf [pos ++]) << 16; x |= (long) (buf [pos ++]) << 8; x |= (long) (buf [pos ++]); return *this; } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::Retrieve | ( | long long & | x | ) | [inline] |
Definition at line 641 of file mwdata.h.
{ if (len - pos < 8) return *this; x = (long long) (buf [pos ++]) << 56; x |= (long long) (buf [pos ++]) << 48; x |= (long long) (buf [pos ++]) << 40; x |= (long long) (buf [pos ++]) << 32; x |= (long long) (buf [pos ++]) << 24; x |= (long long) (buf [pos ++]) << 16; x |= (long long) (buf [pos ++]) << 8; x |= buf [pos ++]; return *this; } /* mwData::Retrieve */
mwData & chomp::multiwork::mwData::RetrieveBytes | ( | unsigned char * | x, | |
int | n | |||
) | [inline, private] |
Retrieve a data piece from the buffer and swaps bytes if necessary.
Definition at line 371 of file mwdata.h.
Referenced by Retrieve().
{ // if there is not enough data in the buffer, do nothing if (len - pos < n) return *this; // if the system is big-endian, the bytes must be just copied const int testnumber = 1; if (!*((char *) &testnumber)) { for (int i = 0; i < n; ++ i) *(x ++) = buf [pos ++]; } // otherwise the bytes must be swapped else { x += n; for (int i = 0; i < n; ++ i) *(-- x) = buf [pos ++]; } return *this; } /* mwData::RetrieveBytes */
mwData & chomp::multiwork::mwData::Rewind | ( | ) | [inline] |
mwData & chomp::multiwork::mwData::SkipString | ( | ) | [inline] |
Swaps the data with another data structure.
Definition at line 245 of file mwdata.h.
References allocated, buf, len, and pos.
{ // swap the buffers unsigned char *buf0; buf0 = buf; buf = x. buf; x. buf = buf0; // swap the numbers int number; number = pos; pos = x. pos; x. pos = number; number = len; len = x. len; x. len = number; number = allocated; allocated = x. allocated; x. allocated = number; return *this; } /* mwData::Swap */
mwData & chomp::multiwork::mwData::Take | ( | char * | buffer, | |
int | length | |||
) | [inline] |
mwData & chomp::multiwork::mwData::Take | ( | unsigned char * | buffer, | |
int | length | |||
) | [inline] |
int chomp::multiwork::mwData::allocated [private] |
The length of the allocated buffer memory space.
Definition at line 176 of file mwdata.h.
Referenced by IncreaseBuffer(), mwData(), operator=(), Reset(), Swap(), and Take().
unsigned char* chomp::multiwork::mwData::buf [private] |
The data buffer.
Definition at line 170 of file mwdata.h.
Referenced by Append(), AppendBytes(), Buffer(), Current(), IncreaseBuffer(), mwData(), operator=(), Reset(), Retrieve(), RetrieveBytes(), SkipString(), Swap(), Take(), and ~mwData().
int chomp::multiwork::mwData::len [private] |
The length of the data in the buffer and the write position.
Definition at line 173 of file mwdata.h.
Referenced by Append(), AppendBytes(), IncreaseBuffer(), Length(), mwData(), operator=(), Position(), Reset(), Retrieve(), RetrieveBytes(), SkipString(), Swap(), and Take().
int chomp::multiwork::mwData::pos [private] |
The current read position in the buffer.
Definition at line 179 of file mwdata.h.
Referenced by Current(), mwData(), operator=(), Position(), Reset(), Retrieve(), RetrieveBytes(), Rewind(), SkipString(), Swap(), and Take().