Go to the documentation of this file.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 #ifndef StringMapH
00033 #define StringMapH
00034
00035 #include <zeusbase/System/String.h>
00036 #include <zeusbase/System/ZObject.h>
00037 #include <zeusbase/System/Interfaces/IStringMap.hpp>
00038 #include <zeusbase/System/Interfaces/IStringList.hpp>
00039 #include <zeusbase/System/Iterators.hpp>
00040
00041 #if defined(ENABLE_LINUX_BINDING)
00042 #include <string.h>
00043 #include <string>
00044 #include <map>
00045 #else
00046 #include <string.h>
00047
00048 #ifdef _MSC_VER
00049 #pragma warning(push, 1) // Avoid Visual Studio 6.0 level 4 and 3 compiler warnings.
00050 #endif
00051
00052 #include <string>
00053 #include <map>
00054
00055 #ifdef _MSC_VER
00056 #pragma warning(pop)
00057 #endif
00058 #endif
00059
00060 BEGIN_NAMESPACE_Zeus
00061 using namespace std;
00062
00063
00066
00067 template <class T> class TStringMap : public IStringMap<T>
00068 {
00069 typedef std::map<std::wstring, T> TStrMap;
00070 public:
00071
00074
00075 inline TStringMap()
00076 : m_tEmpty(T())
00077 {
00078 m_pIterator = NULL;
00079
00080
00081
00082 }
00083
00084
00088
00089 inline TStringMap(const T& rEmptyItem)
00090 : m_tEmpty(rEmptyItem)
00091 {
00092 m_pIterator = NULL;
00093 }
00094
00095
00099
00100 inline TStringMap(const TStringMap<T>& rMap)
00101 #if defined(__COLINUX__)
00102 : IStringMap<T>(rMap),
00103 m_tEmpty(rMap.m_tEmpty),
00104 #else
00105 : m_tEmpty(rMap.m_tEmpty),
00106 #endif
00107 m_Map(rMap.m_Map)
00108 {
00109 m_pIterator = NULL;
00110 }
00111
00112
00115
00116 virtual ~TStringMap()
00117 {
00118 clear();
00119
00120
00121 TAbstractMapIterator* pIt = m_pIterator;
00122 while (pIt != NULL)
00123 {
00124 pIt->clearParent();
00125 pIt = pIt->getNextIterator();
00126 }
00127 }
00128
00129
00132
00133 virtual void MQUALIFIER copyToMap(IMap<IString, T>& rMap) const
00134 {
00135 TConstMapIterator<IString, T> It = this->getConstIterator();
00136 while(It.hasNextItem())
00137 {
00138 TString strKey;
00139 const T& rItem = It.getNextItemWithKeyConst(strKey);
00140 rMap.setItem(strKey, rItem);
00141 }
00142 }
00143
00144
00147
00148 virtual void MQUALIFIER copyValuesToList(IList<T>& rList) const
00149 {
00150 TConstIterator<T> It = this->getConstIterator();
00151 while(It.hasNextItem())
00152 {
00153 rList.add(It.getNextItemConst());
00154 }
00155 }
00156
00157
00160
00161 virtual void MQUALIFIER copyKeysToList(IStringList& rList) const
00162 {
00163 TConstMapIterator<IString, T> It = this->getConstIterator();
00164 while(It.hasNextItem())
00165 {
00166 TString strKey;
00167 It.getNextKeyConst(strKey);
00168 rList.add(strKey);
00169 }
00170 }
00171
00172
00175
00176 inline T& getItem(const TString& rKey)
00177 {
00178 return getItem((const IString&)rKey);
00179 }
00180
00181
00184
00185 inline void setItem(const wchar_t* pKey, const T& rData)
00186 {
00187 setItem(TString(pKey), rData);
00188 }
00189
00190
00193
00194 inline bool deleteItem(const TString& rKey)
00195 {
00196 return deleteItem((const IString&)rKey);
00197 }
00198
00199
00202
00203 inline bool hasItem(const TString& rKey) const
00204 {
00205 return hasItem((const IString&)rKey);
00206 }
00207
00208
00211
00212 virtual T& MQUALIFIER getItem(const IString& rKey)
00213 {
00214
00215
00216
00217
00218 std::wstring strKey = TString(&rKey).c_bstr();
00219 typename TStrMap::iterator it = m_Map.find(strKey);
00220 if (it != m_Map.end())
00221 {
00222 return (*it).second;
00223 }
00224 return m_tEmpty;
00225 }
00226
00227
00230
00231 virtual const T& MQUALIFIER getItemConst(const IString& rKey) const
00232 {
00233
00234
00235
00236
00237 std::wstring strKey = TString(&rKey).c_bstr();
00238 typename TStrMap::const_iterator it = m_Map.find(strKey);
00239 if (it != m_Map.end())
00240 {
00241 return (*it).second;
00242 }
00243 return m_tEmpty;
00244 }
00245
00246
00249
00250 virtual inline T& MQUALIFIER getOrCreateItem(const IString& rKey)
00251 {
00252 return m_Map[TString(rKey).c_bstr()];
00253 }
00254
00255
00258
00259 inline virtual void MQUALIFIER setItem(const IString& rKey, const T& rData)
00260 {
00261
00262
00263
00264
00265 std::wstring strKey = TString(&rKey).c_bstr();
00266 m_Map[strKey] = rData;
00267 }
00268
00269
00272
00273 inline virtual bool MQUALIFIER isEmpty() const
00274 {
00275 return m_Map.empty();
00276 }
00277
00278
00281
00282 inline virtual Int MQUALIFIER getCount() const
00283 {
00284 return (Int)m_Map.size();
00285 }
00286
00287
00290
00291 inline virtual bool MQUALIFIER deleteItem(const IString& rKey)
00292 {
00293
00294
00295
00296
00297 std::wstring strKey = TString(&rKey).c_bstr();
00298 return (m_Map.erase(strKey) == 1);
00299 }
00300
00301
00304
00305 virtual bool MQUALIFIER hasItem(const IString& rKey) const
00306 {
00307 bool bRetval = false;
00308
00309
00310
00311
00312
00313 std::wstring strKey = TString(&rKey).c_bstr();
00314
00315 typename TStrMap::const_iterator it;
00316 it = m_Map.find(strKey);
00317 if (it != m_Map.end())
00318 {
00319 bRetval = true;
00320 }
00321 return bRetval;
00322 }
00323
00324
00327
00328 inline virtual void MQUALIFIER clear()
00329 {
00330 m_Map.clear();
00331 }
00332
00333
00336
00337 inline virtual IMapIterator<IString,T>* MQUALIFIER getIterator()
00338 {
00339 TStringMapIterator* pIterator = new TStringMapIterator(*this, m_Map, m_tEmpty);
00340 pIterator->setNextIterator(m_pIterator);
00341 m_pIterator = pIterator;
00342 return m_pIterator;
00343 }
00344
00345
00348
00349 inline virtual const IMapIterator<IString, T>* MQUALIFIER getConstIterator() const
00350 {
00351 TConstStringMapIterator* pIterator = new TConstStringMapIterator(*this, m_Map, m_tEmpty);
00352 pIterator->setNextIterator(m_pIterator);
00353 m_pIterator = pIterator;
00354 return m_pIterator;
00355 }
00356
00357
00360
00361 virtual void MQUALIFIER releaseIterator(const IListIterator<T>* pIterator) const
00362 {
00363 TAbstractMapIterator* pIt = m_pIterator;
00364 TAbstractMapIterator* pItPrev = NULL;
00365 while (pIt != NULL)
00366 {
00367 if (pIt == pIterator)
00368 {
00369
00370 if (pItPrev == NULL)
00371 {
00372 m_pIterator = pIt->getNextIterator();
00373 delete pIt;
00374 }
00375 else
00376 {
00377 pItPrev->setNextIterator(pIt->getNextIterator());
00378 delete pIt;
00379 }
00380 break;
00381 }
00382 pItPrev = pIt;
00383 pIt = pIt->getNextIterator();
00384 }
00385 }
00386
00387
00390
00391 virtual bool MQUALIFIER equals(const IMap<IString, T>& rMap) const
00392 {
00393 bool bRetval = (this->getCount() == rMap.getCount());
00394
00395 const IMapIterator<IString, T>* pIt1 = getConstIterator();
00396 const IMapIterator<IString, T>* pIt2 = rMap.getConstIterator();
00397
00398 while(bRetval && pIt1->hasNextItem() && pIt2->hasNextItem())
00399 {
00400 TString strKey1;
00401 TString strKey2;
00402 bRetval = (pIt1->getNextItemWithKeyConst(strKey1) == pIt2->getNextItemWithKeyConst(strKey2));
00403
00404 bRetval &= (strKey1 == strKey2);
00405 }
00406 pIt1->release();
00407 pIt2->release();
00408 return bRetval;
00409 }
00410
00411
00414
00415 inline virtual bool equals(const TStringMap<T>& rMap) const
00416 {
00417 return (this->getCount() == rMap.getCount() &&
00418 m_Map == rMap.m_Map);
00419 }
00420
00421
00424
00425 inline TStringMap<T>& operator=(const TStringMap<T>& rInPar)
00426 {
00427 m_Map = rInPar.m_Map;
00428 m_tEmpty = rInPar.m_tEmpty;
00429 return *this;
00430 }
00431
00432
00435
00436 inline bool operator==(const TStringMap<T>& rMap) const
00437 {
00438 return equals(rMap);
00439 }
00440
00441
00444
00445 inline bool operator==(const IStringMap<T>& rMap) const
00446 {
00447 return equals(rMap);
00448 }
00449
00450
00453
00454 inline bool operator!=(const TStringMap<T>& rMap) const
00455 {
00456 return !equals(rMap);
00457 }
00458
00459
00462
00463 inline bool operator!=(const IStringMap<T>& rMap) const
00464 {
00465 return !equals(rMap);
00466 }
00467
00468 private:
00470 T m_tEmpty;
00472 TStrMap m_Map;
00473
00474
00477
00478 class TAbstractMapIterator : public IMapIterator<IString, T>
00479 {
00480 public:
00481
00484
00485 inline TAbstractMapIterator(const TStringMap<T>& rParent)
00486 {
00487 m_iRefCounter = 1;
00488 m_pNextIterator = NULL;
00489 m_pParent = const_cast<TStringMap<T>*>(&rParent);
00490 }
00491
00492
00495
00496 inline virtual ~TAbstractMapIterator()
00497 {}
00498
00499
00502
00503 inline TAbstractMapIterator* getNextIterator()
00504 { return m_pNextIterator; }
00505
00506
00509
00510 inline void setNextIterator(TAbstractMapIterator* pIt)
00511 { m_pNextIterator = pIt; }
00512
00513
00516
00517 inline Retval MQUALIFIER askForInterface(const InterfaceID& , IZUnknown*& )
00518 {
00519 return RET_UNKNOWN_INTERFACE;
00520 }
00521
00522
00525
00526 inline void MQUALIFIER addRef() const
00527 {
00528 ++m_iRefCounter;
00529 }
00530
00531
00534
00535 void MQUALIFIER release() const
00536 {
00537 --m_iRefCounter;
00538 if (m_iRefCounter <= 0)
00539 {
00540 if (m_pParent != NULL)
00541 {
00542 m_pParent->releaseIterator(this);
00543 }
00544 else
00545 {
00546 delete this;
00547 }
00548 }
00549 }
00550
00551
00554
00555 inline void clearParent()
00556 {
00557 m_pParent = NULL;
00558 }
00559
00560 protected:
00562 mutable TAbstractMapIterator* m_pNextIterator;
00564 mutable TStringMap<T>* m_pParent;
00565
00566 private:
00568 mutable Int m_iRefCounter;
00569 };
00570
00571
00574
00575 class TStringMapIterator : public TAbstractMapIterator
00576 {
00577 public:
00578
00581
00582 inline TStringMapIterator(const TStringMap<T>& rParent,
00583 TStrMap& rMap,
00584 T& rEmptyElement)
00585 : TAbstractMapIterator(rParent),
00586 m_rMap(rMap),
00587 m_tEmpty(rEmptyElement),
00588 m_Iterator(rMap.begin())
00589 {
00590 }
00591
00594
00595 inline virtual ~TStringMapIterator()
00596 {}
00597
00598
00601
00602 inline virtual Retval MQUALIFIER getNextKey(IString& rKey)
00603 {
00604 return getNextKeyConst(rKey);
00605 }
00606
00607
00610
00611 virtual Retval MQUALIFIER getNextKeyConst(IString& rKey) const
00612 {
00613 Retval retValue = RET_REQUEST_FAILED;
00614 if (this->hasNextItem())
00615 {
00616 retValue = RET_NOERROR;
00617 rKey.assign((*m_Iterator).first.c_str());
00618 m_Iterator++;
00619 }
00620 return retValue;
00621 }
00622
00623
00626
00627 virtual T& MQUALIFIER getNextItemWithKey(IString& rKey) const
00628 {
00629 if (this->hasNextItem())
00630 {
00631 rKey.assign((*m_Iterator).first.c_str());
00632 T& rRetval = (*m_Iterator).second;
00633 m_Iterator++;
00634
00635 return rRetval;
00636 }
00637 else
00638 {
00639 return const_cast<T&>(m_tEmpty);
00640 }
00641 }
00642
00643
00646
00647 inline virtual const T& MQUALIFIER getNextItemWithKeyConst(IString& rKey) const
00648 {
00649 return const_cast<const T&>(getNextItemWithKey(rKey));
00650 }
00651
00652
00655
00656 inline virtual void MQUALIFIER reset() const
00657 {
00658 m_Iterator = m_rMap.begin();
00659 }
00660
00661
00664
00665 virtual T& MQUALIFIER getNextItem() const
00666 {
00667 if (this->hasNextItem())
00668 {
00669 T& tRetval = (*m_Iterator).second;
00670 m_Iterator++;
00671 return tRetval;
00672 }
00673 else
00674 {
00675 return const_cast<T&>(m_tEmpty);
00676 }
00677 }
00678
00679
00682
00683 inline virtual const T& MQUALIFIER getNextItemConst() const
00684 {
00685 return const_cast<const T&>(this->getNextItem());
00686 }
00687
00688
00691
00692 inline virtual bool MQUALIFIER hasNextItem() const
00693 {
00694 return (m_rMap.end() != m_Iterator);
00695 }
00696
00697 private:
00699 TStrMap& m_rMap;
00701 T m_tEmpty;
00703 mutable typename TStrMap::iterator m_Iterator;
00704 };
00705
00706
00707
00710
00711 class TConstStringMapIterator : public TAbstractMapIterator
00712 {
00713 public:
00714
00717
00718 inline TConstStringMapIterator(const TStringMap<T>& rParent,
00719 const TStrMap& rMap,
00720 const T& rEmptyElement)
00721 : TAbstractMapIterator(rParent),
00722 m_rMap(rMap),
00723 m_tEmpty(rEmptyElement),
00724 m_tRetval(rEmptyElement),
00725 m_Iterator(rMap.begin())
00726 {
00727 }
00728
00729
00732
00733 inline virtual ~TConstStringMapIterator()
00734 {}
00735
00736
00739
00740 inline virtual Retval MQUALIFIER getNextKey(IString& rKey)
00741 {
00742 return getNextKeyConst(rKey);
00743 }
00744
00745
00748
00749 virtual Retval MQUALIFIER getNextKeyConst(IString& rKey) const
00750 {
00751 Retval retValue = RET_REQUEST_FAILED;
00752 if (this->hasNextItem())
00753 {
00754 retValue = RET_NOERROR;
00755 rKey.assign((*m_Iterator).first.c_str());
00756 m_Iterator++;
00757 }
00758 return retValue;
00759 }
00760
00761
00764
00765 virtual T& MQUALIFIER getNextItemWithKey(IString& rKey) const
00766 {
00767 if (this->hasNextItem())
00768 {
00769 rKey.assign((*m_Iterator).first.c_str());
00770 m_tRetval = (*m_Iterator).second;
00771 m_Iterator++;
00772 }
00773 else
00774 {
00775 m_tRetval = m_tEmpty;
00776 }
00777 return m_tRetval;
00778 }
00779
00780
00783
00784 virtual const T& MQUALIFIER getNextItemWithKeyConst(IString& rKey) const
00785 {
00786 if (this->hasNextItem())
00787 {
00788 rKey.assign((*m_Iterator).first.c_str());
00789 const T& rRetval = (*m_Iterator).second;
00790 m_Iterator++;
00791 return rRetval;
00792 }
00793 else
00794 {
00795 return const_cast<const T&>(m_tEmpty);
00796 }
00797 }
00798
00799
00802
00803 inline virtual void MQUALIFIER reset() const
00804 {
00805 m_Iterator = m_rMap.begin();
00806 }
00807
00808
00811
00812 virtual T& MQUALIFIER getNextItem() const
00813 {
00814 if (this->hasNextItem())
00815 {
00816 m_tRetval = (*m_Iterator).second;
00817 m_Iterator++;
00818 }
00819 else
00820 {
00821 m_tRetval = m_tEmpty;
00822 }
00823 return const_cast<T&>(m_tRetval);
00824 }
00825
00826
00829
00830 virtual const T& MQUALIFIER getNextItemConst() const
00831 {
00832 if (this->hasNextItem())
00833 {
00834 const T& rRetval = (*m_Iterator).second;
00835 m_Iterator++;
00836 return rRetval;
00837 }
00838 else
00839 {
00840 return m_tEmpty;
00841 }
00842 }
00843
00844
00847
00848 inline virtual bool MQUALIFIER hasNextItem() const
00849 {
00850 return (m_rMap.end() != m_Iterator);
00851 }
00852
00853 private:
00855 const TStrMap& m_rMap;
00857 T m_tEmpty;
00859 mutable T m_tRetval;
00861 mutable typename TStrMap::const_iterator m_Iterator;
00862 };
00863
00865 mutable TAbstractMapIterator* m_pIterator;
00866 };
00867
00868 END_NAMESPACE_Zeus
00869
00870 #endif
00871