Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
2.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: stlutil.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _STLUTIL_H__
  11. #define _STLUTIL_H__
  12. #if defined (DBG)
  13. #define _DUMP
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////
  16. // STL based helper template functions
  17. template <class CNT> void _Clear(CNT* pCnt, BOOL bDel)
  18. {
  19. if (bDel)
  20. {
  21. CNT::iterator i;
  22. for (i = pCnt->begin(); i != pCnt->end(); ++i)
  23. {
  24. if (*i != NULL)
  25. {
  26. delete (*i);
  27. *i = NULL;
  28. }
  29. }
  30. }
  31. pCnt->clear();
  32. }
  33. template <class CNT, class T> BOOL _Remove(CNT* pCnt, T* p, BOOL bDel = TRUE)
  34. {
  35. CNT::iterator i;
  36. i = find(pCnt->begin(), pCnt->end(), p);
  37. if (i == pCnt->end())
  38. {
  39. return FALSE;
  40. }
  41. pCnt->erase(i);
  42. if (bDel)
  43. delete p;
  44. return TRUE;
  45. }
  46. ///////////////////////////////////////////////////////////////////////
  47. // CCompare<>
  48. template <class T> class CCompare
  49. {
  50. public:
  51. bool operator()(const T x, const T y)
  52. {
  53. return *x < *y;
  54. }
  55. };
  56. ///////////////////////////////////////////////////////////////////////
  57. // CPtrList<>
  58. template <class T> class CPtrList : public list<T>
  59. {
  60. public:
  61. CPtrList(BOOL bOwnMem)
  62. {
  63. m_bOwnMem = bOwnMem;
  64. }
  65. ~CPtrList()
  66. {
  67. Clear();
  68. }
  69. #ifdef _DUMP
  70. void Dump()
  71. {
  72. CPtrList<T>::iterator i;
  73. for (i = this->begin(); i != this->end(); ++i)
  74. {
  75. (*i)->Dump();
  76. }
  77. }
  78. #endif // _DUMP
  79. void Clear() { _Clear(this, m_bOwnMem);}
  80. BOOL Remove(T p) { return _Remove(this, p, m_bOwnMem);}
  81. private:
  82. BOOL m_bOwnMem;
  83. };
  84. ///////////////////////////////////////////////////////////////////////
  85. // CGrowableArr<>
  86. template <class T, class CMP = CCompare<T*> > class CGrowableArr
  87. {
  88. public:
  89. CGrowableArr(BOOL bOwnMem = TRUE)
  90. {
  91. m_bOwnMem = bOwnMem;
  92. };
  93. virtual ~CGrowableArr()
  94. {
  95. Clear();
  96. }
  97. size_t GetCount() { return m_pEntries.size(); }
  98. BOOL Alloc(long n)
  99. {
  100. return TRUE;
  101. }
  102. void Clear()
  103. {
  104. _Clear(&m_pEntries, m_bOwnMem);
  105. }
  106. T* operator[](long i)
  107. {
  108. return m_pEntries[i];
  109. }
  110. BOOL Add(T* p)
  111. {
  112. m_pEntries.push_back(p);
  113. return TRUE;
  114. }
  115. void Sort()
  116. {
  117. sort(m_pEntries.begin(), m_pEntries.end(), CMP());
  118. }
  119. #ifdef _DUMP
  120. void Dump()
  121. {
  122. vector<T*>::iterator i;
  123. for (i = m_pEntries.begin(); i != m_pEntries.end(); ++i)
  124. {
  125. (*i)->Dump();
  126. }
  127. }
  128. #endif // _DUMP
  129. private:
  130. BOOL m_bOwnMem;
  131. vector<T*> m_pEntries;
  132. };
  133. #endif // _STLUTIL_H__