LISTNODE.H 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/LISTNODE.H 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : LISTNODE.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 05/16/96 *
  30. * *
  31. * Last Update : May 16, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef LISTNODE_H
  37. #define LISTNODE_H
  38. #include <stddef.h>
  39. #include <assert.h>
  40. /*
  41. ** The "bool" integral type was defined by the C++ comittee in
  42. ** November of '94. Until the compiler supports this, use the following
  43. ** definition.
  44. */
  45. #ifndef __BORLANDC__
  46. #ifndef TRUE_FALSE_DEFINED
  47. #define TRUE_FALSE_DEFINED
  48. enum {false=0,true=1};
  49. typedef int bool;
  50. #endif
  51. #endif
  52. #pragma warn -inl
  53. /*
  54. ** This is a doubly linked list node. Typical use of this node is to derive
  55. ** objects from this node. The interface class for this node can be used for
  56. ** added convenience.
  57. */
  58. class GenericList;
  59. class GenericNode {
  60. public:
  61. GenericNode(void) : NextNode(0), PrevNode(0) {}
  62. ~GenericNode(void) {Unlink();}
  63. GenericNode(GenericNode & node) {node.Link(this);}
  64. GenericNode & operator = (GenericNode & node) {
  65. if (&node != this) {
  66. node.Link(this);
  67. }
  68. return(*this);
  69. }
  70. void Unlink(void) {
  71. if (Is_Valid()) {
  72. PrevNode->NextNode = NextNode;
  73. NextNode->PrevNode = PrevNode;
  74. PrevNode = 0;
  75. NextNode = 0;
  76. }
  77. }
  78. GenericList * Main_List(void) const {
  79. GenericNode const * node = this;
  80. while (node->PrevNode) {
  81. node = PrevNode;
  82. }
  83. return((GenericList *)this);
  84. }
  85. void Link(GenericNode * node) {
  86. assert(node != NULL);
  87. node->Unlink();
  88. node->NextNode = NextNode;
  89. node->PrevNode = this;
  90. if (NextNode) NextNode->PrevNode = node;
  91. NextNode = node;
  92. }
  93. GenericNode * Next(void) const {return(NextNode);}
  94. GenericNode * Prev(void) const {return(PrevNode);}
  95. bool Is_Valid(void) const {return(this != NULL && NextNode != NULL && PrevNode != NULL);}
  96. protected:
  97. GenericNode * NextNode;
  98. GenericNode * PrevNode;
  99. };
  100. /*
  101. ** This is a generic list handler. It manages N generic nodes. Use the interface class
  102. ** to the generic list for added convenience.
  103. */
  104. class GenericList {
  105. public:
  106. GenericList(void) {
  107. FirstNode.Link(&LastNode);
  108. }
  109. GenericNode * First(void) const {return(FirstNode.Next());}
  110. GenericNode * Last(void) const {return(LastNode.Prev());}
  111. bool Is_Empty(void) const {return(!FirstNode.Next()->Is_Valid());}
  112. void Add_Head(GenericNode * node) {FirstNode.Link(node);}
  113. void Add_Tail(GenericNode * node) {LastNode.Prev()->Link(node);}
  114. void Delete(void) {while (FirstNode.Next()->Is_Valid()) delete FirstNode.Next();}
  115. protected:
  116. GenericNode FirstNode;
  117. GenericNode LastNode;
  118. private:
  119. GenericList(GenericList & list);
  120. GenericList & operator = (GenericList const &);
  121. };
  122. /*
  123. ** This node class serves only as an "interface class" for the normal node
  124. ** object. In order to use this interface class you absolutely must be sure
  125. ** that the node is the root base object of the "class T". If it is true that the
  126. ** address of the node is the same as the address of the "class T", then this
  127. ** interface class will work. You can usually ensure this by deriving the
  128. ** class T object from this node.
  129. */
  130. template<class T> class List;
  131. template<class T>
  132. class Node : public GenericNode {
  133. public:
  134. List<T> * Main_List(void) const {return((List<T> *)GenericNode::Main_List());}
  135. T * Next(void) const {return((T *)GenericNode::Next());}
  136. T * Prev(void) const {return((T *)GenericNode::Prev());}
  137. bool Is_Valid(void) const {return(GenericNode::Is_Valid());}
  138. };
  139. /*
  140. ** This is an "interface class" for a list of nodes. The rules for the class T object
  141. ** are the same as the requirements required of the node class.
  142. */
  143. template<class T>
  144. class List : public GenericList {
  145. public:
  146. T * First(void) const {return((T*)GenericList::First());}
  147. T * Last(void) const {return((T*)GenericList::Last());}
  148. };
  149. #endif