Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

Iterator.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (c) 2003-2005 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 
00023 #ifndef _DWFCORE_ITERATOR_H
00024 #define _DWFCORE_ITERATOR_H
00025 
00026 
00031 
00032 
00033 #include "dwfcore/Core.h"
00034 #include "dwfcore/STL.h"
00035 #include "dwfcore/String.h"
00036 #include "dwfcore/Exception.h"
00037 
00038 
00039 namespace DWFCore
00040 {
00041 
00042 
00071 template<class T>
00072 class DWFIterator
00073 {
00074 
00075 public:
00076 
00082     virtual ~DWFIterator()
00083         throw()
00084     {;}
00085 
00091     virtual void reset()
00092         throw() = 0;
00093 
00101     virtual bool valid()
00102         throw() = 0;
00103 
00111     virtual bool next()
00112         throw() = 0;
00113 
00120     virtual T& get()
00121         throw( DWFException ) = 0;
00122 
00123 protected:
00124 
00130     DWFIterator()
00131         throw()
00132     {;}
00133 };
00134 
00135 
00136 
00145 class DWFStringVectorIterator : public DWFCoreMemory
00146                               , public DWFIterator<DWFString>
00147 {
00148 
00149 public:
00150 
00157     DWFStringVectorIterator( vector<DWFString>& rVector )
00158         throw()
00159         : _rVector( rVector )
00160         , _iCurrent( 0 )
00161     {;}
00162 
00171     DWFStringVectorIterator( const DWFStringVectorIterator& i )
00172         throw()
00173         : _rVector( i._rVector )
00174         , _iCurrent( i._iCurrent )
00175     {;}
00176 
00185     DWFStringVectorIterator& operator=( const DWFStringVectorIterator& i )
00186         throw()
00187     {
00188         _iCurrent = i._iCurrent;
00189 
00190         size_t n = 0;
00191         for (; n < i._rVector.size(); n++)
00192         {
00193             _rVector.push_back( i._rVector[n] );
00194         }
00195 
00196         return *this;
00197     }
00198 
00204     virtual ~DWFStringVectorIterator()
00205         throw()
00206     {;}
00207 
00211     void reset()
00212         throw()
00213     {
00214         _iCurrent = 0;
00215     }
00216 
00220     bool valid()
00221         throw()
00222     {
00223         return (_iCurrent < _rVector.size());
00224     }
00225 
00229     bool next()
00230         throw()
00231     {
00232         if (valid())
00233         {
00234             _iCurrent++;
00235             return valid();
00236         }
00237         else
00238         {
00239             return false;
00240         }
00241     }
00242 
00246     DWFString& get()
00247         throw( DWFException )
00248     {
00249         if (valid())
00250         {
00251             return _rVector[_iCurrent];
00252         }
00253         else
00254         {
00255             _DWFCORE_THROW( DWFDoesNotExistException, L"No more elements" );
00256         }
00257     }
00258 
00259 private:
00260 
00261     vector<DWFString>& _rVector;
00262     size_t  _iCurrent;
00263 };
00264 
00284 template<class T>
00285 class DWFBasicIteratorImpl : public DWFCoreMemory
00286                            , public DWFIterator<T>
00287 {
00288 
00289 public:
00290 
00299     DWFBasicIteratorImpl( uint16_t nHint = 16 )
00300         throw()
00301         : _ppT( NULL )
00302         , _nCount( 0 )
00303         , _nAlloc( 0 )
00304         , _nHint( nHint )
00305         , _iCurrent( -1 )
00306     {;}
00307 
00313     virtual ~DWFBasicIteratorImpl()
00314         throw()
00315     {
00316         if (_ppT)
00317         {
00318             DWFCORE_FREE_MEMORY( _ppT );
00319         }
00320     }
00321 
00325     virtual void reset()
00326         throw()
00327     {
00328         _iCurrent = (_nCount > 0) ? 0 : -1;
00329     }
00330 
00334     virtual bool valid()
00335         throw()
00336     {
00337         return (_iCurrent != -1);
00338     }
00339 
00343     virtual bool next()
00344         throw()
00345     {
00346         if (_iCurrent != -1)
00347         {
00348                 //
00349                 // cast ok - _iCurrent never less than -1
00350                 //
00351             if ((size_t)(++_iCurrent) == _nCount)
00352             {
00353                 _iCurrent = -1;
00354             }
00355         }
00356 
00357         return (_iCurrent != -1);
00358     }
00359 
00363     virtual T& get()
00364         throw( DWFException )
00365     {
00366         if (_iCurrent != -1)
00367         {
00368             return *(_ppT[_iCurrent]);
00369         }
00370         else
00371         {
00372             _DWFCORE_THROW( DWFDoesNotExistException, L"No element" );
00373         }
00374     }
00375 
00384     virtual void add( T& rT )
00385         throw( DWFException )
00386     {
00387         if ((_nAlloc - _nCount) == 0)
00388         {
00389             _nAlloc = max(2*_nAlloc, _nAlloc + _nHint);
00390             T** ppNew = (T**) DWFCORE_ALLOC_MEMORY( T*, _nAlloc );
00391             if (ppNew == NULL)
00392             {
00393                 _DWFCORE_THROW( DWFMemoryException, L"Failed to allocate buffer" );
00394             }
00395 
00396             DWFCORE_COPY_MEMORY( ppNew, _ppT, _nCount*sizeof(T*) );
00397             DWFCORE_FREE_MEMORY( _ppT );
00398 
00399             _ppT = ppNew;
00400         }
00401 
00402         _ppT[_nCount++] = &rT;
00403 
00404         if (_iCurrent == -1)
00405         {
00406             _iCurrent = 0;
00407         }
00408     }
00409 
00410 private:
00411 
00412     T**         _ppT;
00413     size_t      _nCount;
00414     size_t      _nAlloc;
00415     uint16_t    _nHint;
00416 
00417     off_t       _iCurrent;
00418 };
00419 
00420 
00421 
00431 template<class K, class V>
00432 class DWFKVIterator
00433 {
00434 
00435 public:
00436 
00442     virtual ~DWFKVIterator()
00443         throw()
00444     {;}
00445 
00449     virtual void reset()
00450         throw() = 0;
00451 
00455     virtual bool valid()
00456         throw() = 0;
00457 
00461     virtual bool next()
00462         throw() = 0;
00463 
00470     virtual K& key()
00471         throw( DWFException ) = 0;
00472 
00479     virtual V& value()
00480         throw( DWFException ) = 0;
00481 
00482 protected:
00483 
00489     DWFKVIterator()
00490         throw()
00491     {;}
00492 };
00493 
00494 }
00495 
00496 
00497 #endif

Generated on Tue May 17 12:05:10 2005 for Autodesk DWF Core Library by  doxygen 1.4.1