C++ Based OPC UA Client/Server/PubSub SDK  1.7.6.537

The UaByteArray handles the access to an array of bytes. More...

#include <uabytearray.h>

Public Member Functions

 UaByteArray ()
 Constructs an empty byte array. More...
 
 UaByteArray (const char *pData, int iSize)
 Constructs a byte array as copy of pData. More...
 
 UaByteArray (const UaByteArray &other)
 Constructs a copy of other. More...
 
 UaByteArray (const OpcUa_ByteString &other)
 construction More...
 
 UaByteArray (const UaByteString &other)
 Constructs a byte array from a given byte string. More...
 
 ~UaByteArray ()
 Destroys the array. More...
 
UaByteArrayoperator= (const UaByteArray &other)
 Assigns other to this array and returns a reference to this array. More...
 
void attach (OpcUa_UInt32 length, OpcUa_Byte *data)
 Attach a raw OpcUa_Byte array. More...
 
void attach (OpcUa_Int32 length, OpcUa_Byte *data)
 Attach a raw OpcUa_Byte array. More...
 
OpcUa_Byte * detach ()
 Detach from the internal OpcUa_Byte array. More...
 
void create (unsigned int size)
 Creates the array with size.
 
void resize (int iSize)
 Resizes the array. More...
 
int size () const
 Returns the size of this array. More...
 
int capacity () const
 Returns the capacity of this array. More...
 
char operator[] (int i) const
 Returns the byte at the given position. More...
 
char operator[] (unsigned int i) const
 Returns the byte at the given position. More...
 
UaByteRef operator[] (int i)
 This function is provided for convenience and behaves like the function above. More...
 
UaByteRef operator[] (unsigned int i)
 This function is provided for convenience and behaves like the function above. More...
 
const char * data () const
 Returns the pointer to the internal array data (as const char*). More...
 
 operator const char * () const
 Returns the pointer to the internal array data as (const char*). More...
 
 operator const UaUShort * () const
 Returns the pointer to the internal array data as (const UaUShort*). More...
 
 operator const void * () const
 Returns the pointer to the internal array data as (const void*). More...
 
void copyTo (OpcUa_ByteString *) const
 Copies the UaByteArray Date into a OpcUa_ByteString. More...
 
UaString toHex (bool bSeparateWithSpace=false) const
 Converts the byte array into a hex string. More...
 

Static Public Member Functions

static UaByteArray fromHex (const UaString &src)
 Converts a hexadecimal string representation of a byte array into a UaByteArray. More...
 

Detailed Description

The UaByteArray handles the access to an array of bytes.

UaByteArray can be used to store both raw bytes (including \0s) and traditional 8-bit \0-terminated strings. Using UaByteArray is much more convenient than using const char *. Behind the scenes, it always ensures that the data is followed by a \0 terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data. You don't need to care about resizing or cleaning up the memory.

Constructor & Destructor Documentation

UaByteArray::UaByteArray ( )

Constructs an empty byte array.

UaByteArray::UaByteArray ( const char *  pData,
int  iSize 
)

Constructs a byte array as copy of pData.

Parameters
pDatadata to copy
iSizelength of the data in bytes
UaByteArray::UaByteArray ( const UaByteArray other)

Constructs a copy of other.

This operation takes constant time, because UaByteArray is implicitly shared. This makes returning a UaByteArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.

UaByteArray::UaByteArray ( const OpcUa_ByteString other)

construction

Parameters
otherthe OpcUa_ByteString to assign.
UaByteArray::UaByteArray ( const UaByteString other)

Constructs a byte array from a given byte string.

Parameters
otherthe byte string to copy
UaByteArray::~UaByteArray ( )

Destroys the array.

Member Function Documentation

void UaByteArray::attach ( OpcUa_UInt32  length,
OpcUa_Byte *  data 
)

Attach a raw OpcUa_Byte array.

Note
Don't manually free the attached data afterwards, because UaByteArray is responsible for it now.
Parameters
lengthThe length of the raw OpcUa_Byte array
dataThe raw OpcUa_Byte array
void UaByteArray::attach ( OpcUa_Int32  length,
OpcUa_Byte *  data 
)

