Leaked source code of windows server 2003
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.

232 lines
5.1 KiB

  1. #pragma once
  2. #include "windows.h"
  3. #include "oleauto.h"
  4. #include "stdlib.h"
  5. #include "stdio.h"
  6. #include "comip.h"
  7. #include "msxml2.h"
  8. #include <time.h>
  9. typedef _bstr_t CString;
  10. #define LIST_GROW_CHUNKS (10)
  11. template<typename T>
  12. class CSimpleList
  13. {
  14. public:
  15. T* m_pItems;
  16. SIZE_T m_ItemCount;
  17. SIZE_T m_HighWaterMark;
  18. CSimpleList() : m_pItems(NULL), m_ItemCount(0), m_HighWaterMark(0) {
  19. }
  20. ~CSimpleList() {
  21. if ( m_pItems )
  22. {
  23. delete[] m_pItems;
  24. m_pItems = NULL;
  25. }
  26. }
  27. SIZE_T RoundSize( SIZE_T c )
  28. {
  29. return ( ( c / LIST_GROW_CHUNKS ) + 1 ) * LIST_GROW_CHUNKS;
  30. }
  31. bool Contains( const T &t )
  32. {
  33. if ( this->Size() == 0 ) return false;
  34. for ( SIZE_T c = 0; c < this->Size(); c++ )
  35. if ((*this)[c] == t) break;
  36. return ( c != this->Size() );
  37. }
  38. void EnsureSize( SIZE_T count )
  39. {
  40. if ( count > m_ItemCount )
  41. {
  42. count = RoundSize(count);
  43. T* pItems = new T[count];
  44. for ( SIZE_T i = 0; i < m_HighWaterMark; i++ ) {
  45. pItems[i] = m_pItems[i];
  46. }
  47. if ( m_pItems ) delete[] m_pItems;
  48. m_pItems = pItems;
  49. m_ItemCount = count;
  50. }
  51. }
  52. void Clear()
  53. {
  54. if ( m_pItems )
  55. {
  56. delete[] m_pItems;
  57. m_pItems = NULL;
  58. m_ItemCount = 0;
  59. m_HighWaterMark = 0;
  60. }
  61. }
  62. T& operator[](SIZE_T i) {
  63. EnsureSize(i+1);
  64. if ( (i+1) > m_HighWaterMark )
  65. m_HighWaterMark = i + 1;
  66. return m_pItems[i];
  67. }
  68. const T& operator[](SIZE_T i) const {
  69. if ( m_ItemCount <= m_HighWaterMark ) throw new OutOfRangeException(this, i);
  70. return m_pItems[i];
  71. }
  72. SIZE_T MaxSize() const { return m_ItemCount; }
  73. SIZE_T Size() const { return m_HighWaterMark; }
  74. void Append(T t) { (*this)[Size()] = t; }
  75. class OutOfRangeException
  76. {
  77. OutOfRangeException(const OutOfRangeException&);
  78. OutOfRangeException& operator=(const OutOfRangeException&);
  79. public:
  80. OutOfRangeException( const CSimpleList* ref, int i ) : List(ref), Index(i) { }
  81. const CSimpleList* List;
  82. int Index;
  83. };
  84. };
  85. template<typename Key, typename Value>
  86. class CSimpleMap
  87. {
  88. class CSimpleBucket
  89. {
  90. public:
  91. Key m_Key;
  92. Value m_Value;
  93. CSimpleBucket(const Key key, const Value val) : m_Key(key), m_Value(val) { }
  94. CSimpleBucket(const Key k) : m_Key(k) { }
  95. CSimpleBucket() { }
  96. };
  97. CSimpleList<CSimpleBucket> m_Buckets;
  98. SIZE_T m_LastPosition;
  99. SIZE_T m_CurrentIndex;
  100. public:
  101. CSimpleMap() : m_LastPosition(0), m_CurrentIndex(0) { }
  102. Value& operator[](const Key k) {
  103. if ( Contains(k) )
  104. return m_Buckets[m_LastPosition].m_Value;
  105. else
  106. {
  107. CSimpleBucket bkt(k);
  108. m_Buckets.Append(bkt);
  109. return (*this)[k];
  110. }
  111. }
  112. class OutOfBoundsException
  113. {
  114. public:
  115. };
  116. class KeyNotFoundException
  117. {
  118. public:
  119. KeyNotFoundException(const Key k) : m_Key(k) { }
  120. KeyNotFoundException();
  121. Key m_Key;
  122. };
  123. bool Contains(const Key k)
  124. {
  125. for ( SIZE_T sz = 0; sz < m_Buckets.Size(); sz++ )
  126. {
  127. if ( m_Buckets[sz].m_Key == k )
  128. {
  129. this->m_LastPosition = sz;
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. void GetKeys( CSimpleList<Key> &KeyList )
  136. {
  137. for ( SIZE_T sz = 0; sz < m_Buckets.Size(); sz++ )
  138. {
  139. KeyList.Append(m_Buckets[sz].m_Key);
  140. }
  141. }
  142. void Reset()
  143. {
  144. m_CurrentIndex = 0;
  145. }
  146. bool More() const
  147. {
  148. return ( m_CurrentIndex < m_Buckets.Size() );
  149. }
  150. void Next()
  151. {
  152. m_CurrentIndex++;
  153. }
  154. const Key& CurrentKey() const {
  155. if ( !More() ) throw OutOfBoundsException();
  156. return m_Buckets[m_CurrentIndex].m_Key;
  157. }
  158. const Value& CurrentValue() const {
  159. if ( !More() ) throw OutOfBoundsException();
  160. return m_Buckets[m_CurrentIndex].m_Value;
  161. }
  162. Value& CurrentValue() {
  163. if ( !More() ) throw OutOfBoundsException();
  164. return m_Buckets[m_CurrentIndex].m_Value;
  165. }
  166. void Clear()
  167. {
  168. m_Buckets.Clear();
  169. }
  170. SIZE_T Size() { return m_Buckets.Size(); }
  171. };
  172. template<typename T> class CSmartPointer : public _com_ptr_t<_com_IIID<T, &__uuidof(T)> >
  173. {
  174. public:
  175. template<class Q>
  176. CSmartPointer<T>& operator=(CSmartPointer<Q>& right)
  177. {
  178. right.QueryInterface(this->GetIID(), &(*this));
  179. return *this;
  180. }
  181. template<class Q>
  182. CSmartPointer( CSmartPointer<Q> thing )
  183. {
  184. (*this) = thing;
  185. }
  186. CSmartPointer() { }
  187. };
  188. CString
  189. StringFromCLSID(REFCLSID rclsid);
  190. CString
  191. FormatVersion(DWORD dwMaj, DWORD dwMin);