PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | Static Public Member Functions | Related Functions | List of all members
PIByteArray Class Reference

The PIByteArray class provides an array of bytes with efficient memory management and various manipulation operations. More...

#include <pibytearray.h>

Inheritance diagram for PIByteArray:
Inheritance graph
[legend]

Public Member Functions

 PIByteArray ()
 Constructs an empty byte array.
 
 PIByteArray (const PIByteArray &o)
 Constructs copy of byte array "o".
 
 PIByteArray (const PIDeque< uchar > &o)
 Constructs copy of byte array "o".
 
 PIByteArray (PIByteArray &&o)
 Move constructor.
 
 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 (const uint size, uchar t)
 Constructs byte array with size "size" filled by "t".
 
 PIByteArray (std::initializer_list< uchar > init_list)
 Constructs array from C++11 initializer list. More...
 
void swap (PIByteArray &other)
 Swaps array v other with this array. More...
 
PIDeque< uchar >::iterator begin ()
 Iterator to the first element. More...
 
PIDeque< uchar >::iterator end ()
 Iterator to the element following the last element. More...
 
PIDeque< uchar >::reverse_iterator rbegin ()
 Returns a reverse iterator to the first element of the reversed array. More...
 
PIDeque< uchar >::reverse_iterator rend ()
 Returns a reverse iterator to the element. following the last element of the reversed array. More...
 
size_t size () const
 Number of elements in the container. More...
 
ssize_t size_s () const
 Number of elements in the container as signed value. More...
 
size_t length () const
 Same as size(). More...
 
size_t capacity () const
 Number of elements that the container has currently allocated space for. More...
 
bool isEmpty () const
 Checks if the container has no elements. More...
 
bool isNotEmpty () const
 Checks if the container has elements. More...
 
bool any (std::function< bool(uchar e)> test) const
 Tests whether at least one element in the array passes the test implemented by the provided function test. More...
 
bool every (std::function< bool(uchar e)> test) const
 Tests whether all elements in the array passes the test implemented by the provided function test. More...
 
uchar & operator[] (size_t index)
 Full access to element by index. More...
 
uchar at (size_t index) const
 Read only access to element by index. More...
 
uchar & back ()
 Last element. More...
 
uchar & front ()
 Last element. More...
 
bool contains (uchar e, ssize_t start=0) const
 Tests if element e exists in the array. More...
 
int entries (uchar e, ssize_t start=0) const
 Count elements equal e in the array. More...
 
int entries (std::function< bool(uchar e)> test, ssize_t start=0) const
 Count elements in the array passes the test implemented by the provided function test. More...
 
ssize_t indexOf (const uchar &e, ssize_t start=0) const
 Returns the first index at which a given element e can be found in the array, or -1 if it is not present. More...
 
ssize_t indexWhere (std::function< bool(const uchar &e)> test, ssize_t start=0) const
 Returns the first index passes the test implemented by the provided function test, or -1 if it is not present. can be found in the array, or -1 if it is not present. More...
 
ssize_t lastIndexOf (const uchar &e, ssize_t start=-1) const
 Returns the last index at which a given element e can be found in the array, or -1 if it is not present. More...
 
ssize_t lastIndexWhere (std::function< bool(const uchar &e)> test, ssize_t start=-1) const
 Returns the last index passes the test implemented by the provided function test, or -1 if it is not present. More...
 
uchar * data (size_t index=0)
 Pointer to array. More...
 
const uchar * data (size_t index=0) const
 Read only pointer to array. More...
 
PIByteArrayclear ()
 Clear array, remove all elements. More...
 
PIByteArrayfill (uchar e=0)
 Assigns element 'e' to all items in the array. More...
 
PIByteArrayfill (std::function< uchar(size_t i)> f)
 Assigns result of function 'f(size_t i)' to all items in the array. More...
 
PIByteArrayassign (uchar e=0)
 Same as fill(). More...
 
PIByteArrayassign (size_t new_size, uchar e)
 First does resize(new_size) then fill(e). More...
 
PIByteArrayresize (size_t new_size, uchar e=0)
 Sets size of the array, new elements are copied from e. More...
 
PIByteArrayresize (size_t new_size, std::function< uchar(size_t i)> f)
 Sets size of the array, new elements created by function f(size_t i). More...
 
PIByteArray resized (uint new_size) const
 Return resized byte array.
 
PIByteArrayreserve (size_t new_size)
 Attempts to allocate memory for at least new_size elements. More...
 
PIByteArrayinsert (size_t index, uchar e=0)
 Inserts value e at index position in the array. More...
 
PIByteArrayinsert (size_t index, const PIByteArray &v)
 Inserts array v at index position in the array. More...
 
PIByteArrayinsert (size_t index, std::initializer_list< uchar > init_list)
 Inserts the given elements at index position in the array. More...
 
PIByteArrayremove (size_t index, size_t count=1)
 Removes count elements from the middle of the array, starting at index position. More...
 
PIByteArray getRange (size_t index, size_t count) const
 Return sub-array starts from "index" and has "count" or less bytes.
 
PIByteArray takeRange (size_t index, size_t count)
 Cut sub-array of this array. More...
 
PIByteArrayreverse ()
 Reverses this array. More...
 
PIByteArray reversed () const
 Returns reversed array. More...
 
PIByteArrayenlarge (ssize_t add_size, uchar e=0)
 Increases or decreases the size of the array by add_size elements. More...
 
PIByteArrayremoveOne (uchar e)
 Remove no more than one element equal e. More...
 
PIByteArrayremoveAll (uchar e)
 Remove all elements equal e. More...
 
PIByteArrayremoveWhere (std::function< bool(uchar e)> test)
 Remove all elements in the array passes the test implemented by the provided function test. More...
 
