/src/FreeImage/Source/OpenEXR/IlmImf/ImfArray.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 261 lines · 103 code · 55 blank · 103 comment · 0 complexity · d2de1c18b53de1b0a584c71404a53876 MD5 · raw file

  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. //-------------------------------------------------------------------------
  37. //
  38. // class Array
  39. // class Array2D
  40. //
  41. // "Arrays of T" whose sizes are not known at compile time.
  42. // When an array goes out of scope, its elements are automatically
  43. // deleted.
  44. //
  45. // Usage example:
  46. //
  47. // struct C
  48. // {
  49. // C () {std::cout << "C::C (" << this << ")\n";};
  50. // virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
  51. // };
  52. //
  53. // int
  54. // main ()
  55. // {
  56. // Array <C> a(3);
  57. //
  58. // C &b = a[1];
  59. // const C &c = a[1];
  60. // C *d = a + 2;
  61. // const C *e = a;
  62. //
  63. // return 0;
  64. // }
  65. //
  66. //-------------------------------------------------------------------------
  67. namespace Imf {
  68. template <class T>
  69. class Array
  70. {
  71. public:
  72. //-----------------------------
  73. // Constructors and destructors
  74. //-----------------------------
  75. Array () {_data = 0;}
  76. Array (long size) {_data = new T[size];}
  77. ~Array () {delete [] _data;}
  78. //-----------------------------
  79. // Access to the array elements
  80. //-----------------------------
  81. operator T * () {return _data;}
  82. operator const T * () const {return _data;}
  83. //------------------------------------------------------
  84. // Resize and clear the array (the contents of the array
  85. // are not preserved across the resize operation).
  86. //
  87. // resizeEraseUnsafe() is more memory efficient than
  88. // resizeErase() because it deletes the old memory block
  89. // before allocating a new one, but if allocating the
  90. // new block throws an exception, resizeEraseUnsafe()
  91. // leaves the array in an unusable state.
  92. //
  93. //------------------------------------------------------
  94. void resizeErase (long size);
  95. void resizeEraseUnsafe (long size);
  96. private:
  97. Array (const Array &); // Copying and assignment
  98. Array & operator = (const Array &); // are not implemented
  99. T * _data;
  100. };
  101. template <class T>
  102. class Array2D
  103. {
  104. public:
  105. //-----------------------------
  106. // Constructors and destructors
  107. //-----------------------------
  108. Array2D (); // empty array, 0 by 0 elements
  109. Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
  110. ~Array2D ();
  111. //-----------------------------
  112. // Access to the array elements
  113. //-----------------------------
  114. T * operator [] (long x);
  115. const T * operator [] (long x) const;
  116. //------------------------------------------------------
  117. // Resize and clear the array (the contents of the array
  118. // are not preserved across the resize operation).
  119. //
  120. // resizeEraseUnsafe() is more memory efficient than
  121. // resizeErase() because it deletes the old memory block
  122. // before allocating a new one, but if allocating the
  123. // new block throws an exception, resizeEraseUnsafe()
  124. // leaves the array in an unusable state.
  125. //
  126. //------------------------------------------------------
  127. void resizeErase (long sizeX, long sizeY);
  128. void resizeEraseUnsafe (long sizeX, long sizeY);
  129. private:
  130. Array2D (const Array2D &); // Copying and assignment
  131. Array2D & operator = (const Array2D &); // are not implemented
  132. long _sizeY;
  133. T * _data;
  134. };
  135. //---------------
  136. // Implementation
  137. //---------------
  138. template <class T>
  139. inline void
  140. Array<T>::resizeErase (long size)
  141. {
  142. T *tmp = new T[size];
  143. delete [] _data;
  144. _data = tmp;
  145. }
  146. template <class T>
  147. inline void
  148. Array<T>::resizeEraseUnsafe (long size)
  149. {
  150. delete [] _data;
  151. _data = 0;
  152. _data = new T[size];
  153. }
  154. template <class T>
  155. inline
  156. Array2D<T>::Array2D ():
  157. _sizeY (0), _data (0)
  158. {
  159. // emtpy
  160. }
  161. template <class T>
  162. inline
  163. Array2D<T>::Array2D (long sizeX, long sizeY):
  164. _sizeY (sizeY), _data (new T[sizeX * sizeY])
  165. {
  166. // emtpy
  167. }
  168. template <class T>
  169. inline
  170. Array2D<T>::~Array2D ()
  171. {
  172. delete [] _data;
  173. }
  174. template <class T>
  175. inline T *
  176. Array2D<T>::operator [] (long x)
  177. {
  178. return _data + x * _sizeY;
  179. }
  180. template <class T>
  181. inline const T *
  182. Array2D<T>::operator [] (long x) const
  183. {
  184. return _data + x * _sizeY;
  185. }
  186. template <class T>
  187. inline void
  188. Array2D<T>::resizeErase (long sizeX, long sizeY)
  189. {
  190. T *tmp = new T[sizeX * sizeY];
  191. delete [] _data;
  192. _sizeY = sizeY;
  193. _data = tmp;
  194. }
  195. template <class T>
  196. inline void
  197. Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
  198. {
  199. delete [] _data;
  200. _data = 0;
  201. _sizeY = 0;
  202. _data = new T[sizeX * sizeY];
  203. _sizeY = sizeY;
  204. }
  205. } // namespace Imf
  206. #endif