00001 #ifndef SCT_RANGEDVECTOR_H 00002 #define SCT_RANGEDVECTOR_H 00003 00004 #include <vector> 00005 #include <string> 00006 #include <sstream> 00007 #include "LogicErrors.h" 00008 #include "OutOfRangeError.h" 00009 00010 using std::vector; 00011 using std::string; 00012 00013 namespace Sct { 00014 00020 template <class T> 00021 class RangedVector : public vector<T> { 00022 public: 00024 RangedVector(string name) : name(name) {} 00025 00028 T& operator[] (const unsigned i) ; 00029 00032 const T& operator[] (const unsigned i) const ; 00033 00035 RangedVector<T>& operator=(const vector<T>& copyme); 00036 00037 protected: 00038 RangedVector() throw(); 00039 OutOfRangeError<int> createException(unsigned int i)const; 00040 string name; 00041 }; 00042 00043 template <class T> 00044 inline OutOfRangeError<int> RangedVector<T>::createException(unsigned int i) const { 00045 std::ostringstream text; 00046 text << name << " [] "; 00047 return OutOfRangeError<int>(text.str(), __FILE__, __LINE__, i,0,this->size()-1); 00048 } 00049 00050 template <class T> 00051 inline T& RangedVector<T>::operator[] (const unsigned i) { 00052 #ifndef NDEBUG 00053 if (i>this->size()) { 00054 throw createException(i); 00055 } 00056 #endif 00057 return vector<T>::operator[](i); 00058 } 00059 00060 template <class T> 00061 inline const T& RangedVector<T>::operator[] (const unsigned i) const { 00062 #ifndef NDEBUG 00063 if (i>this->size()) { 00064 throw createException(i); 00065 } 00066 #endif 00067 return vector<T>::operator[](i); 00068 } 00069 00070 template <class T> 00071 inline RangedVector<T>& RangedVector<T>::operator=(const vector<T>& copyme) { 00072 this->vector<T>::operator=(copyme); 00073 return *this; 00074 } 00075 00076 } 00077 #endif //SCT_RANGEDVECTOR_H