LINK.CPP 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/LINK.CPP 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 : LINK.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 01/15/95 *
  30. * *
  31. * Last Update : January 19, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * LinkClass::Add -- This object adds itself to the given list *
  36. * LinkClass::Add_Head -- This gadget makes itself the head of the given list. *
  37. * LinkClass::Add_Tail -- Add myself to the end of the given list. *
  38. * LinkClass::Get_Next -- Fetches the next object in list. *
  39. * LinkClass::Get_Prev -- Fetches previous object in linked list. *
  40. * LinkClass::Head_Of_List -- Finds the head of the list. *
  41. * LinkClass::LinkClass -- Copy constructor for linked list object. *
  42. * LinkClass::Remove -- Removes the specified object from the list. *
  43. * LinkClass::Tail_Of_List -- Scans for the object at the end of the list. *
  44. * LinkClass::Zap -- Forces the link pointers to NULL. *
  45. * LinkClass::operator= -- Assignment operator for linked list class object. *
  46. * LinkClass::~LinkClass -- Default destructor for linked list object. *
  47. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  48. #include "function.h"
  49. #include "link.h"
  50. /***********************************************************************************************
  51. * LinkClass::LinkClass -- Copy constructor for linked list object. *
  52. * *
  53. * This copy constructor, unlike the assignment operator, doesn't have to deal with an *
  54. * already initialized and legal link object to the left of the "=". It merely puts the *
  55. * destination object into the same list as the source object. *
  56. * *
  57. * INPUT: link -- Reference to the object on the right of the "=". *
  58. * *
  59. * OUTPUT: none *
  60. * *
  61. * WARNINGS: none *
  62. * *
  63. * HISTORY: *
  64. * 01/16/1995 JLB : Created. *
  65. *=============================================================================================*/
  66. LinkClass::LinkClass(LinkClass const & link) :
  67. Next(0), Prev(0)
  68. {
  69. /*
  70. ** Add this object to the same list that the copy object
  71. ** resides in.
  72. */
  73. Add((LinkClass &)link);
  74. }
  75. /***********************************************************************************************
  76. * LinkClass::~LinkClass -- Default destructor for linked list object. *
  77. * *
  78. * This default destructor will remove the object from any linked list it may be part of. *
  79. * *
  80. * INPUT: none *
  81. * *
  82. * OUTPUT: none *
  83. * *
  84. * WARNINGS: none *
  85. * *
  86. * HISTORY: *
  87. * 01/15/1995 JLB : Created. *
  88. *=============================================================================================*/
  89. LinkClass::~LinkClass(void)
  90. {
  91. Remove();
  92. }
  93. /***********************************************************************************************
  94. * LinkClass::Zap -- Forces the link pointers to NULL. *
  95. * *
  96. * This routine will "zap" out the link pointers. This is usually necessary when the link *
  97. * pointers start in an undefined state, but we KNOW that they aren't pointing to anything *
  98. * valid. In such a case it becomes necessary to zap them so that when the object is added *
  99. * to a list, it will be added correctly. *
  100. * *
  101. * INPUT: none *
  102. * *
  103. * OUTPUT: none *
  104. * *
  105. * WARNINGS: none *
  106. * *
  107. * HISTORY: *
  108. * 01/19/1995 JLB : Created. *
  109. *=============================================================================================*/
  110. void LinkClass::Zap(void)
  111. {
  112. Next = 0;
  113. Prev = 0;
  114. }
  115. /***********************************************************************************************
  116. * LinkClass::operator= -- Assignment operator for linked list class object. *
  117. * *
  118. * The assignment operator makes sure that the previous and next pointers remain valid. *
  119. * Because this class only consists of pointers, the assignment operator doesn't actually *
  120. * transfer any data from the source object. It merely makes the destination object part *
  121. * of the same list as the source object. In essence, this is transferring information *
  122. * but not the actual values. *
  123. * *
  124. * If the destination object is already part of another list, it is removed from that list *
  125. * before being added to the source object's list. This ensures that either list remains *
  126. * in a valid condition. *
  127. * *
  128. * INPUT: link -- The object to the right of the "=" operator. *
  129. * *
  130. * OUTPUT: Returns a reference to the rightmost object -- per standard assignment rules. *
  131. * *
  132. * WARNINGS: none *
  133. * *
  134. * HISTORY: *
  135. * 01/16/1995 JLB : Created. *
  136. *=============================================================================================*/
  137. LinkClass & LinkClass::operator = (LinkClass const & link)
  138. {
  139. if (&link == this) return(*this);
  140. Remove();
  141. Add((LinkClass &)link);
  142. return(*this);
  143. }
  144. /***********************************************************************************************
  145. * LinkClass::Get_Next -- Fetches the next object in list. *
  146. * *
  147. * This routine will return with a pointer to the next object in the list. If there are *
  148. * no more objects, then NULL is returned. *
  149. * *
  150. * INPUT: none *
  151. * *
  152. * OUTPUT: Returns with pointer to next object in list or NULL if at end of list. *
  153. * *
  154. * WARNINGS: none *
  155. * *
  156. * HISTORY: *
  157. * 01/15/1995 JLB : Created. *
  158. *=============================================================================================*/
  159. LinkClass * LinkClass::Get_Next(void) const
  160. {
  161. return(Next);
  162. }
  163. /***********************************************************************************************
  164. * LinkClass::Get_Prev -- Fetches previous object in linked list. *
  165. * *
  166. * Use this routine to get a pointer to the previous object in the linked list. If there *
  167. * are no previous objects (such as at the head of the list), then NULL is returned. *
  168. * *
  169. * INPUT: none *
  170. * *
  171. * OUTPUT: Returns with a pointer to the previous object in the list or NULL if none. *
  172. * *
  173. * WARNINGS: none *
  174. * *
  175. * HISTORY: *
  176. * 01/15/1995 JLB : Created. *
  177. *=============================================================================================*/
  178. LinkClass * LinkClass::Get_Prev(void) const
  179. {
  180. return(Prev);
  181. }
  182. /***********************************************************************************************
  183. * LinkClass::Head_Of_List -- Finds the head of the list. *
  184. * *
  185. * Use this routine to scan for and return a reference to the object at the head of the *
  186. * list. *
  187. * *
  188. * INPUT: none *
  189. * *
  190. * OUTPUT: Returns with a reference to the object at the head of the list. *
  191. * *
  192. * WARNINGS: none *
  193. * *
  194. * HISTORY: *
  195. * 01/19/1995 JLB : Created. *
  196. *=============================================================================================*/
  197. LinkClass & LinkClass::Head_Of_List(void)
  198. {
  199. LinkClass * link = this;
  200. while (link->Prev) {
  201. link = link->Prev;
  202. if (link == this) break; // Safety check
  203. }
  204. return(*link);
  205. }
  206. /***********************************************************************************************
  207. * LinkClass::Tail_Of_List -- Scans for the object at the end of the list. *
  208. * *
  209. * Use this routine to scan for and return a reference to the object at the end of the *
  210. * list. *
  211. * *
  212. * INPUT: none *
  213. * *
  214. * OUTPUT: Returns with a reference to the object at the end of the list. *
  215. * *
  216. * WARNINGS: none *
  217. * *
  218. * HISTORY: *
  219. * 01/19/1995 JLB : Created. *
  220. *=============================================================================================*/
  221. LinkClass & LinkClass::Tail_Of_List(void)
  222. {
  223. LinkClass * link = this;
  224. while (link->Next) {
  225. link = link->Next;
  226. if (link == this) break; // Safety check
  227. }
  228. return(*link);
  229. }
  230. /***********************************************************************************************
  231. * LinkClass::Add -- This object adds itself to the given list *
  232. * *
  233. * Use this routine to add a link object to the list, but to be added right after the *
  234. * given link. This allows inserting a link in the middle of the chain. A quite necessary *
  235. * ability if the chain is order dependant (e.g., the gadget system). *
  236. * *
  237. * INPUT: list -- gadget object to add this one to *
  238. * *
  239. * OUTPUT: Returns with a pointer to the head of the list. *
  240. * *
  241. * WARNINGS: none *
  242. * *
  243. * HISTORY: *
  244. * 01/19/1995 JLB : Created. *
  245. *=============================================================================================*/
  246. LinkClass & LinkClass::Add(LinkClass & list)
  247. {
  248. LinkClass * ptr;
  249. /*
  250. ** Save ptr to next gadget.
  251. */
  252. ptr = list.Next;
  253. /*
  254. ** Link myself in after 'list'.
  255. */
  256. list.Next = this;
  257. Prev = &list;
  258. /*
  259. ** Link myself to next gadget, if there is one.
  260. */
  261. Next = ptr;
  262. if (ptr) {
  263. ptr->Prev = this;
  264. }
  265. return(Head_Of_List());
  266. }
  267. /***********************************************************************************************
  268. * LinkClass::Add_Head -- This gadget makes itself the head of the given list. *
  269. * *
  270. * INPUT: list -- the list to make myself the head of *
  271. * *
  272. * OUTPUT: Returns with a reference to the object at the head of the list. This should be *
  273. * the same object that is passed in. *
  274. * *
  275. * WARNINGS: none *
  276. * *
  277. * HISTORY: *
  278. * 01/19/1995 JLB : Created. *
  279. *=============================================================================================*/
  280. LinkClass & LinkClass::Add_Head(LinkClass & list)
  281. {
  282. LinkClass * ptr;
  283. /*
  284. ** Get head of given list.
  285. */
  286. ptr = &list.Head_Of_List();
  287. /*
  288. ** Link myself in front of it.
  289. */
  290. ptr->Prev = this;
  291. Next = ptr;
  292. Prev = NULL;
  293. return(*this);
  294. }
  295. /***********************************************************************************************
  296. * LinkClass::Add_Tail -- Add myself to the end of the given list. *
  297. * *
  298. * INPUT: list -- list to add myself to *
  299. * *
  300. * OUTPUT: the head of the list *
  301. * *
  302. * WARNINGS: The previous and next pointers for the added object MUST have been properly *
  303. * initialized for this routine to work correctly. *
  304. * *
  305. * HISTORY: *
  306. * 01/15/1995 JLB : Created. *
  307. *=============================================================================================*/
  308. LinkClass & LinkClass::Add_Tail(LinkClass & list)
  309. {
  310. LinkClass * ptr;
  311. /*
  312. ** Get head of given list.
  313. */
  314. ptr = &list.Tail_Of_List();
  315. /*
  316. ** Link myself in front of it.
  317. */
  318. ptr->Next = this;
  319. Prev = ptr;
  320. Next = NULL;
  321. return(Head_Of_List());
  322. }
  323. /***********************************************************************************************
  324. * LinkClass::Remove -- Removes the specified object from the list. *
  325. * *
  326. * This routine will remove the specified object from the list of objects. Because of the *
  327. * previous and next pointers, it is possible to remove an object from the list without *
  328. * knowing the head of the list. To do this, just call Remove() with the parameter of *
  329. * "this". *
  330. * *
  331. * INPUT: none *
  332. * *
  333. * OUTPUT: Returns with the new head of list. *
  334. * *
  335. * WARNINGS: none *
  336. * *
  337. * HISTORY: *
  338. * 01/15/1995 JLB : Created. *
  339. *=============================================================================================*/
  340. LinkClass * LinkClass::Remove(void)
  341. {
  342. LinkClass * head = &Head_Of_List();
  343. LinkClass * tail = &Tail_Of_List();
  344. if (Prev) {
  345. Prev->Next = Next;
  346. }
  347. if (Next) {
  348. Next->Prev = Prev;
  349. }
  350. Prev = 0;
  351. Next = 0;
  352. if (head==this) {
  353. if (tail==this) {
  354. return(0);
  355. }
  356. return(&tail->Head_Of_List());
  357. }
  358. return(head);
  359. }