HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_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: UT_Defaults.h (UT Library, C++)
7  *
8  * COMMENTS: A class for representing default values for tuples of arbitrary length.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_Defaults__
14 #define __UT_Defaults__
15 
16 #include "UT_API.h"
17 #include "UT_Assert.h"
18 #include "UT_Storage.h"
19 #include <SYS/SYS_Inline.h>
20 #include <SYS/SYS_Types.h>
21 #include <string.h>
22 
24 {
25 public:
28  {
29  initZero();
30  }
31  template<typename T>
32  explicit SYS_FORCE_INLINE
34  {
35  initValue(value);
36  }
37  template<typename T>
38  UT_Defaults(const T *values, exint tuplesize)
39  {
41  if (tuplesize <= 0)
42  {
43  initZero();
44  return;
45  }
46 
47  // Check for constant value
48  T constvalue = values[0];
49  bool equal = true;
50  for (exint i = 1; i < tuplesize; ++i)
51  {
52  if (values[i] != constvalue)
53  {
54  equal = false;
55  break;
56  }
57  }
58  if (equal)
59  initValue(constvalue);
61  {
62  myITuple = new int64[tuplesize];
63  for (exint i = 0; i < tuplesize; ++i)
64  myITuple[i] = int64(values[i]);
65  myType.setTupleSize(tuplesize);
66  myType.setStorage(UT_Storage::INT64);
67  }
68  else
69  {
70  myFTuple = new fpreal64[tuplesize];
71  for (exint i = 0; i < tuplesize; ++i)
72  myFTuple[i] = fpreal64(values[i]);
73  myType.setTupleSize(tuplesize);
74  myType.setStorage(UT_Storage::REAL64);
75  }
76  }
77 protected:
78  /// NOTE: This assumes that the UT_Defaults hasn't been initialized to
79  /// something that owns memory.
81  void initZero()
82  {
83  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myITuple));
84  myI = 0;
85  myType.setTupleSize(1);
86  myType.setStorage(UT_Storage::REAL64);
87  }
88  /// NOTE: This assumes that the UT_Defaults hasn't been initialized to
89  /// something that owns memory.
90  template<typename T>
93  {
96  {
97  myI = int64(value);
98  myType.setTupleSize(1);
99  myType.setStorage(UT_Storage::INT64);
100  }
101  else
102  {
103  myF = fpreal64(value);
104  myType.setTupleSize(1);
105  myType.setStorage(UT_Storage::REAL64);
106  }
107  }
108 public:
110  {
111  if (this == &that)
112  return *this;
113 
114  if (getTupleSize() != 1)
115  clear();
116 
117  if (that.getTupleSize() == 1)
118  {
119  memcpy(this, &that, sizeof(UT_Defaults));
120  }
121  else
122  {
123  if (that.getStorage() == UT_Storage::REAL64)
124  {
125  myFTuple = new fpreal64[that.getTupleSize()];
126  memcpy(myFTuple,that.myFTuple,sizeof(fpreal64)*that.getTupleSize());
127  }
128  else
129  {
130  myITuple = new int64[that.getTupleSize()];
131  memcpy(myITuple,that.myITuple,sizeof(int64)*that.getTupleSize());
132  }
133  myType = that.myType;
134  }
135  return *this;
136  }
139  {
140  memcpy(this, &that, sizeof(UT_Defaults));
141  // Indicate that that doesn't have any memory to free.
142  that.myType.setTupleSize(1);
143  return *this;
144  }
147  {
148  // Indicate that *this* doesn't have any memory to free.
149  myType.setTupleSize(1);
150  *this = that;
151  }
154  {
155  memcpy(this, &that, sizeof(UT_Defaults));
156  // Indicate that that doesn't have any memory to free.
157  that.myType.setTupleSize(1);
158  }
159  bool operator==(const UT_Defaults &that) const
160  {
161  if (getTupleSize() != that.getTupleSize())
162  return false;
163  if (getStorage() != that.getStorage())
164  return false;
165  if (getTupleSize() == 1)
166  {
167  if (getStorage() == UT_Storage::REAL64)
168  return myF == that.myF;
169  return myI == that.myI;
170  }
171  exint tuplesize = getTupleSize();
172  if (getStorage() == UT_Storage::REAL64)
173  {
174  for (exint i = 0; i < tuplesize; ++i)
175  {
176  if (myFTuple[i] != that.myFTuple[i])
177  return false;
178  }
179  }
180  else
181  {
182  for (exint i = 0; i < tuplesize; ++i)
183  {
184  if (myITuple[i] != that.myITuple[i])
185  return false;
186  }
187  }
188  return true;
189  }
191  bool operator!=(const UT_Defaults &that) const
192  {
193  return !(*this == that);
194  }
197  {
198  clear();
199  }
201  void clear()
202  {
203  if (myType.getTupleSize() > 1)
204  {
205  if (myType.getStorage()==UT_Storage::REAL64)
206  delete [] myFTuple;
207  else
208  delete [] myITuple;
209  }
210  initZero();
211  }
212  int64 getMemoryUsage(bool inclusive) const
213  {
214  int64 mem = inclusive ? sizeof(*this) : 0;
215  if (myType.getTupleSize() > 1)
216  {
217  if (myType.getStorage()==UT_Storage::REAL64)
218  mem += sizeof(fpreal64)*myType.getTupleSize();
219  else
220  mem += sizeof(int64)*myType.getTupleSize();
221  }
222  return mem;
223  }
224 
226  bool isZero() const
227  {
228  return myI == 0;
229  }
232  {
233  return myType.getStorage();
234  }
237  {
238  return myType.getTupleSize();
239  }
240 
241  fpreal64 getF(exint i = 0) const
242  {
243  // Zero is such a common default that it deserves a special case
244  if (isZero())
245  return 0;
246 
247  // It's also extremely common that all components have
248  // the same default, so we only need one entry.
249  if (myType.getTupleSize()==1)
250  {
251  if (myType.getStorage()==UT_Storage::REAL64)
252  return myF;
253  return fpreal64(myI);
254  }
255 
256  if (i >= myType.getTupleSize())
257  i = myType.getTupleSize()-1;
258  else if (i < 0)
259  i = 0;
260  if (myType.getStorage()==UT_Storage::REAL64)
261  return myFTuple[i];
262  return fpreal64(myITuple[i]);
263  }
264 
265  int64 getI(exint i = 0) const
266  {
267  if (isZero())
268  return 0;
269  if (myType.getTupleSize()==1)
270  {
271  if (myType.getStorage()==UT_Storage::REAL64)
272  return int64(myF);
273  return myI;
274  }
275  if (i >= myType.getTupleSize())
276  i = myType.getTupleSize()-1;
277  else if (i < 0)
278  i = 0;
279  if (myType.getStorage()==UT_Storage::REAL64)
280  return int64(myFTuple[i]);
281  return myITuple[i];
282  }
283 
284 protected:
285  union {
290  };
292 };
293 
294 #endif
#define UT_ASSERT_COMPILETIME(expr)
Definition: UT_Assert.h:109
UT_Storage
Definition: UT_Storage.h:26
int64 getI(exint i=0) const
Definition: UT_Defaults.h:265
SYS_FORCE_INLINE UT_Defaults()
Definition: UT_Defaults.h:27
#define UT_API
Definition: UT_API.h:12
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Defaults.h:231
SYS_FORCE_INLINE UT_Defaults & operator=(UT_Defaults &&that)
Definition: UT_Defaults.h:138
png_uint_32 i
Definition: png.h:2877
SYS_FORCE_INLINE UT_Defaults(T value)
Definition: UT_Defaults.h:33
SYS_FORCE_INLINE bool operator!=(const UT_Defaults &that) const
Definition: UT_Defaults.h:191
long long int64
Definition: SYS_Types.h:106
SYS_FORCE_INLINE UT_Defaults(UT_Defaults &&that)
Definition: UT_Defaults.h:153
int64 exint
Definition: SYS_Types.h:115
double fpreal64
Definition: SYS_Types.h:191
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_TupleType<-1, void > myType
Definition: UT_Defaults.h:291
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
fpreal64 myF
Definition: UT_Defaults.h:288
SYS_FORCE_INLINE UT_Defaults(const UT_Defaults &that)
Definition: UT_Defaults.h:146
bool operator==(const UT_Defaults &that) const
Definition: UT_Defaults.h:159
fpreal64 * myFTuple
Definition: UT_Defaults.h:286
SYS_FORCE_INLINE void initValue(T value)
Definition: UT_Defaults.h:92
SYS_FORCE_INLINE bool isZero() const
Definition: UT_Defaults.h:226
GLsizei const GLfloat * value
Definition: glcorearb.h:823
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Defaults.h:212
int64 * myITuple
Definition: UT_Defaults.h:287
fpreal64 getF(exint i=0) const
Definition: UT_Defaults.h:241
bool equal(T1 a, T2 b, T3 t)
Definition: ImathFun.h:143
UT_Defaults(const T *values, exint tuplesize)
Definition: UT_Defaults.h:38
UT_Defaults & operator=(const UT_Defaults &that)
Definition: UT_Defaults.h:109
SYS_FORCE_INLINE void clear()
Definition: UT_Defaults.h:201
SYS_FORCE_INLINE ~UT_Defaults()
Definition: UT_Defaults.h:196
SYS_FORCE_INLINE constexpr bool UTisIntStorage(UT_Storage storage)
Returns true iff the given storage type represents an integer.
Definition: UT_Storage.h:40
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Defaults.h:236
SYS_FORCE_INLINE void initZero()
Definition: UT_Defaults.h:81
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324