sharebuf.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. ** Command & Conquer Renegade(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 RefCountClass
  48. {
  49. public:
  50. ShareBufferClass(int count);
  51. ShareBufferClass(const ShareBufferClass & that);
  52. ~ShareBufferClass(void);
  53. // Get the internal pointer to the array
  54. // CAUTION! This pointer is not refcounted so only use it in a context
  55. // where you are keeping a reference to the enclosing ShareBufferClass
  56. // to avoid the possibility of a dangling pointer.
  57. T * Get_Array(void) { return Array; }
  58. int Get_Count(void) { return Count; }
  59. // Access to the elements in the array
  60. void Set_Element(int index, const T & thing);
  61. const T & Get_Element(int index) const;
  62. T & Get_Element(int index);
  63. // Clear the memory in this array.
  64. // CAUTION! Be careful calling this if 'T' is a class. You could be wiping out
  65. // virtual function table pointers. Not a good idea to memset 0 over the top of
  66. // an array of objects but useful if you are creating an array of some basic type
  67. // like pointers or ints...
  68. void Clear(void);
  69. protected:
  70. T * Array;
  71. int Count;
  72. // not implemented!
  73. ShareBufferClass & operator = (const ShareBufferClass &);
  74. };
  75. template <class T>
  76. ShareBufferClass<T>::ShareBufferClass(int count) :
  77. Count(count)
  78. {
  79. assert(Count > 0);
  80. Array = new T[Count];
  81. }
  82. template <class T>
  83. ShareBufferClass<T>::ShareBufferClass(const ShareBufferClass<T> & that) :
  84. Count(that.Count)
  85. {
  86. assert(Count > 0);
  87. Array = new T[Count];
  88. for (int i=0; i<Count; i++) {
  89. Array[i] = that.Array[i];
  90. }
  91. }
  92. template <class T>
  93. ShareBufferClass<T>::~ShareBufferClass(void)
  94. {
  95. if (Array) {
  96. delete[] Array;
  97. Array = NULL;
  98. }
  99. }
  100. template<class T>
  101. void ShareBufferClass<T>::Set_Element(int index,const T & thing)
  102. {
  103. assert(index >= 0);
  104. assert(index < Count);
  105. Array[index] = thing;
  106. }
  107. template<class T>
  108. const T& ShareBufferClass<T>::Get_Element(int index) const
  109. {
  110. return Array[index];
  111. }
  112. template<class T>
  113. T& ShareBufferClass<T>::Get_Element(int index)
  114. {
  115. return Array[index];
  116. }
  117. template<class T>
  118. void ShareBufferClass<T>::Clear(void)
  119. {
  120. memset(Array,0,Count * sizeof(T));
  121. }
  122. #endif // SHAREBUF_H