BUFF.CPP 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/BUFF.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : BUFF.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 07/29/96 *
  30. * *
  31. * Last Update : September 7, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Buffer::Buffer -- Constructor for buffer object. *
  36. * Buffer::Buffer -- Copy constructor for buffer object. *
  37. * Buffer::Buffer -- Self-allocating constructor for buffer object. *
  38. * Buffer::Reset -- Clears the buffer object to null state. *
  39. * Buffer::operator = -- Assignment operator for the buffer object. *
  40. * Buffer::~Buffer -- Destructor for buffer object. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "buff.h"
  43. #include <stddef.h>
  44. /***********************************************************************************************
  45. * Buffer::Buffer -- Constructor for buffer object. *
  46. * *
  47. * This is the normal constructor for a buffer object. The buffer pointer and size are *
  48. * specified as parameters. *
  49. * *
  50. * INPUT: buffer -- Pointer to the buffer. *
  51. * *
  52. * size -- The size of the buffer. *
  53. * *
  54. * OUTPUT: none *
  55. * *
  56. * WARNINGS: It is possible to construct a Buffer object that has a pointer but a size *
  57. * value of zero. The Buffer object can still be used for its pointer, but it *
  58. * any function that requires a size will fail. *
  59. * *
  60. * HISTORY: *
  61. * 07/29/1996 JLB : Created. *
  62. *=============================================================================================*/
  63. Buffer::Buffer(void * buffer, long size) :
  64. BufferPtr(buffer),
  65. Size(size),
  66. IsAllocated(false)
  67. {
  68. }
  69. // Alternate constructor for char * pointer.
  70. Buffer::Buffer(char * buffer, long size) :
  71. BufferPtr(buffer),
  72. Size(size),
  73. IsAllocated(false)
  74. {
  75. }
  76. // Alternate constructor for void const * pointer.
  77. Buffer::Buffer(void const * buffer, long size) :
  78. BufferPtr((void*)buffer),
  79. Size(size),
  80. IsAllocated(false)
  81. {
  82. }
  83. /***********************************************************************************************
  84. * Buffer::Buffer -- Self-allocating constructor for buffer object. *
  85. * *
  86. * This construtor for a buffer object will automatically allocate the bytes necessary *
  87. * to fulfill the size requested. This object is also responsible for deleting the buffer *
  88. * it allocated. *
  89. * *
  90. * INPUT: size -- The size of the buffer to allocated. *
  91. * *
  92. * OUTPUT: none *
  93. * *
  94. * WARNINGS: There is no way to tell if the allocation failed. To verify, call Get_Buffer *
  95. * and compare with NULL. *
  96. * *
  97. * HISTORY: *
  98. * 07/29/1996 JLB : Created. *
  99. *=============================================================================================*/
  100. Buffer::Buffer(long size) :
  101. BufferPtr(NULL),
  102. Size(size),
  103. IsAllocated(false)
  104. {
  105. if (size > 0) {
  106. BufferPtr = new char[size];
  107. IsAllocated = true;
  108. }
  109. }
  110. /***********************************************************************************************
  111. * Buffer::Buffer -- Copy constructor for buffer object. *
  112. * *
  113. * This will make a duplicate of the specified buffer object. The ownership of the pointer *
  114. * remains with the original object. This prevents multiple deletion of the same pointer. *
  115. * *
  116. * INPUT: buffer -- Reference to the buffer object to be dupilcated. *
  117. * *
  118. * OUTPUT: none *
  119. * *
  120. * WARNINGS: none *
  121. * *
  122. * HISTORY: *
  123. * 08/02/1996 JLB : Created. *
  124. *=============================================================================================*/
  125. Buffer::Buffer(Buffer const & buffer) :
  126. IsAllocated(false)
  127. {
  128. BufferPtr = buffer.BufferPtr;
  129. Size = buffer.Size;
  130. }
  131. /***********************************************************************************************
  132. * Buffer::operator = -- Assignment operator for the buffer object. *
  133. * *
  134. * This will make a duplicate of the buffer object specified. Any buffer pointed to by the *
  135. * left hand buffer will be lost (possibley freed as a result). *
  136. * *
  137. * INPUT: buffer -- Reference to the right hand buffer object. *
  138. * *
  139. * OUTPUT: Returns with a reference to the copied buffer object. *
  140. * *
  141. * WARNINGS: none *
  142. * *
  143. * HISTORY: *
  144. * 08/02/1996 JLB : Created. *
  145. *=============================================================================================*/
  146. Buffer & Buffer::operator = (Buffer const & buffer)
  147. {
  148. if (buffer != this) {
  149. if (IsAllocated) {
  150. delete [] BufferPtr;
  151. }
  152. IsAllocated = false;
  153. BufferPtr = buffer.BufferPtr;
  154. Size = buffer.Size;
  155. }
  156. return(*this);
  157. }
  158. /***********************************************************************************************
  159. * Buffer::~Buffer -- Destructor for buffer object. *
  160. * *
  161. * This destructor will free any buffer it is responsible for. *
  162. * *
  163. * INPUT: none *
  164. * *
  165. * OUTPUT: none *
  166. * *
  167. * WARNINGS: none *
  168. * *
  169. * HISTORY: *
  170. * 07/29/1996 JLB : Created. *
  171. *=============================================================================================*/
  172. Buffer::~Buffer(void)
  173. {
  174. Reset();
  175. }
  176. /***********************************************************************************************
  177. * Buffer::Reset -- Clears the buffer object to null state. *
  178. * *
  179. * This routine will bring the buffer object into a null (newly constructed) state. If *
  180. * there was any buffer allocated or referred to by this object, it will be freed or *
  181. * dereferenced as necessary. *
  182. * *
  183. * INPUT: none *
  184. * *
  185. * OUTPUT: none *
  186. * *
  187. * WARNINGS: This routine will free the buffer if it is responsible for doing so when *
  188. * it is no longer referenced. *
  189. * *
  190. * HISTORY: *
  191. * 09/07/1996 JLB : Created. *
  192. *=============================================================================================*/
  193. void Buffer::Reset(void)
  194. {
  195. if (IsAllocated) {
  196. delete [] BufferPtr;
  197. }
  198. BufferPtr = NULL;
  199. Size = 0;
  200. IsAllocated = false;
  201. }