PIByteArraypush_back (uchar e)
 Appends the given element e to the end of the array. More...
 
PIByteArraypush_back (std::initializer_list< uchar > init_list)
 Appends the given elements to the end of the array. More...
 
PIByteArraypush_back (const PIByteArray &v)
 Appends the given array v to the end of the array. More...
 
PIByteArraypush_back (const void *data_, int size_)
 Add to the end data "data" with size "size".
 
PIByteArraypush_front (uchar e)
 Appends the given element e to the begin of the array. More...
 
PIByteArraypush_front (const PIByteArray &v)
 Appends the given array v to the begin of the array. More...
 
PIByteArraypush_front (std::initializer_list< uchar > init_list)
 Appends the given elements to the begin of the array. More...
 
PIByteArrayprepend (uchar e)
 Appends the given element e to the begin of the array. More...
 
PIByteArrayprepend (const PIByteArray &v)
 Appends the given array v to the begin of the array. More...
 
PIByteArrayprepend (std::initializer_list< uchar > init_list)
 Appends the given elements to the begin of the array. More...
 
PIByteArraypop_back ()
 Remove one element from the end of the array. More...
 
PIByteArraypop_front ()
 Remove one element from the begining of the array. More...
 
uchar take_back ()
 Remove one element from the end of the array and return it. More...
 
uchar take_front ()
 Remove one element from the begining of the array and return it. More...
 
PIByteArray filter (std::function< bool(const uchar &e)> test) const
 Returns a new array with all elements that pass the test implemented by the provided function test. More...
 
void forEach (std::function< void(const uchar &e)> f) const
 Execute function void f(const uchar & e) for every element in array. More...
 
PIByteArrayforEach (std::function< void(uchar &e)> f)
 Execute function void f(uchar & e) for every element in array. More...
 
template<typename ST >
PIDeque< ST > map (std::function< ST(const uchar &e)> f) const
 Сreates a new array populated with the results of calling a provided function ST f(const uchar & e) on every element in the calling array. More...
 
template<typename ST >
ST reduce (std::function< ST(const uchar &e, const ST &acc)> f, const ST &initial=ST()) const
 Applies the function ST f(const uchar & e, const ST & acc) to each element of the array (from left to right), returns one value. More...
 
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.
 
PIString toString (int base=16) const
 Return string representation of data, each byte in "base" base, separated by spaces.
 
PIString toHex () const
 Returns a hex encoded copy of the byte array, without spaces. The hex encoding uses the numbers 0-9 and the letters a-f.
 
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".
 
PIByteArrayappend (uchar t)
 Add to the end "t".
 
PIByteArrayappend (std::initializer_list< uchar > init_list)
 Appends the given elements to the end of the array. More...
 
uchar checksumPlain8 (bool inverse=true) const
 Returns 8-bit checksum. More...
 
uint checksumPlain32 (bool inverse=true) const
 Returns 32-bit checksum. More...
 
uchar checksumCRC8 () const
 Returns 8-bit checksum CRC-8.
 
ushort checksumCRC16 () const
 Returns 16-bit checksum CRC-16.
 
uint checksumCRC32 () const
 Returns 32-bit checksum CRC-32.
 
uint hash () const
 Returns hash of content.
 
- Public Member Functions inherited from PIBinaryStream< PIByteArray >
bool binaryStreamAppend (const void *d, size_t s)
 Writes raw bytes to the underlying stream.
 
void binaryStreamAppend (T v)
 Writes one trivially copied value as raw bytes.
 
bool binaryStreamTake (void *d, size_t s)
 Reads raw bytes from the underlying stream and sets read error state on short reads.
 
ssize_t binaryStreamSize () const
 Returns remaining readable byte count. More...
 
int binaryStreamTakeInt ()
 Reads one int value from the stream.
 
bool wasReadError () const
 Returns whether there has been an incomplete read since last resetReadError() or after the stream was created.
 
void resetReadError ()
 Reset incomplete read flag.
 

Static Public Member Functions

static PIByteArray fromBase64 (const PIByteArray &base64)
 Return converted from Base 64 data.
 

Related Functions

(Note that these are not member functions.)

bool operator< (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator> (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator== (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator!= (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
PIByteArray operator& (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "and". If non-equal size, then returns empty PIByteArray.
 
PIByteArray operator| (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "or". If non-equal size, then returns empty PIByteArray.
 
PIByteArray operator^ (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "xor". If non-equal size, then returns empty PIByteArray.
 
PICout operator<< (PICout s, const PIByteArray &ba)
 Output operator to PICout.
 
template<>
uint piHash (const PIByteArray &ba)
 Returns PIByteArray::hash() of "ba".
 
template<>
void piSwap (PIByteArray &f, PIByteArray &s)
 Swap contents betwee "f" and "s".
 
template<typename T >
PIByteArray piSerialize (const T &value)
 Store "value" to bytearray and returns it.
 
template<typename T >
piDeserialize (const PIByteArray &data)
 Restore type "T" from bytearray "data" and returns it.
 

Detailed Description

The PIByteArray class provides an array of bytes with efficient memory management and various manipulation operations.

PIByteArray used to store raw bytes. It can be constructed from any data and size. You can use PIByteArray as binary stream to serialize/deserialize any objects and data. See details Input/Output stream. This class use PIDeque<uchar> and provide some handle function to manipulate it.

Usage

PIByteArray subclass PIBinaryStream and can be used to store custom data and manipulate it. Store operators places data at the end of array, restore operators takes data from the beginning of array. In addition there are Hex and Base64 convertions.

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}
The PIByteArray class provides an array of bytes with efficient memory management and various manipul...
Definition: pibytearray.h:42
#define piCout
Definition: picout.h:36