HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Storage.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_Storage.h (UT Library, C++)
7  *
8  * COMMENTS: An enum for common, numeric data types.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_Storage__
14 #define __UT_Storage__
15 
16 #include <SYS/SYS_Types.h>
17 #include <SYS/SYS_TypeTraits.h>
18 #include <SYS/SYS_Inline.h>
19 
20 #include "UT_Assert.h"
21 
22 /// An enum representing numeric data storage types.
23 ///
24 /// NOTE: The order must be the conversion precedence order.
25 /// For example, int8(0)+int16(0) will be an int16,
26 /// and int64(0)+fpreal32(0) will be an fpreal32.
27 /// This enables UT_StorageBetter.
28 enum class UT_Storage
29 {
30  INVALID = -1,
31  INT8,
32  UINT8,
33  INT16,
34  INT32,
35  INT64,
36  REAL16,
37  REAL32,
38  REAL64
39 };
40 
41 /// Returns true iff the given storage type represents an integer.
44 {
45  return (int(storage) >= int(UT_Storage::INT8) && int(storage) <= int(UT_Storage::INT64));
46 }
47 
48 /// Returns true iff the given storage type represents a floating-point number.
51 {
52  return (int(storage) >= int(UT_Storage::REAL16) && int(storage) <= int(UT_Storage::REAL64));
53 }
54 
55 /// Returns the number of bytes in the given storage type.
58 {
59  switch (storage)
60  {
62  return 0;
63  case UT_Storage::INT8:
64  case UT_Storage::UINT8:
65  return 1;
66  case UT_Storage::INT16:
67  case UT_Storage::REAL16:
68  return 2;
69  case UT_Storage::INT32:
70  case UT_Storage::REAL32:
71  return 4;
72  case UT_Storage::INT64:
73  case UT_Storage::REAL64:
74  return 8;
75  }
76 
77  // gcc can't figure out that the switch statement above always returns...
78  return 0;
79 }
80 
81 /// These template classes get the UT_Storage from the type, e.g.
82 /// UT_Storage s = UT_StorageType<DATA_T>::theStorage;
83 /// @{
84 template<typename T>
85 struct UT_StorageNum {
87  typedef void Type;
88  typedef void AtLeast32Bit;
90  typedef void SecondGuess;
91  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
92  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
93 };
94 template<>
97  typedef int8 Type;
101  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
102  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
103 };
104 template<>
107  typedef uint8 Type;
111  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
112  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
113 };
114 template<>
117  typedef int16 Type;
121  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
122  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
123 };
124 template<>
127  typedef int32 Type;
131  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
132  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
133 };
134 template<>
137  typedef int64 Type;
141  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
142  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
143 };
144 template<>
147  typedef fpreal16 Type;
151  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
152  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
153 };
154 template<>
157  typedef fpreal32 Type;
161  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
162  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
163 };
164 template<>
167  typedef fpreal64 Type;
171  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
172  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
173 };
174 
175 template<typename T>
177 
178 /// @}
179 
180 /// These template classes get the type from the UT_Storage, e.g.
181 /// return (UT_StorageType<STORAGE>::theType)source;
182 /// @{
183 template<UT_Storage STORAGE>
186  typedef void Type;
187  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
188  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
189 };
190 template<>
193  typedef int8 Type;
194  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
195  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
196 };
197 template<>
200  typedef uint8 Type;
201  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
202  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
203 };
204 template<>
207  typedef int16 Type;
208  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
209  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
210 };
211 template<>
214  typedef int32 Type;
215  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
216  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
217 };
218 template<>
221  typedef int64 Type;
222  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
223  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
224 };
225 template<>
228  typedef fpreal16 Type;
229  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
230  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
231 };
232 template<>
235  typedef fpreal32 Type;
236  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
237  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
238 };
239 template<>
242  typedef fpreal64 Type;
243  static constexpr bool theIsFloat = SYS_IsFloatingPoint_v< Type >;
244  static constexpr bool theIsInt = SYS_IsIntegral_v< Type >;
245 };
246 /// @}
247 
248 /// This allows one to determine the type of T0+T1 at compile-time.
249 template<typename T0,typename T1>
251  // NOTE: The casts to int are only necessary for GCC 4.4's broken support for enum classes
253 };
254 
255 template<typename T0,typename T1>
257 
258 /// This allows one to determine the type of T0+T1 at compile-time.
259 template<typename T0,typename T1>
262 };
263 
264 template<typename T0,typename T1>
266 
267 template<exint TSIZE,typename DATA_T>
269 {
270  SYS_FORCE_INLINE exint getTupleSize() const { return TSIZE; }
271  SYS_FORCE_INLINE void setTupleSize(exint tuplesize) { UT_ASSERT_P(tuplesize == TSIZE); }
274 };
275 
276 template<typename DATA_T>
277 struct UT_TupleType<-1,DATA_T>
278 {
279  SYS_FORCE_INLINE exint getTupleSize() const { return myTupleSize; }
280  SYS_FORCE_INLINE void setTupleSize(exint tuplesize) { myTupleSize = tuplesize; }
283 private:
284  int myTupleSize;
285 };
286 
287 template<exint TSIZE>
288 struct UT_TupleType<TSIZE, void>
289 {
290  SYS_FORCE_INLINE exint getTupleSize() const { return TSIZE; }
291  SYS_FORCE_INLINE void setTupleSize(exint tuplesize) { UT_ASSERT_P(tuplesize == TSIZE); }
292  SYS_FORCE_INLINE UT_Storage getStorage() const { return myStorage; }
294 private:
295  int myTupleSizePlaceholder;
296  UT_Storage myStorage;
297 };
298 
299 template<>
300 struct UT_TupleType<-1, void>
301 {
302  SYS_FORCE_INLINE exint getTupleSize() const { return myTupleSize; }
303  SYS_FORCE_INLINE void setTupleSize(exint tuplesize) { myTupleSize = tuplesize; }
304  SYS_FORCE_INLINE UT_Storage getStorage() const { return myStorage; }
306 private:
307  int myTupleSize;
308  UT_Storage myStorage;
309 };
310 
311 template<typename TO,typename FROM>
313 TO UTconvertStorage(FROM from)
314 { return TO(from); }
315 
316 #define DECLARE_CONVERT_STORAGE_ROUND(T,F) \
317 template<> \
318 SYS_FORCE_INLINE \
319 T UTconvertStorage<T,F>(F from) \
320 { return T(from + ((from > 0) ? F(0.5) : F(-0.5))); } \
321 /* end of macro */
322 
338 
339 #undef DECLARE_CONVERT_STORAGE_ROUND
340 
341 
342 #endif
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Storage.h:281
typename UT_StorageNum< T >::MathFloat UT_StorageMathFloat_t
Definition: UT_Storage.h:176
void SecondGuess
Definition: UT_Storage.h:90
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Storage.h:272
int int32
Definition: SYS_Types.h:39
UT_Storage
Definition: UT_Storage.h:28
SYS_FORCE_INLINE void setTupleSize(exint tuplesize)
Definition: UT_Storage.h:303
This allows one to determine the type of T0+T1 at compile-time.
Definition: UT_Storage.h:260
void
Definition: png.h:1083
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
static const UT_Storage theStorage
Definition: UT_Storage.h:86
static constexpr bool theIsInt
Definition: UT_Storage.h:92
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Storage.h:270
SYS_FORCE_INLINE void setStorage(UT_Storage storage)
Definition: UT_Storage.h:305
int64 exint
Definition: SYS_Types.h:125
void AtLeast32Bit
Definition: UT_Storage.h:88
static constexpr bool theIsFloat
Definition: UT_Storage.h:187
SYS_FORCE_INLINE constexpr bool UTisFloatStorage(UT_Storage storage)
Returns true iff the given storage type represents a floating-point number.
Definition: UT_Storage.h:50
typename SYS_SelectType< typename UT_StorageNum< UT_StorageBetter_t< T0, T1 >>::AtLeast32Bit, T0, UT_StorageNum< UT_StorageBetter_t< T0, T1 >>::theStorage==UT_Storage::INVALID >::type type
Definition: UT_Storage.h:261
float fpreal32
Definition: SYS_Types.h:200
static constexpr bool theIsInt
Definition: UT_Storage.h:188
static const UT_Storage theStorage
Definition: UT_Storage.h:185
SYS_FORCE_INLINE TO UTconvertStorage(FROM from)
Definition: UT_Storage.h:313
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Storage.h:290
#define DECLARE_CONVERT_STORAGE_ROUND(T, F)
Definition: UT_Storage.h:316
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Storage.h:302
SYS_FORCE_INLINE void setTupleSize(exint tuplesize)
Definition: UT_Storage.h:280
fpreal32 MathFloat
Definition: UT_Storage.h:89
typename UT_StorageAtLeast32Bit< T0, T1 >::type UT_StorageAtLeast32Bit_t
Definition: UT_Storage.h:265
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
This allows one to determine the type of T0+T1 at compile-time.
Definition: UT_Storage.h:250
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
long long int64
Definition: SYS_Types.h:116
static constexpr bool theIsFloat
Definition: UT_Storage.h:91
typename UT_StorageBetter< T0, T1 >::type UT_StorageBetter_t
Definition: UT_Storage.h:256
signed char int8
Definition: SYS_Types.h:35
SYS_FORCE_INLINE exint getTupleSize() const
Definition: UT_Storage.h:279
SYS_FORCE_INLINE void setStorage(UT_Storage storage)
Definition: UT_Storage.h:293
typename SYS_SelectType< T0, T1,(int(UT_StorageNum< T0 >::theStorage)< int(UT_StorageNum< T1 >::theStorage))>::type type
Definition: UT_Storage.h:252
short int16
Definition: SYS_Types.h:37
SYS_FORCE_INLINE void setTupleSize(exint tuplesize)
Definition: UT_Storage.h:291
SYS_FORCE_INLINE void setTupleSize(exint tuplesize)
Definition: UT_Storage.h:271
SYS_FORCE_INLINE void setStorage(UT_Storage storage)
Definition: UT_Storage.h:282
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Storage.h:304
SYS_FORCE_INLINE void setStorage(UT_Storage storage)
Definition: UT_Storage.h:273
SYS_FORCE_INLINE constexpr bool UTisIntStorage(UT_Storage storage)
Returns true iff the given storage type represents an integer.
Definition: UT_Storage.h:43
SYS_FORCE_INLINE constexpr int UTstorageSize(UT_Storage storage)
Returns the number of bytes in the given storage type.
Definition: UT_Storage.h:57
SYS_FORCE_INLINE UT_Storage getStorage() const
Definition: UT_Storage.h:292