00001 /*!@file Image/CheckedIterator.H A range-checked iterator */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/CheckedIterator.H $ 00035 // $Id: CheckedIterator.H 6352 2006-03-07 19:54:28Z rjpeters $ 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 //! A range-checked iterator class for memory debugging. 00056 /*! A CheckedIterator<T> object exposes essentially the same interface 00057 as a T*, but stores a start/stop range that should bound the 00058 iterator. This range is checked with a call to ck_range() every time 00059 the iterator is derefenced, by either operator*() or 00060 operator->(). */ 00061 00062 template<class T> 00063 class CheckedIterator 00064 { 00065 public: 00066 //! This typedef is used by STL functions 00067 typedef T value_type; 00068 00069 //! This typedef is used by STL functions 00070 typedef T* pointer; 00071 00072 //! This typedef is used by STL functions 00073 typedef T& reference; 00074 00075 //! This typedef is used by STL functions 00076 typedef ptrdiff_t difference_type; 00077 00078 //! This typedef is used by STL functions 00079 typedef std::random_access_iterator_tag iterator_category; 00080 00081 //! Default constructor 00082 /*! This builds an iterator with an empty valid range, so a new value 00083 must be assigned to the iterator before it can be 00084 dereferenced. However, providing a default constructor helps keep 00085 source compatibility with non-debug iterators (i.e., raw 00086 pointers). */ 00087 inline CheckedIterator(); 00088 //! Constructor 00089 inline CheckedIterator(T* p, T* a, T* b); 00090 //! Constructor 00091 template<class UU> inline CheckedIterator(const CheckedIterator<UU>& other); 00092 00093 /*! @name Pointer arithmetic operators */ 00094 //@{ 00095 00096 //! Pre-increment operator. 00097 inline CheckedIterator<T>& operator++(); 00098 00099 //! Post-increment operator. 00100 inline CheckedIterator<T> operator++(int); 00101 00102 //! Pre-decrement operator. 00103 inline CheckedIterator<T>& operator--(); 00104 00105 //! Post-decrement operator. 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 /*! @name Comparison operators */ 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 /*! @name Dereferencing operators */ 00137 //@{ 00138 00139 inline T* operator->() const; 00140 inline T& operator*() const; 00141 inline T& operator[](diff_t d) const; 00142 00143 //@} 00144 00145 // ##### data members: 00146 00147 //! Pointer to the data 00148 T* ptr; 00149 00150 //! Pointer to the first element we can iterate on 00151 T* start; 00152 00153 //! Pointer to the last element we can iterete on 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 /* So things look consistent in everyone's emacs... */ 00249 /* Local Variables: */ 00250 /* indent-tabs-mode: nil */ 00251 /* End: */