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.

180 lines
3.1 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. List.hxx
  5. Abstract:
  6. Base class for an embeddable doubly linked list class.
  7. Author:
  8. Mario Goertzel [MarioGo]
  9. Revision History:
  10. MarioGo 12/20/1995 Broke plist.hxx into two parts
  11. --*/
  12. #ifndef __LIST_HXX
  13. #define __LIST_HXX
  14. class CList;
  15. class CListElement
  16. {
  17. friend CList;
  18. public:
  19. CListElement() :
  20. #if DBG
  21. fInList(FALSE),
  22. #endif
  23. _flink(0), _blink(0) { }
  24. ~CListElement() {
  25. // PERF: Make sure this disappears on retail
  26. ASSERT(NotInList());
  27. }
  28. CListElement *Next() {
  29. return(_flink);
  30. }
  31. CListElement *Previous() {
  32. return(_blink);
  33. }
  34. private:
  35. CListElement *_flink;
  36. CListElement *_blink;
  37. #if DBG
  38. BOOL fInList;
  39. public:
  40. void Inserted() { fInList = TRUE; }
  41. void Removed() { fInList = FALSE; }
  42. BOOL NotInList() { return(fInList == FALSE); }
  43. BOOL InList() { return(fInList == TRUE); }
  44. #endif
  45. protected:
  46. void
  47. Insert(
  48. IN CListElement *p
  49. )
  50. {
  51. // Insert new element (p) after this element.
  52. ASSERT(this);
  53. ASSERT(p->_flink == 0 && p->_blink == 0);
  54. if (_flink)
  55. {
  56. ASSERT(_flink->_blink == this);
  57. _flink->_blink = p;
  58. }
  59. p->_flink = _flink;
  60. p->_blink = this;
  61. _flink = p;
  62. }
  63. void
  64. Unlink()
  65. {
  66. if (_flink)
  67. {
  68. ASSERT(_flink->_blink == this);
  69. _flink->_blink = _blink;
  70. }
  71. if (_blink)
  72. {
  73. ASSERT(_blink->_flink == this);
  74. _blink->_flink = _flink;
  75. _blink = 0;
  76. }
  77. _flink = 0;
  78. }
  79. };
  80. class CList
  81. {
  82. private:
  83. CListElement *_first;
  84. CListElement *_last;
  85. public:
  86. CList() : _first(0), _last(0) { }
  87. ~CList() {
  88. ASSERT((_first == 0) && (_last == 0));
  89. }
  90. void
  91. Insert( IN CListElement *p ) {
  92. if (_last)
  93. {
  94. _last->Insert(p);
  95. _last = p;
  96. }
  97. else
  98. {
  99. ASSERT(0 == _first);
  100. _first = _last = p;
  101. }
  102. #if DBG
  103. p->Inserted();
  104. #endif
  105. }
  106. CListElement *
  107. Remove(CListElement *p) {
  108. if (0 == p)
  109. {
  110. return(0);
  111. }
  112. if (p == _first)
  113. {
  114. _first = _first->Next();
  115. ASSERT(p != _first);
  116. }
  117. if (p == _last)
  118. {
  119. _last = _last->Previous();
  120. ASSERT(p != _last);
  121. }
  122. ASSERT((_first == 0) ? (_last == 0) : 1);
  123. ASSERT((_last == 0) ? (_first == 0) : 1);
  124. // Take the element out of the list.
  125. p->Unlink();
  126. #if DBG
  127. p->Removed();
  128. #endif
  129. return(p);
  130. }
  131. CListElement *First() {
  132. return(_first);
  133. }
  134. CListElement *Last() {
  135. return(_last);
  136. }
  137. };
  138. #endif