HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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(myF));
84  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myITuple));
85  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myFTuple));
86 
87  myI = 0;
88  myType.setTupleSize(1);
89  myType.setStorage(UT_Storage::REAL64);
90  }
91  /// NOTE: This assumes that the UT_Defaults hasn't been initialized to
92  /// something that owns memory.
93  template<typename T>
96  {
99  {
100  myI = int64(value);
101  myType.setTupleSize(1);
102  myType.setStorage(UT_Storage::INT64);
103  }
104  else
105  {
106  myF = fpreal64(value);
107  myType.setTupleSize(1);
108  myType.setStorage(UT_Storage::REAL64);
109  }
110  }
111 public:
113  {
114  if (this == &that)
115  return *this;
116 
117  if (getTupleSize() != 1)
118  clear();
119 
120  if (that.getTupleSize() == 1)
121  {
122  copy(that);
123  }
124  else
125  {
126  if (that.getStorage() == UT_Storage::REAL64)
127  {
128  myFTuple = new fpreal64[that.getTupleSize()];
129  memcpy(myFTuple,that.myFTuple,sizeof(fpreal64)*that.getTupleSize());
130  }
131  else
132  {
133  myITuple = new int64[that.getTupleSize()];
134  memcpy(myITuple,that.myITuple,sizeof(int64)*that.getTupleSize());
135  }
136  myType = that.myType;
137  }
138  return *this;
139  }
142  {
143  copy(that);
144  // Indicate that that doesn't have any memory to free.
145  that.myType.setTupleSize(1);
146  return *this;
147  }
150  {
151  // Indicate that *this* doesn't have any memory to free.
152  myType.setTupleSize(1);
153  *this = that;
154  }
157  {
158  copy(that);
159  // Indicate that that doesn't have any memory to free.
160  that.myType.setTupleSize(1);
161  }
162  bool operator==(const UT_Defaults &that) const
163  {
164  if (getTupleSize() != that.getTupleSize())
165  return false;
166  if (getStorage() != that.getStorage())
167  return false;
168  if (getTupleSize() == 1)
169  {
170  if (getStorage() == UT_Storage::REAL64)
171  return myF == that.myF;
172  return myI == that.myI;
173  }
174  exint tuplesize = getTupleSize();
175  if (getStorage() == UT_Storage::REAL64)
176  {
177  for (exint i = 0; i < tuplesize; ++i)
178  {
179  if (myFTuple[i] != that.myFTuple[i])
180  return false;
181  }
182  }
183  else
184  {
185  for (exint i = 0; i < tuplesize; ++i)
186  {
187  if (myITuple[i] != that.myITuple[i])
188  return false;
189  }
190  }
191  return true;
192  }
194  bool operator!=(const UT_Defaults &that) const
195  {
196  return !(*this == that);
197  }
200  {
201  clear();
202  }
204  void clear()
205  {
206  if (myType.getTupleSize() > 1)
207  {
208  if (myType.getStorage()==UT_Storage::REAL64)
209  delete [] myFTuple;
210  else
211  delete [] myITuple;
212  }
213  initZero();
214  }
215  int64 getMemoryUsage(bool inclusive) const
216  {
217  int64 mem = inclusive ? sizeof(*this) : 0;
218  if (myType.getTupleSize() > 1)
219  {
220  if (myType.getStorage()==UT_Storage::REAL64)
221  mem += sizeof(fpreal64)*myType.getTupleSize();
222  else
223  mem += sizeof(int64)*myType.getTupleSize();
224  }
225  return mem;
226  }
227 
229  bool isZero() const
230  {
231  return myI == 0;
232  }
235  {
236  return myType.getStorage();
237  }
240  {
241  return myType.getTupleSize();
242  }
243 
244  fpreal64 getF(exint i = 0) const
245  {
246  // Zero is such a common default that it deserves a special case
247  if (isZero())
248  return 0;
249 
250  // It's also extremely common that all components have
251  // the same default, so we only need one entry.
252  if (myType.getTupleSize()==1)
253  {
254  if (myType.getStorage()==UT_Storage::REAL64)
255  return myF;
256  return fpreal64(myI);
257  }
258 
259  if (i >= myType.getTupleSize())
260  i = myType.getTupleSize()-1;
261  else if (i < 0)
262  i = 0;
263  if (myType.getStorage()==UT_Storage::REAL64)
264  return myFTuple[i];
265  return fpreal64(myITuple[i]);
266  }
267 
268  int64 getI(exint i = 0) const
269  {
270  if (isZero())
271  return 0;
272  if (myType.getTupleSize()==1)
273  {
274  if (myType.getStorage()==UT_Storage::REAL64)
275  return int64(myF);
276  return myI;
277  }
278  if (i >= myType.getTupleSize())
279  i = myType.getTupleSize()-1;
280  else if (i < 0)
281  i = 0;
282  if (myType.getStorage()==UT_Storage::REAL64)
283  return int64(myFTuple[i]);
284  return myITuple[i];
285  }
286 
287 private:
288  void copy(const UT_Defaults &that)
289  {
290  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myF));
291  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myITuple));
292  UT_ASSERT_COMPILETIME(sizeof(myI) >= sizeof(myFTuple));
293 
294  myI = that.myI;
295  myType = that.myType;
296  }
297 
298 protected:
299  union {
304  };
306 };
307 
308 #endif
UT_Storage
Definition: UT_Storage.h:26
int64 getI(exint i=0) const
Definition: UT_Defaults.h:268
SYS_FORCE_INLINE UT_Defaults()
Definition: UT_Defaults.h:27
#define UT_API
Definition: UT_API.h:13
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Defaults.h:234
SYS_FORCE_INLINE UT_Defaults & operator=(UT_Defaults &&that)
Definition: UT_Defaults.h:141
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:194
long long int64
Definition: SYS_Types.h:107
SYS_FORCE_INLINE UT_Defaults(UT_Defaults &&that)
Definition: UT_Defaults.h:156
int64 exint
Definition: SYS_Types.h:116
double fpreal64
Definition: SYS_Types.h:192
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_TupleType<-1, void > myType
Definition: UT_Defaults.h:305
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
fpreal64 myF
Definition: UT_Defaults.h:302
SYS_FORCE_INLINE UT_Defaults(const UT_Defaults &that)
Definition: UT_Defaults.h:149
bool operator==(const UT_Defaults &that) const
Definition: UT_Defaults.h:162
fpreal64 * myFTuple
Definition: UT_Defaults.h:300
SYS_FORCE_INLINE void initValue(T value)
Definition: UT_Defaults.h:95
SYS_FORCE_INLINE bool isZero() const
Definition: UT_Defaults.h:229
GLsizei const GLfloat * value
Definition: glcorearb.h:823
int64 getMemoryUsage(bool inclusive) const
Definition: UT_Defaults.h:215
int64 * myITuple
Definition: UT_Defaults.h:301
fpreal64 getF(exint i=0) const
Definition: UT_Defaults.h:244
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:112
SYS_FORCE_INLINE void clear()
Definition: UT_Defaults.h:204
SYS_FORCE_INLINE ~UT_Defaults()
Definition: UT_Defaults.h:199
SYS_FORCE_INLINE constexpr bool UTisIntStorage(UT_Storage storage)
Returns true iff the given storage type represents an integer.
Definition: UT_Storage.h:40
UT_ASSERT_COMPILETIME(BRAY_EVENT_MAXFLAGS<=32)
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Defaults.h:239
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:308