Attach a raw OpcUa_Byte array.

Note
Don't manually free the attached data afterwards, because UaByteArray is responsible for it now.
Parameters
lengthThe length of the raw OpcUa_Byte array
dataThe raw OpcUa_Byte array
int UaByteArray::capacity ( ) const

Returns the capacity of this array.

You will rarely ever need this function. You can check how many bytes you can until an automatic resize happens.

Returns
Capacity of this array.
void UaByteArray::copyTo ( OpcUa_ByteString pDst) const

Copies the UaByteArray Date into a OpcUa_ByteString.

Parameters
pDstthe destination of this operation.
const char * UaByteArray::data ( ) const

Returns the pointer to the internal array data (as const char*).

The pointer stays valid until you modify the array or it's deleted.

OpcUa_Byte * UaByteArray::detach ( )

Detach from the internal OpcUa_Byte array.

After detaching, the detached array data will not be cleared automatically anymore. If more than one reference exists, the data is copied to the return value instead of being detached.

Note
Remember to retrieve the length of the array before detaching.
UaByteArray UaByteArray::fromHex ( const UaString src)
static

Converts a hexadecimal string representation of a byte array into a UaByteArray.

This string should not contain spaces and the string length must be a multiple of 2 (2 chars per byte).

Parameters
srcthe source of this operation.
Returns
If an error occurs an empty byte array is returned.
UaByteArray::operator const char * ( ) const

Returns the pointer to the internal array data as (const char*).

The pointer stays valid until you modify the array or it's deleted.

UaByteArray::operator const UaUShort * ( ) const

Returns the pointer to the internal array data as (const UaUShort*).

The pointer stays valid until you modify the array or it's deleted.

UaByteArray::operator const void * ( ) const

Returns the pointer to the internal array data as (const void*).

The pointer stays valid until you modify the array or it's deleted.

UaByteArray & UaByteArray::operator= ( const UaByteArray other)

Assigns other to this array and returns a reference to this array.

Parameters
otherthe UaByteArray to assign.
Returns
a reference to this array.
char UaByteArray::operator[] ( int  i) const

Returns the byte at the given position.

char UaByteArray::operator[] ( unsigned int  i) const

Returns the byte at the given position.

UaByteRef UaByteArray::operator[] ( int  i)
inline

This function is provided for convenience and behaves like the function above.

Returns the byte at index position i as a modifiable reference.

See also
operator[](int i)

If an assignment is made beyond the end of the byte array, the array is extended with resize() before the assignment takes place.

Example:

   UaByteArray ba;
   for (int i = 0; i < 10; ++i)
       ba[i] = 'A' + i;
   // ba == "ABCDEFGHIJ"

The return value is of type UaByteRef, a helper class for UaByteArray. When you get an object of type UaByteRef, you can use it as if it were a char &. If you assign to it, the assignment will apply to the character in the UaByteArray from which you got the reference.

See also
operator[](unsigned int i)
Parameters
ia variable to compare with.
Returns
a ByteRef reference.
UaByteRef UaByteArray::operator[] ( unsigned int  i)
inline

This function is provided for convenience and behaves like the function above.

See also
operator[](int i)
operator[](int i)
Parameters
ia variable to compare with.
Returns
ByteRef reference.
void UaByteArray::resize ( int  iSize)

Resizes the array.

If more than one reference exists to this array, this function creates a copy this array with the new size iSize.

Parameters
iSizethe actual size to resize the array. If <= 0, the ByteArray gets cleared and the size and the capacity are set to 0.
int UaByteArray::size ( ) const

Returns the size of this array.

Returns
the size of this array.
UaString UaByteArray::toHex ( bool  bSeparateWithSpace = false) const

Converts the byte array into a hex string.

Parameters
bSeparateWithSpaceSeparates each byte in the hex string with a space. Per default bSeparateWithSpace=false, you can use bSeparateWithSpace = true for better display.
Returns
a hex string.

The documentation for this class was generated from the following files: