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:
104  return UT_OPTION_STORE_STRING;
105 
106  case UT_OPTION_DICT:
107  case UT_OPTION_DICTARRAY:
108  return UT_OPTION_STORE_DICT;
109 
110  case UT_OPTION_BOOL:
111  return UT_OPTION_STORE_BOOL;
112 
113  case UT_OPTION_INVALID:
114  case UT_OPTION_NUM_TYPES:
115  return UT_OPTION_STORE_INVALID;
116  }
117  UT_ASSERT(0);
118  return UT_OPTION_STORE_INVALID;
119  }
120  /// Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
122  {
123  switch (t)
124  {
125  case UT_OPTION_INT:
126  case UT_OPTION_FPREAL:
127  case UT_OPTION_STRING:
128  case UT_OPTION_STRINGRAW:
130  case UT_OPTION_DICT:
131  case UT_OPTION_BOOL:
132  return 1;
133 
134  case UT_OPTION_VECTOR2:
135  case UT_OPTION_UV:
136  return 2;
137 
138  case UT_OPTION_VECTOR3:
139  case UT_OPTION_UVW:
140  return 3;
141 
142  case UT_OPTION_VECTOR4:
143  case UT_OPTION_MATRIX2:
145  return 4;
146  case UT_OPTION_MATRIX3:
147  return 9;
148  case UT_OPTION_MATRIX4:
149  return 16;
150 
151  case UT_OPTION_INTARRAY:
156  case UT_OPTION_DICTARRAY:
157  return 0;
158 
159  case UT_OPTION_INVALID:
160  case UT_OPTION_NUM_TYPES:
161  return -1;
162  }
163  UT_ASSERT(0);
164  return -1;
165  }
167  { return optionStorage(getType()); }
169  { return optionSize(getType()); }
170 
171  bool importOption(int64 &value) const;
172  bool importOption(bool &value) const;
173  bool importOption(fpreal64 &value) const;
174  bool importOption(UT_StringHolder &value) const;
175  bool importOption(UT_OptionsHolder &value) const;
176  bool importOption(UT_Vector2D &value) const;
177  bool importOption(UT_Vector3D &value) const;
178  bool importOption(UT_Vector4D &value) const;
179  bool importOption(UT_QuaternionD &value) const;
180  bool importOption(UT_Matrix2D &value) const;
181  bool importOption(UT_Matrix3D &value) const;
182  bool importOption(UT_Matrix4D &value) const;
183 
184  // Generic import which handles arbitrary length int/float/string data
185  bool importOption(UT_Array<int32> &value) const;
186  bool importOption(UT_Array<int64> &value) const;
187  bool importOption(UT_Array<fpreal32> &value) const;
188  bool importOption(UT_Array<fpreal64> &value) const;
189  bool importOption(UT_StringArray &value) const;
190  bool importOption(UT_Array<UT_StringHolder> &value) const;
191  bool importOption(UT_Array<UT_OptionsHolder> &value) const;
192 
193  // Returns the values directly, or a default value if the data is an
194  // inappropriate type.
195  int64 getOptionI() const;
196  bool getOptionB() const;
197  fpreal64 getOptionF() const;
198  const UT_Vector2D &getOptionV2() const;
199  const UT_Vector3D &getOptionV3() const;
200  const UT_Vector4D &getOptionV4() const;
201  const UT_QuaternionD &getOptionQ() const;
202  const UT_Matrix2D &getOptionM2() const;
203  const UT_Matrix3D &getOptionM3() const;
204  const UT_Matrix4D &getOptionM4() const;
205  const UT_Vector2D &getOptionUV() const;
206  const UT_Vector3D &getOptionUVW() const;
207 
208  // Note that, if the string value contains binary data with null
209  // characters, the UT_String version of getOptionS will not be able to
210  // tell you the length of the data, since UT_String doesn't store the
211  // length.
212  UT_StringHolder getOptionS() const;
213  const UT_OptionsHolder &getOptionDict() const;
214  const UT_Int64Array &getOptionIArray() const;
215  const UT_Fpreal64Array &getOptionFArray() const;
216  const UT_StringArray &getOptionSArray() const;
217  const UT_Array<UT_OptionsHolder> &getOptionDictArray() const;
218 
219  /// Save a single option to a JSON stream. This is saved with the schema
220  /// @code
221  /// {
222  /// "name":"UT_OptionEntry",
223  /// "type":"object",
224  /// "properties": {
225  /// "type":{
226  /// "type":"string",
227  /// "enum":{ UT_Options::getStringFromOptionType() },
228  /// },
229  /// "value":{
230  /// "type":["bool", "integer", "float", "string", "array"],
231  /// "items":{"type":"float"},
232  /// "description":"Basic type or an array.
233  /// Arrays must be 2,3,4,9 or 16 floats.
234  /// Data should match given type",
235  /// },
236  /// },
237  /// }
238  /// @endcode
239  bool saveJSON(UT_JSONWriter &w, bool compact) const;
240  /// As above, except save to a UT_JSONValue instead of requiring the value
241  /// go directly to a stream.
242  void saveJSON(UT_JSONValue &v, bool compact) const;
243 
244  /// Create a UT_OptionEntry from a simple JSON value. This is not a
245  /// dictionary with type information. It is a single value which may be
246  /// an int, float, string, etc. The UT_OptionEntry type is inferred from
247  /// the JSON type.
248  /// If allow_type, then if a value contains "type" and "value" it is parsed
249  /// as a TypedMap. Otherwise, it will always be a option.
250  /// If allow_dict, then nested maps will become dictionaries. If both
251  /// allow_type and allow_dict, things that look like types will be
252  /// treated as types and then fall back to dictionaries.
253  static UT_OptionEntryPtr createEntry(const UT_JSONValue &v, bool allow_type = true, bool allow_dict = false);
254 
255  /// Create a UT_OptionEntry from a JSON map. This is a dictionary
256  /// with type information.
257  static UT_OptionEntryPtr createEntryFromTypedMap(const UT_JSONValueMap &map,
258  UT_String &error_str);
259 
260  /// Convert string to and from UT_OptionType values.
261  static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
262  static const char *getStringFromOptionType(UT_OptionType optiontype);
263 };
264 
265 UT_API size_t format(char *buffer, size_t bufsize, const UT_OptionEntry &o);
266 
267 
268 // A templatized version of the class above for holding specific data types.
269 template<class T, UT_OptionType ID>
271 {
272 public:
273  typedef T value_type;
274 
275  explicit UT_OptionEntryImpl(const T &value)
276  : myValue(value)
277  { }
278  ~UT_OptionEntryImpl() override = default;
279 
280  const T &getValue() const
281  { return myValue; }
282  UT_OptionType getType() const override
283  { return ID; }
284  UT_OptionEntryPtr clone() const override
285  {
286  return UTmakeUnique<UT_OptionEntryImpl>(myValue);
287  }
288 
289  int64 getMemoryUsage(bool inclusive) const override
290  {
291  int64 mem = inclusive ? sizeof(*this) : 0;
292  return mem;
293  }
294 
295 protected:
297 };
298 
311 
313  public UT_OptionEntryImpl<UT_StringHolder, UT_OPTION_STRING>
314 {
315 public:
317 
318  explicit UT_OptionString(const UT_StringHolder &value) : Base(value) {}
319 
320  int64 getMemoryUsage(bool inclusive) const override
321  {
322  int64 mem = inclusive ? sizeof(*this) : 0;
323  mem += myValue.getMemoryUsage(false);
324  return mem;
325  }
326 };
327 
329  public UT_OptionEntryImpl<UT_OptionsHolder, UT_OPTION_DICT>
330 {
331 public:
333 
334  explicit UT_OptionDict(const UT_OptionsHolder &value) : Base(value) {}
335 
336  int64 getMemoryUsage(bool inclusive) const override
337  {
338  int64 mem = inclusive ? sizeof(*this) : 0;
339  mem += myValue.getMemoryUsage(false);
340  return mem;
341  }
342 };
343 
345  public UT_OptionEntryImpl<UT_Int64Array, UT_OPTION_INTARRAY>
346 {
347 public:
349 
351  : Base(value) {}
353  : Base(UT_Int64Array())
354  {
355  myValue = value;
356  }
358  : Base(UT_Int64Array())
359  {
360  myValue.setSizeNoInit(value.entries());
361  for (exint i = 0; i < value.entries(); ++i)
362  myValue(i) = value(i);
363  }
364 
365  void fetchValue(UT_Int64Array &value) const { value = myValue; }
367  {
368  value.setSizeNoInit(myValue.entries());
369  for (exint i = 0; i < myValue.entries(); ++i)
370  value(i) = myValue(i);
371  }
372 
373  int64 getMemoryUsage(bool inclusive) const override
374  {
375  int64 mem = inclusive ? sizeof(*this) : 0;
376  mem += myValue.getMemoryUsage(false);
377  return mem;
378  }
379 };
380 
382  public UT_OptionEntryImpl<UT_Fpreal64Array, UT_OPTION_FPREALARRAY>
383 {
384 public:
386 
388  : Base(value) {}
391  {
392  myValue = value;
393  }
396  {
397  myValue.setSizeNoInit(value.entries());
398  for (exint i = 0; i < value.entries(); ++i)
399  myValue(i) = value(i);
400  }
401 
402  void fetchValue(UT_Fpreal64Array &value) const { value = myValue; }
404  {
405  value.setSizeNoInit(myValue.entries());
406  for (exint i = 0; i < myValue.entries(); ++i)
407  value(i) = myValue(i);
408  }
409 
410  int64 getMemoryUsage(bool inclusive) const override
411  {
412  int64 mem = inclusive ? sizeof(*this) : 0;
413  mem += myValue.getMemoryUsage(false);
414  return mem;
415  }
416 };
417 
419  public UT_OptionEntryImpl<UT_Array<UT_OptionsHolder>, UT_OPTION_DICTARRAY>
420 {
421 public:
423 
425  : Base(value) {}
426  int64 getMemoryUsage(bool inclusive) const override
427  {
428  int64 mem = inclusive ? sizeof(*this) : 0;
429  mem += myValue.getMemoryUsage(false);
430  return mem;
431  }
432 };
433 
434 // UT_OptionStringRaw is the same as UT_OptionString, but it returns a getType
435 // value tagging it as a "raw" string so that getPyOptionString() and
436 // appendPyDictionary() won't add quotes around the value.
438 {
439 public:
441  : UT_OptionString(value) {}
442 
443  UT_OptionType getType() const override { return UT_OPTION_STRINGRAW; }
444  UT_OptionEntryPtr clone() const override
445  {
446  return UTmakeUnique<UT_OptionStringRaw>(myValue);
447  }
448 };
449 
450 // UT_OptionStringUtf8 is the same as UT_OptionString, but it returns a getType
451 // value tagging it as a UTF8 string so that getPyOptionString() and
452 // appendPyDictionary() will encode the string properly (using \x, \u, or \U
453 // escape sequences depending on the length of each code point).
455 {
456 public:
458  : UT_OptionString(value) {}
459 
460  UT_OptionType getType() const override { return UT_OPTION_STRINGUTF8; }
461  UT_OptionEntryPtr clone() const override
462  {
463  return UTmakeUnique<UT_OptionStringUtf8>(myValue);
464  }
465 };
466 
468  public UT_OptionEntryImpl<UT_StringArray, UT_OPTION_STRINGARRAY>
469 {
470 public:
472 
474  : Base(value) {}
476  : Base(UT_StringArray())
477  {
478  myValue.setSize(value.entries());
479  for (exint i = 0; i < value.entries(); ++i)
480  myValue(i) = value(i);
481  }
482 
483  int64 getMemoryUsage(bool inclusive) const override
484  {
485  int64 mem = inclusive ? sizeof(*this) : 0;
486  mem += myValue.getMemoryUsage(false);
487  return mem;
488  }
489 };
490 
491 // An array of strings that should be treated like an array of
492 // UT_OptionStringRaw
494 {
495 public:
497  : UT_OptionStringArray(value) {}
499  : UT_OptionStringArray(value) {}
500 
501  UT_OptionType getType() const override { return UT_OPTION_STRINGRAWARRAY; }
502  UT_OptionEntryPtr clone() const override
503  {
504  return UTmakeUnique<UT_OptionStringRawArray>(myValue);
505  }
506 };
507 
508 // An array of strings that should be treated like an array of
509 // UT_OptionStringUtf8
511 {
512 public:
514  : UT_OptionStringArray(value) {}
516  : UT_OptionStringArray(value) {}
517 
518  UT_OptionType getType() const override { return UT_OPTION_STRINGUTF8ARRAY; }
519  UT_OptionEntryPtr clone() const override
520  {
521  return UTmakeUnique<UT_OptionStringUtf8Array>(myValue);
522  }
523 };
524 
525 #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)
UT_OptionStringUtf8(const UT_StringHolder &value)
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.
UT_OptionType getType() const override
void fetchValue(UT_Int64Array &value) const
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Options.h:1020
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
UT_OptionEntryPtr clone() const override
exint optionSize() const
const T & getValue() const
int64 getMemoryUsage(bool inclusive) const
UT_OptionEntryImpl< int64, UT_OPTION_INT > UT_OptionInt
UT_OptionType getType() const override
virtual UT_OptionType getType() const
~UT_OptionEntryImpl() override=default
UT_OptionStringUtf8Array(const UT_StringArray &value)
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
UT_OptionEntryPtr clone() const override
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_OptionStringRawArray(const UT_Array< UT_StringHolder > &value)
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
UT_OptionType getType() const override
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:74
UT_OptionDictArray(const UT_Array< UT_OptionsHolder > &value)
UT_OptionStringUtf8Array(const UT_Array< UT_StringHolder > &value)
UT_OptionEntryPtr clone() const override
UT_OptionString(const UT_StringHolder &value)
UT_OptionStringRaw(const UT_StringHolder &value)
UT_UniquePtr< UT_OptionEntry > UT_OptionEntryPtr
UT_OptionStringRawArray(const UT_StringArray &value)