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()));
49 template <
typename T,
typename Deleter = std::default_delete<T>>
class PointerVector
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);
125 m_Vector = std::move(other.m_Vector);
127 other.m_Vector.clear();
134 freeVectorUnsafe(m_Vector);
139 void pushBack(std::nullptr_t element,
bool freeElementOnError =
true) =
delete;
146 void pushBack(T* element,
bool freeElementOnError =
true)
148 if (element ==
nullptr)
150 throw std::invalid_argument(
"Element is nullptr");
155 m_Vector.push_back(element);
157 catch (
const std::exception&)
159 if (freeElementOnError)
175 throw std::invalid_argument(
"Element is nullptr");
181 m_Vector.push_back(element.get());
189 return m_Vector.begin();
196 return m_Vector.begin();
203 return m_Vector.end();
210 return m_Vector.end();
217 return m_Vector.size();
223 return m_Vector.front();
229 return m_Vector.front();
235 return m_Vector.back();
241 return m_Vector.back();
250 Deleter{}(*position);
251 return m_Vector.erase(position);
260 PCPP_DEPRECATED(
"Please use the memory safe 'getAndDetach' instead.")
263 T* result = *position;
264 position = m_Vector.erase(position);
282 std::unique_ptr<T> result(*position);
283 position = m_Vector.erase(position);
292 std::unique_ptr<T> result(*position);
293 m_Vector.erase(position);
302 return m_Vector.at(index);
308 const T*
at(
int index)
const
310 return m_Vector.at(index);
317 static std::vector<T*> deepCopyUnsafe(
const std::vector<T*>& origin)
319 std::vector<T*> copyVec;
321 copyVec.reserve(origin.size());
325 for (
const auto iter : origin)
329 copyVec.push_back(objCopy.release());
332 catch (
const std::exception&)
334 freeVectorUnsafe(copyVec);
345 static void freeVectorUnsafe(
const std::vector<T*>& origin)
347 for (
auto& obj : origin)
353 std::vector<T*> m_Vector;
Definition: PointerVector.h:50
std::unique_ptr< T > getAndDetach(const VectorIterator &position)
Definition: PointerVector.h:290
T * back()
Definition: PointerVector.h:233
void pushBack(std::nullptr_t element, bool freeElementOnError=true)=delete
Adding a nullptr to the vector is not allowed.
void clear()
Clears all elements of the vector while freeing them.
Definition: PointerVector.h:132
VectorIterator erase(VectorIterator position)
Definition: PointerVector.h:248
T * front()
Definition: PointerVector.h:221
ConstVectorIterator end() const
Definition: PointerVector.h:208
ConstVectorIterator begin() const
Definition: PointerVector.h:194
T const * back() const
Definition: PointerVector.h:239
const T * at(int index) const
Definition: PointerVector.h:308
T const * front() const
Definition: PointerVector.h:227
typename std::vector< T * >::iterator VectorIterator
Iterator object that is used for iterating all elements in the vector.
Definition: PointerVector.h:53
PointerVector()=default
A constructor that create an empty instance of this object.
PointerVector & operator=(const PointerVector &other)
Definition: PointerVector.h:86
std::unique_ptr< T > getAndDetach(VectorIterator &position)
Definition: PointerVector.h:280
size_t size() const
Definition: PointerVector.h:215
PointerVector(PointerVector &&other) noexcept
Definition: PointerVector.h:71
T * at(int index)
Definition: PointerVector.h:300
void pushBack(std::unique_ptr< T > element)
Definition: PointerVector.h:171
PointerVector & operator=(PointerVector &&other) noexcept
Definition: PointerVector.h:115
~PointerVector()
A destructor for this class. The destructor frees all elements that are binded to the vector.
Definition: PointerVector.h:77
VectorIterator begin()
Definition: PointerVector.h:187
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
PointerVector(const PointerVector &other)
Definition: PointerVector.h:66
std::unique_ptr< T > getAndDetach(size_t index)
Definition: PointerVector.h:271
VectorIterator end()
Definition: PointerVector.h:201
T * getAndRemoveFromVector(VectorIterator &position)
Definition: PointerVector.h:261
void pushBack(T *element, bool freeElementOnError=true)
Definition: PointerVector.h:146
The main namespace for the PcapPlusPlus lib.
A helper struct to facilitate the creation of a copy of an object.
Definition: PointerVector.h:25