| 22 | |
| 23 | template <class T> |
| 24 | class FixedArray2D |
| 25 | { |
| 26 | T * _ptr; |
| 27 | IMATH_NAMESPACE::Vec2<size_t> _length; |
| 28 | IMATH_NAMESPACE::Vec2<size_t> _stride; |
| 29 | size_t _size; //flattened size of the array |
| 30 | |
| 31 | // this handle optionally stores a shared_array to allocated array data |
| 32 | // so that everything is freed properly on exit. |
| 33 | boost::any _handle; |
| 34 | |
| 35 | public: |
| 36 | |
| 37 | FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX = 1) |
| 38 | : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, lengthX), _handle() |
| 39 | { |
| 40 | if (lengthX < 0 || lengthY < 0) |
| 41 | throw std::domain_error("Fixed array 2d lengths must be non-negative"); |
| 42 | if (strideX <= 0) |
| 43 | throw std::domain_error("Fixed array 2d strides must be positive"); |
| 44 | initializeSize(); |
| 45 | //std::cout << "fixed array external construct" << std::endl; |
| 46 | // nothing |
| 47 | } |
| 48 | |
| 49 | FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX, Py_ssize_t strideY) |
| 50 | : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, strideY), _handle() |
| 51 | { |
| 52 | if (lengthX < 0 || lengthY < 0) |
| 53 | throw std::domain_error("Fixed array 2d lengths must be non-negative"); |
| 54 | if (strideX <= 0 || strideY < 0) |
| 55 | throw std::domain_error("Fixed array 2d strides must be positive"); |
| 56 | initializeSize(); |
| 57 | //std::cout << "fixed array external construct" << std::endl; |
| 58 | // nothing |
| 59 | } |
| 60 | |
| 61 | FixedArray2D(T *ptr, Py_ssize_t lengthX, Py_ssize_t lengthY, Py_ssize_t strideX, Py_ssize_t strideY, boost::any handle) |
| 62 | : _ptr(ptr), _length(lengthX, lengthY), _stride(strideX, strideY), _handle(handle) |
| 63 | { |
| 64 | initializeSize(); |
| 65 | //std::cout << "fixed array external construct with handle" << std::endl; |
| 66 | // nothing |
| 67 | } |
| 68 | |
| 69 | explicit FixedArray2D(Py_ssize_t lengthX, Py_ssize_t lengthY) |
| 70 | : _ptr(0), _length(lengthX, lengthY), _stride(1, lengthX), _handle() |
| 71 | { |
| 72 | if (lengthX < 0 || lengthY < 0) |
| 73 | throw std::domain_error("Fixed array 2d lengths must be non-negative"); |
| 74 | initializeSize(); |
| 75 | T tmp = FixedArrayDefaultValue<T>::value(); |
| 76 | boost::shared_array<T> a(new T[_size]); |
| 77 | for (size_t i=0; i<_size; ++i) a[i] = tmp; |
| 78 | _handle = a; |
| 79 | _ptr = a.get(); |
| 80 | } |
| 81 | |