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()));
74 other.m_Vector.clear();
80 freeVectorUnsafe(m_Vector);
90 auto oldValues = m_Vector;
93 m_Vector = deepCopyUnsafe(other.m_Vector);
96 catch (
const std::exception&)
98 m_Vector = std::move(oldValues);
102 freeVectorUnsafe(oldValues);
115 m_Vector = std::move(other.m_Vector);
117 other.m_Vector.clear();
124 freeVectorUnsafe(m_Vector);
129 void pushBack(std::nullptr_t element,
bool freeElementOnError =
true) =
delete;
136 void pushBack(T* element,
bool freeElementOnError =
true)
138 if (element ==
nullptr)
140 throw std::invalid_argument(
"Element is nullptr");
145 m_Vector.push_back(element);
147 catch (
const std::exception&)
149 if (freeElementOnError)
165 throw std::invalid_argument(
"Element is nullptr");
171 m_Vector.push_back(element.get());
179 return m_Vector.begin();
186 return m_Vector.begin();
193 return m_Vector.end();
200 return m_Vector.end();
207 return m_Vector.size();
213 return m_Vector.front();
219 return m_Vector.front();
225 return m_Vector.back();
231 return m_Vector.back();
241 return m_Vector.erase(position);
250 PCPP_DEPRECATED(
"Please use the memory safe 'getAndDetach' instead.")
253 T* result = *position;
254 position = m_Vector.erase(position);
272 std::unique_ptr<T> result(*position);
273 position = m_Vector.erase(position);
282 std::unique_ptr<T> result(*position);
283 m_Vector.erase(position);
292 return m_Vector.at(index);
298 const T*
at(
int index)
const
300 return m_Vector.at(index);
307 static std::vector<T*> deepCopyUnsafe(std::vector<T*>
const& origin)
309 std::vector<T*> copyVec;
311 copyVec.reserve(origin.size());
315 for (
const auto iter : origin)
319 copyVec.push_back(objCopy.release());
322 catch (
const std::exception&)
324 for (
auto obj : copyVec)
338 static void freeVectorUnsafe(std::vector<T*>
const& origin)
340 for (
auto& obj : origin)
346 std::vector<T*> m_Vector;
Definition: PointerVector.h:50
VectorIterator begin()
Definition: PointerVector.h:177
PointerVector()
A constructor that create an empty instance of this object.
Definition: PointerVector.h:59
ConstVectorIterator end() const
Definition: PointerVector.h:198
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:261
T * getAndRemoveFromVector(VectorIterator &position)
Definition: PointerVector.h:251
void pushBack(std::unique_ptr< T > element)
Definition: PointerVector.h:161
VectorIterator erase(VectorIterator position)
Definition: PointerVector.h:238
PointerVector(const PointerVector &other)
Definition: PointerVector.h:67
PointerVector & operator=(const PointerVector &other)
Definition: PointerVector.h:87
std::unique_ptr< T > getAndDetach(VectorIterator const &position)
Definition: PointerVector.h:280
~PointerVector()
A destructor for this class. The destructor frees all elements that are binded to the vector.
Definition: PointerVector.h:78
T const * front() const
Definition: PointerVector.h:217
void pushBack(T *element, bool freeElementOnError=true)
Definition: PointerVector.h:136
T * back()
Definition: PointerVector.h:223
T * at(int index)
Definition: PointerVector.h:290
size_t size() const
Definition: PointerVector.h:205
const T * at(int index) const
Definition: PointerVector.h:298
T * front()
Definition: PointerVector.h:211
PointerVector(PointerVector &&other) noexcept
Definition: PointerVector.h:72
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:229
ConstVectorIterator begin() const
Definition: PointerVector.h:184
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:122
PointerVector & operator=(PointerVector &&other) noexcept
Definition: PointerVector.h:110
std::unique_ptr< T > getAndDetach(VectorIterator &position)
Definition: PointerVector.h:270
VectorIterator end()
Definition: PointerVector.h:191
The main namespace for the PcapPlusPlus lib.
A helper struct to facilitate the creation of a copy of an object.
Definition: PointerVector.h:25