// // SPDX-License-Identifier: BSD-3-Clause // Copyright Contributors to the OpenEXR Project. // // clang-format off #ifndef _PyImathFixedVArray_h_ #define _PyImathFixedVArray_h_ #include #include #include #include "PyImathFixedArray.h" namespace PyImath { template class FixedVArray { // This class (at least for now) holds a std::vector of 'T' types. // This will give us the 'variable' part of the array. Generally, // we will initially support only a very small subset of accessor // methods before the semantics are fully defined. Currently, the // VArray semantics are defined in the 'varraySemantics.txt' file. std::vector * _ptr; size_t _length; size_t _stride; bool _writable; // This handle optionally stores a shared_array to allocated array data // so that everything is freed properly on exit. boost::any _handle; boost::shared_array _indices; // non-NULL if we're a masked reference size_t _unmaskedLength; public: typedef T BaseType; FixedVArray (std::vector* ptr, Py_ssize_t length, Py_ssize_t stride = 1, bool writable = true); FixedVArray (std::vector* ptr, Py_ssize_t length, Py_ssize_t stride, boost::any handle, bool writable = true); FixedVArray (const std::vector* ptr, Py_ssize_t length, Py_ssize_t stride = 1); FixedVArray (const std::vector* ptr, Py_ssize_t length, Py_ssize_t stride, boost::any handle); explicit FixedVArray (Py_ssize_t length); FixedVArray (const T& initialValue, Py_ssize_t length); FixedVArray (FixedVArray& f, const FixedArray& mask); FixedVArray (const FixedArray& size, const T& initialValue); FixedVArray (const FixedVArray& other); const FixedVArray& operator = (const FixedVArray& other); ~FixedVArray(); // ---------------- const boost::any& handle() { return _handle; } Py_ssize_t len() const { return _length; } size_t stride() const { return _stride; } bool writable() const { return _writable; } // This method is mainly here for use in confidence tests, but there may // be other use-cases where a writable array needs to be made read-only. // Note that we do not provide a 'makeWritable' method here, because that // type of operation shouldn't be allowed. void makeReadOnly() { _writable = false; } bool isMaskedReference() const { return _indices.get() != 0; } size_t unmaskedLength() const { return _unmaskedLength; } std::vector& operator [] (size_t i); const std::vector& operator [] (size_t i) const; // ---------------- FixedArray getitem (Py_ssize_t index); FixedVArray getslice (PyObject* index) const; FixedVArray getslice_mask (const FixedArray& mask); void setitem_scalar (PyObject* index, const FixedArray& data); void setitem_scalar_mask (const FixedArray& mask, const FixedArray& data); void setitem_vector (PyObject* index, const FixedVArray& data); void setitem_vector_mask (const FixedArray& mask, const FixedVArray& data); struct SizeHelper { SizeHelper(FixedVArray &a) : _a(a) {} int getitem(Py_ssize_t index) const; FixedArray getitem_slice(PyObject* index) const; FixedArray getitem_mask(const FixedArray& mask) const; void setitem_scalar (PyObject* index, size_t size); void setitem_scalar_mask (const FixedArray& mask, size_t size); void setitem_vector (PyObject* index, const FixedArray& size); void setitem_vector_mask (const FixedArray& mask, const FixedArray& size); private: FixedVArray &_a; }; boost::shared_ptr getSizeHelper(); friend struct SizeHelper; // ---------------- static boost::python::class_ > register_(const char* doc); // Instantiations of fixed variable arrays must implement this static member. static const char* name(); template size_t match_dimension (const FixedArray& mask, bool strictComparison = true) const { if (len() == mask.len()) { return len(); } bool throwExc = false; if (strictComparison) { throwExc = true; } else if (_indices) { if (_unmaskedLength != (size_t) mask.len()) { throwExc = true; } } else { throwExc = true; } if (throwExc) { throw std::invalid_argument("Dimensions of source do not match destination"); } return len(); } size_t match_dimension (const FixedVArray& other, bool strictComparison = true) const { if (len() == other.len()) { return len(); } bool throwExc = false; if (strictComparison) { throwExc = true; } else if (_indices) { if (_unmaskedLength != (size_t) other.len()) { throwExc = true; } } else { throwExc = true; } if (throwExc) { throw std::invalid_argument("Dimensions of source do not match destination"); } return len(); } protected: size_t raw_ptr_index (size_t i) const; }; } // namespace PyImath #endif // _PyImathFixedVArray_h_