CheckedIterator.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef CHECKEDITERATOR_H_DEFINED
00039 #define CHECKEDITERATOR_H_DEFINED
00040
00041 #include <assert.h>
00042 #include <stddef.h>
00043
00044 #include <iterator>
00045
00046 namespace CheckedIteratorAux
00047 {
00048 void ck_range_helper(const void* ptr, size_t offset,
00049 const void* start, const void* stop,
00050 size_t size);
00051 }
00052
00053 typedef unsigned long diff_t;
00054
00055
00056
00057
00058
00059
00060
00061
00062 template<class T>
00063 class CheckedIterator
00064 {
00065 public:
00066
00067 typedef T value_type;
00068
00069
00070 typedef T* pointer;
00071
00072
00073 typedef T& reference;
00074
00075
00076 typedef ptrdiff_t difference_type;
00077
00078
00079 typedef std::random_access_iterator_tag iterator_category;
00080
00081
00082
00083
00084
00085
00086
00087 inline CheckedIterator();
00088
00089 inline CheckedIterator(T* p, T* a, T* b);
00090
00091 template<class UU> inline CheckedIterator(const CheckedIterator<UU>& other);
00092
00093
00094
00095
00096
00097 inline CheckedIterator<T>& operator++();
00098
00099
00100 inline CheckedIterator<T> operator++(int);
00101
00102
00103 inline CheckedIterator<T>& operator--();
00104
00105
00106 inline CheckedIterator<T> operator--(int);
00107
00108 inline void operator+=(diff_t d);
00109 inline void operator-=(diff_t d);
00110
00111 inline CheckedIterator<T> operator+(diff_t d) const;
00112 inline CheckedIterator<T> operator-(diff_t d) const;
00113
00114 inline diff_t operator-(CheckedIterator<T> other) const;
00115
00116
00117
00118
00119
00120
00121 template <class U> inline bool operator==(const CheckedIterator<U>& other) const
00122 { return this->ptr == other.ptr; }
00123 template <class U> inline bool operator!=(const CheckedIterator<U>& other) const
00124 { return this->ptr != other.ptr; }
00125 template <class U> inline bool operator<(const CheckedIterator<U>& other) const
00126 { return this->ptr < other.ptr; }
00127 template <class U> inline bool operator>(const CheckedIterator<U>& other) const
00128 { return this->ptr > other.ptr; }
00129 template <class U> inline bool operator<=(const CheckedIterator<U>& other) const
00130 { return this->ptr <= other.ptr; }
00131 template <class U> inline bool operator>=(const CheckedIterator<U>& other) const
00132 { return this->ptr >= other.ptr; }
00133
00134
00135
00136
00137
00138
00139 inline T* operator->() const;
00140 inline T& operator*() const;
00141 inline T& operator[](diff_t d) const;
00142
00143
00144
00145
00146
00147
00148 T* ptr;
00149
00150
00151 T* start;
00152
00153
00154 T* stop;
00155 inline void ck_range(const diff_t offset) const;
00156 };
00157
00158
00159
00160
00161
00162 template<class T> inline
00163 CheckedIterator<T>::CheckedIterator() :
00164 ptr(0), start(0), stop(0)
00165 {}
00166
00167
00168 template<class T> inline
00169 CheckedIterator<T>::CheckedIterator(T* p, T* a, T* b) :
00170 ptr(p), start(a), stop(b)
00171 {}
00172
00173
00174 template<class T> template<class UU> inline
00175 CheckedIterator<T>::CheckedIterator(const CheckedIterator<UU>& other) :
00176 ptr(other.ptr), start(other.start), stop(other.stop)
00177 {}
00178
00179
00180 template<class T> inline
00181 CheckedIterator<T>& CheckedIterator<T>::operator++()
00182 { ++ptr; return *this; }
00183
00184
00185 template<class T> inline
00186 CheckedIterator<T> CheckedIterator<T>::operator++(int)
00187 { return CheckedIterator(ptr++, start, stop); }
00188
00189
00190 template<class T> inline
00191 CheckedIterator<T>& CheckedIterator<T>::operator--()
00192 { --ptr; return *this; }
00193
00194
00195 template<class T> inline
00196 CheckedIterator<T> CheckedIterator<T>::operator--(int)
00197 { return CheckedIterator(ptr--, start, stop); }
00198
00199
00200 template<class T> inline
00201 void CheckedIterator<T>::operator+=(diff_t d)
00202 { ptr += d; }
00203
00204
00205 template<class T> inline
00206 void CheckedIterator<T>::operator-=(diff_t d)
00207 { ptr -= d; }
00208
00209
00210 template<class T> inline
00211 CheckedIterator<T> CheckedIterator<T>::operator+(diff_t d) const
00212 { return CheckedIterator(ptr+d, start, stop); }
00213
00214
00215 template<class T> inline
00216 CheckedIterator<T> CheckedIterator<T>::operator-(diff_t d) const
00217 { return CheckedIterator(ptr-d, start, stop); }
00218
00219
00220 template<class T> inline
00221 diff_t CheckedIterator<T>::operator-(CheckedIterator<T> other) const
00222 { return ptr - other.ptr; }
00223
00224
00225 template<class T> inline
00226 T* CheckedIterator<T>::operator->() const
00227 { ck_range(0); return ptr; }
00228
00229
00230 template<class T> inline
00231 T& CheckedIterator<T>::operator*() const
00232 { ck_range(0); return *ptr; }
00233
00234
00235 template<class T> inline
00236 T& CheckedIterator<T>::operator[](diff_t d) const
00237 { ck_range(d); return ptr[d]; }
00238
00239
00240 template<class T> inline
00241 void CheckedIterator<T>::ck_range(const diff_t offset) const
00242 { CheckedIteratorAux::ck_range_helper(ptr, offset,
00243 start, stop, sizeof(T)); }
00244
00245 #endif
00246
00247
00248
00249
00250
00251