HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_Defaults.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: GA_Defaults.h ( GA Library, C++)
7  *
8  * COMMENTS: Defaults for attribute data
9  */
10 
11 #ifndef __GA_Defaults__
12 #define __GA_Defaults__
13 
14 #include "GA_API.h"
15 #include "GA_Types.h"
16 #include <UT/UT_Defaults.h>
17 #include <SYS/SYS_Types.h>
18 #include <SYS/SYS_Inline.h>
19 #include <SYS/SYS_StaticAssert.h>
20 
21 class UT_JSONWriter;
22 class UT_JSONParser;
23 class UT_Options;
24 class UT_StringRef;
25 
26 #define GA_DEFAULT_BUFSIZE 4
27 
28 #define USE_UT_DEFAULTS 1
29 
30 ///@brief Class which stores the default values for a GA_Attribute
31 ///
32 /// This class stores a list of integer/real values which can be used to
33 /// initialize attribute data.
34 #if USE_UT_DEFAULTS
36 #else
38 #endif
39 {
40 public:
41  typedef UT_Defaults Base;
42 
43  /// Default constructor -- the default will be 0.
44 #if USE_UT_DEFAULTS
45  GA_Defaults() : Base() {}
46 #else
47  GA_Defaults() { ctorInit(); }
48 #endif
49 
50  /// Construct by extracting data out of a UT_Options
51  explicit GA_Defaults(const UT_Options *options);
52 
53  /// Construct a set of defaults from variadic arguments. For example
54  /// @code
55  /// GA_Defaults a(GA_STORE_INT32, 3, 0, 1, 2);
56  /// GA_Defaults a(GA_STORE_INT64, 2, int64(0), int64(1));
57  /// GA_Defaults b(GA_STORE_REAL64, 1, fpreal64(0));
58  /// @endcode
59  ///
60  /// This style of construction is a little bit dangerous, so please ensure
61  /// - All data arguments are the right type
62  /// - You have the proper count of variadic arguments
63  /// - You use only one of the supported types: INT32, INT64 or REAL64
64  /// All other types will either cause compiler errors or crashing
65  /// Otherwise, core dumping may occur.
67 
68  /// @{
69  /// Type classes to construct an identity matrix / quaternion as a default.
70  class quaternion {};
71  class matrix3 {};
72  class matrix4 {};
76  /// @}
77 
78  /// @{
79  /// Construct by repeating the value
80  explicit GA_Defaults(int64 value) // Repeated value
81 #if USE_UT_DEFAULTS
82  : Base(value)
83 #endif
84  {
85 #if !USE_UT_DEFAULTS
86  myData.myInt = myBuffer.myInt;
87  myBuffer.myInt[0] = value;
88  mySize = 1;
89  myStorage = GA_STORE_INT64;
90 #endif
91  }
92  explicit GA_Defaults(int32 value) // Repeated value
93 #if USE_UT_DEFAULTS
94  : Base(value)
95 #endif
96  {
97 #if !USE_UT_DEFAULTS
98  myData.myInt = myBuffer.myInt;
99  myBuffer.myInt[0] = value;
100  mySize = 1;
101  myStorage = GA_STORE_INT64;
102 #endif
103  }
104  /// Construct by repeating the value
105  explicit GA_Defaults(fpreal64 value) // Repeated value
106 #if USE_UT_DEFAULTS
107  : Base(value)
108 #endif
109  {
110 #if !USE_UT_DEFAULTS
111  myData.myReal = myBuffer.myReal;
112  myBuffer.myReal[0] = value;
113  mySize = 1;
114  myStorage = GA_STORE_REAL64;
115 #endif
116  }
117  explicit GA_Defaults(fpreal32 value) // Repeated value
118 #if USE_UT_DEFAULTS
119  : Base(value)
120 #endif
121  {
122 #if !USE_UT_DEFAULTS
123  myData.myReal = myBuffer.myReal;
124  myBuffer.myReal[0] = value;
125  mySize = 1;
126  myStorage = GA_STORE_REAL64;
127 #endif
128  }
129  /// @}
130 
131  /// @{
132  /// Construct by passing a vector of scalar values
133  /// Note that the defaults will @b copy the data out of the values passed
134  /// in (so the array doesn't have to be persistent).
135 #if USE_UT_DEFAULTS
136  GA_Defaults(const int32 *values, int count) : Base(values, count) {}
137  GA_Defaults(const int64 *values, int count) : Base(values, count) {}
138  GA_Defaults(const fpreal32 *values, int count) : Base(values, count) {}
139  GA_Defaults(const fpreal64 *values, int count) : Base(values, count) {}
140 #else
141  GA_Defaults(const int32 *values, int count);
142  GA_Defaults(const int64 *values, int count);
143  GA_Defaults(const fpreal32 *values, int count);
144  GA_Defaults(const fpreal64 *values, int count);
145 #endif
146  /// @}
147 
148  /// Copy constructor
149 #if USE_UT_DEFAULTS
150  GA_Defaults(const GA_Defaults &src) : Base((const Base &)src) {}
152 
154  {
155  *((Base*)this) = (const Base&)src;
156  return *this;
157  }
158 #else
159  GA_Defaults(const GA_Defaults &src)
160  { ctorInit(); copy(src); }
161  ~GA_Defaults()
162  { clear(); }
163 
165  { copy(src); return *this; }
166 #endif
167 
168 #if USE_UT_DEFAULTS
169  /// Copy values from another set of defaults
171  {
172  *this = src;
173  }
174 #else
175  /// Report memory usage
176  int64 getMemoryUsage(bool inclusive) const;
177 
178  /// Copy values from another set of defaults
179  SYS_FORCE_INLINE void copy(const GA_Defaults &src)
180  {
181  // Free our buffer if present.
182  clear();
183  if (src.mySize)
184  {
185  if (src.mySize <= GA_DEFAULT_BUFSIZE)
186  {
187  // Build inline!
188  myData.myInt = myBuffer.myInt;
190  myData.myInt[0] = src.myData.myInt[0];
191  myData.myInt[1] = src.myData.myInt[1];
192  myData.myInt[2] = src.myData.myInt[2];
193  myData.myInt[3] = src.myData.myInt[3];
194  myStorage = src.myStorage;
195  mySize = src.mySize;
196  }
197  else
198  {
199  switch (src.myStorage)
200  {
201  case GA_STORE_INT64:
202  set(src.myData.myInt, src.mySize);
203  break;
204  case GA_STORE_REAL64:
205  set(src.myData.myReal, src.mySize);
206  break;
207  default:
208  UT_ASSERT(0);
209  }
210  }
211  }
212  }
213 #endif
214 
215  /// Set the GA_OPTION_DEFAULTS option in the options
216  /// If @c always is false and there are no defaults (i.e. getSize() == 0),
217  /// then the options will not be written.
218  void saveToOptions(UT_Options &options, bool always=true) const;
219 
220  /// @{
221  /// Get a value out of the defaults.
222  /// - Automatically casts defaults into appropriate return type
223  /// - Clamps index to tuple size
224  /// - If there is no default data, then, the result will be 0
225 #if !USE_UT_DEFAULTS
226  int64 getI(int index=0) const;
227  fpreal getF(int index=0) const;
228 #endif
229  void get(int index, int32 &v) const
230  { v = getI(index); }
231  void get(int index, int64 &v) const
232  { v = getI(index); }
233  void get(int index, fpreal32 &v) const
234  { v = getF(index); }
235  void get(int index, fpreal64 &v) const
236  { v = getF(index); }
237  /// @}
238 
239  /// Set defaults to the list of integer values.
240  void set(const int32 *values, int count);
241  /// Set defaults to the list of integer values.
242  void set(const int64 *values, int count);
243  /// Set defaults to the list of real values.
244  void set(const fpreal32 *values, int count);
245  /// Set defaults to the list of real values.
246  void set(const fpreal64 *values, int count);
247 
248  /// Set this to a reasonable guess based on the specified name,
249  /// e.g. Cd -> 1.0; id -> -1; orient -> (0,0,0,1)
250  void guessFromName(const UT_StringRef &name, int tuple_size);
251 
252  /// Get the size of the defaults.
253  /// You can have an attribute tuple size that's
254  /// larger than its GA_Defaults size; the GA_Defaults
255  /// will repeat its last element.
257  int size() const
258  {
259 #if USE_UT_DEFAULTS
260  return getTupleSize();
261 #else
262  return mySize;
263 #endif
264  }
265 
266  /// Save data to a JSON stream.
267  /// @section JSON-GA_Defaults JSON Schema: GA_Defaults
268  /// @code
269  /// {
270  /// "name" : "GA_Defaults",
271  /// "description" : "An array of numbers used for attribute defaults",
272  /// "type" : "orderedmap",
273  /// "properties": {
274  /// "size": {
275  /// "type" : "integer",
276  /// "description" : "Number of values",
277  /// },
278  /// "storage": {
279  /// "type" : "string",
280  /// "enum" : ["fpreal64", "int64"],
281  /// "description" : "The type data",
282  /// },
283  /// "values": {
284  /// "type" : "array",
285  /// "items" : "number",
286  /// "description" : "The default values",
287  /// },
288  /// },
289  /// }
290  /// @endcode
291  /// @see @ref JSON_FileFormat
292  bool jsonSave(UT_JSONWriter &w) const;
293 
294  /// Load data from a JSON stream
295  bool jsonLoad(UT_JSONParser &p);
296 
297 #if !USE_UT_DEFAULTS
298 private:
299  SYS_FORCE_INLINE void clear()
300  {
301  if (mySize > GA_DEFAULT_BUFSIZE)
302  {
303  switch (myStorage)
304  {
305  case GA_STORE_INT64:
306  UT_ASSERT(myData.myInt != myBuffer.myInt);
307  delete [] myData.myInt;
308  break;
309  case GA_STORE_REAL64:
310  UT_ASSERT(myData.myReal != myBuffer.myReal);
311  delete [] myData.myReal;
312  break;
313  default:
314  UT_ASSERT(0);
315  }
316  }
317  mySize = 0;
318  }
319  SYS_FORCE_INLINE void ctorInit()
320  {
321  myData.myInt = 0;
322  myStorage = GA_STORE_INT64;
323  mySize = 0;
324  }
325 
326  union {
327  int64 *myInt;
328  fpreal64 *myReal;
329  } myData;
330  union {
331  int64 myInt[GA_DEFAULT_BUFSIZE];
333  } myBuffer;
334  GA_Storage myStorage;
335  int mySize;
336 #endif
337 };
338 
339 #endif
340 
#define SYS_STATIC_ASSERT(expr)
int int32
Definition: SYS_Types.h:39
GA_Defaults(const int64 *values, int count)
Definition: GA_Defaults.h:137
GA_Defaults()
Default constructor – the default will be 0.
Definition: GA_Defaults.h:45
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
Class which stores the default values for a GA_Attribute.
Definition: GA_Defaults.h:35
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
const GLdouble * v
Definition: glcorearb.h:837
GA_Defaults & operator=(const GA_Defaults &src)
Definition: GA_Defaults.h:153
GLsizei const GLfloat * value
Definition: glcorearb.h:824
int64 getI(exint i=0) const
Definition: UT_Defaults.h:268
GA_Defaults(fpreal64 value)
Construct by repeating the value.
Definition: GA_Defaults.h:105
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define GA_API
Definition: GA_API.h:14
#define GA_DEFAULT_BUFSIZE
Definition: GA_Defaults.h:26
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
float fpreal32
Definition: SYS_Types.h:200
void guessFromName(const UT_StringRef &name, int tuple_size)
GA_Defaults(const GA_Defaults &src)
Copy constructor.
Definition: GA_Defaults.h:150
double fpreal64
Definition: SYS_Types.h:201
bool jsonSave(UT_JSONWriter &w) const
SYS_FORCE_INLINE void copy(const GA_Defaults &src)
Copy values from another set of defaults.
Definition: GA_Defaults.h:170
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE int size() const
Definition: GA_Defaults.h:257
GA_Defaults(int32 value)
Definition: GA_Defaults.h:92
long long int64
Definition: SYS_Types.h:116
GA_Defaults(const fpreal64 *values, int count)
Definition: GA_Defaults.h:139
bool jsonLoad(UT_JSONParser &p)
Load data from a JSON stream.
GLuint const GLchar * name
Definition: glcorearb.h:786
UT_Defaults Base
Definition: GA_Defaults.h:41
A map of string to various well defined value types.
Definition: UT_Options.h:84
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Defaults.h:215
fpreal64 getF(exint i=0) const
Definition: UT_Defaults.h:244
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
GA_Defaults(fpreal32 value)
Definition: GA_Defaults.h:117
GA_Defaults(const fpreal32 *values, int count)
Definition: GA_Defaults.h:138
void set(const int32 *values, int count)
Set defaults to the list of integer values.
GA_Defaults(int64 value)
Definition: GA_Defaults.h:80
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: core.h:1131
SYS_FORCE_INLINE void clear()
Definition: UT_Defaults.h:204
GA_Defaults(const int32 *values, int count)
Definition: GA_Defaults.h:136
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Defaults.h:239
GA_Storage
Definition: GA_Types.h:51
void saveToOptions(UT_Options &options, bool always=true) const
GLint GLsizei count
Definition: glcorearb.h:405
GLenum src
Definition: glcorearb.h:1793