ImfArray.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_ARRAY_H
  6. #define INCLUDED_IMF_ARRAY_H
  7. #include "ImfForward.h"
  8. //-------------------------------------------------------------------------
  9. //
  10. // class Array
  11. // class Array2D
  12. //
  13. // "Arrays of T" whose sizes are not known at compile time.
  14. // When an array goes out of scope, its elements are automatically
  15. // deleted.
  16. //
  17. // Usage example:
  18. //
  19. // struct C
  20. // {
  21. // C () {std::cout << "C::C (" << this << ")\n";};
  22. // virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
  23. // };
  24. //
  25. // int
  26. // main ()
  27. // {
  28. // Array <C> a(3);
  29. //
  30. // C &b = a[1];
  31. // const C &c = a[1];
  32. // C *d = a + 2;
  33. // const C *e = a;
  34. //
  35. // return 0;
  36. // }
  37. //
  38. //-------------------------------------------------------------------------
  39. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  40. template <class T>
  41. class IMF_EXPORT_TEMPLATE_TYPE Array
  42. {
  43. public:
  44. //-----------------------------
  45. // Constructors and destructors
  46. //-----------------------------
  47. Array () {_data = 0; _size = 0;}
  48. Array (long size) {_data = new T[size]; _size = size;}
  49. ~Array () {delete [] _data;}
  50. //-----------------------------
  51. // Access to the array elements
  52. //-----------------------------
  53. operator T * () {return _data;}
  54. operator const T * () const {return _data;}
  55. //------------------------------------------------------
  56. // Resize and clear the array (the contents of the array
  57. // are not preserved across the resize operation).
  58. //
  59. // resizeEraseUnsafe() is more memory efficient than
  60. // resizeErase() because it deletes the old memory block
  61. // before allocating a new one, but if allocating the
  62. // new block throws an exception, resizeEraseUnsafe()
  63. // leaves the array in an unusable state.
  64. //
  65. //------------------------------------------------------
  66. void resizeErase (long size);
  67. void resizeEraseUnsafe (long size);
  68. //-------------------------------
  69. // Return the size of this array.
  70. //-------------------------------
  71. long size() const {return _size;}
  72. private:
  73. Array (const Array &) = delete;
  74. Array & operator = (const Array &) = delete;
  75. Array (Array &&) = delete;
  76. Array & operator = (Array &&) = delete;
  77. long _size;
  78. T * _data;
  79. };
  80. template <class T>
  81. class IMF_EXPORT_TEMPLATE_TYPE Array2D
  82. {
  83. public:
  84. //-----------------------------
  85. // Constructors and destructors
  86. //-----------------------------
  87. Array2D (); // empty array, 0 by 0 elements
  88. Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
  89. ~Array2D ();
  90. //-----------------------------
  91. // Access to the array elements
  92. //-----------------------------
  93. T * operator [] (long x);
  94. const T * operator [] (long x) const;
  95. //------------------------------------------------------
  96. // Resize and clear the array (the contents of the array
  97. // are not preserved across the resize operation).
  98. //
  99. // resizeEraseUnsafe() is more memory efficient than
  100. // resizeErase() because it deletes the old memory block
  101. // before allocating a new one, but if allocating the
  102. // new block throws an exception, resizeEraseUnsafe()
  103. // leaves the array in an unusable state.
  104. //
  105. //------------------------------------------------------
  106. void resizeErase (long sizeX, long sizeY);
  107. void resizeEraseUnsafe (long sizeX, long sizeY);
  108. //-------------------------------
  109. // Return the size of this array.
  110. //-------------------------------
  111. long height() const {return _sizeX;}
  112. long width() const {return _sizeY;}
  113. private:
  114. Array2D (const Array2D &) = delete;
  115. Array2D & operator = (const Array2D &) = delete;
  116. Array2D (Array2D &&) = delete;
  117. Array2D & operator = (Array2D &&) = delete;
  118. long _sizeX;
  119. long _sizeY;
  120. T * _data;
  121. };
  122. //---------------
  123. // Implementation
  124. //---------------
  125. template <class T>
  126. inline void
  127. Array<T>::resizeErase (long size)
  128. {
  129. T *tmp = new T[size];
  130. delete [] _data;
  131. _size = size;
  132. _data = tmp;
  133. }
  134. template <class T>
  135. inline void
  136. Array<T>::resizeEraseUnsafe (long size)
  137. {
  138. delete [] _data;
  139. _data = 0;
  140. _size = 0;
  141. _data = new T[size];
  142. _size = size;
  143. }
  144. template <class T>
  145. inline
  146. Array2D<T>::Array2D ():
  147. _sizeX(0), _sizeY (0), _data (0)
  148. {
  149. // empty
  150. }
  151. template <class T>
  152. inline
  153. Array2D<T>::Array2D (long sizeX, long sizeY):
  154. _sizeX (sizeX), _sizeY (sizeY), _data (new T[sizeX * sizeY])
  155. {
  156. // empty
  157. }
  158. template <class T>
  159. inline
  160. Array2D<T>::~Array2D ()
  161. {
  162. delete [] _data;
  163. }
  164. template <class T>
  165. inline T *
  166. Array2D<T>::operator [] (long x)
  167. {
  168. return _data + x * _sizeY;
  169. }
  170. template <class T>
  171. inline const T *
  172. Array2D<T>::operator [] (long x) const
  173. {
  174. return _data + x * _sizeY;
  175. }
  176. template <class T>
  177. inline void
  178. Array2D<T>::resizeErase (long sizeX, long sizeY)
  179. {
  180. T *tmp = new T[sizeX * sizeY];
  181. delete [] _data;
  182. _sizeX = sizeX;
  183. _sizeY = sizeY;
  184. _data = tmp;
  185. }
  186. template <class T>
  187. inline void
  188. Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
  189. {
  190. delete [] _data;
  191. _data = 0;
  192. _sizeX = 0;
  193. _sizeY = 0;
  194. _data = new T[sizeX * sizeY];
  195. _sizeX = sizeX;
  196. _sizeY = sizeY;
  197. }
  198. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  199. #endif