00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifndef __MapHPP__1246139532
00070 #define __MapHPP__1246139532
00071
00072 #include <zeusbase/System/Interfaces/IMap.hpp>
00073 #include <zeusbase/System/Iterators.hpp>
00074 #include <zeusbase/System/ZObject.h>
00075 #include <string.h>
00076
00077 #ifdef _MSC_VER
00078 #pragma warning(push, 1) // Avoid Visual Studio 6.0 level 4 and 3 compiler warnings.
00079 #endif
00080
00081 #include <map>
00082 #include <string>
00083
00084 #ifdef _MSC_VER
00085 #pragma warning(pop)
00086 #endif
00087
00088
00089 BEGIN_NAMESPACE_Zeus
00090
00091 using namespace std;
00092
00093
00097
00098 template <class TKeyType, class T> class TMap : public IMap<TKeyType,T>
00099 {
00100 public:
00101
00104
00105 inline TMap()
00106
00107
00108
00109
00110
00111
00112
00113 : m_tEmpty(T())
00114 {
00115 m_pIterator = NULL;
00116
00117
00118
00119 }
00120
00121
00125
00126 inline TMap(const T& rEmptyItem)
00127 : m_tEmpty(rEmptyItem)
00128 {
00129 m_pIterator = NULL;
00130 }
00131
00132 #ifdef USE_STL_BINDINGS
00133
00137
00138 inline TMap(const std::map<TKeyType, T>& rMap)
00139 : m_Map(rMap),
00140 m_tEmpty(T())
00141 {
00142 m_pIterator = NULL;
00143 }
00144 #endif
00145
00146
00150
00151 inline TMap(const TMap<TKeyType, T>& rMap)
00152 : m_Map(rMap.m_Map),
00153 m_tEmpty(rMap.m_tEmpty)
00154 {
00155 m_pIterator = NULL;
00156 }
00157
00158
00161
00162 virtual ~TMap()
00163 {
00164 clear();
00165
00166
00167 TInternalAbstractMapIterator* pIt = m_pIterator;
00168 while (pIt != NULL)
00169 {
00170 pIt->clearParent();
00171 pIt = pIt->getNextIterator();
00172 }
00173 }
00174
00175
00178
00179 virtual void MQUALIFIER copyToMap(IMap<TKeyType, T>& rMap) const
00180 {
00181 TConstMapIterator<TKeyType, T> It = this->getConstIterator();
00182 while(It.hasNextItem())
00183 {
00184 TKeyType Key;
00185 const T& rItem = It.getNextItemWithKeyConst(Key);
00186 rMap.setItem(Key, rItem);
00187 }
00188 }
00189
00190
00193
00194 virtual T& MQUALIFIER getItem(const TKeyType& rKey)
00195 {
00196 typename std::map<TKeyType, T>::iterator it = m_Map.find(rKey);
00197 if (it != m_Map.end())
00198 {
00199 return (*it).second;
00200 }
00201 return m_tEmpty;
00202 }
00203
00204
00207
00208 inline virtual const T& MQUALIFIER getItemConst(const TKeyType& rKey) const
00209 {
00210 typename std::map<TKeyType, T>::const_iterator it = m_Map.find(rKey);
00211 if (it != m_Map.end())
00212 {
00213 return (*it).second;
00214 }
00215 return m_tEmpty;
00216 }
00217
00218
00221
00222 inline virtual void MQUALIFIER setItem(const TKeyType& rKey, const T& rData)
00223 {
00224 m_Map[rKey] = rData;
00225 }
00226
00227
00230
00231 inline virtual bool MQUALIFIER isEmpty() const
00232 {
00233 return m_Map.empty();
00234 }
00235
00236
00239
00240 inline virtual Int MQUALIFIER getCount() const
00241 {
00242 return (Int)m_Map.size();
00243 }
00244
00245
00248
00249 inline virtual bool MQUALIFIER deleteItem(const TKeyType& rKey)
00250 {
00251 return (m_Map.erase(rKey) == 1);
00252 }
00253
00254
00257
00258 virtual bool MQUALIFIER hasItem(const TKeyType& rKey) const
00259 {
00260 bool bRetval = false;
00261 typename std::map<TKeyType, T>::const_iterator it;
00262 it = m_Map.find(rKey);
00263 if (it != m_Map.end())
00264 {
00265 bRetval = true;
00266 }
00267 return bRetval;
00268 }
00269
00270
00273
00274 inline virtual void MQUALIFIER clear()
00275 {
00276 m_Map.clear();
00277 }
00278
00279
00282
00283 inline virtual IMapIterator<TKeyType, T>* MQUALIFIER getIterator()
00284 {
00285 TInternalMapIterator* pIterator = new TInternalMapIterator(*this, m_Map, m_tEmpty);
00286 pIterator->setNextIterator(m_pIterator);
00287 m_pIterator = pIterator;
00288 return m_pIterator;
00289 }
00290
00291
00294
00295 inline virtual const IMapIterator<TKeyType, T>* MQUALIFIER getConstIterator() const
00296 {
00297 TInternalConstMapIterator* pIterator = new TInternalConstMapIterator(*this, m_Map, m_tEmpty);
00298 pIterator->setNextIterator(m_pIterator);
00299 m_pIterator = pIterator;
00300 return m_pIterator;
00301 }
00302
00303
00304
00307
00308 virtual void MQUALIFIER releaseIterator(const IListIterator<T>* pIterator) const
00309 {
00310 TInternalAbstractMapIterator* pIt = m_pIterator;
00311 TInternalAbstractMapIterator* pItPrev = NULL;
00312 while (pIt != NULL)
00313 {
00314 if (pIt == pIterator)
00315 {
00316
00317 if (pItPrev == NULL)
00318 {
00319 m_pIterator = pIt->getNextIterator();
00320 delete pIt;
00321 }
00322 else
00323 {
00324 pItPrev->setNextIterator(pIt->getNextIterator());
00325 delete pIt;
00326 }
00327 break;
00328 }
00329 pItPrev = pIt;
00330 pIt = pIt->getNextIterator();
00331 }
00332 }
00333
00334
00337
00338 virtual bool MQUALIFIER equals(const IMap<TKeyType, T>& rMap) const
00339 {
00340 bool bRetval = (this->getCount() == rMap.getCount());
00341
00342 const IMapIterator<TKeyType, T>* pIt1 = getConstIterator();
00343 const IMapIterator<TKeyType, T>* pIt2 = rMap.getConstIterator();
00344
00345 while(bRetval && pIt1->hasNextItem() && pIt2->hasNextItem())
00346 {
00347 TKeyType Key1;
00348 TKeyType Key2;
00349 bRetval = (pIt1->getNextItemWithKeyConst(Key1) == pIt2->getNextItemWithKeyConst(Key2));
00350
00351 bRetval &= (Key1 == Key2);
00352 }
00353 pIt1->release();
00354 pIt2->release();
00355 return bRetval;
00356 }
00357
00358
00361
00362 inline virtual bool equals(const TMap<TKeyType, T>& rMap) const
00363 {
00364 return (this->getCount() == rMap.getCount() &&
00365 m_Map == rMap.m_Map);
00366 }
00367
00368
00373
00374 inline TMap<TKeyType, T>& operator= (const TMap<TKeyType, T>& rInPar)
00375 {
00376 m_Map = rInPar.m_Map;
00377 m_tEmpty = rInPar.m_tEmpty;
00378 return *this;
00379 }
00380
00381 #ifdef USE_STL_BINDINGS
00382
00387
00388 inline TMap<TKeyType, T>& operator= (const std::map<TKeyType, T>& rInPar)
00389 {
00390 m_Map = rInPar;
00391 return *this;
00392 }
00393 #endif
00394
00395
00401
00402 inline bool operator== (const TMap<TKeyType, T>& rMap) const
00403 {
00404 return equals(rMap);
00405 }
00406
00407
00413
00414 inline bool operator== (const IMap<TKeyType, T>& rMap) const
00415 {
00416 return equals(rMap);
00417 }
00418
00419
00422
00423 inline bool operator!= (const TMap<TKeyType, T>& rMap) const
00424 {
00425 return !equals(rMap);
00426 }
00427
00428
00431
00432 inline bool operator!= (const IMap<TKeyType, T>& rMap) const
00433 {
00434 return !equals(rMap);
00435 }
00436
00437 protected:
00438
00439 private:
00441 T m_tEmpty;
00443 std::map<TKeyType, T> m_Map;
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00462
00463 class TInternalAbstractMapIterator : public IMapIterator<TKeyType, T>
00464 {
00465 public:
00466
00469
00470 inline TInternalAbstractMapIterator(const TMap<TKeyType, T>& rParent)
00471 {
00472 m_iRefCounter = 1;
00473 m_pNextIterator = NULL;
00474 m_pParent = (TMap<TKeyType, T>*) &rParent;
00475 }
00476
00477
00480
00481 inline virtual ~TInternalAbstractMapIterator()
00482 {}
00483
00484
00487
00488 inline TInternalAbstractMapIterator* getNextIterator()
00489 { return m_pNextIterator; }
00490
00491
00494
00495 inline void setNextIterator(TInternalAbstractMapIterator* pIt)
00496 { m_pNextIterator = pIt; }
00497
00498
00499
00502
00503 inline Retval MQUALIFIER askForInterface(const InterfaceID& , IZUnknown*& )
00504 {
00505 return RET_UNKNOWN_INTERFACE;
00506 }
00507
00508
00511
00512 inline void MQUALIFIER addRef() const
00513 {
00514 ++m_iRefCounter;
00515 }
00516
00517
00520
00521 void MQUALIFIER release() const
00522 {
00523 --m_iRefCounter;
00524 if (m_iRefCounter <= 0)
00525 {
00526 if (m_pParent != NULL)
00527 {
00528 m_pParent->releaseIterator(this);
00529 }
00530 else
00531 {
00532 delete this;
00533 }
00534 }
00535 }
00536
00537
00540
00541 inline void clearParent()
00542 {
00543 m_pParent = NULL;
00544 }
00545
00546 protected:
00548 mutable TInternalAbstractMapIterator* m_pNextIterator;
00550 mutable TMap<TKeyType, T>* m_pParent;
00551
00552 private:
00554 mutable Int m_iRefCounter;
00555
00556 };
00557
00558
00561
00562 class TInternalMapIterator : public TInternalAbstractMapIterator
00563 {
00564 public:
00565
00568
00569 inline TInternalMapIterator(const TMap<TKeyType, T>& rParent,
00570 std::map<TKeyType, T>& tMap,
00571 T& tEmptyItem)
00572 : TInternalAbstractMapIterator(rParent),
00573 m_Map(tMap),
00574 m_tEmpty(tEmptyItem),
00575 m_Iterator(tMap.begin())
00576 {
00577 }
00578
00579
00582
00583 inline virtual ~TInternalMapIterator()
00584 {}
00585
00586
00589
00590 inline virtual Retval MQUALIFIER getNextKey(TKeyType& rKey)
00591 {
00592 return getNextKeyConst(rKey);
00593 }
00594
00595
00598
00599 inline virtual Retval MQUALIFIER getNextKeyConst(TKeyType& rKey) const
00600 {
00601 Retval retValue = RET_REQUEST_FAILED;
00602 if (this->hasNextItem())
00603 {
00604 retValue = RET_NOERROR;
00605 rKey = (*m_Iterator).first;
00606 m_Iterator++;
00607 }
00608 return retValue;
00609 }
00610
00611
00614
00615 virtual T& MQUALIFIER getNextItemWithKey(TKeyType& rKey) const
00616 {
00617 if (this->hasNextItem())
00618 {
00619 rKey = (*m_Iterator).first;
00620 T& rRetval = (*m_Iterator).second;
00621 m_Iterator++;
00622
00623 return rRetval;
00624 }
00625 else
00626 {
00627 return const_cast<T&>(m_tEmpty);
00628 }
00629 }
00630
00631
00634
00635 inline virtual const T& MQUALIFIER getNextItemWithKeyConst(TKeyType& rKey) const
00636 {
00637 return const_cast<const T&>(getNextItemWithKey(rKey));
00638 }
00639
00640
00643
00644 inline virtual void MQUALIFIER reset() const
00645 {
00646 m_Iterator = m_Map.begin();
00647 }
00648
00649
00652
00653 virtual T& MQUALIFIER getNextItem() const
00654 {
00655 if (this->hasNextItem())
00656 {
00657 T& tRetval = (*m_Iterator).second;
00658 m_Iterator++;
00659 return tRetval;
00660 }
00661 else
00662 {
00663 return const_cast<T&>(m_tEmpty);
00664 }
00665 }
00666
00667
00670
00671 inline virtual const T& MQUALIFIER getNextItemConst() const
00672 {
00673 return const_cast<const T&>(this->getNextItem());
00674 }
00675
00676
00679
00680 inline virtual bool MQUALIFIER hasNextItem() const
00681 {
00682 return (m_Map.end() != m_Iterator);
00683 }
00684
00685 private:
00687 std::map<TKeyType, T>& m_Map;
00689 T m_tEmpty;
00691 mutable typename std::map<TKeyType, T>::iterator m_Iterator;
00692 };
00693
00694
00697
00698 class TInternalConstMapIterator : public TInternalAbstractMapIterator
00699 {
00700 public:
00701
00704
00705 inline TInternalConstMapIterator(const TMap<TKeyType, T>& rParent,
00706 const std::map<TKeyType, T>& tMap,
00707 const T& tEmptyItem)
00708 : TInternalAbstractMapIterator(rParent),
00709 m_Map(tMap),
00710 m_tEmpty(tEmptyItem),
00711 m_tRetval(tEmptyItem),
00712 m_Iterator(tMap.begin())
00713 {
00714 }
00715
00716
00719
00720 inline virtual ~TInternalConstMapIterator()
00721 {}
00722
00723
00726
00727 inline virtual Retval MQUALIFIER getNextKey(TKeyType& rKey)
00728 {
00729 return getNextKeyConst(rKey);
00730 }
00731
00732
00735
00736 virtual Retval MQUALIFIER getNextKeyConst(TKeyType& rKey) const
00737 {
00738 Retval retValue = RET_REQUEST_FAILED;
00739 if (this->hasNextItem())
00740 {
00741 retValue = RET_NOERROR;
00742 rKey = (*m_Iterator).first;
00743 m_Iterator++;
00744 }
00745 return retValue;
00746 }
00747
00748
00751
00752 virtual T& MQUALIFIER getNextItemWithKey(TKeyType& rKey) const
00753 {
00754 if (this->hasNextItem())
00755 {
00756 rKey = (*m_Iterator).first;
00757 m_tRetval = const_cast<T&>((*m_Iterator).second);
00758 m_Iterator++;
00759 }
00760 else
00761 {
00762 m_tRetval = m_tEmpty;
00763 }
00764 return m_tRetval;
00765 }
00766
00767
00770
00771 virtual const T& MQUALIFIER getNextItemWithKeyConst(TKeyType& rKey) const
00772 {
00773 if (this->hasNextItem())
00774 {
00775 rKey = (*m_Iterator).first;
00776 const T& rRetval = (*m_Iterator).second;
00777 m_Iterator++;
00778
00779 return rRetval;
00780 }
00781 else
00782 {
00783 return m_tEmpty;
00784 }
00785 }
00786
00787
00790
00791 inline virtual void MQUALIFIER reset() const
00792 {
00793 m_Iterator = m_Map.begin();
00794 }
00795
00796
00799
00800 virtual T& MQUALIFIER getNextItem() const
00801 {
00802 if (this->hasNextItem())
00803 {
00804 m_tRetval = (*m_Iterator).second;
00805 m_Iterator++;
00806 }
00807 else
00808 {
00809 m_tRetval = m_tEmpty;
00810 }
00811 return m_tRetval;
00812 }
00813
00814
00817
00818 virtual const T& MQUALIFIER getNextItemConst() const
00819 {
00820 if (this->hasNextItem())
00821 {
00822 const T& rRetval = (*m_Iterator).second;
00823 m_Iterator++;
00824 return rRetval;
00825 }
00826 else
00827 {
00828 return m_tEmpty;
00829 }
00830 }
00831
00832
00835
00836 inline virtual bool MQUALIFIER hasNextItem() const
00837 {
00838 return (m_Map.end() != m_Iterator);
00839 }
00840
00841 private:
00843 const std::map<TKeyType, T>& m_Map;
00845 T m_tEmpty;
00847 mutable T m_tRetval;
00849 mutable typename std::map<TKeyType, T>::const_iterator m_Iterator;
00850 };
00851
00853 mutable TInternalAbstractMapIterator* m_pIterator;
00854 };
00855
00856
00857 END_NAMESPACE_Zeus
00858
00859 #endif
00860