24 template <
class T,
class Enable =
void>
struct Copier
26 std::unique_ptr<T> operator()(
const T& obj)
const
28 return std::unique_ptr<T>(
new T(obj));
34 template <
class T>
struct Copier<T, typename std::enable_if<std::is_polymorphic<T>::value>::type>
36 std::unique_ptr<T> operator()(
const T& obj)
const
39 return std::unique_ptr<T>(std::move(obj.clone()));
73 other.m_Vector.clear();
79 freeVectorUnsafe(m_Vector);
95 auto oldValues = m_Vector;
98 m_Vector = deepCopyUnsafe(other.m_Vector);
101 catch (
const std::exception&)
103 m_Vector = std::move(oldValues);
107 freeVectorUnsafe(oldValues);
120 m_Vector = std::move(other.m_Vector);
122 other.m_Vector.clear();
129 freeVectorUnsafe(m_Vector);
134 void pushBack(std::nullptr_t element,
bool freeElementOnError =
true) =
delete;
141 void pushBack(T* element,
bool freeElementOnError =
true)
143 if (element ==
nullptr)
145 throw std::invalid_argument(
"Element is nullptr");
150 m_Vector.push_back(element);
152 catch (
const std::exception&)
154 if (freeElementOnError)
170 throw std::invalid_argument(
"Element is nullptr");
176 m_Vector.push_back(element.get());
184 return m_Vector.begin();
191 return m_Vector.begin();
198 return m_Vector.end();
205 return m_Vector.end();
212 return m_Vector.size();
218 return m_Vector.front();
224 return m_Vector.front();
230 return m_Vector.back();
236 return m_Vector.back();
246 return m_Vector.erase(position);
255 PCPP_DEPRECATED(
"Please use the memory safe 'getAndDetach' instead.")
258 T* result = *position;
259 position = m_Vector.erase(position);
277 std::unique_ptr<T> result(*position);
278 position = m_Vector.erase(position);
287 std::unique_ptr<T> result(*position);
288 m_Vector.erase(position);
297 return m_Vector.at(index);
303 const T*
at(
int index)
const
305 return m_Vector.at(index);
312 static std::vector<T*> deepCopyUnsafe(
const std::vector<T*>& origin)
314 std::vector<T*> copyVec;
316 copyVec.reserve(origin.size());
320 for (
const auto iter : origin)
324 copyVec.push_back(objCopy.release());
327 catch (
const std::exception&)
329 for (
auto obj : copyVec)
343 static void freeVectorUnsafe(
const std::vector<T*>& origin)
345 for (
auto& obj : origin)
351 std::vector<T*> m_Vector;
Definition: PointerVector.h:50
PointerVector()=default
A constructor that create an empty instance of this object.
VectorIterator begin()
Definition: PointerVector.h:182
ConstVectorIterator end() const
Definition: PointerVector.h:203
typename std::vector< T * >::const_iterator ConstVectorIterator
Const iterator object that is used for iterating all elements in a constant vector.
Definition: PointerVector.h:56
std::unique_ptr< T > getAndDetach(size_t index)
Definition: PointerVector.h:266
T * getAndRemoveFromVector(VectorIterator &position)
Definition: PointerVector.h:256
void pushBack(std::unique_ptr< T > element)
Definition: PointerVector.h:166
VectorIterator erase(VectorIterator position)
Definition: PointerVector.h:243
PointerVector(const PointerVector &other)
Definition: PointerVector.h:66
std::unique_ptr< T > getAndDetach(const VectorIterator &position)
Definition: PointerVector.h:285
PointerVector & operator=(const PointerVector &other)
Definition: PointerVector.h:86
~PointerVector()
A destructor for this class. The destructor frees all elements that are binded to the vector.
Definition: PointerVector.h:77
T const * front() const
Definition: PointerVector.h:222
void pushBack(T *element, bool freeElementOnError=true)
Definition: PointerVector.h:141
T * back()
Definition: PointerVector.h:228
T * at(int index)
Definition: PointerVector.h:295
size_t size() const
Definition: PointerVector.h:210
const T * at(int index) const
Definition: PointerVector.h:303
T * front()
Definition: PointerVector.h:216
PointerVector(PointerVector &&other) noexcept
Definition: PointerVector.h:71
void pushBack(std::nullptr_t element, bool freeElementOnError=true)=delete
Adding a nullptr to the vector is not allowed.
T const * back() const
Definition: PointerVector.h:234
ConstVectorIterator begin() const
Definition: PointerVector.h:189
typename std::vector< T * >::iterator VectorIterator
Iterator object that is used for iterating all elements in the vector.
Definition: PointerVector.h:53
void clear()
Clears all elements of the vector while freeing them.
Definition: PointerVector.h:127
PointerVector & operator=(PointerVector &&other) noexcept
Definition: PointerVector.h:115
std::unique_ptr< T > getAndDetach(VectorIterator &position)
Definition: PointerVector.h:275
VectorIterator end()
Definition: PointerVector.h:196
The main namespace for the PcapPlusPlus lib.
A helper struct to facilitate the creation of a copy of an object.
Definition: PointerVector.h:25