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:
40  { }
41  virtual ~UT_OptionEntry()
42  { }
43 
44  UT_OptionEntry(const UT_OptionEntry &) = delete;
45  UT_OptionEntry &operator=(const UT_OptionEntry &) = delete;
46 
47  virtual UT_OptionType getType() const
48  { return UT_OPTION_INVALID; }
49  virtual UT_OptionEntryPtr clone() const
50  {
51  return UTmakeUnique<UT_OptionEntry>();
52  }
53  virtual int64 getMemoryUsage(bool inclusive) const
54  { return inclusive ? sizeof(*this) : 0; }
55 
56  /// Hash a single option entry.
57  unsigned hash() const;
58 
59  /// Compute equivalence for a given option entry
60  unsigned isEqual(const UT_OptionEntry &cmp,
61  fpreal64 tolerance = 0.0) const;
62 
63  bool getOptionString(UT_OptionFormat format,
64  UT_WorkBuffer &result) const;
65  bool getOptionString(UT_OptionFormat format,
66  UT_String &result) const;
67 
69  {
70  UT_OPTION_STORE_INVALID=-1,
76  };
77 
78  /// Return the underlying storage type for the option
80  {
81  switch (t)
82  {
83  case UT_OPTION_INT:
84  case UT_OPTION_INTARRAY:
85  return UT_OPTION_STORE_INT;
86 
87  case UT_OPTION_FPREAL:
88  case UT_OPTION_VECTOR2:
89  case UT_OPTION_VECTOR3:
90  case UT_OPTION_VECTOR4:
92  case UT_OPTION_MATRIX3:
93  case UT_OPTION_MATRIX4:
94  case UT_OPTION_UV:
95  case UT_OPTION_UVW:
97  case UT_OPTION_MATRIX2:
98  return UT_OPTION_STORE_REAL;
99 
100  case UT_OPTION_STRING:
101  case UT_OPTION_STRINGRAW:
103  return UT_OPTION_STORE_STRING;
104 
105  case UT_OPTION_DICT:
106  case UT_OPTION_DICTARRAY:
107  return UT_OPTION_STORE_DICT;
108 
109  case UT_OPTION_BOOL:
110  return UT_OPTION_STORE_BOOL;
111 
112  case UT_OPTION_INVALID:
113  case UT_OPTION_NUM_TYPES:
114  return UT_OPTION_STORE_INVALID;
115  }
116  UT_ASSERT(0);
117  return UT_OPTION_STORE_INVALID;
118  }
119  /// Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
121  {
122  switch (t)
123  {
124  case UT_OPTION_INT:
125  case UT_OPTION_FPREAL:
126  case UT_OPTION_STRING:
127  case UT_OPTION_STRINGRAW:
128  case UT_OPTION_DICT:
129  case UT_OPTION_BOOL:
130  return 1;
131 
132  case UT_OPTION_VECTOR2:
133  case UT_OPTION_UV:
134  return 2;
135 
136  case UT_OPTION_VECTOR3:
137  case UT_OPTION_UVW:
138  return 3;
139 
140  case UT_OPTION_VECTOR4:
141  case UT_OPTION_MATRIX2:
143  return 4;
144  case UT_OPTION_MATRIX3:
145  return 9;
146  case UT_OPTION_MATRIX4:
147  return 16;
148 
149  case UT_OPTION_INTARRAY:
152  case UT_OPTION_DICTARRAY:
153  return 0;
154 
155  case UT_OPTION_INVALID:
156  case UT_OPTION_NUM_TYPES:
157  return -1;
158  }
159  UT_ASSERT(0);
160  return -1;
161  }
163  { return optionStorage(getType()); }
165  { return optionSize(getType()); }
166 
167  bool importOption(int64 &value) const;
168  bool importOption(bool &value) const;
169  bool importOption(fpreal64 &value) const;
170  bool importOption(UT_StringHolder &value) const;
171  bool importOption(UT_OptionsHolder &value) const;
172  bool importOption(UT_Vector2D &value) const;
173  bool importOption(UT_Vector3D &value) const;
174  bool importOption(UT_Vector4D &value) const;
175  bool importOption(UT_QuaternionD &value) const;
176  bool importOption(UT_Matrix2D &value) const;
177  bool importOption(UT_Matrix3D &value) const;
178  bool importOption(UT_Matrix4D &value) const;
179 
180  // Generic import which handles arbitrary length int/float/string data
181  bool importOption(UT_Array<int32> &value) const;
182  bool importOption(UT_Array<int64> &value) const;
183  bool importOption(UT_Array<fpreal32> &value) const;
184  bool importOption(UT_Array<fpreal64> &value) const;
185  bool importOption(UT_StringArray &value) const;
186  bool importOption(UT_Array<UT_StringHolder> &value) const;
187  bool importOption(UT_Array<UT_OptionsHolder> &value) const;
188 
189  // Returns the values directly, or a default value if the data is an
190  // inappropriate type.
191  int64 getOptionI() const;
192  bool getOptionB() const;
193  fpreal64 getOptionF() const;
194  const UT_Vector2D &getOptionV2() const;
195  const UT_Vector3D &getOptionV3() const;
196  const UT_Vector4D &getOptionV4() const;
197  const UT_QuaternionD &getOptionQ() const;
198  const UT_Matrix2D &getOptionM2() const;
199  const UT_Matrix3D &getOptionM3() const;
200  const UT_Matrix4D &getOptionM4() const;
201  const UT_Vector2D &getOptionUV() const;
202  const UT_Vector3D &getOptionUVW() const;
203 
204  // Note that, if the string value contains binary data with null
205  // characters, the UT_String version of getOptionS will not be able to
206  // tell you the length of the data, since UT_String doesn't store the
207  // length.
208  UT_StringHolder getOptionS() const;
209  const UT_OptionsHolder &getOptionDict() const;
210  const UT_Int64Array &getOptionIArray() const;
211  const UT_Fpreal64Array &getOptionFArray() const;
212  const UT_StringArray &getOptionSArray() const;
213  const UT_Array<UT_OptionsHolder> &getOptionDictArray() const;
214 
215  /// Save a single option to a JSON stream. This is saved with the schema
216  /// @code
217  /// {
218  /// "name":"UT_OptionEntry",
219  /// "type":"object",
220  /// "properties": {
221  /// "type":{
222  /// "type":"string",
223  /// "enum":{ UT_Options::getStringFromOptionType() },
224  /// },
225  /// "value":{
226  /// "type":["bool", "integer", "float", "string", "array"],
227  /// "items":{"type":"float"},
228  /// "description":"Basic type or an array.
229  /// Arrays must be 2,3,4,9 or 16 floats.
230  /// Data should match given type",
231  /// },
232  /// },
233  /// }
234  /// @endcode
235  bool saveJSON(UT_JSONWriter &w, bool compact) const;
236  /// As above, except save to a UT_JSONValue instead of requiring the value
237  /// go directly to a stream.
238  void saveJSON(UT_JSONValue &v, bool compact) const;
239 
240  /// Create a UT_OptionEntry from a simple JSON value. This is not a
241  /// dictionary with type information. It is a single value which may be
242  /// an int, float, string, etc. The UT_OptionEntry type is inferred from
243  /// the JSON type.
244  /// If allow_type, then if a value contains "type" and "value" it is parsed
245  /// as a TypedMap. Otherwise, it will always be a option.
246  /// If allow_dict, then nested maps will become dictionaries. If both
247  /// allow_type and allow_dict, things that look like types will be
248  /// treated as types and then fall back to dictionaries.
249  static UT_OptionEntryPtr createEntry(const UT_JSONValue &v, bool allow_type = true, bool allow_dict = false);
250 
251  /// Create a UT_OptionEntry from a JSON map. This is a dictionary
252  /// with type information.
253  static UT_OptionEntryPtr createEntryFromTypedMap(const UT_JSONValueMap &map,
254  UT_String &error_str);
255 
256  /// Convert string to and from UT_OptionType values.
257  static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
258  static const char *getStringFromOptionType(UT_OptionType optiontype);
259 };
260 
261 UT_API size_t format(char *buffer, size_t bufsize, const UT_OptionEntry &o);
262 
263 
264 // A templatized version of the class above for holding specific data types.
265 template<class T, UT_OptionType ID>
267 {
268 public:
269  typedef T value_type;
270 
272  : myValue(value)
273  { }
275  { }
276 
277  const T &getValue() const
278  { return myValue; }
279  UT_OptionType getType() const override
280  { return ID; }
281  UT_OptionEntryPtr clone() const override
282  {
283  return UTmakeUnique<UT_OptionEntryImpl>(myValue);
284  }
285 
286  int64 getMemoryUsage(bool inclusive) const override
287  {
288  int64 mem = inclusive ? sizeof(*this) : 0;
289  return mem;
290  }
291 
292 protected:
294 };
295 
308 
310  public UT_OptionEntryImpl<UT_StringHolder, UT_OPTION_STRING>
311 {
312 public:
314 
316 
317  int64 getMemoryUsage(bool inclusive) const override
318  {
319  int64 mem = inclusive ? sizeof(*this) : 0;
320  mem += myValue.getMemoryUsage(false);
321  return mem;
322  }
323 };
324 
326  public UT_OptionEntryImpl<UT_OptionsHolder, UT_OPTION_DICT>
327 {
328 public:
330 
332 
333  int64 getMemoryUsage(bool inclusive) const override
334  {
335  int64 mem = inclusive ? sizeof(*this) : 0;
336  mem += myValue.getMemoryUsage(false);
337  return mem;
338  }
339 };
340 
342  public UT_OptionEntryImpl<UT_Int64Array, UT_OPTION_INTARRAY>
343 {
344 public:
346 
348  : Base(value) {}
350  : Base(UT_Int64Array())
351  {
352  myValue = value;
353  }
355  : Base(UT_Int64Array())
356  {
357  myValue.setSizeNoInit(value.entries());
358  for (exint i = 0; i < value.entries(); ++i)
359  myValue(i) = value(i);
360  }
361 
362  void fetchValue(UT_Int64Array &value) const { value = myValue; }
364  {
365  value.setSizeNoInit(myValue.entries());
366  for (exint i = 0; i < myValue.entries(); ++i)
367  value(i) = myValue(i);
368  }
369 
370  int64 getMemoryUsage(bool inclusive) const override
371  {
372  int64 mem = inclusive ? sizeof(*this) : 0;
373  mem += myValue.getMemoryUsage(false);
374  return mem;
375  }
376 };
377 
379  public UT_OptionEntryImpl<UT_Fpreal64Array, UT_OPTION_FPREALARRAY>
380 {
381 public:
383 
385  : Base(value) {}
388  {
389  myValue = value;
390  }
393  {
394  myValue.setSizeNoInit(value.entries());
395  for (exint i = 0; i < value.entries(); ++i)
396  myValue(i) = value(i);
397  }
398 
399  void fetchValue(UT_Fpreal64Array &value) const { value = myValue; }
401  {
402  value.setSizeNoInit(myValue.entries());
403  for (exint i = 0; i < myValue.entries(); ++i)
404  value(i) = myValue(i);
405  }
406 
407  int64 getMemoryUsage(bool inclusive) const override
408  {
409  int64 mem = inclusive ? sizeof(*this) : 0;
410  mem += myValue.getMemoryUsage(false);
411  return mem;
412  }
413 };
414 
416  public UT_OptionEntryImpl<UT_StringArray, UT_OPTION_STRINGARRAY>
417 {
418 public:
420 
423  : Base(UT_StringArray())
424  {
425  myValue.setSize(value.entries());
426  for (exint i = 0; i < value.entries(); ++i)
427  myValue(i) = value(i);
428  }
429 
430  int64 getMemoryUsage(bool inclusive) const override
431  {
432  int64 mem = inclusive ? sizeof(*this) : 0;
433  mem += myValue.getMemoryUsage(false);
434  return mem;
435  }
436 };
437 
439  public UT_OptionEntryImpl<UT_Array<UT_OptionsHolder>, UT_OPTION_DICTARRAY>
440 {
441 public:
443 
445  int64 getMemoryUsage(bool inclusive) const override
446  {
447  int64 mem = inclusive ? sizeof(*this) : 0;
448  mem += myValue.getMemoryUsage(false);
449  return mem;
450  }
451 };
452 
453 // UT_OptionStringRaw is the same as UT_OptionString, but it returns a getType
454 // value tagging it as a "raw" string so that getPyOptionString() and
455 // appendPyDictionary() won't add quotes around the value.
457 {
458 public:
460 
461  UT_OptionType getType() const override { return UT_OPTION_STRINGRAW; }
462  UT_OptionEntryPtr clone() const override
463  {
464  return UTmakeUnique<UT_OptionStringRaw>(myValue);
465  }
466 };
467 
468 #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:946
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:695
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:657
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)
~UT_OptionEntryImpl() override
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:613
static UT_OptionTypeStorage optionStorage(UT_OptionType t)
Return the underlying storage type for the option.
void fetchValue(UT_IntArray &value) const
void setSize(exint newsize)
Definition: UT_Array.h:666
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
Definition: core.h:760
int64 getMemoryUsage(bool inclusive) const
UT_OptionEntryImpl< int64, UT_OPTION_INT > UT_OptionInt
virtual UT_OptionType getType() const
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:648
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)
virtual ~UT_OptionEntry()
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
Definition: core.h:1131
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_OptionEntry()
UT_UniquePtr< UT_OptionEntry > UT_OptionEntryPtr