HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_OptionEntry.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  * NAME: UT_OptionEntry.h (C++, Utility Library)
7  *
8  * COMMENTS: Individual options held within a UT_Options class.
9  *
10  */
11 
12 #ifndef _UT_OPTIONENTRY_H_
13 #define _UT_OPTIONENTRY_H_
14 
15 #include "UT_API.h"
16 #include "UT_SmallObject.h"
17 #include "UT_Assert.h"
18 #include "UT_IntArray.h"
19 #include "UT_StringArray.h"
20 #include "UT_StringHolder.h"
21 #include "UT_VectorTypes.h"
22 #include "UT_UniquePtr.h"
23 #include "UT_Options.h"
24 
25 class UT_JSONValue;
26 class UT_JSONValueMap;
27 class UT_JSONWriter;
28 
29 class UT_OptionEntry;
31 
32 // The base class for data held by the UT_Options.
33 class UT_API UT_OptionEntry : public UT_SmallObject<UT_OptionEntry,
34  UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
35  UT_SMALLOBJECT_PAGESIZE_DEFAULT,
36  UT_SMALLOBJECT_THREADSAFE_ON>
37 {
38 public:
39  UT_OptionEntry() = default;
40  virtual ~UT_OptionEntry() = default;
41 
42  UT_OptionEntry(const UT_OptionEntry &) = delete;
43  UT_OptionEntry &operator=(const UT_OptionEntry &) = delete;
44 
45  virtual UT_OptionType getType() const
46  { return UT_OPTION_INVALID; }
47  virtual UT_OptionEntryPtr clone() const
48  {
49  return UTmakeUnique<UT_OptionEntry>();
50  }
51  virtual int64 getMemoryUsage(bool inclusive) const
52  { return inclusive ? sizeof(*this) : 0; }
53 
54  /// Hash a single option entry.
55  unsigned hash() const;
56 
57  /// Compute equivalence for a given option entry
58  unsigned isEqual(const UT_OptionEntry &cmp,
59  fpreal64 tolerance = 0.0) const;
60 
61  bool getOptionString(UT_OptionFormat format,
62  UT_WorkBuffer &result) const;
63  bool getOptionString(UT_OptionFormat format,
64  UT_String &result) const;
65 
67  {
68  UT_OPTION_STORE_INVALID=-1,
74  };
75 
76  /// Return the underlying storage type for the option
78  {
79  switch (t)
80  {
81  case UT_OPTION_INT:
82  case UT_OPTION_INTARRAY:
83  return UT_OPTION_STORE_INT;
84 
85  case UT_OPTION_FPREAL:
86  case UT_OPTION_VECTOR2:
87  case UT_OPTION_VECTOR3:
88  case UT_OPTION_VECTOR4:
90  case UT_OPTION_MATRIX3:
91  case UT_OPTION_MATRIX4:
92  case UT_OPTION_UV:
93  case UT_OPTION_UVW:
95  case UT_OPTION_MATRIX2:
96  return UT_OPTION_STORE_REAL;
97 
98  case UT_OPTION_STRING:
101  return UT_OPTION_STORE_STRING;
102 
103  case UT_OPTION_DICT:
104  case UT_OPTION_DICTARRAY:
105  return UT_OPTION_STORE_DICT;
106 
107  case UT_OPTION_BOOL:
108  return UT_OPTION_STORE_BOOL;
109 
110  case UT_OPTION_INVALID:
111  case UT_OPTION_NUM_TYPES:
112  return UT_OPTION_STORE_INVALID;
113  }
114  UT_ASSERT(0);
115  return UT_OPTION_STORE_INVALID;
116  }
117  /// Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
119  {
120  switch (t)
121  {
122  case UT_OPTION_INT:
123  case UT_OPTION_FPREAL:
124  case UT_OPTION_STRING:
125  case UT_OPTION_STRINGRAW:
126  case UT_OPTION_DICT:
127  case UT_OPTION_BOOL:
128  return 1;
129 
130  case UT_OPTION_VECTOR2:
131  case UT_OPTION_UV:
132  return 2;
133 
134  case UT_OPTION_VECTOR3:
135  case UT_OPTION_UVW:
136  return 3;
137 
138  case UT_OPTION_VECTOR4:
139  case UT_OPTION_MATRIX2:
141  return 4;
142  case UT_OPTION_MATRIX3:
143  return 9;
144  case UT_OPTION_MATRIX4:
145  return 16;
146 
147  case UT_OPTION_INTARRAY:
150  case UT_OPTION_DICTARRAY:
151  return 0;
152 
153  case UT_OPTION_INVALID:
154  case UT_OPTION_NUM_TYPES:
155  return -1;
156  }
157  UT_ASSERT(0);
158  return -1;
159  }
161  { return optionStorage(getType()); }
163  { return optionSize(getType()); }
164 
165  bool importOption(int64 &value) const;
166  bool importOption(bool &value) const;
167  bool importOption(fpreal64 &value) const;
168  bool importOption(UT_StringHolder &value) const;
169  bool importOption(UT_OptionsHolder &value) const;
170  bool importOption(UT_Vector2D &value) const;
171  bool importOption(UT_Vector3D &value) const;
172  bool importOption(UT_Vector4D &value) const;
173  bool importOption(UT_QuaternionD &value) const;
174  bool importOption(UT_Matrix2D &value) const;
175  bool importOption(UT_Matrix3D &value) const;
176  bool importOption(UT_Matrix4D &value) const;
177 
178  // Generic import which handles arbitrary length int/float/string data
179  bool importOption(UT_Array<int32> &value) const;
180  bool importOption(UT_Array<int64> &value) const;
181  bool importOption(UT_Array<fpreal32> &value) const;
182  bool importOption(UT_Array<fpreal64> &value) const;
183  bool importOption(UT_StringArray &value) const;
184  bool importOption(UT_Array<UT_StringHolder> &value) const;
185  bool importOption(UT_Array<UT_OptionsHolder> &value) const;
186 
187  // Returns the values directly, or a default value if the data is an
188  // inappropriate type.
189  int64 getOptionI() const;
190  bool getOptionB() const;
191  fpreal64 getOptionF() const;
192  const UT_Vector2D &getOptionV2() const;
193  const UT_Vector3D &getOptionV3() const;
194  const UT_Vector4D &getOptionV4() const;
195  const UT_QuaternionD &getOptionQ() const;
196  const UT_Matrix2D &getOptionM2() const;
197  const UT_Matrix3D &getOptionM3() const;
198  const UT_Matrix4D &getOptionM4() const;
199  const UT_Vector2D &getOptionUV() const;
200  const UT_Vector3D &getOptionUVW() const;
201 
202  // Note that, if the string value contains binary data with null
203  // characters, the UT_String version of getOptionS will not be able to
204  // tell you the length of the data, since UT_String doesn't store the
205  // length.
206  UT_StringHolder getOptionS() const;
207  const UT_OptionsHolder &getOptionDict() const;
208  const UT_Int64Array &getOptionIArray() const;
209  const UT_Fpreal64Array &getOptionFArray() const;
210  const UT_StringArray &getOptionSArray() const;
211  const UT_Array<UT_OptionsHolder> &getOptionDictArray() const;
212 
213  /// Save a single option to a JSON stream. This is saved with the schema
214  /// @code
215  /// {
216  /// "name":"UT_OptionEntry",
217  /// "type":"object",
218  /// "properties": {
219  /// "type":{
220  /// "type":"string",
221  /// "enum":{ UT_Options::getStringFromOptionType() },
222  /// },
223  /// "value":{
224  /// "type":["bool", "integer", "float", "string", "array"],
225  /// "items":{"type":"float"},
226  /// "description":"Basic type or an array.
227  /// Arrays must be 2,3,4,9 or 16 floats.
228  /// Data should match given type",
229  /// },
230  /// },
231  /// }
232  /// @endcode
233  bool saveJSON(UT_JSONWriter &w, bool compact) const;
234  /// As above, except save to a UT_JSONValue instead of requiring the value
235  /// go directly to a stream.
236  void saveJSON(UT_JSONValue &v, bool compact) const;
237 
238  /// Create a UT_OptionEntry from a simple JSON value. This is not a
239  /// dictionary with type information. It is a single value which may be
240  /// an int, float, string, etc. The UT_OptionEntry type is inferred from
241  /// the JSON type.
242  /// If allow_type, then if a value contains "type" and "value" it is parsed
243  /// as a TypedMap. Otherwise, it will always be a option.
244  /// If allow_dict, then nested maps will become dictionaries. If both
245  /// allow_type and allow_dict, things that look like types will be
246  /// treated as types and then fall back to dictionaries.
247  static UT_OptionEntryPtr createEntry(const UT_JSONValue &v, bool allow_type = true, bool allow_dict = false);
248 
249  /// Create a UT_OptionEntry from a JSON map. This is a dictionary
250  /// with type information.
251  static UT_OptionEntryPtr createEntryFromTypedMap(const UT_JSONValueMap &map,
252  UT_String &error_str);
253 
254  /// Convert string to and from UT_OptionType values.
255  static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
256  static const char *getStringFromOptionType(UT_OptionType optiontype);
257 };
258 
259 UT_API size_t format(char *buffer, size_t bufsize, const UT_OptionEntry &o);
260 
261 
262 // A templatized version of the class above for holding specific data types.
263 template<class T, UT_OptionType ID>
265 {
266 public:
267  typedef T value_type;
268 
269  explicit UT_OptionEntryImpl(const T &value)
270  : myValue(value)
271  { }
272  ~UT_OptionEntryImpl() override = default;
273 
274  const T &getValue() const
275  { return myValue; }
276  UT_OptionType getType() const override
277  { return ID; }
278  UT_OptionEntryPtr clone() const override
279  {
280  return UTmakeUnique<UT_OptionEntryImpl>(myValue);
281  }
282 
283  int64 getMemoryUsage(bool inclusive) const override
284  {
285  int64 mem = inclusive ? sizeof(*this) : 0;
286  return mem;
287  }
288 
289 protected:
291 };
292 
305 
307  public UT_OptionEntryImpl<UT_StringHolder, UT_OPTION_STRING>
308 {
309 public:
311 
312  explicit UT_OptionString(const UT_StringHolder &value) : Base(value) {}
313 
314  int64 getMemoryUsage(bool inclusive) const override
315  {
316  int64 mem = inclusive ? sizeof(*this) : 0;
317  mem += myValue.getMemoryUsage(false);
318  return mem;
319  }
320 };
321 
323  public UT_OptionEntryImpl<UT_OptionsHolder, UT_OPTION_DICT>
324 {
325 public:
327 
328  explicit UT_OptionDict(const UT_OptionsHolder &value) : Base(value) {}
329 
330  int64 getMemoryUsage(bool inclusive) const override
331  {
332  int64 mem = inclusive ? sizeof(*this) : 0;
333  mem += myValue.getMemoryUsage(false);
334  return mem;
335  }
336 };
337 
339  public UT_OptionEntryImpl<UT_Int64Array, UT_OPTION_INTARRAY>
340 {
341 public:
343 
345  : Base(value) {}
347  : Base(UT_Int64Array())
348  {
349  myValue = value;
350  }
352  : Base(UT_Int64Array())
353  {
354  myValue.setSizeNoInit(value.entries());
355  for (exint i = 0; i < value.entries(); ++i)
356  myValue(i) = value(i);
357  }
358 
359  void fetchValue(UT_Int64Array &value) const { value = myValue; }
361  {
362  value.setSizeNoInit(myValue.entries());
363  for (exint i = 0; i < myValue.entries(); ++i)
364  value(i) = myValue(i);
365  }
366 
367  int64 getMemoryUsage(bool inclusive) const override
368  {
369  int64 mem = inclusive ? sizeof(*this) : 0;
370  mem += myValue.getMemoryUsage(false);
371  return mem;
372  }
373 };
374 
376  public UT_OptionEntryImpl<UT_Fpreal64Array, UT_OPTION_FPREALARRAY>
377 {
378 public:
380 
382  : Base(value) {}
385  {
386  myValue = value;
387  }
390  {
391  myValue.setSizeNoInit(value.entries());
392  for (exint i = 0; i < value.entries(); ++i)
393  myValue(i) = value(i);
394  }
395 
396  void fetchValue(UT_Fpreal64Array &value) const { value = myValue; }
398  {
399  value.setSizeNoInit(myValue.entries());
400  for (exint i = 0; i < myValue.entries(); ++i)
401  value(i) = myValue(i);
402  }
403 
404  int64 getMemoryUsage(bool inclusive) const override
405  {
406  int64 mem = inclusive ? sizeof(*this) : 0;
407  mem += myValue.getMemoryUsage(false);
408  return mem;
409  }
410 };
411 
413  public UT_OptionEntryImpl<UT_StringArray, UT_OPTION_STRINGARRAY>
414 {
415 public:
417 
418  explicit UT_OptionStringArray(const UT_StringArray &value) : Base(value) {}
420  : Base(UT_StringArray())
421  {
422  myValue.setSize(value.entries());
423  for (exint i = 0; i < value.entries(); ++i)
424  myValue(i) = value(i);
425  }
426 
427  int64 getMemoryUsage(bool inclusive) const override
428  {
429  int64 mem = inclusive ? sizeof(*this) : 0;
430  mem += myValue.getMemoryUsage(false);
431  return mem;
432  }
433 };
434 
436  public UT_OptionEntryImpl<UT_Array<UT_OptionsHolder>, UT_OPTION_DICTARRAY>
437 {
438 public:
440 
442  int64 getMemoryUsage(bool inclusive) const override
443  {
444  int64 mem = inclusive ? sizeof(*this) : 0;
445  mem += myValue.getMemoryUsage(false);
446  return mem;
447  }
448 };
449 
450 // UT_OptionStringRaw is the same as UT_OptionString, but it returns a getType
451 // value tagging it as a "raw" string so that getPyOptionString() and
452 // appendPyDictionary() won't add quotes around the value.
454 {
455 public:
457 
458  UT_OptionType getType() const override { return UT_OPTION_STRINGRAW; }
459  UT_OptionEntryPtr clone() const override
460  {
461  return UTmakeUnique<UT_OptionStringRaw>(myValue);
462  }
463 };
464 
465 #endif
UT_OptionEntryImpl< UT_StringHolder, UT_OPTION_STRING > Base
UT_OptionEntryPtr clone() const override
void fetchValue(UT_Fpreal64Array &value) const
static exint optionSize(UT_OptionType t)
Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
UT_OptionEntryImpl< UT_Matrix2D, UT_OPTION_MATRIX2 > UT_OptionMatrix2
UT_JSONValueMap stores a map/dictionary of UT_JSONValue objects.
void fetchValue(UT_Int64Array &value) const
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Options.h:1008
UT_OptionFpreal64Array(const UT_Array< fpreal32 > &value)
UT_OptionInt64Array(const UT_Int64Array &value)
UT_OptionFpreal64Array(const UT_Array< fpreal64 > &value)
UT_OptionType getType() const override
const GLdouble * v
Definition: glcorearb.h:837
UT_OptionEntryImpl< UT_Int64Array, UT_OPTION_INTARRAY > Base
T myValue
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void setSizeNoInit(exint newsize)
Definition: UT_Array.h:702
UT_OptionEntryImpl< UT_Matrix4D, UT_OPTION_MATRIX4 > UT_OptionMatrix4
UT_OptionType
Definition: UT_Options.h:44
int64 exint
Definition: SYS_Types.h:125
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:664
void fetchValue(UT_Fpreal32Array &value) const
#define UT_API
Definition: UT_API.h:14
UT_OptionEntryImpl< UT_OptionsHolder, UT_OPTION_DICT > Base
UT_OptionFpreal64Array(const UT_Fpreal64Array &value)
int64 getMemoryUsage(bool inclusive) const override
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
**But if you need a result
Definition: thread.h:622
static UT_OptionTypeStorage optionStorage(UT_OptionType t)
Return the underlying storage type for the option.
void fetchValue(UT_IntArray &value) const
GLuint buffer
Definition: glcorearb.h:660
void setSize(exint newsize)
Definition: UT_Array.h:673
virtual UT_OptionEntryPtr clone() const
UT_OptionEntryImpl< UT_Vector4D, UT_OPTION_VECTOR4 > UT_OptionVector4
int64 getMemoryUsage(bool inclusive) const override
UT_OptionTypeStorage
IMATH_HOSTDEVICE constexpr int cmp(T a, T b) IMATH_NOEXCEPT
Definition: ImathFun.h:84
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
double fpreal64
Definition: SYS_Types.h:201
UT_OptionType getType() const override
exint optionSize() const
const T & getValue() const
int64 getMemoryUsage(bool inclusive) const
UT_OptionEntryImpl< int64, UT_OPTION_INT > UT_OptionInt
virtual UT_OptionType getType() const
~UT_OptionEntryImpl() override=default
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
UT_OptionEntryImpl< UT_Array< UT_OptionsHolder >, UT_OPTION_DICTARRAY > Base
UT_OptionEntryImpl< bool, UT_OPTION_BOOL > UT_OptionBool
long long int64
Definition: SYS_Types.h:116
UT_OptionTypeStorage optionStorage() const
T value_type
UT_API size_t format(char *buffer, size_t bufsize, const UT_OptionEntry &o)
UT_OptionEntryImpl(const T &value)
UT_OptionEntryImpl< UT_Fpreal64Array, UT_OPTION_FPREALARRAY > Base
GLdouble t
Definition: glad.h:2397
UT_OptionEntryPtr clone() const override
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:655
UT_OptionEntryImpl< UT_Vector3D, UT_OPTION_UVW > UT_OptionUVW
int64 getMemoryUsage(bool inclusive) const override
int64 getMemoryUsage(bool inclusive) const override
int64 getMemoryUsage(bool inclusive) const override
UT_OptionEntryImpl< UT_Vector2D, UT_OPTION_VECTOR2 > UT_OptionVector2
int64 getMemoryUsage(bool inclusive) const override
UT_OptionEntryImpl< UT_Vector2D, UT_OPTION_UV > UT_OptionUV
UT_OptionInt64Array(const UT_Array< int64 > &value)
UT_OptionEntryImpl< UT_StringArray, UT_OPTION_STRINGARRAY > Base
UT_OptionStringArray(const UT_Array< UT_StringHolder > &value)
UT_OptionEntryImpl< fpreal64, UT_OPTION_FPREAL > UT_OptionFpreal
LeafData & operator=(const LeafData &)=delete
UT_OptionEntryImpl< UT_QuaternionD, UT_OPTION_QUATERNION > UT_OptionQuaternion
virtual int64 getMemoryUsage(bool inclusive) const
UT_OptionEntryImpl< UT_Vector3D, UT_OPTION_VECTOR3 > UT_OptionVector3
UT_OptionEntryImpl< UT_Matrix3D, UT_OPTION_MATRIX3 > UT_OptionMatrix3
Class to store JSON objects as C++ objects.
Definition: UT_JSONValue.h:99
UT_OptionInt64Array(const UT_Array< int32 > &value)
int64 getMemoryUsage(bool inclusive) const override
UT_OptionDict(const UT_OptionsHolder &value)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
UT_OptionStringArray(const UT_StringArray &value)
UT_OptionFormat
String formatting options for getOptionString.
Definition: UT_Options.h:71
UT_OptionDictArray(const UT_Array< UT_OptionsHolder > &value)
UT_OptionString(const UT_StringHolder &value)
UT_OptionStringRaw(const UT_StringHolder &value)
UT_UniquePtr< UT_OptionEntry > UT_OptionEntryPtr