00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : Iterators 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef IteratorsHPP 00033 #define IteratorsHPP 00034 00035 #include <zeusbase/System/Interfaces/IListIterator.hpp> 00036 #include <zeusbase/System/Interfaces/IMapIterator.hpp> 00037 #include <zeusbase/System/AutoPtr.hpp> 00038 #include <assert.h> 00039 00040 BEGIN_NAMESPACE_Zeus 00041 00042 /****************************************************************************/ 00048 /****************************************************************************/ 00049 template <class T> class TAbstractIterator : public TAutoPtrBase<IListIterator<T> >, private IListIterator<T> 00050 { 00051 public: 00052 00053 /***********************************************************************/ 00056 /***********************************************************************/ 00057 inline virtual void MQUALIFIER reset() const 00058 { 00059 if (this->m_pInterface != NULL) 00060 { 00061 this->m_pInterface->reset(); 00062 } 00063 } 00064 00065 /***********************************************************************/ 00068 /***********************************************************************/ 00069 inline virtual const T& MQUALIFIER getNextItemConst() const 00070 { 00071 assert(this->m_pInterface != NULL); 00072 return this->m_pInterface->getNextItemConst(); 00073 } 00074 00075 /***********************************************************************/ 00078 /***********************************************************************/ 00079 inline virtual bool MQUALIFIER hasNextItem() const 00080 { 00081 bool bRetval = false; 00082 if (this->m_pInterface != NULL) 00083 { 00084 bRetval = this->m_pInterface->hasNextItem(); 00085 } 00086 return bRetval; 00087 } 00088 00089 protected: 00090 /***********************************************************************/ 00093 /***********************************************************************/ 00094 inline TAbstractIterator() 00095 : TAutoPtrBase<IListIterator<T> >() 00096 {} 00097 00098 /***********************************************************************/ 00104 /***********************************************************************/ 00105 inline TAbstractIterator(const IListIterator<T>* pIt, bool bAllocPointer = false) 00106 : TAutoPtrBase<IListIterator<T> >(pIt, bAllocPointer) 00107 {} 00108 00109 /***********************************************************************/ 00115 /***********************************************************************/ 00116 inline TAbstractIterator(IListIterator<T>* pIt, bool bAllocPointer = false) 00117 : TAutoPtrBase<IListIterator<T> >(pIt, bAllocPointer) 00118 {} 00119 00120 /***********************************************************************/ 00123 /***********************************************************************/ 00124 inline virtual T& MQUALIFIER getNextItem() const = 0; 00125 00126 private: 00127 /***********************************************************************/ 00130 /***********************************************************************/ 00131 inline virtual Retval MQUALIFIER askForInterface(const InterfaceID& /*rInterfaceID*/, IZUnknown*& /*rpIface*/) 00132 { 00133 return RET_METHOD_NOT_IMPL; 00134 } 00135 00136 /***********************************************************************/ 00139 /***********************************************************************/ 00140 inline virtual void MQUALIFIER addRef() const {} 00141 00142 /***********************************************************************/ 00145 /***********************************************************************/ 00146 inline virtual void MQUALIFIER release() const 00147 {} 00148 00150 TAbstractIterator(const TAbstractIterator<T>& rIt); 00152 TAbstractIterator<T>& operator=(const TAbstractIterator<T>& rIt); 00153 }; 00154 00155 //############################################################################ 00156 // LIST ITERATORS 00157 /****************************************************************************/ 00160 /****************************************************************************/ 00161 template <class T> class TIterator : public TAbstractIterator<T> 00162 { 00163 public: 00164 /***********************************************************************/ 00167 /***********************************************************************/ 00168 inline TIterator() 00169 : TAbstractIterator<T>() 00170 {} 00171 00172 /***********************************************************************/ 00178 /***********************************************************************/ 00179 inline TIterator(IListIterator<T>* pIt, bool bAllocPointer = false) 00180 : TAbstractIterator<T>(pIt, bAllocPointer) 00181 {} 00182 00183 /***********************************************************************/ 00186 /***********************************************************************/ 00187 inline TIterator(const TIterator<T>& rIt) 00188 : TAbstractIterator<T>(rIt.m_pInterface, true) 00189 {} 00190 00191 /***********************************************************************/ 00195 /***********************************************************************/ 00196 inline TIterator<T>& operator=(const TIterator<T>& rIt) 00197 { 00198 assign(rIt.m_pInterface); 00199 return *this; 00200 } 00201 00202 /***********************************************************************/ 00206 /***********************************************************************/ 00207 inline TIterator<T>& operator=(IListIterator<T>* pIt) 00208 { 00209 attach(pIt); 00210 return *this; 00211 } 00212 00213 /***********************************************************************/ 00217 /***********************************************************************/ 00218 inline bool operator==(const TIterator<T>& rWrapper) const 00219 { 00220 return equals(rWrapper.m_pInterface); 00221 } 00222 00223 /***********************************************************************/ 00227 /***********************************************************************/ 00228 inline bool operator==(const IListIterator<T>* pIt) const 00229 { 00230 return equals(pIt); 00231 } 00232 00233 /***********************************************************************/ 00237 /***********************************************************************/ 00238 inline bool operator!=(const TIterator<T>& rWrapper) const 00239 { 00240 return !equals(rWrapper.m_pInterface); 00241 } 00242 00243 /***********************************************************************/ 00247 /***********************************************************************/ 00248 inline bool operator!=(const IListIterator<T>* pIt) const 00249 { 00250 return !equals(pIt); 00251 } 00252 00253 /***********************************************************************/ 00256 /***********************************************************************/ 00257 inline virtual T& MQUALIFIER getNextItem() const 00258 { 00259 assert(this->m_pInterface != NULL); 00260 return this->m_pInterface->getNextItem(); 00261 } 00262 00263 private: 00264 }; 00265 00266 /****************************************************************************/ 00269 /****************************************************************************/ 00270 template <class T> class TConstIterator : public TAbstractIterator<T> 00271 { 00272 public: 00273 /***********************************************************************/ 00276 /***********************************************************************/ 00277 inline TConstIterator() 00278 : TAbstractIterator<T>() 00279 {} 00280 00281 /***********************************************************************/ 00287 /***********************************************************************/ 00288 inline TConstIterator(const IListIterator<T>* pIt, bool bAllocPointer = false) 00289 : TAbstractIterator<T>(pIt, bAllocPointer) 00290 {} 00291 00292 /***********************************************************************/ 00295 /***********************************************************************/ 00296 inline TConstIterator(const TConstIterator<T>& rIt) 00297 : TAbstractIterator<T>(rIt.m_pInterface, true) 00298 {} 00299 00300 /***********************************************************************/ 00304 /***********************************************************************/ 00305 inline TConstIterator<T>& operator=(const TConstIterator<T>& rIt) 00306 { 00307 assign(rIt.m_pInterface); 00308 return *this; 00309 } 00310 00311 /***********************************************************************/ 00315 /***********************************************************************/ 00316 inline TConstIterator<T>& operator=(const IListIterator<T>* pIt) 00317 { 00318 attach(pIt); 00319 return *this; 00320 } 00321 00322 /***********************************************************************/ 00326 /***********************************************************************/ 00327 inline bool operator==(const TConstIterator<T>& rWrapper) const 00328 { 00329 return equals(rWrapper.m_pInterface); 00330 } 00331 00332 /***********************************************************************/ 00336 /***********************************************************************/ 00337 inline bool operator==(const IListIterator<T>* pIt) const 00338 { 00339 return equals(pIt); 00340 } 00341 00342 /***********************************************************************/ 00346 /***********************************************************************/ 00347 inline bool operator!=(const TConstIterator<T>& rWrapper) const 00348 { 00349 return !equals(rWrapper.m_pInterface); 00350 } 00351 00352 /***********************************************************************/ 00356 /***********************************************************************/ 00357 inline bool operator!=(const IListIterator<T>* pIt) const 00358 { 00359 return !equals(pIt); 00360 } 00361 00362 private: 00363 /***********************************************************************/ 00366 /***********************************************************************/ 00367 inline virtual T& MQUALIFIER getNextItem() const 00368 { 00369 assert(this->m_pInterface != NULL); 00370 return this->m_pInterface->getNextItem(); 00371 } 00372 }; 00373 00374 //############################################################################ 00375 // MAP ITERATORS 00376 /****************************************************************************/ 00379 /****************************************************************************/ 00380 template <class TKeyType, class T> class TAbstractMapIterator 00381 : public TAutoPtrBase<IMapIterator<TKeyType, T> >, 00382 private IMapIterator<TKeyType, T> 00383 { 00384 public: 00385 /***********************************************************************/ 00388 /***********************************************************************/ 00389 inline virtual void MQUALIFIER reset() const 00390 { 00391 if (this->m_pInterface != NULL) 00392 { 00393 this->m_pInterface->reset(); 00394 } 00395 } 00396 00397 /***********************************************************************/ 00400 /***********************************************************************/ 00401 inline virtual const T& MQUALIFIER getNextItemConst() const 00402 { 00403 assert(this->m_pInterface != NULL); 00404 return this->m_pInterface->getNextItemConst(); 00405 } 00406 00407 /***********************************************************************/ 00410 /***********************************************************************/ 00411 inline virtual bool MQUALIFIER hasNextItem() const 00412 { 00413 bool bRetval = false; 00414 if (this->m_pInterface != NULL) 00415 { 00416 bRetval = this->m_pInterface->hasNextItem(); 00417 } 00418 return bRetval; 00419 } 00420 00421 /*************************************************************************/ 00424 /*************************************************************************/ 00425 inline virtual Retval MQUALIFIER getNextKeyConst(TKeyType& rKey) const 00426 { 00427 assert(this->m_pInterface != NULL); 00428 return this->m_pInterface->getNextKeyConst(rKey); 00429 } 00430 00431 /*************************************************************************/ 00434 /*************************************************************************/ 00435 inline virtual const T& MQUALIFIER getNextItemWithKeyConst(TKeyType& rKey) const 00436 { 00437 assert(this->m_pInterface != NULL); 00438 return this->m_pInterface->getNextItemWithKeyConst(rKey); 00439 } 00440 00441 protected: 00442 /***********************************************************************/ 00445 /***********************************************************************/ 00446 inline TAbstractMapIterator() 00447 : TAutoPtrBase<IMapIterator<TKeyType, T> >() 00448 {} 00449 00450 /***********************************************************************/ 00456 /***********************************************************************/ 00457 inline TAbstractMapIterator(const IMapIterator<TKeyType, T>* pIt, bool bAllocPointer = false) 00458 : TAutoPtrBase<IMapIterator<TKeyType, T> >(pIt, bAllocPointer) 00459 {} 00460 00461 /***********************************************************************/ 00467 /***********************************************************************/ 00468 inline TAbstractMapIterator(IMapIterator<TKeyType, T>* pIt, bool bAllocPointer = false) 00469 : TAutoPtrBase<IMapIterator<TKeyType, T> >(pIt, bAllocPointer) 00470 {} 00471 00472 virtual T& MQUALIFIER getNextItem() const = 0; 00473 virtual Retval MQUALIFIER getNextKey(TKeyType& rKey) =0; 00474 virtual T& MQUALIFIER getNextItemWithKey(TKeyType& rKey) const = 0; 00475 00476 00477 private: 00478 /***********************************************************************/ 00481 /***********************************************************************/ 00482 inline virtual Retval MQUALIFIER askForInterface(const InterfaceID& /*rInterfaceID*/, IZUnknown*& /*rpIface*/) 00483 { 00484 return RET_METHOD_NOT_IMPL; 00485 } 00486 00487 /***********************************************************************/ 00490 /***********************************************************************/ 00491 inline virtual void MQUALIFIER addRef() const {} 00492 00493 /***********************************************************************/ 00496 /***********************************************************************/ 00497 inline virtual void MQUALIFIER release() const 00498 {} 00499 00501 TAbstractMapIterator(const TAbstractMapIterator<TKeyType, T>& rIt); 00503 TAbstractMapIterator<TKeyType, T>& operator=(const TAbstractMapIterator<TKeyType, T>& rIt); 00504 }; 00505 00506 00507 /****************************************************************************/ 00510 /****************************************************************************/ 00511 template <class TKeyType, class T> class TMapIterator : public TAbstractMapIterator<TKeyType, T> 00512 { 00513 public: 00514 /***********************************************************************/ 00517 /***********************************************************************/ 00518 inline TMapIterator() 00519 : TAbstractMapIterator<TKeyType, T>() 00520 {} 00521 00522 /***********************************************************************/ 00528 /***********************************************************************/ 00529 inline TMapIterator(IMapIterator<TKeyType, T>* pIt, bool bAllocPointer = false) 00530 : TAbstractMapIterator<TKeyType, T>(pIt, bAllocPointer) 00531 {} 00532 00533 /***********************************************************************/ 00536 /***********************************************************************/ 00537 inline TMapIterator(const TMapIterator<TKeyType, T>& rIt) 00538 : TAbstractMapIterator<TKeyType, T>(rIt.m_pInterface, true) 00539 {} 00540 00541 /***********************************************************************/ 00545 /***********************************************************************/ 00546 inline TMapIterator<TKeyType, T>& operator=(const TMapIterator<TKeyType, T>& rIt) 00547 { 00548 assign(rIt.m_pInterface); 00549 return *this; 00550 } 00551 00552 /***********************************************************************/ 00556 /***********************************************************************/ 00557 inline TMapIterator<TKeyType, T>& operator=(IMapIterator<TKeyType, T>* pIt) 00558 { 00559 attach(pIt); 00560 return *this; 00561 } 00562 00563 /***********************************************************************/ 00567 /***********************************************************************/ 00568 inline bool operator==(const TMapIterator<TKeyType, T>& rWrapper) const 00569 { 00570 return equals(rWrapper.m_pInterface); 00571 } 00572 00573 /***********************************************************************/ 00577 /***********************************************************************/ 00578 inline bool operator==(const IMapIterator<TKeyType, T>* pIt) const 00579 { 00580 return equals(pIt); 00581 } 00582 00583 /***********************************************************************/ 00587 /***********************************************************************/ 00588 inline bool operator!=(const TMapIterator<TKeyType, T>& rWrapper) const 00589 { 00590 return !equals(rWrapper.m_pInterface); 00591 } 00592 00593 /***********************************************************************/ 00597 /***********************************************************************/ 00598 inline bool operator!=(const IMapIterator<TKeyType, T>* pIt) const 00599 { 00600 return !equals(pIt); 00601 } 00602 00603 /***********************************************************************/ 00606 /***********************************************************************/ 00607 inline virtual T& MQUALIFIER getNextItem() const 00608 { 00609 assert(this->m_pInterface != NULL); 00610 return this->m_pInterface->getNextItem(); 00611 } 00612 00613 /***********************************************************************/ 00616 /***********************************************************************/ 00617 inline virtual Retval MQUALIFIER getNextKey(TKeyType& rKey) 00618 { 00619 assert(this->m_pInterface != NULL); 00620 return this->m_pInterface->getNextKey(rKey); 00621 } 00622 00623 /***********************************************************************/ 00626 /***********************************************************************/ 00627 inline virtual T& MQUALIFIER getNextItemWithKey(TKeyType& rKey) const 00628 { 00629 assert(this->m_pInterface != NULL); 00630 return this->m_pInterface->getNextItemWithKey(rKey); 00631 } 00632 00633 private: 00634 }; 00635 00636 00637 /****************************************************************************/ 00640 /****************************************************************************/ 00641 template <class TKeyType, class T> class TConstMapIterator : public TAbstractMapIterator<TKeyType, T> 00642 { 00643 public: 00644 /***********************************************************************/ 00647 /***********************************************************************/ 00648 inline TConstMapIterator() 00649 : TAbstractMapIterator<TKeyType, T>() 00650 {} 00651 00652 /***********************************************************************/ 00658 /***********************************************************************/ 00659 inline TConstMapIterator(const IMapIterator<TKeyType, T>* pIt, bool bAllocPointer = false) 00660 : TAbstractMapIterator<TKeyType, T>(pIt, bAllocPointer) 00661 {} 00662 00663 /***********************************************************************/ 00666 /***********************************************************************/ 00667 inline TConstMapIterator(const TConstMapIterator<TKeyType, T>& rIt) 00668 : TAbstractMapIterator<TKeyType, T>(rIt.m_pInterface, true) 00669 {} 00670 00671 /***********************************************************************/ 00675 /***********************************************************************/ 00676 inline TConstMapIterator<TKeyType, T>& operator=(const TConstMapIterator<TKeyType, T>& rIt) 00677 { 00678 assign(rIt.m_pInterface); 00679 return *this; 00680 } 00681 00682 /***********************************************************************/ 00686 /***********************************************************************/ 00687 inline TConstMapIterator<TKeyType, T>& operator=(const IMapIterator<TKeyType, T>* pIt) 00688 { 00689 attach(pIt); 00690 return *this; 00691 } 00692 00693 /***********************************************************************/ 00697 /***********************************************************************/ 00698 inline bool operator==(const TConstMapIterator<TKeyType, T>& rWrapper) const 00699 { 00700 return equals(rWrapper.m_pInterface); 00701 } 00702 00703 /***********************************************************************/ 00707 /***********************************************************************/ 00708 inline bool operator==(const IMapIterator<TKeyType, T>* pIt) const 00709 { 00710 return equals(pIt); 00711 } 00712 00713 /***********************************************************************/ 00717 /***********************************************************************/ 00718 inline bool operator!=(const TConstMapIterator<TKeyType, T>& rWrapper) const 00719 { 00720 return !equals(rWrapper.m_pInterface); 00721 } 00722 00723 /***********************************************************************/ 00727 /***********************************************************************/ 00728 inline bool operator!=(const IMapIterator<TKeyType, T>* pIt) const 00729 { 00730 return !equals(pIt); 00731 } 00732 00733 private: 00734 /***********************************************************************/ 00737 /***********************************************************************/ 00738 inline virtual T& MQUALIFIER getNextItem() const 00739 { 00740 assert(this->m_pInterface != NULL); 00741 return this->m_pInterface->getNextItem(); 00742 } 00743 00744 /***********************************************************************/ 00747 /***********************************************************************/ 00748 inline virtual Retval MQUALIFIER getNextKey(TKeyType& rKey) 00749 { 00750 assert(this->m_pInterface != NULL); 00751 return this->m_pInterface->getNextKey(rKey); 00752 } 00753 00754 /***********************************************************************/ 00757 /***********************************************************************/ 00758 inline virtual T& MQUALIFIER getNextItemWithKey(TKeyType& rKey) const 00759 { 00760 assert(this->m_pInterface != NULL); 00761 return this->m_pInterface->getNextItemWithKey(rKey); 00762 } 00763 }; 00764 00765 END_NAMESPACE_Zeus 00766 00767 #endif 00768 00769