Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ChunkIterator.h

00001 #ifndef _STR_ChunkIterator_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 2000-2002 by Eric M. Hopper <hopper@omnifarious.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/ChunkIterator.h,v 1.9 2002/11/25 05:40:05 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #include <StrMod/StrChunk.h>
00030 #include <LCore/GenTypes.h>
00031 #include <iterator>
00032 
00033 #define _STR_ChunkIterator_H_
00034 
00035 namespace strmod {
00036 namespace strmod {
00037 
00038 /** \class StrChunk::__iterator ChunkIterator.h StrMod/ChunkIterator.h
00039  * The const_iterator class for StrChunk.
00040  */
00041 class StrChunk::__iterator
00042 {
00043  private:
00044    class shared;
00045    friend class shared;
00046    typedef lcore::U1Byte U1Byte;
00047 
00048  public:
00049    typedef ::std::bidirectional_iterator_tag iterator_category;
00050    typedef U1Byte value_type;
00051    typedef int difference_type;
00052    typedef const U1Byte *pointer;
00053    typedef const U1Byte &reference;
00054 
00055    //! Creates an iterator that's always at the end()
00056    __iterator();
00057    //! Creates an interator pointat the beginning of a StrChunk.
00058    __iterator(const StrChunkPtr &chnk);
00059    //! Creates an iterator using the shared data already generated.
00060    __iterator(shared *sh);
00061    //! Copies one iterator to another.  Manages reference count on shared area.
00062    __iterator(const __iterator &other);
00063    //! Destroys an iterator, possibly destroying shared data too if refcount is only 1
00064    ~__iterator();
00065 
00066    //! Am I an iterator over this chunk?
00067    bool isFor(const StrChunkPtr &chnk) const;
00068 
00069    //! Am I equal to other?  Do I iterator over the same StrChunk and am I at
00070    //! the same spot?
00071    bool isEqual(const __iterator &other) const;
00072    //! Am I less than other?
00073    bool isLessThan(const __iterator &other) const;
00074 
00075    //! How many characters are there between me and another iterator?
00076    difference_type distance(const __iterator &other) const;
00077 
00078    //! Make me into a copy of another iterator.
00079    const __iterator &operator =(const __iterator &other);
00080 
00081    //! Using this operator will almost certainly result in a compiler error.
00082    inline pointer operator->() const;
00083    //! What character am I looking at?
00084    inline reference operator *() const;
00085 
00086    //! Move forward one.  Prefix operator, so it's more efficient.
00087    inline const __iterator &operator ++();
00088    //! Move forward one.  Postfix operator, so it's less efficient.
00089    inline const __iterator operator ++(int);
00090    //! Move to one past the last character.
00091    void moveToEnd();
00092    //! Move backward one.  Prefix operator, so it's more efficient.
00093    inline const __iterator &operator --();
00094    //! Move backward one.  Postfix operator, so it's less efficient.
00095    inline const __iterator operator --(int);
00096    //! Move to the first character (which may be one past the last character
00097    //! if the StrChunk is empty.
00098    void moveToBegin();
00099 
00100  protected:
00101    //! Set the secret storage compartment in a StrChunk to val.
00102    inline static void setStorage(StrChunk &chnk, void *val);
00103    //! Get what's in the secret storage compartment inside a StrChunk.
00104    inline static void *getStorage(StrChunk &chnk);
00105 
00106  private:
00107    class ExtVisitor;
00108    friend class ExtVisitor;
00109    shared *shared_;
00110    unsigned int abspos_;
00111    unsigned int extpos_;
00112    unsigned int extlast_;  // Index of last element (not length) of extbase_.
00113    unsigned int curext_;
00114    const unsigned char *extbase_;
00115 
00116    inline void move_forward();
00117    void move_forward_complex();
00118    inline void move_backward();
00119    void move_backward_complex();
00120 };
00121 
00122 //-----------------------------inline functions--------------------------------
00123 
00124 inline StrChunk::__iterator::pointer
00125 StrChunk::__iterator::operator->() const
00126 {
00127    return(&(extbase_[extpos_]));
00128 }
00129 
00130 inline StrChunk::__iterator::reference
00131 StrChunk::__iterator::operator *() const
00132 {
00133    return(extbase_[extpos_]);
00134 }
00135 
00136 inline const StrChunk::__iterator &StrChunk::__iterator::operator ++()
00137 {
00138    move_forward();
00139    return(*this);
00140 }
00141 
00142 inline const StrChunk::__iterator StrChunk::__iterator::operator ++(int)
00143 {
00144    __iterator tmp(*this);
00145    move_forward();
00146    return(tmp);
00147 }
00148 
00149 inline const StrChunk::__iterator &StrChunk::__iterator::operator --()
00150 {
00151    move_backward();
00152    return(*this);
00153 }
00154 
00155 inline const StrChunk::__iterator StrChunk::__iterator::operator --(int)
00156 {
00157    __iterator tmp(*this);
00158    move_backward();
00159    return(tmp);
00160 }
00161 
00162 inline void StrChunk::__iterator::move_forward()
00163 {
00164    if (extpos_ < extlast_)
00165    {
00166       ++extpos_;
00167       ++abspos_;
00168    }
00169    else
00170    {
00171       move_forward_complex();
00172    }
00173 }
00174 
00175 inline void StrChunk::__iterator::move_backward()
00176 {
00177    if (extpos_ > 0)
00178    {
00179       --extpos_;
00180       --abspos_;
00181    }
00182    else
00183    {
00184       move_backward_complex();
00185    }
00186 }
00187 
00188 inline void StrChunk::__iterator::setStorage(StrChunk &chnk, void *val)
00189 {
00190    chnk.iter_storage = val;
00191 }
00192 
00193 inline void *StrChunk::__iterator::getStorage(StrChunk &chnk)
00194 {
00195    return(chnk.iter_storage);
00196 }
00197 
00198 //--
00199 
00200 inline bool
00201 operator ==(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00202 {
00203    return(a.isEqual(b));
00204 }
00205 
00206 inline bool
00207 operator !=(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00208 {
00209    return(! a.isEqual(b));
00210 }
00211 
00212 inline bool
00213 operator <(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00214 {
00215    return(a.isLessThan(b));
00216 }
00217 
00218 inline bool
00219 operator >(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00220 {
00221    return(b.isLessThan(a));
00222 }
00223 
00224 inline bool
00225 operator <=(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00226 {
00227    return(! b.isLessThan(a));
00228 }
00229 
00230 inline bool
00231 operator >=(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00232 {
00233    return(! a.isLessThan(b));
00234 }
00235 
00236 inline StrChunk::__iterator::difference_type
00237 operator -(const StrChunk::__iterator &a, const StrChunk::__iterator &b)
00238 {
00239    return(a.distance(b));
00240 }
00241 
00242 };  // namespace strmod
00243 };  // namespace strmod
00244 
00245 #endif

Generated on Wed Jan 29 00:32:43 2003 for libNet by doxygen1.3-rc1