MeshNet  1.0.0
Public Member Functions | Protected Attributes
MeshVariableArray< T > Class Template Reference

This class safetly handles allocation and deallocation of memory. It can be used in conjunction with meshPacket or meshFrame to create data(payload) part before creating packet or frame. Main advantage is, that std::move can be used to set data (payload) with variable array without manually copying each element in array. More...

#include <meshVariableArray.h>

Public Member Functions

 MeshVariableArray ()
 Constructor. More...
 
 MeshVariableArray (const uint16_t length)
 Create uninitialized array with specified length. More...
 
 MeshVariableArray (const T *newData, const uint16_t newDataLength)
 Sets new data to whole and sets its size. More...
 
 MeshVariableArray (const T fillItem, const uint16_t length)
 Fills whole array with the specified item. More...
 
 MeshVariableArray (const MeshVariableArray< T > &mva)
 Copy constructor. More...
 
 MeshVariableArray (MeshVariableArray< T > &&mva) noexcept
 Move constructor. More...
 
 ~MeshVariableArray ()
 Destructor. More...
 
MeshVariableArray< T > & operator= (const MeshVariableArray< T > &mva)
 Copy assignment operator. More...
 
MeshVariableArray< T > & operator= (MeshVariableArray< T > &&mva) noexcept
 Move assignment operator. More...
 
uint16_t Length () const
 Length of data. More...
 
operator[] (uint16_t i) const
 
T & operator[] (uint16_t i)
 
GetAt (uint16_t index) const
 Reads item at specified index. More...
 
bool SetAt (uint16_t index, T value)
 Sets item at specified index. More...
 
bool SetArray (const T *newData, const uint16_t newDataLength)
 Sets new data to whole and resizes (expand/shrink) it. More...
 
bool SetData (const T *newData, uint16_t startIndex, const uint16_t newDataLength)
 Sets new data. Array will be expanded if data won't fit. More...
 
void FillArray (const T fillItem)
 Fills whole array with the specified item. More...
 
bool FillData (const T fillItem, uint16_t startIndex, uint16_t count)
 Fills specified part of array with the specified item. Array can be expanded when startIndex + count is more than array length. More...
 
void GetData (T *data, uint16_t &dataLength) const
 Gets data from whole array. More...
 
bool ResizeArray (uint16_t size, bool doNotCopy=false)
 Resizes array. More...
 
void Clear ()
 Sets array size to 0. More...
 
bool CopyFrom (const MeshVariableArray< T > &another)
 Copyies data from another array to current array. More...
 
void MoveFrom (MeshVariableArray< T > &another)
 Moves data from another array to current array. More...
 
void MoveFrom (T *&dataPtr, uint16_t dataLength)
 Moves data from another array to current array. More...
 
T * UnsafeDataMove ()
 Takes pointer to allocated array and removes that pointer from current class. More...
 
const T * d_ptr () const
 Pointer to data. More...
 

Protected Attributes

T * Data = NULL
 Pointer to array of data. More...
 
uint16_t DataLength = 0
 Length of current data. More...
 

Detailed Description

template<typename T>
class MeshVariableArray< T >

This class safetly handles allocation and deallocation of memory. It can be used in conjunction with meshPacket or meshFrame to create data(payload) part before creating packet or frame. Main advantage is, that std::move can be used to set data (payload) with variable array without manually copying each element in array.

See also
meshVariableArray.h

Constructor & Destructor Documentation

