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 #ifndef MYSQLPP_RESITER_H
00034 #define MYSQLPP_RESITER_H
00035
00036 #include "common.h"
00037
00038 #include <iterator>
00039
00040 namespace mysqlpp {
00041
00042 template <class OnType, class ReturnType, class SizeType,
00043 class DiffType> class subscript_iterator;
00044
00049
00050
00051 template <class OnType,
00052 class ValueType,
00053 class ReturnType = const ValueType&,
00054 class SizeType = unsigned int,
00055 class DiffType = int>
00056 class const_subscript_container
00057 {
00058 public:
00059 typedef const_subscript_container<OnType, ValueType, ReturnType,
00060 SizeType, DiffType> this_type;
00061 typedef subscript_iterator<const this_type, ReturnType, SizeType,
00062 DiffType> iterator;
00063 typedef iterator const_iterator;
00064 typedef const std::reverse_iterator<iterator>
00065 reverse_iterator;
00066 typedef const std::reverse_iterator<const_iterator>
00067 const_reverse_iterator;
00068
00069 typedef ValueType value_type;
00070 typedef value_type& reference;
00071 typedef value_type& const_reference;
00072 typedef value_type* pointer;
00073 typedef value_type* const_pointer;
00074
00075 typedef DiffType difference_type;
00076 typedef SizeType size_type;
00077
00079 virtual ~const_subscript_container() { }
00080
00082 virtual size_type size() const = 0;
00083
00085 virtual ReturnType at(SizeType i) const = 0;
00086
00089 size_type max_size() const { return size(); }
00090
00092 bool empty() const { return size() == 0; }
00093
00096 iterator begin() const { return iterator(this, 0); }
00097
00100 iterator end() const { return iterator(this, size()); }
00101
00104 reverse_iterator rbegin() const { return reverse_iterator(end()); }
00105
00108 reverse_iterator rend() const { return reverse_iterator(begin()); }
00109 };
00110
00111
00116
00117 template <class OnType, class ReturnType, class SizeType,
00118 class DiffType>
00119 class subscript_iterator : public std::iterator<ReturnType, SizeType>
00120 {
00121 public:
00123 subscript_iterator() { }
00124
00127 subscript_iterator(OnType* what, SizeType pos)
00128 {
00129 d_ = what;
00130 i_ = pos;
00131 }
00132
00135 bool operator ==(const subscript_iterator& j) const
00136 {
00137 return (d_ == j.d_ && i_ == j.i_);
00138 }
00139
00142 bool operator !=(const subscript_iterator& j) const
00143 {
00144 return (d_ == j.d_ && i_ != j.i_);
00145 }
00146
00150 bool operator <(const subscript_iterator& j) const
00151 {
00152 return (d_ == j.d_ && i_ < j.i_);
00153 }
00154
00158 bool operator >(const subscript_iterator & j) const
00159 {
00160 return (d_ == j.d_ && i_ > j.i_);
00161 }
00162
00166 bool operator <=(const subscript_iterator & j) const
00167 {
00168 return (d_ == j.d_ && i_ <= j.i_);
00169 }
00170
00174 bool operator >=(const subscript_iterator & j) const
00175 {
00176 return (d_ == j.d_ && i_ >= j.i_);
00177 }
00178
00181 ReturnType operator *() const { return d_->at(i_); }
00182
00185 ReturnType operator [](SizeType n) const { return d_->at(n); }
00186
00189 subscript_iterator& operator ++() { ++i_; return *this; }
00190
00193 subscript_iterator operator ++(int)
00194 {
00195 subscript_iterator tmp = *this;
00196 ++i_;
00197 return tmp;
00198 }
00199
00202 subscript_iterator& operator --()
00203 {
00204 --i_;
00205 return *this;
00206 }
00207
00210 subscript_iterator operator --(int)
00211 {
00212 subscript_iterator tmp = *this;
00213 --i_;
00214 return tmp;
00215 }
00216
00218 subscript_iterator& operator +=(SizeType n)
00219 {
00220 i_ += n;
00221 return *this;
00222 }
00223
00225 subscript_iterator operator +(SizeType n) const
00226 {
00227 subscript_iterator tmp = *this;
00228 tmp.i_ += n;
00229 return tmp;
00230 }
00231
00233 subscript_iterator& operator -=(SizeType n)
00234 {
00235 i_ -= n;
00236 return *this;
00237 }
00238
00240 subscript_iterator operator -(SizeType n) const
00241 {
00242 subscript_iterator tmp = *this;
00243 tmp.i_ -= n;
00244 return tmp;
00245 }
00246
00248 DiffType operator -(const subscript_iterator& j) const
00249 {
00250 if (d_ == j.d_) {
00251 return static_cast<SizeType>(i_) - j.i_;
00252 }
00253 return 0;
00254 }
00255
00256 private:
00257 SizeType i_;
00258 OnType* d_;
00259 };
00260
00261
00262 #if !defined(DOXYGEN_IGNORE)
00263
00264
00265 template <class OnType, class ReturnType, class SizeType,
00266 class DiffType>
00267 inline subscript_iterator<OnType, ReturnType, SizeType, DiffType>
00268 operator +(SizeType x,
00269 const subscript_iterator <OnType, ReturnType, SizeType, DiffType>& y)
00270 {
00271 return y + x;
00272 }
00273
00274 #endif // !defined(DOXYGEN_IGNORE)
00275
00276 }
00277
00278 #endif