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  virtual UT_OptionType getType() const
45  { return UT_OPTION_INVALID; }
46  virtual UT_OptionEntryPtr clone() const
47  {
48  return UTmakeUnique<UT_OptionEntry>();
49  }
50  virtual int64 getMemoryUsage(bool inclusive) const
51  { return inclusive ? sizeof(*this) : 0; }
52 
53  /// Hash a single option entry.
54  unsigned hash() const;
55 
56  /// Compute equivalence for a given option entry
57  unsigned isEqual(const UT_OptionEntry &cmp,
58  fpreal64 tolerance = 0.0) const;
59 
60  bool getOptionString(UT_OptionFormat format,
61  UT_WorkBuffer &result) const;
62  bool getOptionString(UT_OptionFormat format,
63  UT_String &result) const;
64 
66  {
67  UT_OPTION_STORE_INVALID=-1,
73  };
74 
75  /// Return the underlying storage type for the option
77  {
78  switch (t)
79  {
80  case UT_OPTION_INT:
81  case UT_OPTION_INTARRAY:
82  return UT_OPTION_STORE_INT;
83 
84  case UT_OPTION_FPREAL:
85  case UT_OPTION_VECTOR2:
86  case UT_OPTION_VECTOR3:
87  case UT_OPTION_VECTOR4:
89  case UT_OPTION_MATRIX3:
90  case UT_OPTION_MATRIX4:
91  case UT_OPTION_UV:
92  case UT_OPTION_UVW:
94  case UT_OPTION_MATRIX2:
95  return UT_OPTION_STORE_REAL;
96 
97  case UT_OPTION_STRING:
100  return UT_OPTION_STORE_STRING;
101 
102  case UT_OPTION_DICT:
103  case UT_OPTION_DICTARRAY:
104  return UT_OPTION_STORE_DICT;
105 
106  case UT_OPTION_BOOL:
107  return UT_OPTION_STORE_BOOL;
108 
109  case UT_OPTION_INVALID:
110  case UT_OPTION_NUM_TYPES:
111  return UT_OPTION_STORE_INVALID;
112  }
113  UT_ASSERT(0);
114  return UT_OPTION_STORE_INVALID;
115  }
116  /// Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
118  {
119  switch (t)
120  {
121  case UT_OPTION_INT:
122  case UT_OPTION_FPREAL:
123  case UT_OPTION_STRING:
124  case UT_OPTION_STRINGRAW:
125  case UT_OPTION_DICT:
126  case UT_OPTION_BOOL:
127  return 1;
128 
129  case UT_OPTION_VECTOR2:
130  case UT_OPTION_UV:
131  return 2;
132 
133  case UT_OPTION_VECTOR3:
134  case UT_OPTION_UVW:
135  return 3;
136 
137  case UT_OPTION_VECTOR4:
138  case UT_OPTION_MATRIX2:
140  return 4;
141  case UT_OPTION_MATRIX3:
142  return 9;
143  case UT_OPTION_MATRIX4:
144  return 16;
145 
146  case UT_OPTION_INTARRAY:
149  case UT_OPTION_DICTARRAY:
150  return 0;
151 
152  case UT_OPTION_INVALID:
153  case UT_OPTION_NUM_TYPES:
154  return -1;
155  }
156  UT_ASSERT(0);
157  return -1;
158  }
160  { return optionStorage(getType()); }
162  { return optionSize(getType()); }
163 
164  bool importOption(int64 &value) const;
165  bool importOption(bool &value) const;
166  bool importOption(fpreal64 &value) const;
167  bool importOption(UT_StringHolder &value) const;
168  bool importOption(UT_OptionsHolder &value) const;
169  bool importOption(UT_Vector2D &value) const;
170  bool importOption(UT_Vector3D &value) const;
171  bool importOption(UT_Vector4D &value) const;
172  bool importOption(UT_QuaternionD &value) const;
173  bool importOption(UT_Matrix2D &value) const;
174  bool importOption(UT_Matrix3D &value) const;
175  bool importOption(UT_Matrix4D &value) const;
176 
177  // Generic import which handles arbitrary length int/float/string data
178  bool importOption(UT_Array<int32> &value) const;
179  bool importOption(UT_Array<int64> &value) const;
180  bool importOption(UT_Array<fpreal32> &value) const;
181  bool importOption(UT_Array<fpreal64> &value) const;
182  bool importOption(UT_StringArray &value) const;
183  bool importOption(UT_Array<UT_StringHolder> &value) const;
184  bool importOption(UT_Array<UT_OptionsHolder> &value) const;
185 
186  // Returns the values directly, or a default value if the data is an
187  // inappropriate type.
188  int64 getOptionI() const;
189  bool getOptionB() const;
190  fpreal64 getOptionF() const;
191  const UT_Vector2D &getOptionV2() const;
192  const UT_Vector3D &getOptionV3() const;
193  const UT_Vector4D &getOptionV4() const;
194  const UT_QuaternionD &getOptionQ() const;
195  const UT_Matrix2D &getOptionM2() const;
196  const UT_Matrix3D &getOptionM3() const;
197  const UT_Matrix4D &getOptionM4() const;
198  const UT_Vector2D &getOptionUV() const;
199  const UT_Vector3D &getOptionUVW() const;
200 
201  // Note that, if the string value contains binary data with null
202  // characters, the UT_String version of getOptionS will not be able to
203  // tell you the length of the data, since UT_String doesn't store the
204  // length.
205  UT_StringHolder getOptionS() const;
206  const UT_OptionsHolder &getOptionDict() const;
207  const UT_Int64Array &getOptionIArray() const;
208  const UT_Fpreal64Array &getOptionFArray() const;
209  const UT_StringArray &getOptionSArray() const;
210  const UT_Array<UT_OptionsHolder> &getOptionDictArray() const;
211 
212  /// Save a single option to a JSON stream. This is saved with the schema
213  /// @code
214  /// {
215  /// "name":"UT_OptionEntry",
216  /// "type":"object",
217  /// "properties": {
218  /// "type":{
219  /// "type":"string",
220  /// "enum":{ UT_Options::getStringFromOptionType() },
221  /// },
222  /// "value":{
223  /// "type":["bool", "integer", "float", "string", "array"],
224  /// "items":{"type":"float"},
225  /// "description":"Basic type or an array.
226  /// Arrays must be 2,3,4,9 or 16 floats.
227  /// Data should match given type",
228  /// },
229  /// },
230  /// }
231  /// @endcode
232  bool saveJSON(UT_JSONWriter &w, bool compact) const;
233  /// As above, except save to a UT_JSONValue instead of requiring the value
234  /// go directly to a stream.
235  void saveJSON(UT_JSONValue &v, bool compact) const;
236 
237  /// Create a UT_OptionEntry from a simple JSON value. This is not a
238  /// dictionary with type information. It is a single value which may be
239  /// an int, float, string, etc. The UT_OptionEntry type is inferred from
240  /// the JSON type.
241  /// If allow_type, then if a value contains "type" and "value" it is parsed
242  /// as a TypedMap. Otherwise, it will always be a option.
243  /// If allow_dict, then nested maps will become dictionaries. If both
244  /// allow_type and allow_dict, things that look like types will be
245  /// treated as types and then fall back to dictionaries.
246  static UT_OptionEntryPtr createEntry(const UT_JSONValue &v, bool allow_type = true, bool allow_dict = false);
247 
248  /// Create a UT_OptionEntry from a JSON map. This is a dictionary
249  /// with type information.
250  static UT_OptionEntryPtr createEntryFromTypedMap(const UT_JSONValueMap &map,
251  UT_String &error_str);
252 
253  /// Convert string to and from UT_OptionType values.
254  static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
255  static const char *getStringFromOptionType(UT_OptionType optiontype);
256 };
257 
258 UT_API size_t format(char *buffer, size_t bufsize, const UT_OptionEntry &o);
259 
260 
261 // A templatized version of the class above for holding specific data types.
262 template<class T, UT_OptionType ID>
264 {
265 public:
266  typedef T value_type;
267 
269  : myValue(value)
270  { }
272  { }
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 
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 
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 
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 };
460 
461 #endif
UT_OptionEntryImpl< UT_StringHolder, UT_OPTION_STRING > Base
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_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:915
GLsizei bufsize
Definition: glew.h:2581
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
UT_OptionEntryImpl< UT_Int64Array, UT_OPTION_INTARRAY > Base
T myValue
void setSizeNoInit(exint newsize)
Definition: UT_Array.h:507
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:469
void fetchValue(UT_Fpreal32Array &value) const
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
#define UT_API
Definition: UT_API.h:13
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:34
const GLdouble * v
Definition: glew.h:1391
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:478
virtual UT_OptionEntryPtr clone() const
UT_OptionEntryImpl< UT_Vector4D, UT_OPTION_VECTOR4 > UT_OptionVector4
int64 getMemoryUsage(bool inclusive) const override
UT_OptionTypeStorage
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
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
GLuint buffer
Definition: glew.h:1680
virtual UT_OptionType getType() const
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
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
int cmp(T a, T b)
Definition: ImathFun.h:119
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:460
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
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:77
UT_OptionInt64Array(const UT_Array< int32 > &value)
virtual ~UT_OptionEntry()
GLuint64EXT * result
Definition: glew.h:14007
int64 getMemoryUsage(bool inclusive) const override
UT_OptionDict(const UT_OptionsHolder &value)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
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)
GLsizei const GLfloat * value
Definition: glew.h:1849
GLdouble GLdouble t
Definition: glew.h:1398
UT_OptionEntry()
UT_UniquePtr< UT_OptionEntry > UT_OptionEntryPtr