CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

LINK.CPP (24096B)


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