BUFF.CPP 13 KB

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