PcapPlusPlus  24.09
pcpp::PointerVector< T > Class Template Reference

#include <PointerVector.h>

Public Types

using VectorIterator = typename std::vector< T * >::iterator
 
using ConstVectorIterator = typename std::vector< T * >::const_iterator
 

Public Member Functions

 PointerVector ()
 
 PointerVector (const PointerVector &other)
 
 PointerVector (PointerVector &&other) noexcept
 
 ~PointerVector ()
 
PointerVectoroperator= (const PointerVector &other)
 
PointerVectoroperator= (PointerVector &&other) noexcept
 
void clear ()
 
void pushBack (std::nullptr_t element, bool freeElementOnError=true)=delete
 
void pushBack (T *element, bool freeElementOnError=true)
 
void pushBack (std::unique_ptr< T > element)
 
VectorIterator begin ()
 
ConstVectorIterator begin () const
 
VectorIterator end ()
 
ConstVectorIterator end () const
 
size_t size () const
 
T * front ()
 
T const * front () const
 
T * back ()
 
VectorIterator erase (VectorIterator position)
 
T * getAndRemoveFromVector (VectorIterator &position)
 
std::unique_ptr< T > getAndDetach (size_t index)
 
std::unique_ptr< T > getAndDetach (VectorIterator &position)
 
std::unique_ptr< T > getAndDetach (VectorIterator const &position)
 
T * at (int index)
 
const T * at (int index) const
 

Detailed Description

template<typename T>
class pcpp::PointerVector< T >

A template class for representing a std::vector of pointers. Once (a pointer to) an element is added to this vector, the element responsibility moves to the vector, meaning the PointerVector will free the object once it's removed from the vector This class wraps std::vector and adds the capability of freeing objects once they're removed from it

Member Typedef Documentation

◆ ConstVectorIterator

template<typename T >
using pcpp::PointerVector< T >::ConstVectorIterator = typename std::vector<T*>::const_iterator

Const iterator object that is used for iterating all elements in a constant vector

◆ VectorIterator

template<typename T >
using pcpp::PointerVector< T >::VectorIterator = typename std::vector<T*>::iterator

Iterator object that is used for iterating all elements in the vector

Constructor & Destructor Documentation

◆ PointerVector() [1/3]

template<typename T >
pcpp::PointerVector< T >::PointerVector ( )
inline

A constructor that create an empty instance of this object

◆ PointerVector() [2/3]

template<typename T >
pcpp::PointerVector< T >::PointerVector ( const PointerVector< T > &  other)
inline

Copies the vector along with all elements inside it. All elements inside the copied vector are duplicates and the originals remain unchanged.

Parameters
[in]otherThe vector to copy from.
Remarks
As the vector is copied via deep copy, all pointers obtained from the copied vector reference the duplicates and not the originals.

◆ PointerVector() [3/3]

template<typename T >
pcpp::PointerVector< T >::PointerVector ( PointerVector< T > &&  other)
inlinenoexcept

Move constructor. All elements along with their ownership is transferred to the new vector.

Parameters
[in]otherThe vector to move from.

◆ ~PointerVector()

template<typename T >
pcpp::PointerVector< T >::~PointerVector ( )
inline

A destructor for this class. The destructor frees all elements that are binded to the vector

Member Function Documentation

◆ at() [1/2]

template<typename T >
T* pcpp::PointerVector< T >::at ( int  index)
inline

Return a pointer to the element in a certain index

Parameters
[in]indexThe index to retrieve the element from
Returns
The element at the specified position in the vector

◆ at() [2/2]

template<typename T >
const T* pcpp::PointerVector< T >::at ( int  index) const
inline

Return a const pointer to the element in a certain index

Parameters
[in]indexThe index to retrieve the element from
Returns
The element at the specified position in the vector

◆ back()

template<typename T >
T* pcpp::PointerVector< T >::back ( )
inline
Returns
A pointer to the last element in the vector

◆ begin() [1/2]

template<typename T >
VectorIterator pcpp::PointerVector< T >::begin ( )
inline

Get the first element of the vector

Returns
An iterator object pointing to the first element of the vector

◆ begin() [2/2]

template<typename T >
ConstVectorIterator pcpp::PointerVector< T >::begin ( ) const
inline

Get the first element of a constant vector

Returns
A const iterator object pointing to the first element of the vector

◆ clear()

template<typename T >
void pcpp::PointerVector< T >::clear ( )
inline

Clears all elements of the vector while freeing them

◆ end() [1/2]

