/src/PcapPlusPlus/Common++/header/PointerVector.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef PCAPPP_POINTER_VECTOR |
2 | | #define PCAPPP_POINTER_VECTOR |
3 | | |
4 | | #include <stdio.h> |
5 | | #include <stdint.h> |
6 | | #include <vector> |
7 | | |
8 | | /// @file |
9 | | |
10 | | /** |
11 | | * \namespace pcpp |
12 | | * \brief The main namespace for the PcapPlusPlus lib |
13 | | */ |
14 | | namespace pcpp |
15 | | { |
16 | | |
17 | | /** |
18 | | * @class PointerVector |
19 | | * A template class for representing a std::vector of pointers. Once (a pointer to) an element is added to this vector, |
20 | | * the element responsibility moves to the vector, meaning the PointerVector will free the object once it's removed from the vector |
21 | | * This class wraps std::vector and adds the capability of freeing objects once they're removed from it |
22 | | */ |
23 | | template<typename T> |
24 | | class PointerVector |
25 | | { |
26 | | public: |
27 | | /** |
28 | | * Iterator object that is used for iterating all elements in the vector |
29 | | */ |
30 | | typedef typename std::vector<T*>::iterator VectorIterator; |
31 | | |
32 | | /** |
33 | | * Const iterator object that is used for iterating all elements in a constant vector |
34 | | */ |
35 | | typedef typename std::vector<T*>::const_iterator ConstVectorIterator; |
36 | | |
37 | | /** |
38 | | * A constructor that create an empty instance of this object |
39 | | */ |
40 | | PointerVector() { } |
41 | | |
42 | | /** |
43 | | * A destructor for this class. The destructor frees all elements that are binded to the vector |
44 | | */ |
45 | | ~PointerVector() |
46 | | { |
47 | | for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++) |
48 | | { |
49 | | delete (*iter); |
50 | | } |
51 | | } |
52 | | |
53 | | /** |
54 | | * Copy constructor. Once a vector is copied from another vector, all elements inside it are copied, |
55 | | * meaning the new vector will contain pointers to copied elements, not pointers to the elements of the original vector |
56 | | */ |
57 | | PointerVector(const PointerVector& other) |
58 | | { |
59 | | for (ConstVectorIterator iter = other.begin(); iter != other.end(); iter++) |
60 | | { |
61 | | T* objCopy = new T(**iter); |
62 | | m_Vector.push_back(objCopy); |
63 | | } |
64 | | } |
65 | | |
66 | | /** |
67 | | * Clears all elements of the vector while freeing them |
68 | | */ |
69 | | void clear() |
70 | | { |
71 | | for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++) |
72 | | { |
73 | | delete (*iter); |
74 | | } |
75 | | |
76 | | m_Vector.clear(); |
77 | | } |
78 | | |
79 | | /** |
80 | | * Add a new (pointer to an) element to the vector |
81 | | */ |
82 | 0 | void pushBack(T* element) { m_Vector.push_back(element); } |
83 | | |
84 | | /** |
85 | | * Get the first element of the vector |
86 | | * @return An iterator object pointing to the first element of the vector |
87 | | */ |
88 | | VectorIterator begin() { return m_Vector.begin(); } |
89 | | |
90 | | /** |
91 | | * Get the first element of a constant vector |
92 | | * @return A const iterator object pointing to the first element of the vector |
93 | | */ |
94 | 0 | ConstVectorIterator begin() const { return m_Vector.begin(); } |
95 | | |
96 | | /** |
97 | | * Get the last element of the vector |
98 | | * @return An iterator object pointing to the last element of the vector |
99 | | */ |
100 | | VectorIterator end() { return m_Vector.end(); } |
101 | | |
102 | | /** |
103 | | * Get the last element of a constant vector |
104 | | * @return A const iterator object pointing to the last element of the vector |
105 | | */ |
106 | 0 | ConstVectorIterator end() const { return m_Vector.end(); } |
107 | | |
108 | | |
109 | | //inline size_t size() { return m_Vector.size(); } |
110 | | |
111 | | /** |
112 | | * Get number of elements in the vector |
113 | | * @return The number of elements in the vector |
114 | | */ |
115 | | size_t size() const { return m_Vector.size(); } |
116 | | |
117 | | /** |
118 | | * Returns a pointer of the first element in the vector |
119 | | * @return A pointer of the first element in the vector |
120 | | */ |
121 | | T* front() { return m_Vector.front(); } |
122 | | |
123 | | /** |
124 | | * Removes from the vector a single element (position). Once the element is erased, it's also freed |
125 | | * @param[in] position The position of the element to erase |
126 | | * @return An iterator pointing to the new location of the element that followed the last element erased by the function call |
127 | | */ |
128 | | VectorIterator erase(VectorIterator position) |
129 | | { |
130 | | delete (*position); |
131 | | return m_Vector.erase(position); |
132 | | } |
133 | | |
134 | | /** |
135 | | * Remove an element from the vector without freeing it |
136 | | * param[in] position The position of the element to remove from the vector |
137 | | * @return A pointer to the element which is no longer managed by the vector. It's user responsibility to free it |
138 | | */ |
139 | | T* getAndRemoveFromVector(VectorIterator& position) |
140 | | { |
141 | | T* result = (*position); |
142 | | VectorIterator tempPos = position; |
143 | | tempPos = m_Vector.erase(tempPos); |
144 | | position = tempPos; |
145 | | return result; |
146 | | } |
147 | | |
148 | | /** |
149 | | * Return a pointer to the element in a certain index |
150 | | * @param[in] index The index to retrieve the element from |
151 | | * @return The element at the specified position in the vector |
152 | | */ |
153 | | T* at(int index) |
154 | | { |
155 | | return m_Vector.at(index); |
156 | | } |
157 | | |
158 | | /** |
159 | | * Return a const pointer to the element in a certain index |
160 | | * @param[in] index The index to retrieve the element from |
161 | | * @return The element at the specified position in the vector |
162 | | */ |
163 | | const T* at(int index) const |
164 | | { |
165 | | return m_Vector.at(index); |
166 | | } |
167 | | |
168 | | private: |
169 | | std::vector<T*> m_Vector; |
170 | | }; |
171 | | |
172 | | } // namespace pcpp |
173 | | |
174 | | #endif /* PCAPPP_POINTER_VECTOR */ |