pointer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "debug.h" // For assert()
  19. #include <cstring>
  20. template <typename T>
  21. class Buffer
  22. {
  23. public:
  24. Buffer()
  25. {
  26. m_size = 0;
  27. data = NULL;
  28. }
  29. Buffer(unsigned int size)
  30. {
  31. m_size = size;
  32. if(size != 0)
  33. data = new T[size];
  34. else
  35. data = NULL;
  36. }
  37. Buffer(const Buffer &buffer)
  38. {
  39. m_size = buffer.m_size;
  40. if(m_size != 0)
  41. {
  42. data = new T[buffer.m_size];
  43. memcpy(data, buffer.data, buffer.m_size);
  44. }
  45. else
  46. data = NULL;
  47. }
  48. Buffer(const T *t, unsigned int size)
  49. {
  50. m_size = size;
  51. if(size != 0)
  52. {
  53. data = new T[size];
  54. memcpy(data, t, size);
  55. }
  56. else
  57. data = NULL;
  58. }
  59. ~Buffer()
  60. {
  61. drop();
  62. }
  63. Buffer& operator=(const Buffer &buffer)
  64. {
  65. if(this == &buffer)
  66. return *this;
  67. drop();
  68. m_size = buffer.m_size;
  69. if(m_size != 0)
  70. {
  71. data = new T[buffer.m_size];
  72. memcpy(data, buffer.data, buffer.m_size);
  73. }
  74. else
  75. data = NULL;
  76. return *this;
  77. }
  78. T & operator[](unsigned int i) const
  79. {
  80. return data[i];
  81. }
  82. T * operator*() const
  83. {
  84. return data;
  85. }
  86. unsigned int getSize() const
  87. {
  88. return m_size;
  89. }
  90. private:
  91. void drop()
  92. {
  93. delete[] data;
  94. }
  95. T *data;
  96. unsigned int m_size;
  97. };
  98. /************************************************
  99. * !!! W A R N I N G !!! *
  100. * *
  101. * This smart pointer class is NOT thread safe. *
  102. * ONLY use in a single-threaded context! *
  103. * *
  104. ************************************************/
  105. template <typename T>
  106. class SharedBuffer
  107. {
  108. public:
  109. SharedBuffer()
  110. {
  111. m_size = 0;
  112. data = NULL;
  113. refcount = new unsigned int;
  114. (*refcount) = 1;
  115. }
  116. SharedBuffer(unsigned int size)
  117. {
  118. m_size = size;
  119. if(m_size != 0)
  120. data = new T[m_size];
  121. else
  122. data = NULL;
  123. refcount = new unsigned int;
  124. memset(data,0,sizeof(T)*m_size);
  125. (*refcount) = 1;
  126. }
  127. SharedBuffer(const SharedBuffer &buffer)
  128. {
  129. m_size = buffer.m_size;
  130. data = buffer.data;
  131. refcount = buffer.refcount;
  132. (*refcount)++;
  133. }
  134. SharedBuffer & operator=(const SharedBuffer & buffer)
  135. {
  136. if(this == &buffer)
  137. return *this;
  138. drop();
  139. m_size = buffer.m_size;
  140. data = buffer.data;
  141. refcount = buffer.refcount;
  142. (*refcount)++;
  143. return *this;
  144. }
  145. /*
  146. Copies whole buffer
  147. */
  148. SharedBuffer(const T *t, unsigned int size)
  149. {
  150. m_size = size;
  151. if(m_size != 0)
  152. {
  153. data = new T[m_size];
  154. memcpy(data, t, m_size);
  155. }
  156. else
  157. data = NULL;
  158. refcount = new unsigned int;
  159. (*refcount) = 1;
  160. }
  161. /*
  162. Copies whole buffer
  163. */
  164. SharedBuffer(const Buffer<T> &buffer)
  165. {
  166. m_size = buffer.getSize();
  167. if (m_size != 0) {
  168. data = new T[m_size];
  169. memcpy(data, *buffer, buffer.getSize());
  170. }
  171. else
  172. data = NULL;
  173. refcount = new unsigned int;
  174. (*refcount) = 1;
  175. }
  176. ~SharedBuffer()
  177. {
  178. drop();
  179. }
  180. T & operator[](unsigned int i) const
  181. {
  182. assert(i < m_size);
  183. return data[i];
  184. }
  185. T * operator*() const
  186. {
  187. return data;
  188. }
  189. unsigned int getSize() const
  190. {
  191. return m_size;
  192. }
  193. operator Buffer<T>() const
  194. {
  195. return Buffer<T>(data, m_size);
  196. }
  197. private:
  198. void drop()
  199. {
  200. assert((*refcount) > 0);
  201. (*refcount)--;
  202. if(*refcount == 0)
  203. {
  204. delete[] data;
  205. delete refcount;
  206. }
  207. }
  208. T *data;
  209. unsigned int m_size;
  210. unsigned int *refcount;
  211. };