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.

274 lines
7.5 KiB

  1. /******************************************************************************
  2. Header File: Utility Classes.CPP
  3. These classes are generally useful classes which can be used for a variety
  4. of purposes. I created this separate file for quicker reuse later, and also
  5. to avoid having to include some very specific header file just to get these
  6. general-purpose classes.
  7. Copyright (c) 1997 by Microsoft Corporation. All Rights Reserved.
  8. A Pretty Penny Enterprises Production
  9. Change History:
  10. 03-01-1997 Bob_Kjelgaard@Prodigy.Net Created it
  11. ******************************************************************************/
  12. #include "StdAfx.H"
  13. #if defined(LONG_NAMES)
  14. #include "Utility Classes.H"
  15. #else
  16. #include "Utility.H"
  17. #endif
  18. /******************************************************************************
  19. CMapWordToDWord class
  20. This class uses CMapWordToPtr to do its dirty work. When the need arises, I
  21. will make it serializable
  22. ******************************************************************************/
  23. BOOL CMapWordToDWord::Lookup(WORD wKey, DWORD& dwItem) const {
  24. union {
  25. void* pv;
  26. DWORD dw;
  27. };
  28. if (!CMapWordToPtr::Lookup(wKey, pv))
  29. return FALSE;
  30. dwItem = dw;
  31. return TRUE;
  32. }
  33. /******************************************************************************
  34. CMapWordToDWord::GetNextAssoc
  35. This is the map iteration method. We call the same method on the bas class,
  36. and update the DWORD parameter if the underlying method is successful.
  37. ******************************************************************************/
  38. void CMapWordToDWord::GetNextAssoc(POSITION& pos, WORD& wKey,
  39. DWORD& dwItem) const {
  40. union {
  41. void* pv;
  42. DWORD dw;
  43. };
  44. CMapWordToPtr::GetNextAssoc(pos, wKey, pv);
  45. dwItem = dw;
  46. }
  47. /******************************************************************************
  48. CMapWordToDWord::Operator[]
  49. This implements an l-value only operator usable for adding new associations or
  50. updating existing ones.
  51. ******************************************************************************/
  52. DWORD& CMapWordToDWord::operator[](WORD wKey) {
  53. return (DWORD&) CMapWordToPtr::operator[](wKey);
  54. }
  55. /******************************************************************************
  56. CSafeObArray class implementation
  57. This provides a "Safe" CObArray class which can't leak!
  58. ******************************************************************************/
  59. IMPLEMENT_SERIAL(CSafeObArray, CObject, 0);
  60. /******************************************************************************
  61. CSafeObArray::~CSafeObArray
  62. The class destructor will delete the object foreach non-NULL pointer in the
  63. array.
  64. ******************************************************************************/
  65. CSafeObArray::~CSafeObArray() {
  66. for (unsigned u = 0; u < GetSize(); u++)
  67. if (m_coa[u])
  68. delete m_coa[u];
  69. }
  70. /******************************************************************************
  71. CSafeObArray::RemoveAll
  72. Almost the same as the destructor, isn't it?
  73. ******************************************************************************/
  74. void CSafeObArray::RemoveAll() {
  75. for (unsigned u = 0; u < GetSize(); u++)
  76. if (m_coa[u])
  77. delete m_coa[u];
  78. m_coa.RemoveAll();
  79. }
  80. /******************************************************************************
  81. CSafeObArray::RemoveAt
  82. This removes one element from the array- after deleting it, of course.
  83. ******************************************************************************/
  84. void CSafeObArray::RemoveAt(int i) {
  85. if (m_coa[i])
  86. delete m_coa[i];
  87. m_coa.RemoveAt(i);
  88. }
  89. /******************************************************************************
  90. CSafeObArray::Copy
  91. Copy the contents of one array to another.
  92. ******************************************************************************/
  93. void CSafeObArray::Copy(CSafeObArray& csoa)
  94. {
  95. m_coa.Copy(*(csoa.GetCOA())) ;
  96. }
  97. /******************************************************************************
  98. CSafeObArray::Serialize
  99. I call the CObject serializer to maintain the proper typ einformation, then
  100. let the CObArray serialize itself.
  101. ******************************************************************************/
  102. void CSafeObArray::Serialize(CArchive& car) {
  103. if (car.IsLoading())
  104. RemoveAll();
  105. CObject::Serialize(car);
  106. m_coa.Serialize(car);
  107. }
  108. /******************************************************************************
  109. CSafeMapWordToOb implementation
  110. Making the workd safe for maps.
  111. ******************************************************************************/
  112. IMPLEMENT_SERIAL(CSafeMapWordToOb, CObject, 0)
  113. /******************************************************************************
  114. CSafeMapWordToOb::~CSafeMapWordToOb
  115. The class destructor must ensure the underlying objects are deleted.
  116. ******************************************************************************/
  117. CSafeMapWordToOb::~CSafeMapWordToOb() {
  118. WORD wKey;
  119. CObject *pco;
  120. for (POSITION pos = m_cmw2o.GetStartPosition(); pos; ) {
  121. m_cmw2o.GetNextAssoc(pos, wKey, pco);
  122. if (pco)
  123. delete pco;
  124. }
  125. }
  126. /******************************************************************************
  127. CSafeMapWordToOb::operator[]
  128. The problem here is that this is used only to put elements in the map-
  129. therefore, I intercept the call and delete any existing item. This could
  130. cause problems if the same pointer is re-inserted into the map, but for now,
  131. I'll take my chances.
  132. ******************************************************************************/
  133. CObject*& CSafeMapWordToOb::operator[](WORD wKey) {
  134. CObject*& pco = m_cmw2o.operator[](wKey);
  135. if (pco) delete pco;
  136. return pco;
  137. }
  138. /******************************************************************************
  139. CSafeMapWordToOb::RemoveKey
  140. Pretty Obvious- if there was an object there, remove it.
  141. ******************************************************************************/
  142. BOOL CSafeMapWordToOb::RemoveKey(WORD wKey) {
  143. CObject *pco;
  144. if (!m_cmw2o.Lookup(wKey, pco))
  145. return FALSE;
  146. if (pco)
  147. delete pco;
  148. return m_cmw2o.RemoveKey(wKey);
  149. }
  150. /******************************************************************************
  151. CSafeMapWordToOb::RemoveAll
  152. Again, this is pretty obvious- destroy anything that lives!
  153. ******************************************************************************/
  154. void CSafeMapWordToOb::RemoveAll() {
  155. WORD wKey;
  156. CObject *pco;
  157. for (POSITION pos = m_cmw2o.GetStartPosition(); pos; ) {
  158. GetNextAssoc(pos, wKey, pco);
  159. if (pco)
  160. delete pco;
  161. }
  162. m_cmw2o.RemoveAll();
  163. }
  164. /******************************************************************************
  165. CSafeMapWordToOb::Serialize
  166. First, I depopulate the map if it is being loaded. Then I call the CObject
  167. serializer to handle run-time typing checks, and then serialize the
  168. underlying map.
  169. ******************************************************************************/
  170. void CSafeMapWordToOb::Serialize(CArchive& car) {
  171. if (car.IsLoading())
  172. RemoveAll();
  173. CObject::Serialize(car);
  174. m_cmw2o.Serialize(car);
  175. }