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 #include "PDG_File.h"
17 
18 #include <UT/UT_JSONValue.h>
19 #include <UT/UT_JSONValueArray.h>
20 #include <UT/UT_JSONValueMap.h>
21 #include <UT/UT_JSONWriter.h>
22 
23 #include <UT/UT_StringArray.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_StringMap.h>
26 #include <UT/UT_ValArray.h>
27 
29 
30 /**
31  * Base template for array attribute types
32  */
34  typename DataType,
35  typename ArrayType = UT_ValArray<DataType>>
37 {
38 public:
39  static constexpr PDG_AttributeType TypeEnum = AttribType;
40 
42  using Data = DataType;
43  using Array = ArrayType;
46 
49  {
50  }
51 
52  explicit PDG_AttributeArray(const ArrayType& data)
54  , myData(data)
55  {
56  }
57 
58  ~PDG_AttributeArray() override {}
59 
60  inline bool hasData() const
61  {
62  return (myData.size() > 0);
63  }
64 
65  inline int size() const
66  {
67  return myData.size();
68  }
69 
70  inline bool valid(int component) const
71  {
72  return (component >= 0) && (component < myData.size());
73  }
74 
75  inline bool value(DataType& data, int component) const
76  {
77  int real_index = component;
78 
79  if (real_index < 0)
80  real_index += size();
81 
82  if (!valid(real_index))
83  return false;
84 
85  data = myData[real_index];
86  return true;
87  }
88 
89  inline const DataType& value(int component) const
90  {
91  return myData[component];
92  }
93 
94  inline DataType& value(int component)
95  {
96  return myData[component];
97  }
98 
99  inline void setValue(const ArrayType& data)
100  {
101  myData = data;
102  }
103 
104  inline void setValue(const ArrayType&& data)
105  {
106  myData = data;
107  }
108 
109  inline void setValue(const DataType& value, int component)
110  {
111  expand(value, component);
112  }
113 
114  inline const DataType& appendValue(const DataType& value)
115  {
116  myData.append(value);
117  return myData.last();
118  }
119 
120  inline const ArrayType& values() const
121  {
122  return myData;
123  }
124 
125  inline ArrayType& values()
126  {
127  return myData;
128  }
129 
130  inline void clear()
131  {
132  myData.clear();
133  }
134 
135  inline void truncate(int length)
136  {
137  myData.truncate(length);
138  }
139 
140  inline bool concat(const Self* other)
141  {
142  myData.concat(other->myData);
143  return true;
144  }
145 
146  inline bool adjustSize(int offset)
147  {
148  myData.setSize(myData.size() + offset);
149  return true;
150  }
151 
152  inline bool setSize(int size)
153  {
154  myData.setSize(size);
155  return true;
156  }
157 
159  UT_WorkBuffer& errors) const override
160  {
161  hash_value = SYShashRange(myData.begin(), myData.end());
162  return true;
163  }
164 
165  inline void swap(Self* other)
166  {
167  myData.swap(other->myData);
168  }
169 
170  inline void copy(const Self* other)
171  {
172  myData = other->myData;
173  }
174 
175  bool compare(
176  const PDG_AttributeData* other) const override
177  {
178  return ((const Self*)(other))->myData == myData;
179  }
180 
182  int stride) override
183  {
184  return PDG_AttributeData::merge(myData, merge_op, stride);
185  }
186 
187 protected:
188  void expand(const DataType& value, int component)
189  {
190  if (component < 0)
191  {
192  myData.append(value);
193  }
194  else
195  {
196  myData.setSizeIfNeeded(component+1);
197  myData[component] = value;
198  }
199  }
200 
201 protected:
202  ArrayType myData;
203 };
204 
205 /**
206  * Template for array attributes that can serialize themselves directly to
207  * JSON.
208  */
209 template <PDG_AttributeType AttribType,
210  typename DataType,
211  typename ArrayType = UT_ValArray<DataType>,
212  typename JSONType = ArrayType>
214  AttribType,
215  DataType,
216  ArrayType>
217 {
218 public:
221  AttribType, DataType, ArrayType, JSONType>;
222 
223  using Base::Base;
225 
226  int64 getMemoryUsage(bool inclusive) const override
227  {
228  int64 mem = inclusive ? sizeof(*this) : 0;
229  mem += getDataMemory(Base::myData);
230  return mem;
231  }
232 
233  PDG_AttributeData* clone() const override
234  {
235  return new Self(Base::myData);
236  }
237 
238  bool saveData(UT_JSONWriter& writer) const override
239  {
240  return writer.jsonValue(Base::myData);
241  }
242 
243  bool loadData(const UT_JSONValue& value) override
244  {
245  JSONType json_type;
246  if (!value.import(json_type))
247  return false;
248  for (auto&& entry: json_type)
249  Base::myData.append(entry);
250  return true;
251  }
252 
253 private:
254  static int64 getDataMemory(const UT_StringArray& data)
255  {
256  return UTarrayDeepMemoryUsage(data, false);
257  }
258 
259  template <typename T>
260  static int64 getDataMemory(const UT_ValArray<T>& data)
261  {
262  return data.getMemoryUsage(false);
263  }
264 };
265 
266 /**
267  * Simple array attribute type specializations
268  */
271  fpreal,
276  exint,
283 
284 /**
285  * Result data array attribute type
286  */
288  PDG_AttributeType::eFileArray,
289  PDG_File,
290  PDG_File::Array>
291 {
292 public:
294  PDG_File,
296  using Base::Base;
297  using Base::values;
298  using Base::appendValue;
299 
301  {
304  eQueryExpected
305  };
306 
307 public:
308 
309  ~PDG_AttributeFile() override;
310 
311  int64 getMemoryUsage(bool inclusive) const override;
312  PDG_AttributeData* clone() const override;
313  bool saveData(UT_JSONWriter& writer) const override;
314  bool loadData(const UT_JSONValue& value) override;
315 
316  bool clearExpected();
317  void clearMatching(const PDG_File::Set& file_set);
318 
319  const Data& appendValue(const PDG_WorkItem* work_item,
320  const UT_StringHolder& path,
321  const UT_StringHolder& tag,
322  PDG_File::Hash hash,
323  PDG_File::FileType file_type,
324  bool own,
325  bool replace_expected);
326  const Data& appendValue(const Data&,
327  bool replace_expected);
328 
329  void values(Array& values,
330  QueryType query=eQueryReal) const;
331  void valuesForTag(Array& values,
332  const UT_StringHolder& tag,
333  QueryType query=eQueryReal) const;
334  PDG_AttributeCast valueForTag(Data& value,
335  int index,
336  const UT_StringHolder& tag,
337  QueryType query=eQueryReal) const;
338  PDG_AttributeCast valuesForEvaluator(
340  const PDG_AttributeEvaluator& evaluator,
341  QueryType query=eQueryReal) const;
342 };
343 
344 #endif
GLenum query
Definition: glad.h:2772
bool jsonValue(bool value)
Definition: ImfArray.h:47
DataType & value(int component)
GLboolean * data
Definition: glcorearb.h:131
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define PDG_API
Definition: PDG_API.h:23
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
int64 exint
Definition: SYS_Types.h:125
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:657
std::size_t SYS_HashType
Define the type for hash values.
Definition: SYS_Hash.h:19
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
An array of UT_StringHolder values.
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
void setValue(const DataType &value, int component)
void truncate(int length)
const DataType & value(int component) const
void copy(const Self *other)
const ArrayType & values() const
int64 getMemoryUsage(bool inclusive) const override
Returns the memory usage of the attribute and its data.
bool concat(const Self *other)
virtual bool loadData(const UT_JSONValue &)=0
Loads data from a JSON value.
GLintptr offset
Definition: glcorearb.h:665
int64 Hash
The file hash/modtime type.
Definition: PDG_File.h:38
An array of PDG_File values, e.g. File info structs.
const DataType & appendValue(const DataType &value)
Definition: core.h:760
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)
PDG_AttributeCast
Enumeration of attribute cast results.
bool hash(SYS_HashType &hash_value, UT_WorkBuffer &errors) const override
Return a hash value derived from the attribute data.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
#define PDG_API_TMPL
Definition: PDG_API.h:24
virtual bool saveData(UT_JSONWriter &) const =0
Serializes the data to a JSON writer.
bool valid(int component) const
void expand(const DataType &value, int component)
long long int64
Definition: SYS_Types.h:116
~PDG_AttributeArray() override
PDG_AttributeMergeOp
Enumeration of different ways that attributes can be combined.
bool saveData(UT_JSONWriter &writer) const override
Serializes the data to a JSON writer.
exint append()
Definition: UT_Array.h:142
bool adjustSize(int offset)
GLsizeiptr size
Definition: glcorearb.h:664
An array of fpreal values.
PDG_AttributeType
Enumeration of possible attribute types.
SYS_FORCE_INLINE bool import(bool &result) const
Extract a bool (returns false if type is invalid)
Definition: UT_JSONValue.h:225
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
virtual int64 getMemoryUsage(bool inclusive) const =0
Returns the memory usage of the attribute and its data.
fpreal64 fpreal
Definition: SYS_Types.h:277
bool setSize(int size)
bool value(DataType &data, int component) const
void swap(Self *other)
GLuint index
Definition: glcorearb.h:786
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
PDG_AttributeArray< AttribType, DataType, ArrayType > Self
PDG_AttributeMergeError
Enumerations of errors that can occur when merging attributes.
Definition: core.h:1131
PDG_AttributeMergeError merge(PDG_AttributeMergeOp merge_op, int stride) override
Applies a merge op to the data in this attribute.
An array of int values.
size_t hash_value(const CH_ChannelRef &ref)
bool compare(const PDG_AttributeData *other) const override
Compares this data object with another data object.
virtual PDG_AttributeData * clone() const =0
Deep copies this attribute.
Definition: format.h:895
PDG_AttributeData * clone() const override
Deep copies this attribute.
bool loadData(const UT_JSONValue &value) override
Loads data from a JSON value.