1 | ///////////////////////////////////////////////////////////////// |
---|
2 | // SafeVector.h |
---|
3 | // |
---|
4 | // STL vector with array bounds checking. To enable bounds |
---|
5 | // checking, #define ENABLE_CHECKS. |
---|
6 | ///////////////////////////////////////////////////////////////// |
---|
7 | |
---|
8 | #ifndef SAFEVECTOR_H |
---|
9 | #define SAFEVECTOR_H |
---|
10 | |
---|
11 | #include <cassert> |
---|
12 | #include <vector> |
---|
13 | |
---|
14 | ///////////////////////////////////////////////////////////////// |
---|
15 | // SafeVector |
---|
16 | // |
---|
17 | // Class derived from the STL std::vector for bounds checking. |
---|
18 | ///////////////////////////////////////////////////////////////// |
---|
19 | namespace MXSCARNA { |
---|
20 | template<class TYPE> |
---|
21 | class SafeVector : public std::vector<TYPE>{ |
---|
22 | public: |
---|
23 | |
---|
24 | // miscellaneous constructors |
---|
25 | SafeVector() : std::vector<TYPE>() {} |
---|
26 | SafeVector (size_t size) : std::vector<TYPE>(size) {} |
---|
27 | SafeVector (size_t size, const TYPE &value) : std::vector<TYPE>(size, value) {} |
---|
28 | SafeVector (const SafeVector &source) : std::vector<TYPE>(source) {} |
---|
29 | |
---|
30 | #ifdef ENABLE_CHECKS |
---|
31 | |
---|
32 | // [] array bounds checking |
---|
33 | TYPE &operator[](int index){ |
---|
34 | assert (index >= 0 && index < (int) size()); |
---|
35 | return std::vector<TYPE>::operator[] ((size_t) index); |
---|
36 | } |
---|
37 | |
---|
38 | // [] const array bounds checking |
---|
39 | const TYPE &operator[] (int index) const { |
---|
40 | assert (index >= 0 && index < (int) size()); |
---|
41 | return std::vector<TYPE>::operator[] ((size_t) index) ; |
---|
42 | } |
---|
43 | |
---|
44 | #endif |
---|
45 | |
---|
46 | }; |
---|
47 | |
---|
48 | // some commonly used vector types |
---|
49 | typedef SafeVector<int> VI; |
---|
50 | typedef SafeVector<VI> VVI; |
---|
51 | typedef SafeVector<VVI> VVVI; |
---|
52 | typedef SafeVector<float> VF; |
---|
53 | typedef SafeVector<VF> VVF; |
---|
54 | typedef SafeVector<VVF> VVVF; |
---|
55 | } |
---|
56 | #endif |
---|