Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/tntdb/include/tntdb/bits/result_iterator.h

00001 /*
00002  * Copyright (C) 2005 Tommi Maekitalo
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 
00029 #ifndef TNTDB_BITS_RESULT_ITERATOR_H
00030 #define TNTDB_BITS_RESULT_ITERATOR_H
00031 
00032 #include <tntdb/bits/result.h>
00033 #include <tntdb/bits/row.h>
00034 #include <iostream>
00035 
00036 namespace tntdb
00037 {
00041   class Result::const_iterator
00042     : public std::iterator<std::random_access_iterator_tag, Row>
00043   {
00044     public:
00045       typedef const value_type& const_reference;
00046       typedef const value_type* const_pointer;
00047 
00048     private:
00049       Result result;
00050       Row current;
00051       size_type offset;
00052 
00053       void setOffset(size_type off)
00054       {
00055       if (off != offset)
00056       {
00057         offset = off;
00058         if (offset < result.size())
00059         current = result.getRow(offset);
00060       }
00061       }
00062 
00063     public:
00064       const_iterator(const Result& r, size_type off)
00065       : result(r),
00066         offset(off)
00067       {
00068         if (offset < r.size())
00069         current = r.getRow(offset);
00070       }
00071 
00075       bool operator== (const const_iterator& it) const
00076       { return offset == it.offset; }
00077 
00081       bool operator!= (const const_iterator& it) const
00082       { return !operator== (it); }
00083 
00085       const_iterator& operator++()
00086       { setOffset(offset + 1); return *this; }
00087 
00089       const_iterator operator++(int)
00090       {
00091         const_iterator ret = *this;
00092         setOffset(offset + 1);
00093         return ret;
00094       }
00095 
00097       const_iterator operator--()
00098       { setOffset(offset - 1); return *this; }
00099 
00101       const_iterator operator--(int)
00102       {
00103         const_iterator ret = *this;
00104         setOffset(offset - 1);
00105         return ret;
00106       }
00107 
00109       const_reference operator*() const
00110       { return current; }
00111 
00113       const_pointer operator->() const
00114       { return &current; }
00115 
00117       const_iterator& operator+= (difference_type n)
00118       {
00119         setOffset(offset + n);
00120         return *this;
00121       }
00122 
00124       const_iterator operator+ (difference_type n) const
00125       {
00126         const_iterator it(*this);
00127         it += n;
00128         return it;
00129       }
00130 
00132       const_iterator& operator-= (difference_type n)
00133       {
00134         setOffset(offset - n);
00135         return *this;
00136       }
00137 
00139       const_iterator operator- (difference_type n) const
00140       {
00141         const_iterator it(*this);
00142         it -= n;
00143         return it;
00144       }
00145 
00149       difference_type operator- (const const_iterator& it) const
00150       { return offset - it.offset; }
00151   };
00152 }
00153 
00154 #endif // TNTDB_BITS_RESULT_ITERATOR_H
00155 
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6