CnC_Remastered_Collection

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

LINK.CPP (25817B)


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