ImfArray.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_ARRAY_H
  35. #define INCLUDED_IMF_ARRAY_H
  36. #include "ImfForward.h"
  37. //-------------------------------------------------------------------------
  38. //
  39. // class Array
  40. // class Array2D
  41. //
  42. // "Arrays of T" whose sizes are not known at compile time.
  43. // When an array goes out of scope, its elements are automatically
  44. // deleted.
  45. //
  46. // Usage example:
  47. //
  48. // struct C
  49. // {
  50. // C () {std::cout << "C::C (" << this << ")\n";};
  51. // virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
  52. // };
  53. //
  54. // int
  55. // main ()
  56. // {
  57. // Array <C> a(3);
  58. //
  59. // C &b = a[1];
  60. // const C &c = a[1];
  61. // C *d = a + 2;
  62. // const C *e = a;
  63. //
  64. // return 0;
  65. // }
  66. //
  67. //-------------------------------------------------------------------------
  68. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  69. template <class T>
  70. class Array
  71. {
  72. public:
  73. //-----------------------------
  74. // Constructors and destructors
  75. //-----------------------------
  76. Array () {_data = 0; _size = 0;}
  77. Array (long size) {_data = new T[size]; _size = size;}
  78. ~Array () {delete [] _data;}
  79. //-----------------------------
  80. // Access to the array elements
  81. //-----------------------------
  82. operator T * () {return _data;}
  83. operator const T * () const {return _data;}
  84. //------------------------------------------------------
  85. // Resize and clear the array (the contents of the array
  86. // are not preserved across the resize operation).
  87. //
  88. // resizeEraseUnsafe() is more memory efficient than
  89. // resizeErase() because it deletes the old memory block
  90. // before allocating a new one, but if allocating the
  91. // new block throws an exception, resizeEraseUnsafe()
  92. // leaves the array in an unusable state.
  93. //
  94. //------------------------------------------------------
  95. void resizeErase (long size);
  96. void resizeEraseUnsafe (long size);
  97. //-------------------------------
  98. // Return the size of this array.
  99. //-------------------------------
  100. long size() const {return _size;}
  101. private:
  102. Array (const Array &); // Copying and assignment
  103. Array & operator = (const Array &); // are not implemented
  104. long _size;
  105. T * _data;
  106. };
  107. template <class T>
  108. class Array2D
  109. {
  110. public:
  111. //-----------------------------
  112. // Constructors and destructors
  113. //-----------------------------
  114. Array2D (); // empty array, 0 by 0 elements
  115. Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
  116. ~Array2D ();
  117. //-----------------------------
  118. // Access to the array elements
  119. //-----------------------------
  120. T * operator [] (long x);
  121. const T * operator [] (long x) const;
  122. //------------------------------------------------------
  123. // Resize and clear the array (the contents of the array
  124. // are not preserved across the resize operation).
  125. //
  126. // resizeEraseUnsafe() is more memory efficient than
  127. // resizeErase() because it deletes the old memory block
  128. // before allocating a new one, but if allocating the
  129. // new block throws an exception, resizeEraseUnsafe()
  130. // leaves the array in an unusable state.
  131. //
  132. //------------------------------------------------------
  133. void resizeErase (long sizeX, long sizeY);
  134. void resizeEraseUnsafe (long sizeX, long sizeY);
  135. //-------------------------------
  136. // Return the size of this array.
  137. //-------------------------------
  138. long height() const {return _sizeX;}
  139. long width() const {return _sizeY;}
  140. private:
  141. Array2D (const Array2D &); // Copying and assignment
  142. Array2D & operator = (const Array2D &); // are not implemented
  143. long _sizeX;
  144. long _sizeY;
  145. T * _data;
  146. };
  147. //---------------
  148. // Implementation
  149. //---------------
  150. template <class T>
  151. inline void
  152. Array<T>::resizeErase (long size)
  153. {
  154. T *tmp = new T[size];
  155. delete [] _data;
  156. _size = size;
  157. _data = tmp;
  158. }
  159. template <class T>
  160. inline void
  161. Array<T>::resizeEraseUnsafe (long size)
  162. {
  163. delete [] _data;
  164. _data = 0;
  165. _size = 0;
  166. _data = new T[size];
  167. _size = size;
  168. }
  169. template <class T>
  170. inline
  171. Array2D<T>::Array2D ():
  172. _sizeX(0), _sizeY (0), _data (0)
  173. {
  174. // emtpy
  175. }
  176. template <class T>
  177. inline
  178. Array2D<T>::Array2D (long sizeX, long sizeY):
  179. _sizeX (sizeX), _sizeY (sizeY), _data (new T[sizeX * sizeY])
  180. {
  181. // emtpy
  182. }
  183. template <class T>
  184. inline
  185. Array2D<T>::~Array2D ()
  186. {
  187. delete [] _data;
  188. }
  189. template <class T>
  190. inline T *
  191. Array2D<T>::operator [] (long x)
  192. {
  193. return _data + x * _sizeY;
  194. }
  195. template <class T>
  196. inline const T *
  197. Array2D<T>::operator [] (long x) const
  198. {
  199. return _data + x * _sizeY;
  200. }
  201. template <class T>
  202. inline void
  203. Array2D<T>::resizeErase (long sizeX, long sizeY)
  204. {
  205. T *tmp = new T[sizeX * sizeY];
  206. delete [] _data;
  207. _sizeX = sizeX;
  208. _sizeY = sizeY;
  209. _data = tmp;
  210. }
  211. template <class T>
  212. inline void
  213. Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
  214. {
  215. delete [] _data;
  216. _data = 0;
  217. _sizeX = 0;
  218. _sizeY = 0;
  219. _data = new T[sizeX * sizeY];
  220. _sizeX = sizeX;
  221. _sizeY = sizeY;
  222. }
  223. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  224. #endif