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
37 class GA_API GA_Defaults
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 as a default
70  class matrix3 {};
71  class matrix4 {};
74 
75  /// @{
76  /// Construct by repeating the value
77  explicit GA_Defaults(int64 value) // Repeated value
78 #if USE_UT_DEFAULTS
79  : Base(value)
80 #endif
81  {
82 #if !USE_UT_DEFAULTS
83  myData.myInt = myBuffer.myInt;
84  myBuffer.myInt[0] = value;
85  mySize = 1;
86  myStorage = GA_STORE_INT64;
87 #endif
88  }
89  explicit GA_Defaults(int32 value) // Repeated value
90 #if USE_UT_DEFAULTS
91  : Base(value)
92 #endif
93  {
94 #if !USE_UT_DEFAULTS
95  myData.myInt = myBuffer.myInt;
96  myBuffer.myInt[0] = value;
97  mySize = 1;
98  myStorage = GA_STORE_INT64;
99 #endif
100  }
101  /// Construct by repeating the value
102  explicit GA_Defaults(fpreal64 value) // Repeated value
103 #if USE_UT_DEFAULTS
104  : Base(value)
105 #endif
106  {
107 #if !USE_UT_DEFAULTS
108  myData.myReal = myBuffer.myReal;
109  myBuffer.myReal[0] = value;
110  mySize = 1;
111  myStorage = GA_STORE_REAL64;
112 #endif
113  }
114  explicit GA_Defaults(fpreal32 value) // Repeated value
115 #if USE_UT_DEFAULTS
116  : Base(value)
117 #endif
118  {
119 #if !USE_UT_DEFAULTS
120  myData.myReal = myBuffer.myReal;
121  myBuffer.myReal[0] = value;
122  mySize = 1;
123  myStorage = GA_STORE_REAL64;
124 #endif
125  }
126  /// @}
127 
128  /// @{
129  /// Construct by passing a vector of scalar values
130  /// Note that the defaults will @b copy the data out of the values passed
131  /// in (so the array doesn't have to be persistent).
132 #if USE_UT_DEFAULTS
133  GA_Defaults(const int32 *values, int count) : Base(values, count) {}
134  GA_Defaults(const int64 *values, int count) : Base(values, count) {}
135  GA_Defaults(const fpreal32 *values, int count) : Base(values, count) {}
136  GA_Defaults(const fpreal64 *values, int count) : Base(values, count) {}
137 #else
138  GA_Defaults(const int32 *values, int count);
139  GA_Defaults(const int64 *values, int count);
140  GA_Defaults(const fpreal32 *values, int count);
141  GA_Defaults(const fpreal64 *values, int count);
142 #endif
143  /// @}
144 
145  /// Copy constructor
146 #if USE_UT_DEFAULTS
147  GA_Defaults(const GA_Defaults &src) : Base((const Base &)src) {}
149 
151  {
152  *((Base*)this) = (const Base&)src;
153  return *this;
154  }
155 #else
156  GA_Defaults(const GA_Defaults &src)
157  { ctorInit(); copy(src); }
158  ~GA_Defaults()
159  { clear(); }
160 
162  { copy(src); return *this; }
163 #endif
164 
165 #if USE_UT_DEFAULTS
166  /// Copy values from another set of defaults
168  {
169  *this = src;
170  }
171 #else
172  /// Report memory usage
173  int64 getMemoryUsage(bool inclusive) const;
174 
175  /// Copy values from another set of defaults
176  SYS_FORCE_INLINE void copy(const GA_Defaults &src)
177  {
178  // Free our buffer if present.
179  clear();
180  if (src.mySize)
181  {
182  if (src.mySize <= GA_DEFAULT_BUFSIZE)
183  {
184  // Build inline!
185  myData.myInt = myBuffer.myInt;
187  myData.myInt[0] = src.myData.myInt[0];
188  myData.myInt[1] = src.myData.myInt[1];
189  myData.myInt[2] = src.myData.myInt[2];
190  myData.myInt[3] = src.myData.myInt[3];
191  myStorage = src.myStorage;
192  mySize = src.mySize;
193  }
194  else
195  {
196  switch (src.myStorage)
197  {
198  case GA_STORE_INT64:
199  set(src.myData.myInt, src.mySize);
200  break;
201  case GA_STORE_REAL64:
202  set(src.myData.myReal, src.mySize);
203  break;
204  default:
205  UT_ASSERT(0);
206  }
207  }
208  }
209  }
210 #endif
211 
212  /// Set the GA_OPTION_DEFAULTS option in the options
213  /// If @c always is false and there are no defaults (i.e. getSize() == 0),
214  /// then the options will not be written.
215  void saveToOptions(UT_Options &options, bool always=true) const;
216 
217  /// @{
218  /// Get a value out of the defaults.
219  /// - Automatically casts defaults into appropriate return type
220  /// - Clamps index to tuple size
221  /// - If there is no default data, then, the result will be 0
222 #if !USE_UT_DEFAULTS
223  int64 getI(int index=0) const;
224  fpreal getF(int index=0) const;
225 #endif
226  void get(int index, int32 &v) const
227  { v = getI(index); }
228  void get(int index, int64 &v) const
229  { v = getI(index); }
230  void get(int index, fpreal32 &v) const
231  { v = getF(index); }
232  void get(int index, fpreal64 &v) const
233  { v = getF(index); }
234  /// @}
235 
236  /// Set defaults to the list of integer values.
237  void set(const int32 *values, int count);
238  /// Set defaults to the list of integer values.
239  void set(const int64 *values, int count);
240  /// Set defaults to the list of real values.
241  void set(const fpreal32 *values, int count);
242  /// Set defaults to the list of real values.
243  void set(const fpreal64 *values, int count);
244 
245  /// Set this to a reasonable guess based on the specified name,
246  /// e.g. Cd -> 1.0; id -> -1; orient -> (0,0,0,1)
247  void guessFromName(const UT_StringRef &name, int tuple_size);
248 
249  /// Get the size of the defaults.
250  /// You can have an attribute tuple size that's
251  /// larger than its GA_Defaults size; the GA_Defaults
252  /// will repeat its last element.
254  int size() const
255  {
256 #if USE_UT_DEFAULTS
257  return getTupleSize();
258 #else
259  return mySize;
260 #endif
261  }
262 
263  /// Save data to a JSON stream.
264  /// @section JSON-GA_Defaults JSON Schema: GA_Defaults
265  /// @code
266  /// {
267  /// "name" : "GA_Defaults",
268  /// "description" : "An array of numbers used for attribute defaults",
269  /// "type" : "orderedmap",
270  /// "properties": {
271  /// "size": {
272  /// "type" : "integer",
273  /// "description" : "Number of values",
274  /// },
275  /// "storage": {
276  /// "type" : "string",
277  /// "enum" : ["fpreal64", "int64"],
278  /// "description" : "The type data",
279  /// },
280  /// "values": {
281  /// "type" : "array",
282  /// "items" : "number",
283  /// "description" : "The default values",
284  /// },
285  /// },
286  /// }
287  /// @endcode
288  /// @see @ref JSON_FileFormat
289  bool jsonSave(UT_JSONWriter &w) const;
290 
291  /// Load data from a JSON stream
292  bool jsonLoad(UT_JSONParser &p);
293 
294 #if !USE_UT_DEFAULTS
295 private:
296  SYS_FORCE_INLINE void clear()
297  {
298  if (mySize > GA_DEFAULT_BUFSIZE)
299  {
300  switch (myStorage)
301  {
302  case GA_STORE_INT64:
303  UT_ASSERT(myData.myInt != myBuffer.myInt);
304  delete [] myData.myInt;
305  break;
306  case GA_STORE_REAL64:
307  UT_ASSERT(myData.myReal != myBuffer.myReal);
308  delete [] myData.myReal;
309  break;
310  default:
311  UT_ASSERT(0);
312  }
313  }
314  mySize = 0;
315  }
316  SYS_FORCE_INLINE void ctorInit()
317  {
318  myData.myInt = 0;
319  myStorage = GA_STORE_INT64;
320  mySize = 0;
321  }
322 
323  union {
324  int64 *myInt;
325  fpreal64 *myReal;
326  } myData;
327  union {
328  int64 myInt[GA_DEFAULT_BUFSIZE];
330  } myBuffer;
331  GA_Storage myStorage;
332  int mySize;
333 #endif
334 };
335 
336 #endif
337 
#define SYS_STATIC_ASSERT(expr)
GA_Defaults(const int64 *values, int count)
Definition: GA_Defaults.h:134
GA_Defaults()
Default constructor – the default will be 0.
Definition: GA_Defaults.h:45
Class which stores the default values for a GA_Attribute.
Definition: GA_Defaults.h:35
const GLdouble * v
Definition: glcorearb.h:836
GA_Defaults & operator=(const GA_Defaults &src)
Definition: GA_Defaults.h:150
int64 getI(exint i=0) const
Definition: UT_Defaults.h:268
GA_Defaults(fpreal64 value)
Construct by repeating the value.
Definition: GA_Defaults.h:102
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:75
#define GA_API
Definition: GA_API.h:12
#define GA_DEFAULT_BUFSIZE
Definition: GA_Defaults.h:26
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:32
void guessFromName(const UT_StringRef &name, int tuple_size)
GA_Defaults(const GA_Defaults &src)
Copy constructor.
Definition: GA_Defaults.h:147
bool jsonSave(UT_JSONWriter &w) const
long long int64
Definition: SYS_Types.h:107
SYS_FORCE_INLINE void copy(const GA_Defaults &src)
Copy values from another set of defaults.
Definition: GA_Defaults.h:167
double fpreal64
Definition: SYS_Types.h:192
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE int size() const
Definition: GA_Defaults.h:254
GA_Defaults(int32 value)
Definition: GA_Defaults.h:89
GA_Defaults(const fpreal64 *values, int count)
Definition: GA_Defaults.h:136
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
bool jsonLoad(UT_JSONParser &p)
Load data from a JSON stream.
GLuint const GLchar * name
Definition: glcorearb.h:785
int int32
Definition: SYS_Types.h:35
UT_Defaults Base
Definition: GA_Defaults.h:41
GLint GLsizei count
Definition: glcorearb.h:404
GLsizei const GLfloat * value
Definition: glcorearb.h:823
double fpreal
Definition: SYS_Types.h:270
A map of string to various well defined value types.
Definition: UT_Options.h:42
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Defaults.h:215
fpreal64 getF(exint i=0) const
Definition: UT_Defaults.h:244
GLuint index
Definition: glcorearb.h:785
GA_Defaults(fpreal32 value)
Definition: GA_Defaults.h:114
GA_Defaults(const fpreal32 *values, int count)
Definition: GA_Defaults.h:135
void set(const int32 *values, int count)
Set defaults to the list of integer values.
getOption("OpenEXR.storage") storage
Definition: HDK_Image.dox:276
GA_Defaults(int64 value)
Definition: GA_Defaults.h:77
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
#define const
Definition: zconf.h:214
SYS_FORCE_INLINE void clear()
Definition: UT_Defaults.h:204
GA_Defaults(const int32 *values, int count)
Definition: GA_Defaults.h:133
float fpreal32
Definition: SYS_Types.h:191
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Defaults.h:239
GA_Storage
Definition: GA_Types.h:48
void saveToOptions(UT_Options &options, bool always=true) const
GLenum src
Definition: glcorearb.h:1792