///////////////////////////////////////////////////////////////// // SafeVector.h // // STL vector with array bounds checking. To enable bounds // checking, #define ENABLE_CHECKS. ///////////////////////////////////////////////////////////////// #ifndef SAFEVECTOR_H #define SAFEVECTOR_H #include #include using namespace std; ///////////////////////////////////////////////////////////////// // SafeVector // // Class derived from the STL std::vector for bounds checking. ///////////////////////////////////////////////////////////////// template class SafeVector: public std::vector { public: // miscellaneous constructors SafeVector() : std::vector() { } SafeVector(size_t size) : std::vector(size) { } SafeVector(size_t size, const TYPE &value) : std::vector(size, value) { } SafeVector(const SafeVector &source) : std::vector(source) { } #ifdef ENABLE_CHECKS // [] array bounds checking TYPE &operator[](int index) { assert (index >= 0 && index < (int) size()); return std::vector::operator[] ((size_t) index); } // [] const array bounds checking const TYPE &operator[] (int index) const { assert (index >= 0 && index < (int) size()); return std::vector::operator[] ((size_t) index); } #endif }; // some commonly used vector types typedef SafeVector VI; typedef SafeVector VVI; typedef SafeVector VVVI; typedef SafeVector VF; typedef SafeVector VVF; typedef SafeVector VVVF; #endif