LISTNODE.H 6.0 KB

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