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.

391 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. vs_list.hxx
  5. Abstract:
  6. CVssDLList definition
  7. Author:
  8. Adi Oltean [aoltean] 11/23/1999
  9. Revision History:
  10. --*/
  11. #ifndef __VSS_DLLIST_HXX__
  12. #define __VSS_DLLIST_HXX__
  13. #if _MSC_VER > 1000
  14. #pragma once
  15. #endif
  16. ////////////////////////////////////////////////////////////////////////
  17. // Standard foo for file name aliasing. This code block must be after
  18. // all includes of VSS header files.
  19. //
  20. #ifdef VSS_FILE_ALIAS
  21. #undef VSS_FILE_ALIAS
  22. #endif
  23. #define VSS_FILE_ALIAS "INCLISTH"
  24. //
  25. ////////////////////////////////////////////////////////////////////////
  26. /////////////////////////////////////////////////////////////////////////////
  27. // Constants
  28. const DWORD x_dwElementSignature = 0x47e347e4;
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Forward declarations
  31. template <class T> class CVssDLList;
  32. template <class T> class CVssDLListIterator;
  33. template <class T> class CVssDLListElement;
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CVssDLList
  36. template <class T>
  37. class CVssDLList
  38. {
  39. // Constructors& Destructors
  40. private:
  41. CVssDLList(const CVssDLList&);
  42. public:
  43. CVssDLList():
  44. m_pFirst(NULL), m_pLast(NULL) {};
  45. ~CVssDLList()
  46. {
  47. ClearAll();
  48. }
  49. // Attributes
  50. public:
  51. bool IsEmpty() const;
  52. // Operations
  53. public:
  54. VSS_COOKIE Add(
  55. IN CVssFunctionTracer& ft,
  56. IN const T& object
  57. ) throw(HRESULT);
  58. VSS_COOKIE AddTail(
  59. IN CVssFunctionTracer& ft,
  60. IN const T& object
  61. ) throw(HRESULT);
  62. bool Extract(
  63. OUT T& refObject
  64. );
  65. bool ExtractTail(
  66. OUT T& refObject
  67. );
  68. void ExtractByCookie(
  69. IN VSS_COOKIE cookie,
  70. OUT T& refObject
  71. );
  72. void ClearAll();
  73. private:
  74. bool IsValid() const;
  75. // Data members
  76. private:
  77. CVssDLListElement<T>* m_pFirst;
  78. CVssDLListElement<T>* m_pLast;
  79. friend class CVssDLListIterator<T>;
  80. };
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CVssDLListIterator
  83. template <class T>
  84. class CVssDLListIterator
  85. {
  86. private:
  87. CVssDLListIterator();
  88. CVssDLListIterator(const CVssDLListIterator&);
  89. public:
  90. CVssDLListIterator(const CVssDLList<T>& list):
  91. m_List(list),
  92. m_pNextInEnum(list.m_pFirst)
  93. {};
  94. bool GetNext( OUT T& refObject );
  95. private:
  96. const CVssDLList<T>& m_List;
  97. const CVssDLListElement<T>* m_pNextInEnum;
  98. };
  99. /////////////////////////////////////////////////////////////////////////////
  100. // CVssDLListElement
  101. template <class T>
  102. class CVssDLListElement
  103. {
  104. // Constructors& Destructors
  105. private:
  106. CVssDLListElement();
  107. CVssDLListElement(const CVssDLListElement&);
  108. public:
  109. CVssDLListElement( IN const T& object ):
  110. m_Object(object),
  111. m_dwSignature(x_dwElementSignature),
  112. m_pNext(NULL),
  113. m_pPrev(NULL) {};
  114. // Attributes
  115. public:
  116. bool IsValid() const
  117. {
  118. return (m_dwSignature == x_dwElementSignature);
  119. };
  120. // Data members
  121. public:
  122. DWORD m_dwSignature;
  123. CVssDLListElement* m_pPrev;
  124. CVssDLListElement* m_pNext;
  125. T m_Object;
  126. };
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CVssDLList implementation
  129. template <class T>
  130. bool CVssDLList<T>::IsEmpty() const
  131. {
  132. BS_ASSERT(IsValid());
  133. return (m_pFirst == NULL);
  134. }
  135. template <class T>
  136. VSS_COOKIE CVssDLList<T>::Add(
  137. IN CVssFunctionTracer& ft,
  138. IN const T& object
  139. )
  140. {
  141. BS_ASSERT(IsValid());
  142. CVssDLListElement<T>* pElement = new CVssDLListElement<T>(object);
  143. if (pElement == NULL)
  144. ft.Throw( VSSDBG_GEN, E_OUTOFMEMORY, L"Memory allocation error");
  145. // Setting neighbour element links
  146. if (m_pFirst)
  147. {
  148. BS_ASSERT(m_pFirst->m_pPrev == NULL);
  149. m_pFirst->m_pPrev = pElement;
  150. }
  151. // Setting element links
  152. BS_ASSERT(pElement->m_pNext == NULL);
  153. BS_ASSERT(pElement->m_pPrev == NULL);
  154. if (m_pFirst)
  155. pElement->m_pNext = m_pFirst;
  156. // Setting list head links
  157. m_pFirst = pElement;
  158. if (m_pLast == NULL)
  159. m_pLast = pElement;
  160. return reinterpret_cast<VSS_COOKIE>(pElement);
  161. }
  162. template <class T>
  163. VSS_COOKIE CVssDLList<T>::AddTail(
  164. IN CVssFunctionTracer& ft,
  165. IN const T& object
  166. )
  167. {
  168. BS_ASSERT(IsValid());
  169. CVssDLListElement<T>* pElement = new CVssDLListElement<T>(object);
  170. if (pElement == NULL)
  171. ft.Throw( VSSDBG_GEN, E_OUTOFMEMORY, L"Memory allocation error");
  172. // Setting neighbour element links
  173. if (m_pLast)
  174. {
  175. BS_ASSERT(m_pLast->m_pNext == NULL);
  176. m_pLast->m_pNext = pElement;
  177. }
  178. // Setting element links
  179. BS_ASSERT(pElement->m_pNext == NULL);
  180. BS_ASSERT(pElement->m_pPrev == NULL);
  181. if (m_pLast)
  182. pElement->m_pPrev = m_pLast;
  183. // Setting list head links
  184. if (m_pFirst == NULL)
  185. m_pFirst = pElement;
  186. m_pLast = pElement;
  187. return reinterpret_cast<VSS_COOKIE>(pElement);
  188. }
  189. template <class T>
  190. void CVssDLList<T>::ExtractByCookie(
  191. IN VSS_COOKIE cookie,
  192. OUT T& refObject
  193. )
  194. {
  195. if (cookie == VSS_NULL_COOKIE)
  196. return;
  197. CVssDLListElement<T>* pElement =
  198. reinterpret_cast<CVssDLListElement<T>*>(cookie);
  199. BS_ASSERT(pElement);
  200. BS_ASSERT(pElement->IsValid());
  201. // Setting neighbours links
  202. if (pElement->m_pPrev)
  203. pElement->m_pPrev->m_pNext = pElement->m_pNext;
  204. if (pElement->m_pNext)
  205. pElement->m_pNext->m_pPrev = pElement->m_pPrev;
  206. // Setting list head links
  207. if (m_pFirst == pElement)
  208. m_pFirst = pElement->m_pNext;
  209. if (m_pLast == pElement)
  210. m_pLast = pElement->m_pPrev;
  211. // Destroying the element after getting the original object.
  212. refObject = pElement->m_Object;
  213. delete pElement;
  214. }
  215. template <class T>
  216. bool CVssDLList<T>::Extract(
  217. OUT T& refObject
  218. )
  219. {
  220. CVssDLListElement<T>* pElement = m_pFirst;
  221. if (pElement == NULL)
  222. return false;
  223. BS_ASSERT(pElement->IsValid());
  224. // Setting neighbours links
  225. BS_ASSERT(pElement->m_pPrev == NULL);
  226. if (pElement->m_pNext)
  227. pElement->m_pNext->m_pPrev = NULL;
  228. // Setting list head links
  229. m_pFirst = pElement->m_pNext;
  230. if (m_pLast == pElement)
  231. m_pLast = NULL;
  232. // Destroying the element after getting the original object.
  233. refObject = pElement->m_Object;
  234. delete pElement;
  235. return true;
  236. }
  237. template <class T>
  238. bool CVssDLList<T>::ExtractTail(
  239. OUT T& refObject
  240. )
  241. {
  242. CVssDLListElement<T>* pElement = m_pLast;
  243. if (pElement == NULL)
  244. return false;
  245. BS_ASSERT(pElement->IsValid());
  246. // Setting neighbours links
  247. BS_ASSERT(pElement->m_pNext == NULL);
  248. if (pElement->m_pPrev)
  249. pElement->m_pPrev->m_pNext = NULL;
  250. // Setting list head links
  251. m_pLast = pElement->m_pPrev;
  252. if (m_pFirst == pElement)
  253. m_pFirst = NULL;
  254. // Destroying the element after getting the original object.
  255. refObject = pElement->m_Object;
  256. delete pElement;
  257. return true;
  258. }
  259. template <class T>
  260. void CVssDLList<T>::ClearAll(
  261. )
  262. {
  263. T object;
  264. while(Extract( object ));
  265. }
  266. template <class T>
  267. bool CVssDLList<T>::IsValid() const
  268. {
  269. if ((m_pFirst == NULL) && (m_pLast == NULL))
  270. return true;
  271. if ((m_pFirst != NULL) && (m_pLast != NULL))
  272. return (m_pFirst->IsValid() && m_pLast->IsValid());
  273. return false;
  274. }
  275. /////////////////////////////////////////////////////////////////////////////
  276. // CVssDLListIterator implementation
  277. template <class T>
  278. bool CVssDLListIterator<T>::GetNext( OUT T& object )
  279. {
  280. if (m_pNextInEnum == NULL)
  281. return false;
  282. else
  283. {
  284. object = m_pNextInEnum->m_Object;
  285. m_pNextInEnum = m_pNextInEnum->m_pNext;
  286. return true;
  287. }
  288. }
  289. #endif // __VSS_DLLIST_HXX__