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