Coverage Report

Created: 2024-02-25 06:29

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