# resiter.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008
    00009 /***********************************************************************
    00010  Copyright (c) 1998 by Kevin Atkinson, (c) 1999, 2000 and 2001 by
    00011  MySQL AB, and (c) 2004, 2005 by Educational Technology Resources, Inc.
    00012  Others may also hold copyrights on code in this file.  See the CREDITS
    00013  file in the top directory of the distribution for details.
    00014
    00015  This file is part of MySQL++.
    00016
    00017  MySQL++ is free software; you can redistribute it and/or modify it
    00018  under the terms of the GNU Lesser General Public License as published
    00019  by the Free Software Foundation; either version 2.1 of the License, or
    00020  (at your option) any later version.
    00021
    00022  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
    00023  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00024  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
    00025  License for more details.
    00026
    00027  You should have received a copy of the GNU Lesser General Public
    00028  License along with MySQL++; if not, write to the Free Software
    00029  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
    00030  USA
    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 // \c size() defined for it.
    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 // Doxygen will not generate documentation for this section.
    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 } // end namespace mysqlpp
    00277
    00278 #endif