]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Vector.hpp
FGPUIDialog: fix reading from already free'd memory.
[flightgear.git] / src / FDM / YASim / Vector.hpp
1 #ifndef _VECTOR_HPP
2 #define _VECTOR_HPP
3
4 //
5 // Excruciatingly simple vector-of-pointers class.  Easy & useful.
6 // No support for addition of elements anywhere but at the end of the
7 // list, nor for removal of elements.  Does not delete (or interpret
8 // in any way) its contents.
9 //
10 class Vector {
11 public:
12     Vector();
13     ~Vector();
14     int   add(void* p);
15     void* get(int i);
16     void  set(int i, void* p);
17     int   size();
18     bool  empty();
19 private:
20     void realloc();
21
22     int _nelem;
23     int _sz;
24     void** _array;
25 };
26
27 inline Vector::Vector()
28 {
29     _nelem = 0;
30     _sz = 0;
31     _array = 0;
32 }
33
34 inline Vector::~Vector()
35 {
36     delete[] _array;
37 }
38
39 inline int Vector::add(void* p)
40 {
41     if(_nelem == _sz)
42         realloc();
43     _array[_sz] = p;
44     return _sz++;
45 }
46
47 inline void* Vector::get(int i)
48 {
49     return _array[i];
50 }
51
52 inline void Vector::set(int i, void* p)
53 {
54     _array[i] = p;
55 }
56
57 inline int Vector::size()
58 {
59     return _sz;
60 }
61
62 inline bool Vector::empty()
63 {
64     return _sz == 0;
65 }
66
67 inline void Vector::realloc()
68 {
69     _nelem = 2*_nelem + 1;
70     void** array = new void*[_nelem];
71     for(int i=0; i<_sz; i++)
72         array[i] = _array[i];
73     delete[] _array;
74     _array = array;
75 }
76
77 #endif // _VECTOR_HPP