sharebuf.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. /***************************************************************************
  19. *** 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 ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/sharebuf.h $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 3/20/01 1:24p $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef SHAREBUF_H
  39. #define SHAREBUF_H
  40. #include "refcount.h"
  41. /*
  42. ** SharedBufferClass - a templatized class for buffers which are shared
  43. ** between different objects. This is essentially just a C array with a
  44. ** refcounted wrapper (also a count).
  45. */
  46. template <class T>
  47. class ShareBufferClass : public W3DMPO, public RefCountClass
  48. {
  49. W3DMPO_GLUE(ShareBufferClass)
  50. public:
  51. ShareBufferClass(int count, const char* msg);
  52. ShareBufferClass(const ShareBufferClass & that);
  53. ~ShareBufferClass(void);
  54. // Get the internal pointer to the array
  55. // CAUTION! This pointer is not refcounted so only use it in a context
  56. // where you are keeping a reference to the enclosing ShareBufferClass
  57. // to avoid the possibility of a dangling pointer.
  58. T * Get_Array(void) { return Array; }
  59. int Get_Count(void) { return Count; }
  60. // Access to the elements in the array
  61. void Set_Element(int index, const T & thing);
  62. const T & Get_Element(int index) const;
  63. T & Get_Element(int index);
  64. // Clear the memory in this array.
  65. // CAUTION! Be careful calling this if 'T' is a class. You could be wiping out
  66. // virtual function table pointers. Not a good idea to memset 0 over the top of
  67. // an array of objects but useful if you are creating an array of some basic type
  68. // like pointers or ints...
  69. void Clear(void);
  70. protected:
  71. #if (defined(_DEBUG) || defined(_INTERNAL))
  72. const char* Msg;
  73. #endif
  74. T * Array;
  75. int Count;
  76. // not implemented!
  77. ShareBufferClass & operator = (const ShareBufferClass &);
  78. };
  79. template <class T>
  80. ShareBufferClass<T>::ShareBufferClass(int count, const char* msg) :
  81. Count(count)
  82. #if (defined(_DEBUG) || defined(_INTERNAL))
  83. , Msg(msg)
  84. #endif
  85. {
  86. assert(Count > 0);
  87. Array = MSGW3DNEWARRAY(msg) T[Count];
  88. }
  89. template <class T>
  90. ShareBufferClass<T>::ShareBufferClass(const ShareBufferClass<T> & that) :
  91. Count(that.Count)
  92. {
  93. assert(Count > 0);
  94. #if (defined(_DEBUG) || defined(_INTERNAL))
  95. Msg = that.Msg;
  96. #endif
  97. Array = MSGW3DNEWARRAY(Msg) T[Count];
  98. for (int i=0; i<Count; i++) {
  99. Array[i] = that.Array[i];
  100. }
  101. }
  102. template <class T>
  103. ShareBufferClass<T>::~ShareBufferClass(void)
  104. {
  105. if (Array) {
  106. delete[] Array;
  107. Array = NULL;
  108. }
  109. }
  110. template<class T>
  111. void ShareBufferClass<T>::Set_Element(int index,const T & thing)
  112. {
  113. assert(index >= 0);
  114. assert(index < Count);
  115. Array[index] = thing;
  116. }
  117. template<class T>
  118. const T& ShareBufferClass<T>::Get_Element(int index) const
  119. {
  120. return Array[index];
  121. }
  122. template<class T>
  123. T& ShareBufferClass<T>::Get_Element(int index)
  124. {
  125. return Array[index];
  126. }
  127. template<class T>
  128. void ShareBufferClass<T>::Clear(void)
  129. {
  130. memset(Array,0,Count * sizeof(T));
  131. }
  132. #endif // SHAREBUF_H