◆ MeshVariableArray() [1/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( )
inline

Constructor.

◆ MeshVariableArray() [2/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( const uint16_t  length)
inline

Create uninitialized array with specified length.

Parameters
[in]lengthLength of array to create.
Note
To check if array was sucessfully allocated, check if array length matches to length.

◆ MeshVariableArray() [3/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( const T *  newData,
const uint16_t  newDataLength 
)
inline

Sets new data to whole and sets its size.

Parameters
[in]newDataNew payload data.
[in]newDataLengthLength of new data.
Note
To check if array was sucessfully allocated, check if array length matches to newDataLength.

◆ MeshVariableArray() [4/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( const T  fillItem,
const uint16_t  length 
)
inline

Fills whole array with the specified item.

Parameters
[in]fillItemItem, that will be copyied to each item in the array.
[in]lengthLength of array to fill.
Note
To check if array was sucessfully allocated, check if array length matches to length.

◆ MeshVariableArray() [5/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( const MeshVariableArray< T > &  mva)
inline

Copy constructor.

◆ MeshVariableArray() [6/6]

template<typename T >
MeshVariableArray< T >::MeshVariableArray ( MeshVariableArray< T > &&  mva)
inlinenoexcept

Move constructor.

◆ ~MeshVariableArray()

template<typename T >
MeshVariableArray< T >::~MeshVariableArray ( )
inline

Destructor.

Member Function Documentation

◆ Clear()

template<typename T >
void MeshVariableArray< T >::Clear ( )
inline

Sets array size to 0.

◆ CopyFrom()

template<typename T >
bool MeshVariableArray< T >::CopyFrom ( const MeshVariableArray< T > &  another)
inline

Copyies data from another array to current array.

Parameters
anotherVariable array to copy data from.
Returns
Returns true when copying was done correctly.

◆ d_ptr()

template<typename T >
const T* MeshVariableArray< T >::d_ptr ( ) const
inline

Pointer to data.

Warning
Use carefully!

◆ FillArray()

template<typename T >
void MeshVariableArray< T >::FillArray ( const T  fillItem)
inline

Fills whole array with the specified item.

Parameters
[in]fillItemItem, that will be copyied to each item in the array.

◆ FillData()

template<typename T >
bool MeshVariableArray< T >::FillData ( const T  fillItem,
uint16_t  startIndex,
uint16_t  count 
)
inline

Fills specified part of array with the specified item. Array can be expanded when startIndex + count is more than array length.

Parameters
[in]fillItemItem, that will be copyied to specified items in the array.
[in]startIndexIndex in array, where to start filling.
[in]countCount of items to fill.
Returns
Returns true when items were successfully filled. False if array resizing failed.

◆ GetAt()

template<typename T >
T MeshVariableArray< T >::GetAt ( uint16_t  index) const
inline

Reads item at specified index.

Parameters
indexIndex, where to read item.
Returns
Returns item from data at specified index.

◆ GetData()

template<typename T >
void MeshVariableArray< T >::GetData ( T *  data,
uint16_t &  dataLength 
) const
inline

Gets data from whole array.

Parameters
[out]dataBuffer, where data will be copyied.
[in/out]dataLength Set length of your buffer. After data copying this value will contain copyied data length.

◆ Length()

template<typename T >
uint16_t MeshVariableArray< T >::Length ( ) const
inline

Length of data.

◆ MoveFrom() [1/2]

template<typename T >
void MeshVariableArray< T >::MoveFrom ( MeshVariableArray< T > &  another)
inline

Moves data from another array to current array.

Parameters
anotherVariable array to move data from.
Note
Data from "another" array will be removed.

◆ MoveFrom() [2/2]

template<typename T >
void MeshVariableArray< T >::MoveFrom ( T *&  dataPtr,
uint16_t  dataLength 
)
inline

Moves data from another array to current array.

Parameters
[in,out]dataPtrReference to pointer, that points to allocated array. This will be set to NULL.
dataLengthReference to variable, which holds size of array. Please set it's value to 0 after calling this method.

◆ operator=() [1/2]

template<typename T >
MeshVariableArray<T>& MeshVariableArray< T >::operator= ( const MeshVariableArray< T > &  mva)
inline

Copy assignment operator.

◆ operator=() [2/2]

template<typename T >
MeshVariableArray<T>& MeshVariableArray< T >::operator= ( MeshVariableArray< T > &&  mva)
inlinenoexcept

Move assignment operator.

◆ operator[]() [1/2]

template<typename T >
T& MeshVariableArray< T >::operator[] ( uint16_t  i)
inline

◆ operator[]() [2/2]

template<typename T >
T MeshVariableArray< T >::operator[] ( uint16_t  i) const
inline

◆ ResizeArray()

template<typename T >
bool MeshVariableArray< T >::ResizeArray ( uint16_t  size,
bool  doNotCopy = false 
)
inline

Resizes array.

Parameters
sizeNew size. Allowed value from 0 to 32 767.
doNotCopyTrue to do not copy old data. Old data may stay here.
Returns
Returns true when resizing was sucessfull.

◆ SetArray()

template<typename T >
bool MeshVariableArray< T >::SetArray ( const T *  newData,
const uint16_t  newDataLength 
)
inline

Sets new data to whole and resizes (expand/shrink) it.

Parameters
[in]newDataNew payload data.
[in]newDataLengthLength of new data.
Returns
Returns true when data were successfully copyied.

◆ SetAt()

template<typename T >
bool MeshVariableArray< T >::SetAt ( uint16_t  index,
value 
)
inline

Sets item at specified index.

Parameters
indexIndex, where to set item.
valueItem value to set.
Returns
Returns true when item value was set.

◆ SetData()

template<typename T >
bool MeshVariableArray< T >::SetData ( const T *  newData,
uint16_t  startIndex,
const uint16_t  newDataLength 
)
inline

Sets new data. Array will be expanded if data won't fit.

Parameters
[in]newDataNew payload data.
[in]startIndexIndex, where to start filling.
[in]newDataLengthLength of new data.
Returns
Returns true when data were successfully copyied. False if array resizing failed.

◆ UnsafeDataMove()

template<typename T >
T* MeshVariableArray< T >::UnsafeDataMove ( )
inline

Takes pointer to allocated array and removes that pointer from current class.

Warning
Do not use if you don't know what are you doing. I am not kidding, this can remove pointer to allocated memory, which will not be able to deallocate.
Returns
Pointer to allocated memory.

Field Documentation

◆ Data

template<typename T >
T* MeshVariableArray< T >::Data = NULL
protected

Pointer to array of data.

◆ DataLength

template<typename T >
uint16_t MeshVariableArray< T >::DataLength = 0
protected

Length of current data.


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