LINK.CPP 24 KB

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