PIP  0.4.0_beta2
Platform-Independent Primitives
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PIByteArray Class Reference

Byte array. More...

Inherits PIDeque< T >.

Classes

struct  RawData
 Help struct to store/restore custom blocks of data to/from PIByteArray. More...
 

Public Member Functions

 PIByteArray ()
 Constructs an empty byte array.
 
 PIByteArray (const uint size)
 Constructs 0-filled byte array with size "size".
 
 PIByteArray (const void *data, const uint size)
 Constructs byte array from data "data" and size "size".
 
PIByteArray resized (int new_size) const
 Return resized byte array.
 
PIByteArrayconvertToBase64 ()
 Convert data to Base 64 and return this byte array.
 
PIByteArrayconvertFromBase64 ()
 Convert data from Base 64 and return this byte array.
 
PIByteArray toBase64 () const
 Return converted to Base 64 data.
 
PIByteArray fromBase64 () const
 Return converted from Base 64 data.
 
PIByteArrayappend (const void *data_, int size_)
 Add to the end data "data" with size "size".
 
PIByteArrayappend (const PIByteArray &data_)
 Add to the end byte array "data".
 
uchar checksumPlain8 () const
 Returns plain 8-bit checksum.
 
uint checksumPlain32 () const
 Returns plain 32-bit checksum.
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &s, const PIByteArray &ba)
 Output to std::ostream operator.
 
PICout operator<< (PICout s, const PIByteArray &ba)
 Output to PICout operator.
 
PIByteArrayoperator<< (PIByteArray &s, uchar v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const short v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const int v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const long &v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const llong &v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const ushort v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const uint v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const ulong &v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const ullong &v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const float v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const double &v)
 Store operator.
 
PIByteArrayoperator<< (PIByteArray &s, const PIByteArray &v)
 Store operator, see Attention for details.
 
PIByteArrayoperator<< (PIByteArray &s, const PIByteArray::RawData &v)
 Store operator, see Attention for details.
 
template<typename Type0 , typename Type1 >
PIByteArrayoperator<< (PIByteArray &s, const PIPair< Type0, Type1 > &v)
 Store operator.
 
template<typename T >
PIByteArrayoperator<< (PIByteArray &s, const PIVector< T > &v)
 Store operator.
 
template<typename T >
PIByteArrayoperator<< (PIByteArray &s, const PIList< T > &v)
 Store operator.
 
template<typename T >
PIByteArrayoperator<< (PIByteArray &s, const PIDeque< T > &v)
 Store operator.
 
PIByteArrayoperator>> (PIByteArray &s, uchar &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, short &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, int &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, long &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, llong &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, ushort &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, uint &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, ulong &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, ullong &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, float &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, double &v)
 Restore operator.
 
PIByteArrayoperator>> (PIByteArray &s, PIByteArray &v)
 Restore operator, see Attention for details.
 
PIByteArrayoperator>> (PIByteArray &s, PIByteArray::RawData v)
 Restore operator, see Attention for details.
 
template<typename Type0 , typename Type1 >
PIByteArrayoperator>> (PIByteArray &s, PIPair< Type0, Type1 > &v)
 Restore operator.
 
template<typename T >
PIByteArrayoperator>> (PIByteArray &s, PIVector< T > &v)
 Restore operator.
 
template<typename T >
PIByteArrayoperator>> (PIByteArray &s, PIList< T > &v)
 Restore operator.
 
template<typename T >
PIByteArrayoperator>> (PIByteArray &s, PIDeque< T > &v)
 Restore operator.
 
bool operator== (PIByteArray &f, PIByteArray &s)
 Byte arrays compare operator.
 
bool operator!= (PIByteArray &f, PIByteArray &s)
 Byte arrays compare operator.
 
PIByteArrayoperator<< (PIByteArray &s, const PIString &v)
 Output operator to PIByteArray.
 
PIByteArrayoperator>> (PIByteArray &s, PIString &v)
 Input operator from PIByteArray.
 
PIByteArrayoperator<< (PIByteArray &s, const PIStringList &v)
 Output operator to PIByteArray.
 
PIByteArrayoperator>> (PIByteArray &s, PIStringList &v)
 Input operator from PIByteArray.
 
PICout operator<< (PICout s, const PISystemTime &v)
 Output operator to PICout.
 
PIByteArrayoperator<< (PIByteArray &s, const PISystemTime &v)
 Output operator to PIByteArray.
 
PIByteArrayoperator>> (PIByteArray &s, PISystemTime &v)
 Input operator from PIByteArray.
 

Detailed Description

Byte array.

This class based on PIDeque<uchar> and provide some handle function to manipulate it.

Usage

PIByteArray can be used to store custom data and manipulate it. There are many stream operators to store/restore common types to byte array. Store operators places data at the end of array, restore operators takes data from the beginning of array. In addition there are Base 64 convertions and checksums:

  • plain 8-bit
  • plain 32-bit

One of the major usage of PIByteArray is stream functions. You can form binary packet from many types (also dynamic types, e.g. PIVector) with one line:

int i = -1, j = 2;
float f = 1.;
PIString text("123");
ba << i << j << f << text; // form binary data
piCout << "data =" << ba;
i = j = 0; // clear variables
f = 0; // clear variables
text.clear(); // clear variables
piCout << i << j << f << text; // show variables
ba >> i >> j >> f >> text; // restore data
piCout << i << j << f << text; // show variables
piCout << "data =" << ba;

Or you can descibe stream operator of your own type and store/restore vectors of your type:

struct MyType {
MyType(int i_ = 0, const PIString & t_ = PIString()) {
m_i = i_;
m_text = t_;
}
int m_i;
PIString m_text;
};
inline PIByteArray & operator <<(PIByteArray & s, const MyType & v) {s << v.m_i << v.m_text; return s;}
inline PIByteArray & operator >>(PIByteArray & s, MyType & v) {s >> v.m_i >> v.m_text; return s;}
my_vec << MyType(1, "s1") << MyType(10, "s10"); // add to vector
ba << my_vec; // store to byte array
piCout << "data =" << ba;
my_vec.clear(); // clear vector
ba >> my_vec; // restore from byte array

For store/restore custom data blocks there is PIByteArray::RawData class. Stream operators of this class simply store/restore data block to/from byte array.

const char * chars = "8 bytes";
ba << PIByteArray::RawData(chars, 8); // form binary data
piCout << "data =" << ba;
char rchars[16];
memset(rchars, 0, 16); // clear data
ba >> PIByteArray::RawData(rchars, 8); // restore data
piCout << rchars;
piCout << "data =" << ba;

Attention

Stream operator of PIByteArray store byte array as vector, not simply append content of byte array. This operators useful to transmit custom data as PIByteArray packed into parent byte array, e.g. to form packet from PIByteArray. To append one byte array to another use funtion append().

PIByteArray ba, sba;
uchar uc(127);
sba << uc; // byte array with one byte
ba << sba; // stream operator
piCout << ba; // result
// {1, 0, 0, 0, 127}
ba.clear();
ba.append(sba);
piCout << ba; // result
// {127}