template<typename T >
VectorIterator pcpp::PointerVector< T >::end ( )
inline

Get the last element of the vector

Returns
An iterator object pointing to the last element of the vector

◆ end() [2/2]

template<typename T >
ConstVectorIterator pcpp::PointerVector< T >::end ( ) const
inline

Get the last element of a constant vector

Returns
A const iterator object pointing to the last element of the vector

◆ erase()

template<typename T >
VectorIterator pcpp::PointerVector< T >::erase ( VectorIterator  position)
inline

Removes from the vector a single element (position). Once the element is erased, it's also freed

Parameters
[in]positionThe position of the element to erase
Returns
An iterator pointing to the new location of the element that followed the last element erased by the function call

◆ front() [1/2]

template<typename T >
T* pcpp::PointerVector< T >::front ( )
inline
Returns
A pointer of the first element in the vector

◆ front() [2/2]

template<typename T >
T const* pcpp::PointerVector< T >::front ( ) const
inline
Returns
A pointer to the first element in the vector

◆ getAndDetach() [1/3]

template<typename T >
std::unique_ptr<T> pcpp::PointerVector< T >::getAndDetach ( size_t  index)
inline

Removes an element from the vector and transfers ownership to the returned unique pointer.

Parameters
[in]indexThe index of the element to detach.
Returns
An unique pointer that holds ownership of the detached element.

◆ getAndDetach() [2/3]

template<typename T >
std::unique_ptr<T> pcpp::PointerVector< T >::getAndDetach ( VectorIterator position)
inline

Removes an element from the vector and transfers ownership to the returned unique pointer.

Parameters
[in,out]positionAn iterator pointing to the element to detach. The iterator is shifted to the following element after the detach completes.
Returns
An unique pointer that holds ownership of the detached element.

◆ getAndDetach() [3/3]

template<typename T >
std::unique_ptr<T> pcpp::PointerVector< T >::getAndDetach ( VectorIterator const &  position)
inline

Removes an element from the vector and transfers ownership to the returned unique pointer.

Parameters
[in]positionAn iterator pointing to the element to detach.
Returns
An unique pointer that holds ownership of the detached element.

◆ getAndRemoveFromVector()

template<typename T >
T* pcpp::PointerVector< T >::getAndRemoveFromVector ( VectorIterator position)
inline

Remove an element from the vector without freeing it

Parameters
[in,out]positionThe position of the element to remove from the vector. The iterator is shifted to the following element after the removal is completed.
Returns
A pointer to the element which is no longer managed by the vector. It's user responsibility to free it
Deprecated:
Deprecated in favor of 'getAndDetach' as that function provides memory safety.

◆ operator=() [1/2]

template<typename T >
PointerVector& pcpp::PointerVector< T >::operator= ( const PointerVector< T > &  other)
inline

A copy assignment operator. Replaces the contents with a copy of the contents of other. See copy constructor for more information on the specific copy procedure.

Parameters
[in]otherThe vector to copy from.
Returns
A reference to the current object.

◆ operator=() [2/2]

template<typename T >
PointerVector& pcpp::PointerVector< T >::operator= ( PointerVector< T > &&  other)
inlinenoexcept

A move assignment operator. Replaces the contents with those of other via move semantics. The other vector is left empty.

Parameters
[in]otherThe vector to move from.
Returns
A reference to the current object.

◆ pushBack() [1/3]

template<typename T >
void pcpp::PointerVector< T >::pushBack ( std::nullptr_t  element,
bool  freeElementOnError = true 
)
delete

Adding a nullptr to the vector is not allowed.

◆ pushBack() [2/3]

template<typename T >
void pcpp::PointerVector< T >::pushBack ( std::unique_ptr< T >  element)
inline

Add a new element to the vector that has been managed by an unique pointer.

Parameters
[in]elementA unique pointer holding an element.
Exceptions
std::invalid_argumentThe provided pointer is a nullptr.
Remarks
If pushBack throws the element is freed immediately.

◆ pushBack() [3/3]

template<typename T >
void pcpp::PointerVector< T >::pushBack ( T *  element,
bool  freeElementOnError = true 
)
inline

Add a new (pointer to an) element to the vector

Parameters
[in]elementA pointer to an element to assume ownership of.
[in]freeElementOnErrorIf set to true, the element is freed if an exception is thrown during the push.
Exceptions
std::invalid_argumentThe provided pointer is a nullptr.

◆ size()

template<typename T >
size_t pcpp::PointerVector< T >::size ( ) const
inline

Get number of elements in the vector

Returns
The number of elements in the vector