HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
23 class UT_JSONValue;
24 class UT_JSONValueMap;
25 class UT_JSONWriter;
26 
27 // NOTE: The values of UT_OptionType are saved to disk, thus we should NOT
28 // reorder these options if we want to maintain compatibility with older
29 // saved options.
31 {
45  UT_OPTION_STRINGRAW = 12, // used by getPyOptionString
46  UT_OPTION_INTARRAY = 13, // Array of integers
47  UT_OPTION_FPREALARRAY = 14, // Array of float/double
50 
51  UT_OPTION_NUM_TYPES // Sentinal
52 };
53 
54 /// String formatting options for getOptionString
56 {
57  UT_OPTFMT_DISPLAY, // Format for display
58  UT_OPTFMT_PYTHON, // Format for python
59  UT_OPTFMT_VEX, // Format for VEX arguments
61 };
62 
63 // The base class for data held by the UT_Options.
64 class UT_API UT_OptionEntry : public UT_SmallObject<UT_OptionEntry,
65  UT_SMALLOBJECT_CLEANPAGES_DEFAULT,
66  UT_SMALLOBJECT_PAGESIZE_DEFAULT,
67  UT_SMALLOBJECT_THREADSAFE_ON>
68 {
69 public:
71  { }
72  virtual ~UT_OptionEntry()
73  { }
74 
75  virtual UT_OptionType getType() const
76  { return UT_OPTION_INVALID; }
77  virtual UT_OptionEntry *clone() const
78  { return new UT_OptionEntry(); }
79  virtual int64 getMemoryUsage(bool inclusive) const
80  { return inclusive ? sizeof(this) : 0; }
81 
82  /// Hash a single option entry.
83  unsigned hash() const;
84 
85  /// Compute equivalence for a given option entry
86  unsigned isEqual(const UT_OptionEntry &cmp,
87  fpreal64 tolerance = 0.0) const;
88 
89  bool getOptionString(UT_OptionFormat format,
90  UT_WorkBuffer &result) const;
91  bool getOptionString(UT_OptionFormat format,
92  UT_String &result) const;
93 
95  {
96  UT_OPTION_STORE_INVALID=-1,
101  };
102 
103  /// Return the underlying storage type for the option
105  {
106  switch (t)
107  {
108  case UT_OPTION_INT:
109  case UT_OPTION_INTARRAY:
110  return UT_OPTION_STORE_INT;
111 
112  case UT_OPTION_FPREAL:
113  case UT_OPTION_VECTOR2:
114  case UT_OPTION_VECTOR3:
115  case UT_OPTION_VECTOR4:
117  case UT_OPTION_MATRIX3:
118  case UT_OPTION_MATRIX4:
119  case UT_OPTION_UV:
120  case UT_OPTION_UVW:
122  case UT_OPTION_MATRIX2:
123  return UT_OPTION_STORE_REAL;
124 
125  case UT_OPTION_STRING:
126  case UT_OPTION_STRINGRAW:
128  return UT_OPTION_STORE_STRING;
129 
130  case UT_OPTION_BOOL:
131  return UT_OPTION_STORE_BOOL;
132 
133  case UT_OPTION_INVALID:
134  case UT_OPTION_NUM_TYPES:
135  return UT_OPTION_STORE_INVALID;
136  }
137  UT_ASSERT(0);
138  return UT_OPTION_STORE_INVALID;
139  }
140  /// Returns the tuple size, or 0 if the option is variadic (i.e. INTARRAY)
142  {
143  switch (t)
144  {
145  case UT_OPTION_INT:
146  case UT_OPTION_FPREAL:
147  case UT_OPTION_STRING:
148  case UT_OPTION_STRINGRAW:
149  case UT_OPTION_BOOL:
150  return 1;
151 
152  case UT_OPTION_VECTOR2:
153  case UT_OPTION_UV:
154  return 2;
155 
156  case UT_OPTION_VECTOR3:
157  case UT_OPTION_UVW:
158  return 3;
159 
160  case UT_OPTION_VECTOR4:
161  case UT_OPTION_MATRIX2:
163  return 4;
164  case UT_OPTION_MATRIX3:
165  return 9;
166  case UT_OPTION_MATRIX4:
167  return 16;
168 
169  case UT_OPTION_INTARRAY:
172  return 0;
173 
174  case UT_OPTION_INVALID:
175  case UT_OPTION_NUM_TYPES:
176  return -1;
177  }
178  UT_ASSERT(0);
179  return -1;
180  }
182  { return optionStorage(getType()); }
184  { return optionSize(getType()); }
185 
186  bool importOption(int64 &value) const;
187  bool importOption(bool &value) const;
188  bool importOption(fpreal64 &value) const;
189  bool importOption(UT_StringHolder &value) const;
190  bool importOption(UT_Vector2D &value) const;
191  bool importOption(UT_Vector3D &value) const;
192  bool importOption(UT_Vector4D &value) const;
193  bool importOption(UT_QuaternionD &value) const;
194  bool importOption(UT_Matrix2D &value) const;
195  bool importOption(UT_Matrix3D &value) const;
196  bool importOption(UT_Matrix4D &value) const;
197 
198  // Generic import which handles arbitrary length int/float/string data
199  bool importOption(UT_Array<int32> &value) const;
200  bool importOption(UT_Array<int64> &value) const;
201  bool importOption(UT_Array<fpreal32> &value) const;
202  bool importOption(UT_Array<fpreal64> &value) const;
203  bool importOption(UT_StringArray &value) const;
204 
205  // Returns the values directly, or a default value if the data is an
206  // inappropriate type.
207  int64 getOptionI() const;
208  bool getOptionB() const;
209  fpreal64 getOptionF() const;
210  const UT_Vector2D &getOptionV2() const;
211  const UT_Vector3D &getOptionV3() const;
212  const UT_Vector4D &getOptionV4() const;
213  const UT_QuaternionD &getOptionQ() const;
214  const UT_Matrix2D &getOptionM2() const;
215  const UT_Matrix3D &getOptionM3() const;
216  const UT_Matrix4D &getOptionM4() const;
217  const UT_Vector2D &getOptionUV() const;
218  const UT_Vector3D &getOptionUVW() const;
219 
220  // Note that, if the string value contains binary data with null
221  // characters, the UT_String version of getOptionS will not be able to
222  // tell you the length of the data, since UT_String doesn't store the
223  // length.
224  UT_StringHolder getOptionS() const;
225  const UT_Int64Array &getOptionIArray() const;
226  const UT_Fpreal64Array &getOptionFArray() const;
227  const UT_StringArray &getOptionSArray() const;
228 
229  /// Save a single option to a JSON stream. This is saved with the schema
230  /// @code
231  /// {
232  /// "name":"UT_OptionEntry",
233  /// "type":"object",
234  /// "properties": {
235  /// "type":{
236  /// "type":"string",
237  /// "enum":{ UT_Options::getStringFromOptionType() },
238  /// },
239  /// "value":{
240  /// "type":["bool", "integer", "float", "string", "array"],
241  /// "items":{"type":"float"},
242  /// "description":"Basic type or an array.
243  /// Arrays must be 2,3,4,9 or 16 floats.
244  /// Data should match given type",
245  /// },
246  /// },
247  /// }
248  /// @endcode
249  bool saveJSON(UT_JSONWriter &w, bool compact) const;
250  /// As above, except save to a UT_JSONValue instead of requiring the value
251  /// go directly to a stream.
252  void saveJSON(UT_JSONValue &v, bool compact) const;
253 
254  /// Create a UT_OptionEntry from a simple JSON value. This is not a
255  /// dictionary with type information. It is a single value which may be
256  /// an int, float, string, etc. The UT_OptionEntry type is inferred from
257  /// the JSON type.
258  static UT_OptionEntry *createEntry(const UT_JSONValue &v);
259  static UT_OptionEntry *createEntry(const UT_JSONValueMap &map,
260  UT_String &error_str);
261 
262  /// Convert string to and from UT_OptionType values.
263  static UT_OptionType getOptionTypeFromString(const char *optiontypestr);
264  static const char *getStringFromOptionType(UT_OptionType optiontype);
265 };
266 
267 // A templatized version of the class above for holding specific data types.
268 template<class T, UT_OptionType ID>
270 {
271 public:
272  typedef T value_type;
273 
275  : myValue(value)
276  { }
278  { }
279 
280  const T &getValue() const
281  { return myValue; }
282  virtual UT_OptionType getType() const
283  { return ID; }
284  virtual UT_OptionEntry *clone() const
285  { return new UT_OptionEntryImpl(myValue); }
286 
287  virtual int64 getMemoryUsage(bool inclusive) const
288  {
289  int64 mem = inclusive ? sizeof(*this) : 0;
290  return mem;
291  }
292 
293 protected:
295 };
296 
309 
311  public UT_OptionEntryImpl<UT_StringHolder, UT_OPTION_STRING>
312 {
313 public:
315 
317 
318  virtual int64 getMemoryUsage(bool inclusive) const
319  {
320  int64 mem = inclusive ? sizeof(*this) : 0;
321  mem += myValue.getMemoryUsage(inclusive);
322  return mem;
323  }
324 };
325 
327  public UT_OptionEntryImpl<UT_Int64Array, UT_OPTION_INTARRAY>
328 {
329 public:
331 
333  : Base(value) {}
335  : Base(UT_Int64Array())
336  {
337  myValue = value;
338  }
340  : Base(UT_Int64Array())
341  {
342  myValue.setSizeNoInit(value.entries());
343  for (exint i = 0; i < value.entries(); ++i)
344  myValue(i) = value(i);
345  }
346 
347  void fetchValue(UT_Int64Array &value) const { value = myValue; }
349  {
350  value.setSizeNoInit(myValue.entries());
351  for (exint i = 0; i < myValue.entries(); ++i)
352  value(i) = myValue(i);
353  }
354 
355  virtual int64 getMemoryUsage(bool inclusive) const
356  {
357  int64 mem = inclusive ? sizeof(*this) : 0;
358  mem += myValue.getMemoryUsage(false);
359  return mem;
360  }
361 };
362 
364  public UT_OptionEntryImpl<UT_Fpreal64Array, UT_OPTION_FPREALARRAY>
365 {
366 public:
368 
370  : Base(value) {}
373  {
374  myValue = value;
375  }
378  {
379  myValue.setSizeNoInit(value.entries());
380  for (exint i = 0; i < value.entries(); ++i)
381  myValue(i) = value(i);
382  }
383 
384  void fetchValue(UT_Fpreal64Array &value) const { value = myValue; }
386  {
387  value.setSizeNoInit(myValue.entries());
388  for (exint i = 0; i < myValue.entries(); ++i)
389  value(i) = myValue(i);
390  }
391 
392  virtual int64 getMemoryUsage(bool inclusive) const
393  {
394  int64 mem = inclusive ? sizeof(*this) : 0;
395  mem += myValue.getMemoryUsage(false);
396  return mem;
397  }
398 };
399 
401  public UT_OptionEntryImpl<UT_StringArray, UT_OPTION_STRINGARRAY>
402 {
403 public:
405 
408  : Base(UT_StringArray())
409  {
410  myValue.setSize(value.entries());
411  for (exint i = 0; i < value.entries(); ++i)
412  myValue(i) = value(i);
413  }
414 
415  virtual int64 getMemoryUsage(bool inclusive) const
416  {
417  int64 mem = inclusive ? sizeof(*this) : 0;
418  mem += myValue.getMemoryUsage(false);
419  return mem;
420  }
421 };
422 
423 // UT_OptionStringRaw is the same as UT_OptionString, but it returns a getType
424 // value tagging it as a "raw" string so that getPyOptionString() and
425 // appendPyDictionary() won't add quotes around the value.
427 {
428 public:
430 
431  virtual UT_OptionType getType() const { return UT_OPTION_STRINGRAW; }
432 };
433 
434 #endif
UT_OptionEntryImpl< UT_StringHolder, UT_OPTION_STRING > Base
virtual int64 getMemoryUsage(bool inclusive) const
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
UT_OptionFpreal64Array(const UT_Array< fpreal32 > &value)
virtual UT_OptionType getType() const
virtual int64 getMemoryUsage(bool inclusive) const
UT_OptionInt64Array(const UT_Int64Array &value)
UT_OptionFpreal64Array(const UT_Array< fpreal64 > &value)
virtual UT_OptionEntry * clone() const
virtual int64 getMemoryUsage(bool inclusive) const
const GLdouble * v
Definition: glcorearb.h:836
UT_OptionEntryImpl< UT_Int64Array, UT_OPTION_INTARRAY > Base
T myValue
void setSizeNoInit(exint newsize)
Definition: UT_Array.h:485
UT_OptionEntryImpl< UT_Matrix4D, UT_OPTION_MATRIX4 > UT_OptionMatrix4
int64 getMemoryUsage(bool inclusive) const
int64 getMemoryUsage(bool inclusive=false) const
Definition: UT_Array.h:455
void fetchValue(UT_Fpreal32Array &value) const
UT_OptionType
void setSize(exint n)
#define UT_API
Definition: UT_API.h:12
UT_OptionFpreal64Array(const UT_Fpreal64Array &value)
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
static UT_OptionTypeStorage optionStorage(UT_OptionType t)
Return the underlying storage type for the option.
void fetchValue(UT_IntArray &value) const
png_uint_32 i
Definition: png.h:2877
UT_OptionEntryImpl< UT_Vector4D, UT_OPTION_VECTOR4 > UT_OptionVector4
UT_OptionTypeStorage
long long int64
Definition: SYS_Types.h:100
exint optionSize() const
const T & getValue() const
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
int64 getMemoryUsage(bool inclusive) const
int64 exint
Definition: SYS_Types.h:109
UT_OptionEntryImpl< int64, UT_OPTION_INT > UT_OptionInt
virtual UT_OptionType getType() const
double fpreal64
Definition: SYS_Types.h:185
virtual int64 getMemoryUsage(bool inclusive) const
UT_OptionEntryImpl< bool, UT_OPTION_BOOL > UT_OptionBool
UT_OptionTypeStorage optionStorage() const
T value_type
UT_OptionEntryImpl(const T &value)
UT_OptionEntryImpl< UT_Fpreal64Array, UT_OPTION_FPREALARRAY > Base
int cmp(T a, T b)
Definition: ImathFun.h:119
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:446
virtual ~UT_OptionEntryImpl()
UT_OptionEntryImpl< UT_Vector3D, UT_OPTION_UVW > UT_OptionUVW
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
GLsizei const GLfloat * value
Definition: glcorearb.h:823
UT_OptionEntryImpl< UT_Vector2D, UT_OPTION_VECTOR2 > UT_OptionVector2
virtual int64 getMemoryUsage(bool inclusive) const
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
virtual UT_OptionType getType() 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:75
virtual UT_OptionEntry * clone() const
UT_OptionInt64Array(const UT_Array< int32 > &value)
virtual ~UT_OptionEntry()
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
UT_OptionFormat
String formatting options for getOptionString.
UT_OptionStringArray(const UT_StringArray &value)
UT_OptionString(const UT_StringHolder &value)
UT_OptionStringRaw(const UT_StringHolder &value)
UT_OptionEntry()