Open Inventor Release 2024.1.0
 
Loading...
Searching...
No Matches
SbDataType.h
Go to the documentation of this file.
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2018 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : VSG (MMM YYYY)
22**=======================================================================*/
23#ifndef SB_DATATYPE_H
24#define SB_DATATYPE_H
25
26#include <Inventor/STL/map>
27
28#include <Inventor/SbBase.h>
29#include <Inventor/SbString.h>
30#include <Inventor/sys/port.h>
31#include <Inventor/STL/limits>
33
34#ifdef _MSC_VER
35#pragma warning( push )
36#pragma warning(disable:4251)
37#endif
38
58{
59public:
84
88 explicit SbDataType(DataType type) : m_type(type) {}
89
94 SbDataType(const SbString& type);
95
100
101 /*
102 * Returns the type as an unsigned int (rather than an enum).
103 * See also getType().
104 */
105 inline operator unsigned int() const;
106
111 inline DataType getType() const;
112
116 inline unsigned int getSize() const;
117
121 inline SbBool isSigned() const;
122
126 inline SbBool isInteger() const;
127
131 inline unsigned int getNumBits() const;
132
138
143 double getMin() const;
144
148 double getMax() const;
149
150private:
151
156 double getLowest() const;
157
161 static void initClass();
162
170 inline double normalize(double val) const;
171
173 friend inline std::ostream& operator << (std::ostream& os, const SbDataType type);
174
179 static const bool m_true;
180
184 SbDataType(int type) { m_type = static_cast<DataType>(type); };
185
186 template<typename T> static SbDataType getTemplateType (const T&);
187
189 typedef union
190 {
191 unsigned char l_uCHAR;
192 unsigned short l_uSHORT;
193 unsigned int l_uINT;
194 signed char l_sCHAR;
195 signed short l_sSHORT;
196 signed int l_sINT;
197 float l_FLOAT;
198 double l_DOUBLE;
199 } DataValue;
200
211 template <typename T>
212 DataValue cast( T value ) const;
213
214 template <typename T>
215 T cast( void* value) const;
216
217private:
218 typedef std::map<DataType, SbString> TypeToStrMap;
219
221 template<typename T> static double getMinInternal();
222
224 template<typename T> static double getMaxInternal();
225
227 template<typename T> static double getLowestInternal();
228
230 DataType m_type;
231
233 static TypeToStrMap s_typeToStrMap;
234 static std::map<SbString, DataType> s_strToTypeMap;
235};
236
237/*******************************************************************************/
240{
241 return m_type;
242}
243
244/*******************************************************************************/
245SbDataType::operator unsigned int() const
246{
247 return static_cast<unsigned int >(m_type);
248}
249
250/*******************************************************************************/
251SbBool
253{
254 // signed type start from enum value 4
255 return ( (m_type >> 2)?TRUE:FALSE );
256}
257
258/*******************************************************************************/
259unsigned int
261{
262 return (1 << (m_type % 4));
263}
264
265/*******************************************************************************/
266unsigned int
268{
269 return (1 << (m_type % 4)) * 8;
270}
271
272/*******************************************************************************/
273SbBool
275{
276 return ( (m_type < 10)?TRUE:FALSE );
277}
278
279/*******************************************************************************/
280double
281SbDataType::normalize(double val) const
282{
283 if ( m_type != SbDataType::FLOAT )
284 {
285 double minType = getMin();
286 double maxType = getMax();
287 val = (val-minType)/(maxType-minType);
288
289 if ( isSigned() )
290 val = (val-0.5)*2.;
291 }
292
293 return val;
294}
295
296/*******************************************************************************/
297template <typename T>
298SbDataType::DataValue
299SbDataType::cast( T value ) const
300{
301 DataValue dataValue;
302 switch ( m_type )
303 {
304 case UNSIGNED_BYTE : dataValue.l_uCHAR = (unsigned char)value; break;
305 case UNSIGNED_SHORT: dataValue.l_uSHORT = (unsigned short)value; break;
306 case UNSIGNED_INT32: dataValue.l_uINT = (unsigned int)value; break;
307 case SIGNED_BYTE : dataValue.l_sCHAR = (signed char)value; break;
308 case SIGNED_SHORT : dataValue.l_sSHORT = (signed short)value; break;
309 case SIGNED_INT32 : dataValue.l_sINT = (signed int)value; break;
310 case FLOAT : dataValue.l_FLOAT = (float)value; break;
311 case DOUBLE : dataValue.l_DOUBLE = (double)value; break;
312 default : SoDebugError::postWarning("SbDataType::cast()", "Unknown data type %d", m_type); memset(&dataValue, 0, sizeof(DataValue)); break;
313 }
314 return dataValue;
315}
316
317/*******************************************************************************/
318template <typename T>
319T
320SbDataType::cast( void* value ) const
321{
322 switch ( m_type )
323 {
324 case UNSIGNED_BYTE : return (T)(*(unsigned char*)value); break;
325 case UNSIGNED_SHORT: return (T)(*(unsigned short*)value); break;
326 case UNSIGNED_INT32: return (T)(*(unsigned int*)value); break;
327 case SIGNED_BYTE : return (T)(*(signed char*)value); break;
328 case SIGNED_SHORT : return (T)(*(signed short*)value); break;
329 case SIGNED_INT32 : return (T)(*(signed int*)value); break;
330 case FLOAT : return (T)(*(float*)value); break;
331 case DOUBLE : return (T)(*(double*)value); break;
332 default : SoDebugError::postWarning("SbDataType::cast()", "Unknown data type %d", m_type); break;
333 }
334 return (T)0;
335}
336
337template<>
338inline
340SbDataType::getTemplateType (const unsigned char&)
341{
343}
344
345template<>
346inline
348SbDataType::getTemplateType (const unsigned short&)
349{
351}
352
353template<>
354inline
356SbDataType::getTemplateType (const uint32_t&)
357{
359}
360
361template<>
362inline
364SbDataType::getTemplateType (const signed char&)
365{
367}
368
369template<>
370inline
372SbDataType::getTemplateType (const signed short&)
373{
375}
376
377template<>
378inline
380SbDataType::getTemplateType (const int&)
381{
383}
384
385template<>
386inline
388SbDataType::getTemplateType (const float&)
389{
391}
392
393template<>
394inline
396SbDataType::getTemplateType (const double&)
397{
399}
400
401template<typename T>
402inline
404SbDataType::getTemplateType (const T&)
405{
407}
408
412inline std::ostream& operator << (std::ostream& os, const SbDataType type)
413{
414 switch ( type.m_type )
415 {
417 return os << "uint8";
419 return os << "uint16";
421 return os << "uint32";
423 return os << "int8";
425 return os << "int16";
427 return os << "int32";
429 return os << "float";
431 return os << "double";
432 default:
433 return os << "unknown data type!";
434 }
435}
436
437
438#ifdef _MSC_VER
439#pragma warning( pop )
440#endif
441
442#endif // SB_DATATYPE_H
443
444
#define TRUE
Possible value of SbBool.
Definition SbBase.h:77
#define FALSE
Possible value of SbBool.
Definition SbBase.h:75
std::ostream & operator<<(std::ostream &os, const SbDataType type)
Writes the type to the specified output stream.
Definition SbDataType.h:412
Class encoding a data type.
Definition SbDataType.h:58
SbBool isInteger() const
Returns true if the type is an integer type.
Definition SbDataType.h:274
DataType getType() const
Returns the type as an enum.
Definition SbDataType.h:239
DataType
Supported Data type.
Definition SbDataType.h:64
@ FLOAT
float
Definition SbDataType.h:78
@ SIGNED_BYTE
signed byte
Definition SbDataType.h:72
@ UNSIGNED_INT32
unsigned int (32bits)
Definition SbDataType.h:70
@ UNKNOWN
unknown data type
Definition SbDataType.h:82
@ DOUBLE
Double.
Definition SbDataType.h:80
@ UNSIGNED_BYTE
unsigned byte
Definition SbDataType.h:66
@ SIGNED_SHORT
signed short
Definition SbDataType.h:74
@ UNSIGNED_SHORT
unsigned short
Definition SbDataType.h:68
@ SIGNED_INT32
signed int (32bits)
Definition SbDataType.h:76
double getMax() const
Returns the maximum value of the type.
unsigned int getSize() const
Returns size in bytes of the type.
Definition SbDataType.h:260
SbDataType(const SbString &type)
Constructor from a string.
friend std::ostream & operator<<(std::ostream &os, const SbDataType type)
ostream operator for SbDataType
Definition SbDataType.h:412
SbDataType(DataType type)
Copy constructor.
Definition SbDataType.h:88
SbString getString() const
Returns the type as a string, e.g.
SbDataType()
Default constructor.
Definition SbDataType.h:99
unsigned int getNumBits() const
Returns the numer of bits in the type.
Definition SbDataType.h:267
double getMin() const
Returns the minimum value of the type.
SbBool isSigned() const
Returns true if the type is signed.
Definition SbDataType.h:252
Class for smart character strings.
Definition SbString.h:202
static void postWarning(const char *methodName, const char *formatString ...)
Posts a warning.
int SbBool
Boolean type.
Definition SbBase.h:87