CnC_Remastered_Collection

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

BUFF.CPP (13688B)


      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/BUFF.CPP 1     3/03/97 10:24a 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 : BUFF.CPP                                                     *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 07/29/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : September 7, 1996 [JLB]                                      *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  *   Buffer::Buffer -- Constructor for buffer object.                                          *
     34  *   Buffer::Buffer -- Copy constructor for buffer object.                                     *
     35  *   Buffer::Buffer -- Self-allocating constructor for buffer object.                          *
     36  *   Buffer::Reset -- Clears the buffer object to null state.                                  *
     37  *   Buffer::operator = -- Assignment operator for the buffer object.                          *
     38  *   Buffer::~Buffer -- Destructor for buffer object.                                          *
     39  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     40 
     41 
     42 #include	"buff.h"
     43 #include	<stddef.h>
     44 
     45 
     46 /***********************************************************************************************
     47  * Buffer::Buffer -- Constructor for buffer object.                                            *
     48  *                                                                                             *
     49  *    This is the normal constructor for a buffer object. The buffer pointer and size are      *
     50  *    specified as parameters.                                                                 *
     51  *                                                                                             *
     52  * INPUT:   buffer   -- Pointer to the buffer.                                                 *
     53  *                                                                                             *
     54  *          size     -- The size of the buffer.                                                *
     55  *                                                                                             *
     56  * OUTPUT:  none                                                                               *
     57  *                                                                                             *
     58  * WARNINGS:   It is possible to construct a Buffer object that has a pointer but a size       *
     59  *             value of zero. The Buffer object can still be used for its pointer, but it      *
     60  *             any function that requires a size will fail.                                    *
     61  *                                                                                             *
     62  * HISTORY:                                                                                    *
     63  *   07/29/1996 JLB : Created.                                                                 *
     64  *=============================================================================================*/
     65 Buffer::Buffer(void * buffer, long size) :
     66 	BufferPtr(buffer),
     67 	Size(size),
     68 	IsAllocated(false)
     69 {
     70 }
     71 
     72 
     73 // Alternate constructor for char * pointer.
     74 Buffer::Buffer(char * buffer, long size) :
     75 	BufferPtr(buffer),
     76 	Size(size),
     77 	IsAllocated(false)
     78 {
     79 }
     80 
     81 
     82 // Alternate constructor for void const * pointer.
     83 Buffer::Buffer(void const * buffer, long size) :
     84 	BufferPtr((void*)buffer),
     85 	Size(size),
     86 	IsAllocated(false)
     87 {
     88 }
     89 
     90 
     91 /***********************************************************************************************
     92  * Buffer::Buffer -- Self-allocating constructor for buffer object.                            *
     93  *                                                                                             *
     94  *    This construtor for a buffer object will automatically allocate the bytes necessary      *
     95  *    to fulfill the size requested. This object is also responsible for deleting the buffer   *
     96  *    it allocated.                                                                            *
     97  *                                                                                             *
     98  * INPUT:   size  -- The size of the buffer to allocated.                                      *
     99  *                                                                                             *
    100  * OUTPUT:  none                                                                               *
    101  *                                                                                             *
    102  * WARNINGS:   There is no way to tell if the allocation failed. To verify, call Get_Buffer    *
    103  *             and compare with NULL.                                                          *
    104  *                                                                                             *
    105  * HISTORY:                                                                                    *
    106  *   07/29/1996 JLB : Created.                                                                 *
    107  *=============================================================================================*/
    108 Buffer::Buffer(long size) :
    109 	BufferPtr(NULL),
    110 	Size(size),
    111 	IsAllocated(false)
    112 {
    113 	if (size > 0) {
    114 		BufferPtr = new char[size];
    115 		IsAllocated = true;
    116 	}
    117 }
    118 
    119 
    120 /***********************************************************************************************
    121  * Buffer::Buffer -- Copy constructor for buffer object.                                       *
    122  *                                                                                             *
    123  *    This will make a duplicate of the specified buffer object. The ownership of the pointer  *
    124  *    remains with the original object. This prevents multiple deletion of the same pointer.   *
    125  *                                                                                             *
    126  * INPUT:   buffer   -- Reference to the buffer object to be dupilcated.                       *
    127  *                                                                                             *
    128  * OUTPUT:  none                                                                               *
    129  *                                                                                             *
    130  * WARNINGS:   none                                                                            *
    131  *                                                                                             *
    132  * HISTORY:                                                                                    *
    133  *   08/02/1996 JLB : Created.                                                                 *
    134  *=============================================================================================*/
    135 Buffer::Buffer(Buffer const & buffer) :
    136 	IsAllocated(false)
    137 {
    138 	BufferPtr = buffer.BufferPtr;
    139 	Size = buffer.Size;
    140 }
    141 
    142 
    143 /***********************************************************************************************
    144  * Buffer::operator = -- Assignment operator for the buffer object.                            *
    145  *                                                                                             *
    146  *    This will make a duplicate of the buffer object specified. Any buffer pointed to by the  *
    147  *    left hand buffer will be lost (possibley freed as a result).                             *
    148  *                                                                                             *
    149  * INPUT:   buffer   -- Reference to the right hand buffer object.                             *
    150  *                                                                                             *
    151  * OUTPUT:  Returns with a reference to the copied buffer object.                              *
    152  *                                                                                             *
    153  * WARNINGS:   none                                                                            *
    154  *                                                                                             *
    155  * HISTORY:                                                                                    *
    156  *   08/02/1996 JLB : Created.                                                                 *
    157  *=============================================================================================*/
    158 Buffer & Buffer::operator = (Buffer const & buffer)
    159 {
    160 	if (buffer != this) {
    161 		if (IsAllocated) {
    162 			delete [] BufferPtr;
    163 		}
    164 		IsAllocated = false;
    165 		BufferPtr = buffer.BufferPtr;
    166 		Size = buffer.Size;
    167 	}
    168 	return(*this);
    169 }
    170 
    171 
    172 /***********************************************************************************************
    173  * Buffer::~Buffer -- Destructor for buffer object.                                            *
    174  *                                                                                             *
    175  *    This destructor will free any buffer it is responsible for.                              *
    176  *                                                                                             *
    177  * INPUT:   none                                                                               *
    178  *                                                                                             *
    179  * OUTPUT:  none                                                                               *
    180  *                                                                                             *
    181  * WARNINGS:   none                                                                            *
    182  *                                                                                             *
    183  * HISTORY:                                                                                    *
    184  *   07/29/1996 JLB : Created.                                                                 *
    185  *=============================================================================================*/
    186 Buffer::~Buffer(void)
    187 {
    188 	Reset();
    189 }
    190 
    191 
    192 /***********************************************************************************************
    193  * Buffer::Reset -- Clears the buffer object to null state.                                    *
    194  *                                                                                             *
    195  *    This routine will bring the buffer object into a null (newly constructed) state. If      *
    196  *    there was any buffer allocated or referred to by this object, it will be freed or        *
    197  *    dereferenced as necessary.                                                               *
    198  *                                                                                             *
    199  * INPUT:   none                                                                               *
    200  *                                                                                             *
    201  * OUTPUT:  none                                                                               *
    202  *                                                                                             *
    203  * WARNINGS:   This routine will free the buffer if it is responsible for doing so when        *
    204  *             it is no longer referenced.                                                     *
    205  *                                                                                             *
    206  * HISTORY:                                                                                    *
    207  *   09/07/1996 JLB : Created.                                                                 *
    208  *=============================================================================================*/
    209 void Buffer::Reset(void)
    210 {
    211 	if (IsAllocated) {
    212 		delete [] BufferPtr;
    213 	}
    214 	BufferPtr = NULL;
    215 	Size = 0;
    216 	IsAllocated = false;
    217 }