HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_AttributeArray.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * COMMENTS:
7  */
8 
9 #ifndef __PDG_ATTRIBUTE_ARRAY_H__
10 #define __PDG_ATTRIBUTE_ARRAY_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_AttributeData.h"
15 #include "PDG_AttributeTypes.h"
16 
17 #include <UT/UT_Optional.h>
18 #include <UT/UT_StringMap.h>
19 #include <UT/UT_ValArray.h>
20 
21 /**
22  * Base template for array attribute types
23  */
25  typename DataType,
28 {
29 public:
30  static constexpr PDG_AttributeType TypeEnum = AttribType;
31 
33  using Data = DataType;
34  using Array = ArrayType;
37 
40  {
41  }
42 
45  , myData(data)
46  {
47  }
48 
49  ~PDG_AttributeArray() override {}
50 
51  inline bool hasData() const
52  {
53  return (myData.size() > 0);
54  }
55 
56  inline int size() const
57  {
58  return myData.size();
59  }
60 
61  inline bool valid(int component) const
62  {
63  return (component >= 0) && (component < myData.size());
64  }
65 
66  inline bool value(DataType& data, int component) const
67  {
68  int real_index = component;
69 
70  if (real_index < 0)
71  real_index += size();
72 
73  if (!valid(real_index))
74  return false;
75 
76  data = myData[real_index];
77  return true;
78  }
79 
80  inline const DataType& value(int component) const
81  {
82  return myData[component];
83  }
84 
85  inline DataType& value(int component)
86  {
87  return myData[component];
88  }
89 
90  inline void setValue(const ArrayType& data)
91  {
92  myData = data;
93  }
94 
95  inline void setValue(const ArrayType&& data)
96  {
97  myData = data;
98  }
99 
100  inline void setValue(const DataType& value, int component)
101  {
102  expand(value, component);
103  }
104 
105  inline void setValues(const ArrayType& data)
106  {
107  myData.setSizeIfNeeded(data.size());
108  for (exint i = 0; i < data.size(); i++)
109  myData[i] = data[i];
110  }
111 
112  inline const DataType& appendValue(const DataType& value)
113  {
114  myData.append(value);
115  return myData.last();
116  }
117 
118  inline const ArrayType& values() const
119  {
120  return myData;
121  }
122 
123  inline ArrayType& values()
124  {
125  return myData;
126  }
127 
128  inline void clear()
129  {
130  myData.clear();
131  }
132 
133  inline void truncate(int length)
134  {
135  myData.truncate(length);
136  }
137 
138  inline bool concat(const Self* other)
139  {
140  myData.concat(other->myData);
141  return true;
142  }
143 
144  inline bool adjustSize(int offset)
145  {
146  myData.setSize(myData.size() + offset);
147  return true;
148  }
149 
150  inline bool setSize(int size)
151  {
152  myData.setSize(size);
153  return true;
154  }
155 
156  inline bool hash(
158  exint index,
159  UT_WorkBuffer& errors) const override
160  {
161  if (index < 0)
162  hash_value = SYShashRange(myData.begin(), myData.end());
163  else if (index > myData.size())
164  hash_value = 0;
165  else
166  hash_value = SYShash(myData[index]);
167 
168  return true;
169  }
170 
171  inline void swap(Self* other)
172  {
173  myData.swap(other->myData);
174  }
175 
176  inline void copy(const Self* other, PDG_AttributeCopyOp copy_op)
177  {
178  return copy(other->myData, copy_op);
179  }
180 
181  inline void copy(
182  const ArrayType& data,
183  PDG_AttributeCopyOp copy_op)
184  {
185  if (copy_op == PDG_AttributeCopyOp::eAppend)
186  myData.concat(data);
187  else if (copy_op == PDG_AttributeCopyOp::ePrepend)
188  {
189  ArrayType copy(data);
190  copy.concat(myData);
191  myData = copy;
192  }
193  else if (copy_op == PDG_AttributeCopyOp::eUpdate)
194  {
195  myData.setSizeIfNeeded(data.size());
196  for (int i = 0; i < data.size(); i++)
197  myData[i] = data[i];
198  }
199  else
200  myData = data;
201  }
202 
203  bool compare(
204  const PDG_AttributeData* other) const override
205  {
206  return ((const Self*)(other))->myData == myData;
207  }
208 
210  int stride) override
211  {
212  return PDG_AttributeData::merge(myData, merge_op, stride);
213  }
214 
215  bool slice(
216  ArrayType& result,
217  exint start,
218  UT_Optional<exint> end_opt
219  = UT_Optional<exint>(),
220  UT_Optional<exint> step_opt
221  = UT_Optional<exint>()) const
222  {
223  exint len = size();
224 
225  if (start < 0)
226  start += len;
227 
228  exint end = end_opt.value_or(len);
229  if (end < 0)
230  end += len;
231 
232  exint step = step_opt.value_or(1);
233 
234  if (start < 0 || start >= len)
235  return false;
236 
237  if (end < 0)
238  return false;
239 
240  if (step == 0)
241  return false;
242 
243  exint i = start;
244  while (true)
245  {
246  if (i < 0 || i >= len)
247  break;
248 
249  result.append(myData[i]);
250  i += step;
251 
252  if (step < 0 && i <= end)
253  break;
254 
255  if (step >= 0 && i >= end)
256  break;
257  }
258 
259  return true;
260  }
261 
262 protected:
263  void expand(const DataType& value, int component)
264  {
265  if (component < 0)
266  {
267  myData.append(value);
268  }
269  else
270  {
271  myData.setSizeIfNeeded(component+1);
272  myData[component] = value;
273  }
274  }
275 
277  const DataType& default_value,
278  int component)
279  {
280  if (component < 0)
281  {
282  myData.append(default_value);
283  }
284  else if (component >= myData.size())
285  {
286  myData.setSizeIfNeeded(component+1);
287  myData[component] = default_value;
288  }
289  }
290 
291 protected:
293 };
294 
295 #endif
DataType & value(int component)
Append to the existing attribute if it exists and matches.
constexpr size_t SYShash(const SYS_Flicks f)
Definition: SYS_Flicks.h:46
bool hash(SYS_HashType &hash_value, exint index, UT_WorkBuffer &errors) const override
Return a hash value derived from the attribute data.
GLboolean * data
Definition: glcorearb.h:131
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
GLuint start
Definition: glcorearb.h:475
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void setValues(const ArrayType &data)
int64 exint
Definition: SYS_Types.h:125
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
bool slice(ArrayType &result, exint start, UT_Optional< exint > end_opt=UT_Optional< exint >(), UT_Optional< exint > step_opt=UT_Optional< exint >()) const
**But if you need a result
Definition: thread.h:622
void setValue(const DataType &value, int component)
void truncate(int length)
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
const DataType & value(int component) const
void copy(const Self *other, PDG_AttributeCopyOp copy_op)
const ArrayType & values() const
bool concat(const Self *other)
GLintptr offset
Definition: glcorearb.h:665
const DataType & appendValue(const DataType &value)
void setValue(const ArrayType &&data)
void setValue(const ArrayType &data)
virtual PDG_AttributeMergeError merge(PDG_AttributeMergeOp merge_op, int stride)=0
Applies a merge op to the data in this attribute.
PDG_AttributeArray(const ArrayType &data)
GLuint GLuint end
Definition: glcorearb.h:475
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
#define PDG_API_TMPL
Definition: PDG_API.h:24
bool valid(int component) const
void expand(const DataType &value, int component)
PDG_AttributeType
Enumeration of possible attribute types.
~PDG_AttributeArray() override
PDG_AttributeMergeOp
Enumeration of different ways that attributes can be combined.
Prepend to the existing attribute if it exists and matches.
uint8_t ArrayType
Definition: NanoVDB.h:5566
bool adjustSize(int offset)
GLsizeiptr size
Definition: glcorearb.h:664
void expandIfNeeded(const DataType &default_value, int component)
bool setSize(int size)
bool value(DataType &data, int component) const
void swap(Self *other)
GLuint index
Definition: glcorearb.h:786
PDG_AttributeMergeError
Enumerations of errors that can occur when merging attributes.
PDG_AttributeMergeError merge(PDG_AttributeMergeOp merge_op, int stride) override
Applies a merge op to the data in this attribute.
void copy(const ArrayType &data, PDG_AttributeCopyOp copy_op)
size_t hash_value(const CH_ChannelRef &ref)
bool compare(const PDG_AttributeData *other) const override
Compares this data object with another data object.
PDG_AttributeCopyOp
Enumeration of attribute copy operations.
Definition: format.h:1821