Path: blob/master/3rdparty/openexr/IlmImf/ImfArray.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMF_ARRAY_H37#define INCLUDED_IMF_ARRAY_H3839//-------------------------------------------------------------------------40//41// class Array42// class Array2D43//44// "Arrays of T" whose sizes are not known at compile time.45// When an array goes out of scope, its elements are automatically46// deleted.47//48// Usage example:49//50// struct C51// {52// C () {std::cout << "C::C (" << this << ")\n";};53// virtual ~C () {std::cout << "C::~C (" << this << ")\n";};54// };55//56// int57// main ()58// {59// Array <C> a(3);60//61// C &b = a[1];62// const C &c = a[1];63// C *d = a + 2;64// const C *e = a;65//66// return 0;67// }68//69//-------------------------------------------------------------------------7071namespace Imf {727374template <class T>75class Array76{77public:7879//-----------------------------80// Constructors and destructors81//-----------------------------8283Array () {_data = 0;}84Array (long size) {_data = new T[size];}85~Array () {delete [] _data;}868788//-----------------------------89// Access to the array elements90//-----------------------------9192operator T * () {return _data;}93operator const T * () const {return _data;}949596//------------------------------------------------------97// Resize and clear the array (the contents of the array98// are not preserved across the resize operation).99//100// resizeEraseUnsafe() is more memory efficient than101// resizeErase() because it deletes the old memory block102// before allocating a new one, but if allocating the103// new block throws an exception, resizeEraseUnsafe()104// leaves the array in an unusable state.105//106//------------------------------------------------------107108void resizeErase (long size);109void resizeEraseUnsafe (long size);110111112private:113114Array (const Array &); // Copying and assignment115Array & operator = (const Array &); // are not implemented116117T * _data;118};119120121template <class T>122class Array2D123{124public:125126//-----------------------------127// Constructors and destructors128//-----------------------------129130Array2D (); // empty array, 0 by 0 elements131Array2D (long sizeX, long sizeY); // sizeX by sizeY elements132~Array2D ();133134135//-----------------------------136// Access to the array elements137//-----------------------------138139T * operator [] (long x);140const T * operator [] (long x) const;141142143//------------------------------------------------------144// Resize and clear the array (the contents of the array145// are not preserved across the resize operation).146//147// resizeEraseUnsafe() is more memory efficient than148// resizeErase() because it deletes the old memory block149// before allocating a new one, but if allocating the150// new block throws an exception, resizeEraseUnsafe()151// leaves the array in an unusable state.152//153//------------------------------------------------------154155void resizeErase (long sizeX, long sizeY);156void resizeEraseUnsafe (long sizeX, long sizeY);157158159private:160161Array2D (const Array2D &); // Copying and assignment162Array2D & operator = (const Array2D &); // are not implemented163164long _sizeY;165T * _data;166};167168169//---------------170// Implementation171//---------------172173template <class T>174inline void175Array<T>::resizeErase (long size)176{177T *tmp = new T[size];178delete [] _data;179_data = tmp;180}181182183template <class T>184inline void185Array<T>::resizeEraseUnsafe (long size)186{187delete [] _data;188_data = 0;189_data = new T[size];190}191192193template <class T>194inline195Array2D<T>::Array2D ():196_sizeY (0), _data (0)197{198// emtpy199}200201202template <class T>203inline204Array2D<T>::Array2D (long sizeX, long sizeY):205_sizeY (sizeY), _data (new T[sizeX * sizeY])206{207// emtpy208}209210211template <class T>212inline213Array2D<T>::~Array2D ()214{215delete [] _data;216}217218219template <class T>220inline T *221Array2D<T>::operator [] (long x)222{223return _data + x * _sizeY;224}225226227template <class T>228inline const T *229Array2D<T>::operator [] (long x) const230{231return _data + x * _sizeY;232}233234235template <class T>236inline void237Array2D<T>::resizeErase (long sizeX, long sizeY)238{239T *tmp = new T[sizeX * sizeY];240delete [] _data;241_sizeY = sizeY;242_data = tmp;243}244245246template <class T>247inline void248Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)249{250delete [] _data;251_data = 0;252_sizeY = 0;253_data = new T[sizeX * sizeY];254_sizeY = sizeY;255}256257258} // namespace Imf259260#endif261262263