HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyImathFixedVArray.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 // clang-format off
7 
8 #ifndef _PyImathFixedVArray_h_
9 #define _PyImathFixedVArray_h_
10 
11 #define HBOOST_BIND_GLOBAL_PLACEHOLDERS
12 #include <hboost/python.hpp>
13 #include <hboost/any.hpp>
14 #include <vector>
15 #include "PyImathFixedArray.h"
16 
17 namespace PyImath {
18 
19 template <class T>
21 {
22  // This class (at least for now) holds a std::vector of 'T' types.
23  // This will give us the 'variable' part of the array. Generally,
24  // we will initially support only a very small subset of accessor
25  // methods before the semantics are fully defined. Currently, the
26  // VArray semantics are defined in the 'varraySemantics.txt' file.
27 
28  std::vector<T> * _ptr;
29  size_t _length;
30  size_t _stride;
31  bool _writable;
32 
33  // This handle optionally stores a shared_array to allocated array data
34  // so that everything is freed properly on exit.
35  hboost::any _handle;
36 
37  hboost::shared_array<size_t> _indices; // non-NULL if we're a masked reference
38  size_t _unmaskedLength;
39 
40  public:
41  typedef T BaseType;
42 
43  FixedVArray (std::vector<T>* ptr, Py_ssize_t length,
44  Py_ssize_t stride = 1, bool writable = true);
45 
46  FixedVArray (std::vector<T>* ptr, Py_ssize_t length,
47  Py_ssize_t stride, hboost::any handle, bool writable = true);
48 
49  FixedVArray (const std::vector<T>* ptr, Py_ssize_t length,
50  Py_ssize_t stride = 1);
51 
52  FixedVArray (const std::vector<T>* ptr, Py_ssize_t length,
53  Py_ssize_t stride, hboost::any handle);
54 
55  explicit FixedVArray (Py_ssize_t length);
56 
57  FixedVArray (const T& initialValue, Py_ssize_t length);
58 
60 
61  FixedVArray (const FixedArray<int>& size, const T& initialValue);
62 
63  FixedVArray (const FixedVArray<T>& other);
64 
65  const FixedVArray& operator = (const FixedVArray<T>& other);
66 
67  ~FixedVArray();
68 
69  // ----------------
70 
71  const hboost::any& handle() { return _handle; }
72 
73  Py_ssize_t len() const { return _length; }
74  size_t stride() const { return _stride; }
75  bool writable() const { return _writable; }
76 
77  // This method is mainly here for use in confidence tests, but there may
78  // be other use-cases where a writable array needs to be made read-only.
79  // Note that we do not provide a 'makeWritable' method here, because that
80  // type of operation shouldn't be allowed.
81  void makeReadOnly() { _writable = false; }
82 
83  bool isMaskedReference() const { return _indices.get() != 0; }
84  size_t unmaskedLength() const { return _unmaskedLength; }
85 
86  std::vector<T>& operator [] (size_t i);
87  const std::vector<T>& operator [] (size_t i) const;
88 
89  // ----------------
90 
91  FixedArray<T> getitem (Py_ssize_t index);
92  FixedVArray<T> getslice (PyObject* index) const;
94 
95  void setitem_scalar (PyObject* index, const FixedArray<T>& data);
97  void setitem_vector (PyObject* index, const FixedVArray<T>& data);
99 
100  struct SizeHelper
101  {
102  SizeHelper(FixedVArray &a) : _a(a) {}
103 
104  int getitem(Py_ssize_t index) const;
105  FixedArray<int> getitem_slice(PyObject* index) const;
107 
108  void setitem_scalar (PyObject* index, size_t size);
109  void setitem_scalar_mask (const FixedArray<int>& mask, size_t size);
110  void setitem_vector (PyObject* index, const FixedArray<int>& size);
112 
113  private:
114 
115  FixedVArray &_a;
116  };
117 
118  hboost::shared_ptr<SizeHelper> getSizeHelper();
119 
120  friend struct SizeHelper;
121 
122  // ----------------
123 
124  static hboost::python::class_<FixedVArray<T> > register_(const char* doc);
125 
126  // Instantiations of fixed variable arrays must implement this static member.
127  static const char* name();
128 
129  template <class T2>
131  bool strictComparison = true) const
132  {
133  if (len() == mask.len())
134  {
135  return len();
136  }
137 
138  bool throwExc = false;
139  if (strictComparison)
140  {
141  throwExc = true;
142  }
143  else if (_indices)
144  {
145  if (_unmaskedLength != (size_t) mask.len())
146  {
147  throwExc = true;
148  }
149  }
150  else
151  {
152  throwExc = true;
153  }
154 
155  if (throwExc)
156  {
157  throw std::invalid_argument("Dimensions of source do not match destination");
158  }
159 
160  return len();
161  }
162 
163  size_t match_dimension (const FixedVArray<T>& other,
164  bool strictComparison = true) const
165  {
166  if (len() == other.len())
167  {
168  return len();
169  }
170 
171  bool throwExc = false;
172  if (strictComparison)
173  {
174  throwExc = true;
175  }
176  else if (_indices)
177  {
178  if (_unmaskedLength != (size_t) other.len())
179  {
180  throwExc = true;
181  }
182  }
183  else
184  {
185  throwExc = true;
186  }
187 
188  if (throwExc)
189  {
190  throw std::invalid_argument("Dimensions of source do not match destination");
191  }
192 
193  return len();
194  }
195 
196  protected:
197  size_t raw_ptr_index (size_t i) const;
198 
199 };
200 
201 } // namespace PyImath
202 
203 #endif // _PyImathFixedVArray_h_
Py_ssize_t len() const
const FixedVArray & operator=(const FixedVArray< T > &other)
FixedArray< int > getitem_mask(const FixedArray< int > &mask) const
size_t stride() const
FixedVArray< T > getslice(PyObject *index) const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
size_t match_dimension(const FixedArray< T2 > &mask, bool strictComparison=true) const
GLfloat f
Definition: glcorearb.h:1926
static hboost::python::class_< FixedVArray< T > > register_(const char *doc)
void setitem_scalar_mask(const FixedArray< int > &mask, size_t size)
bool any(const vbool4 &v)
Definition: simd.h:3600
const hboost::any & handle()
void setitem_vector_mask(const FixedArray< int > &mask, const FixedArray< int > &size)
FixedArray< T > getitem(Py_ssize_t index)
FixedArray< int > getitem_slice(PyObject *index) const
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
GLint GLuint mask
Definition: glcorearb.h:124
Py_ssize_t len() const
static const char * name()
bool isMaskedReference() const
GLsizeiptr size
Definition: glcorearb.h:664
void setitem_scalar(PyObject *index, const FixedArray< T > &data)
size_t raw_ptr_index(size_t i) const
std::vector< T > & operator[](size_t i)
GLuint index
Definition: glcorearb.h:786
auto ptr(T p) -> const void *
Definition: format.h:4331
void setitem_scalar_mask(const FixedArray< int > &mask, const FixedArray< T > &data)
void setitem_vector_mask(const FixedArray< int > &mask, const FixedVArray< T > &data)
hboost::shared_ptr< SizeHelper > getSizeHelper()
void setitem_vector(PyObject *index, const FixedArray< int > &size)
void setitem_vector(PyObject *index, const FixedVArray< T > &data)
FixedVArray(std::vector< T > *ptr, Py_ssize_t length, Py_ssize_t stride=1, bool writable=true)
FixedVArray< T > getslice_mask(const FixedArray< int > &mask)
size_t match_dimension(const FixedVArray< T > &other, bool strictComparison=true) const
void setitem_scalar(PyObject *index, size_t size)
int getitem(Py_ssize_t index) const
Definition: format.h:1821
size_t unmaskedLength() const