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 
153  UT_WorkBuffer& errors) const override
154  {
155  hash_value = SYShashRange(myData.begin(), myData.end());
156  return true;
157  }
158 
159  inline void swap(Self* other)
160  {
161  myData.swap(other->myData);
162  }
163 
164  inline void copy(const Self* other)
165  {
166  myData = other->myData;
167  }
168 
169  bool compare(
170  const PDG_AttributeData* other) const override
171  {
172  return ((const Self*)(other))->myData == myData;
173  }
174 
176  int stride) override
177  {
178  return PDG_AttributeData::merge(myData, merge_op, stride);
179  }
180 
181 protected:
182  void expand(const DataType& value, int component)
183  {
184  if (component < 0)
185  {
186  myData.append(value);
187  }
188  else
189  {
190  myData.setSizeIfNeeded(component+1);
191  myData[component] = value;
192  }
193  }
194 
195 protected:
196  ArrayType myData;
197 };
198 
199 /**
200  * Template for array attributes that can serialize themselves directly to
201  * JSON.
202  */
203 template <PDG_AttributeType AttribType,
204  typename DataType,
205  typename ArrayType = UT_ValArray<DataType>,
206  typename JSONType = ArrayType>
208  AttribType,
209  DataType,
210  ArrayType>
211 {
212 public:
215  AttribType, DataType, ArrayType, JSONType>;
216 
217  using Base::Base;
219 
220  int64 getMemoryUsage(bool inclusive) const override
221  {
222  int64 mem = inclusive ? sizeof(*this) : 0;
223  mem += getDataMemory(Base::myData);
224  return mem;
225  }
226 
227  PDG_AttributeData* clone() const override
228  {
229  return new Self(Base::myData);
230  }
231 
232  bool saveData(UT_JSONWriter& writer) const override
233  {
234  return writer.jsonValue(Base::myData);
235  }
236 
237  bool loadData(const UT_JSONValue& value) override
238  {
239  JSONType json_type;
240  if (!value.import(json_type))
241  return false;
242  for (auto&& entry: json_type)
243  Base::myData.append(entry);
244  return true;
245  }
246 
247 private:
248  static int64 getDataMemory(const UT_StringArray& data)
249  {
250  return UTarrayDeepMemoryUsage(data, false);
251  }
252 
253  template <typename T>
254  static int64 getDataMemory(const UT_ValArray<T>& data)
255  {
256  return data.getMemoryUsage(false);
257  }
258 };
259 
260 /**
261  * Simple array attribute type specializations
262  */
265  fpreal,
270  exint,
277 
278 /**
279  * Result data array attribute type
280  */
282  PDG_AttributeType::eFileArray,
283  PDG_File,
284  PDG_File::Array>
285 {
286 public:
288  PDG_File,
290  using Base::Base;
291  using Base::values;
292  using Base::appendValue;
293 
295  {
298  eQueryExpected
299  };
300 
301 public:
302 
303  ~PDG_AttributeFile() override;
304 
305  int64 getMemoryUsage(bool inclusive) const override;
306  PDG_AttributeData* clone() const override;
307  bool saveData(UT_JSONWriter& writer) const override;
308  bool loadData(const UT_JSONValue& value) override;
309 
310  bool clearExpected();
311  void clearMatching(const PDG_File::Set& file_set);
312 
313  const Data& appendValue(const PDG_WorkItem* work_item,
314  const UT_StringHolder& path,
315  const UT_StringHolder& tag,
316  PDG_File::Hash hash,
317  PDG_File::FileType file_type,
318  bool own,
319  bool replace_expected);
320  const Data& appendValue(const Data&,
321  bool replace_expected);
322 
323  void values(Array& values,
324  QueryType query=eQueryReal) const;
325  void valuesForTag(Array& values,
326  const UT_StringHolder& tag,
327  QueryType query=eQueryReal) const;
328  PDG_AttributeCast valueForTag(Data& value,
329  int index,
330  const UT_StringHolder& tag,
331  QueryType query=eQueryReal) const;
332  PDG_AttributeCast valuesForEvaluator(
334  const PDG_AttributeEvaluator& evaluator,
335  QueryType query=eQueryReal) const;
336 };
337 
338 #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 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.