MeVisLabToolboxReference
MeVis/Foundation/Sources/ML/include/mlStdTypeInfos.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //-------------------------------------------------------------------------
00012 //-------------------------------------------------------------------------
00013 #ifndef __mlStdTypeInfos_H
00014 #define __mlStdTypeInfos_H
00015 
00016 // ML-includes
00017 #ifndef __mlInitSystemML_H
00018 #include "mlInitSystemML.h"
00019 #endif
00020 #ifndef __mlBasics_H
00021 #include "mlBasics.h"
00022 #endif
00023 
00024 #include <math.h>
00025 
00026 #ifdef _MSC_VER
00027 #pragma warning(push)
00028 // warning C4244: 'Argument': Konvertierung von 'typ1' in 'typ2', möglicher Datenverlust
00029 #pragma warning(disable: 4244)
00030 // warning C4127: Bedingter Ausdruck ist konstant
00031 #pragma warning(disable: 4127)
00032 #endif
00033 
00034 ML_START_NAMESPACE
00035 
00037 const MLint _ML_STD_SLEN=64;
00038 
00039 //--------------------------------------------------------------------------------------
00041 //--------------------------------------------------------------------------------------
00042 template <typename VTYP>
00043 class MLEXPORT MLTStdTypeInfos : public MLTypeInfos 
00044 {
00045   
00046 public:
00047   
00049 
00050   static inline const char *typeName(MLint8   *) { static const char name[]="int8";           return name; }
00051   static inline const char *typeName(MLuint8  *) { static const char name[]="unsigned int8";  return name; }
00052   static inline const char *typeName(MLint16  *) { static const char name[]="int16";          return name; }
00053   static inline const char *typeName(MLuint16 *) { static const char name[]="unsigned int16"; return name; }
00054   static inline const char *typeName(MLint32  *) { static const char name[]="int32";          return name; }
00055   static inline const char *typeName(MLuint32 *) { static const char name[]="unsigned int32"; return name; }
00056   static inline const char *typeName(MLfloat  *) { static const char name[]="float";          return name; }
00057   static inline const char *typeName(MLdouble *) { static const char name[]="double";         return name; }
00058   static inline const char *typeName(MLint64  *) { static const char name[]="int64";          return name; }
00059   static inline const char *typeName(MLuint64 *) { static const char name[]="unsigned int64"; return name; }
00061   
00062 protected:
00064   static const VTYP _typeMin;
00065   
00067   static const VTYP _typeMax;
00068   
00070   static const VTYP _typeDefault;
00071   
00073   static const char _typeInfoString[_ML_STD_SLEN];
00074   
00076 
00077   inline MLDataType _rangeAndPrecisionEquiv(MLint8   ) { return MLint8Type;    };
00078   inline MLDataType _rangeAndPrecisionEquiv(MLuint8  ) { return MLuint8Type;   };
00079   inline MLDataType _rangeAndPrecisionEquiv(MLint16  ) { return MLint16Type;   };
00080   inline MLDataType _rangeAndPrecisionEquiv(MLuint16 ) { return MLuint16Type;  };
00081   inline MLDataType _rangeAndPrecisionEquiv(MLint32  ) { return MLint32Type;   };
00082   inline MLDataType _rangeAndPrecisionEquiv(MLuint32 ) { return MLuint32Type;  };
00083   inline MLDataType _rangeAndPrecisionEquiv(MLfloat  ) { return MLfloatType;   };
00084   inline MLDataType _rangeAndPrecisionEquiv(MLdouble ) { return MLdoubleType;  };
00085   inline MLDataType _rangeAndPrecisionEquiv(MLint64  ) { return MLint64Type;   };
00086   inline MLDataType _rangeAndPrecisionEquiv(MLuint64 ) { return MLuint64Type;  };
00088 
00090   static inline       VTYP& value_cast(      MLTypeData* v) { return *(reinterpret_cast<      VTYP*>(v)); }
00091   static inline const VTYP& value_cast(const MLTypeData* v) { return *(reinterpret_cast<const VTYP*>(v)); }
00093 
00096   static inline VTYP castToType(MLdouble v)  { return static_cast<VTYP>(floor(v+0.5)); }
00097 
00099 
00100   inline void _getGoodCastTos(MLint8, MLuint &num, const char **&typeNames)
00101   {
00102     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00103     ML_TRY
00104     {
00105       num = 5;
00106       static const char *v[] = 
00107       {
00108         MLTStdTypeInfos<MLint16  >::typeName(static_cast<MLint16  *>(NULL)),
00109         MLTStdTypeInfos<MLint32  >::typeName(static_cast<MLint32  *>(NULL)),
00110         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00111         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00112         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)) 
00113       };
00114       typeNames = v;
00115     }
00116     ML_CATCH_RETHROW;
00117   };
00118   inline void _getGoodCastTos(MLuint8, MLuint &num, const char **&typeNames)
00119   {
00120     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00121     ML_TRY
00122     { 
00123       num = 8;
00124       static const char *v[] = 
00125       {
00126         MLTStdTypeInfos<MLint16  >::typeName(static_cast<MLint16  *>(NULL)),
00127         MLTStdTypeInfos<MLuint16 >::typeName(static_cast<MLuint16 *>(NULL)),
00128         MLTStdTypeInfos<MLint32  >::typeName(static_cast<MLint32  *>(NULL)),
00129         MLTStdTypeInfos<MLuint32 >::typeName(static_cast<MLuint32 *>(NULL)),
00130         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00131         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00132         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)),
00133         MLTStdTypeInfos<MLuint64 >::typeName(static_cast<MLuint64 *>(NULL)) 
00134       };
00135       typeNames = v;
00136     }
00137     ML_CATCH_RETHROW;
00138   };
00139   inline void _getGoodCastTos(MLint16, MLuint &num, const char **&typeNames)
00140   {
00141     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00142     ML_TRY
00143     {      
00144       num = 4;
00145       static const char *v[] = 
00146       {
00147         MLTStdTypeInfos<MLint32  >::typeName(static_cast<MLint32  *>(NULL)),
00148         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00149         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00150         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)) 
00151       };
00152       typeNames = v;
00153     }
00154     ML_CATCH_RETHROW;
00155   };
00156   inline void _getGoodCastTos(MLuint16, MLuint &num, const char **&typeNames)
00157   {
00158     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00159     ML_TRY
00160     {      
00161       num = 6;
00162       static const char *v[] = 
00163       {
00164         MLTStdTypeInfos<MLint32  >::typeName(static_cast<MLint32  *>(NULL)),
00165         MLTStdTypeInfos<MLuint32 >::typeName(static_cast<MLuint32 *>(NULL)),
00166         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00167         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00168         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)),
00169         MLTStdTypeInfos<MLuint64 >::typeName(static_cast<MLuint64 *>(NULL)) 
00170       };
00171       typeNames = v;
00172     }
00173     ML_CATCH_RETHROW;
00174   };
00175   inline void _getGoodCastTos(MLint32, MLuint &num, const char **&typeNames)
00176   {
00177     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00178     ML_TRY
00179     {      
00180       num = 3;
00181       static const char *v[] = 
00182       {
00183         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00184         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00185         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)) 
00186       };
00187       typeNames = v;
00188     }
00189     ML_CATCH_RETHROW;
00190   };
00191   inline void _getGoodCastTos(MLuint32, MLuint &num, const char **&typeNames)
00192   {
00193     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00194     ML_TRY
00195     {      
00196       num = 4;
00197       static const char *v[] = 
00198       {
00199         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00200         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00201         MLTStdTypeInfos<MLint64  >::typeName(static_cast<MLint64  *>(NULL)),
00202         MLTStdTypeInfos<MLuint64 >::typeName(static_cast<MLuint64 *>(NULL)) 
00203       };
00204       typeNames = v;
00205     }
00206     ML_CATCH_RETHROW;
00207   };
00208   inline void _getGoodCastTos(MLfloat, MLuint &num, const char **&typeNames)
00209   {
00210     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00211     ML_TRY
00212     {      
00213       num = 1;
00214       static const char *v[] = 
00215       {
00216         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00217       };
00218       typeNames = v;
00219     }
00220     ML_CATCH_RETHROW;
00221   };
00222   inline void _getGoodCastTos(MLdouble, MLuint &num, const char **&typeNames)
00223   {
00224     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00225     ML_TRY
00226     {      
00227       num = 0;
00228       typeNames = NULL;
00229     }
00230     ML_CATCH_RETHROW;
00231   };
00232   inline void _getGoodCastTos(MLint64, MLuint &num, const char **&typeNames)
00233   {
00234     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00235     ML_TRY
00236     {      
00237       num = 2;
00238       static const char *v[] = 
00239       {
00240         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00241         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00242       };
00243       typeNames = v;
00244     }
00245     ML_CATCH_RETHROW;
00246   };
00247   inline void _getGoodCastTos(MLuint64, MLuint &num, const char **&typeNames)
00248   {
00249     ML_TRACE_IN( "MLTStdTypeInfos::_getGoodCastTos( )" );
00250     ML_TRY
00251     {      
00252       num = 2;
00253       static const char *v[] = 
00254       {
00255         MLTStdTypeInfos<MLfloat  >::typeName(static_cast<MLfloat  *>(NULL)),
00256         MLTStdTypeInfos<MLdouble >::typeName(static_cast<MLdouble *>(NULL)),
00257       };
00258       typeNames = v;
00259     }
00260     ML_CATCH_RETHROW;
00261   };
00263   
00265   static MLTypeInfos *_myInfos;
00266   
00269   static MLint32      _numInstances;
00270   
00271 public:
00272   
00276   MLTStdTypeInfos()
00277   {
00278     ML_TRACE_IN( "MLTStdTypeInfos::MLTStdTypeInfos( )" );
00279     ML_TRY
00280     {
00281       if (_numInstances > 0)
00282       {
00283         ML_PRINT_FATAL_ERROR("MLTStdTypeInfos", ML_PROGRAMMING_ERROR, "Too many instances of MLTStdTypeInfos created.");
00284         ML_CHECK_THROW(0);
00285       }
00286       _numInstances++;
00287       
00288       // Save pointer to the one instance of this class.
00289       _myInfos     = this;
00290       
00292       ML_TYPE_ASSIGN_FUNCTION_POINTERS();
00293       
00294       // Determine the type to which this type can be casted without information loss.
00295       MLuint numLocalGoodCastTos=0;
00296       const char **goodLocalCastTos=NULL;
00297       _getGoodCastTos(static_cast<VTYP>(0), numLocalGoodCastTos, goodLocalCastTos);
00298       
00358       VTYP buf=0;
00359       void *addr[1];
00360       addr[0] = &buf;
00361       MLTypeInfosInit(this,                        
00362                       sizeof(VTYP),                
00363                       typeName(static_cast<VTYP*>(NULL)),
00364                       _typeMin,       
00365                       _typeMax,        
00366                       reinterpret_cast<const MLTypeData*>(&_typeMin),    
00367                       reinterpret_cast<const MLTypeData*>(&_typeMax),    
00368                       reinterpret_cast<const MLTypeData*>(&_typeDefault),
00369                       _typeInfoString,             
00370                       true,
00371                       MLScalarTypeGroup,
00372                       _rangeAndPrecisionEquiv(static_cast<VTYP>(0)),
00373                       addr,
00374                       numLocalGoodCastTos,
00375                       goodLocalCastTos
00376                       );
00377     }
00378     ML_CATCH_RETHROW;
00379   }
00380   
00381   
00382   //-------------------------------------------------------------------------------
00383   // Methods to be overloaded:
00384   //-------------------------------------------------------------------------------
00385   
00387 
00388   
00390   static char       *MLTYPE_getStringValue        (const MLTypeData *p)                                       { return MLTypeComponentsToString(_myInfos, p); }
00392   static void        MLTYPE_setStringValue        (const char *s, MLTypeData *r)                              { MLTypeComponentsFromString(_myInfos, s, reinterpret_cast<const MLTypeData*>(&_typeDefault), r); }
00393   
00395   static void        MLTYPE_setToMinimum          (MLTypeData *p)                                             { value_cast(p) = _typeMin; }
00397   static void        MLTYPE_setToMaximum          (MLTypeData *p)                                             { value_cast(p) = _typeMax; }
00399   static void        MLTYPE_setToDefault          (MLTypeData *p)                                             { value_cast(p) = _typeDefault; }
00401   static MLdouble    MLTYPE_getComponent          (const MLTypeData *p, size_t /*n*/)                         { return static_cast<MLdouble>(value_cast(p)); }
00403   static void        MLTYPE_setComponent          (MLTypeData *p, size_t /*n*/, MLdouble v)                   { value_cast(p) = static_cast<VTYP>(v); }
00405   static void        MLTYPE_copy                  (const MLTypeData *p, MLTypeData *q)                        { value_cast(q) = value_cast(p); }
00407   static void        MLTYPE_arrayCopy             (const MLTypeData *p, MLTypeData *q, size_t n)              { memcpy(q, p, sizeof(VTYP)*n); }
00409   static void        MLTYPE_arrayCopyWithStrides  (const MLTypeData *p, MLssize_t ps, MLTypeData *q, MLssize_t qs, size_t n) { for (size_t i=0;i<n;i++) { value_cast(q) = value_cast(p); q += sizeof(VTYP)*qs; p += sizeof(VTYP)*ps; } }
00411   static void        MLTYPE_arrayFill             (const MLTypeData *p, MLTypeData *q, size_t n)              { for (size_t i=0;i<n;i++) { value_cast(q) = value_cast(p); q += sizeof(VTYP); } }
00412   
00414   static bool        MLTYPE_castToBool            (const MLTypeData *p)                                       { return MLValuesDifferWOM(value_cast(p), static_cast<VTYP>(0)); }
00416   static MLint       MLTYPE_castToInt             (const MLTypeData *p)                                       { return static_cast<MLint>(value_cast(p)); }
00418   static MLdouble    MLTYPE_castToDouble          (const MLTypeData *p)                                       { return static_cast<MLdouble>(value_cast(p)); }
00420   static void        MLTYPE_castToOtherType(const MLTypeData *myData, const MLTypeInfos *otherInfos, MLTypeData *otherData) { MLTypeCastToOtherType(_myInfos, myData,  otherInfos, otherData); }
00421   
00423   static void        MLTYPE_castFromInt           (MLint p, MLTypeData *q)                                  { value_cast(q) = static_cast<VTYP>(p); }
00425   static void        MLTYPE_castFromDouble        (MLdouble p, MLTypeData *q)                                 { value_cast(q) = static_cast<VTYP>(p); }
00427   static void        MLTYPE_castFromOtherType     (const MLTypeInfos *otherInfos, const MLTypeData *otherData, 
00428                                                    MLTypeData *myData)                                        { MLTypeCastToOtherType(otherInfos,otherData, _myInfos, myData);   }
00429     
00431   static bool        MLTYPE_isEqualToType         (const MLTypeData *p, const MLTypeData *q)                  { return MLValuesAreEqualWOM(value_cast(p), value_cast(q)); }
00433   static bool        MLTYPE_isEqualToTypeWithEpsilon(const MLTypeData *p, const MLTypeData *q, MLdouble eps)  { return MLAbs(value_cast(p)-value_cast(q)) < eps; }
00434   
00436   static void        MLTYPE_negate                (const MLTypeData *p, MLTypeData *q)                        { value_cast(q) = -value_cast(p); }
00437   
00439   static void        MLTYPE_normalize             (const MLTypeData * /*p*/, MLTypeData *q)                   { value_cast(q) = 1; }
00440   
00444   static void        MLTYPE_arrayRescale          (const MLTypeData* p, MLdouble q, const MLTypeData* r, MLTypeData* t, size_t s) {
00445     for (size_t i=0;i<s;i++) {
00446       value_cast(t) = castToType(value_cast(p) * q + value_cast(r));
00447       t += sizeof(VTYP);
00448       p += sizeof(VTYP);
00449     }
00450   }
00451 
00455   static bool        MLTYPE_arrayEqualsValue     (const MLTypeData* p, const MLTypeData* q, size_t s) {
00456     for (size_t i=0;i<s;i++) {
00457       if (value_cast(p) != value_cast(q)) { return false; }
00458       q += sizeof(VTYP);
00459     }
00460     return true;
00461   }
00462 
00466   static void        MLTYPE_arrayGetMinMax       (const MLTypeData* p, size_t s, MLdouble& min, MLdouble& max) {
00467     MLdouble tmpMin, tmpMax;
00468     tmpMin = tmpMax = value_cast(p);
00469     p += sizeof(VTYP);
00470     for (size_t i=1;i<s;i++) {
00471       const VTYP v = value_cast(p);
00472       if (v < tmpMin) { tmpMin = v; }
00473       if (v > tmpMax) { tmpMax = v; }
00474       p += sizeof(VTYP);
00475     }
00476     min = tmpMin;
00477     max = tmpMax;
00478   }
00479 
00480 
00484   static void        MLTYPE_interpolate           (const MLTypeData* p, const MLTypeData* q, MLdouble r, MLTypeData* t) {
00485     value_cast(t) = castToType(value_cast(p) * (1.0-r) + value_cast(q) * r);
00486   }
00487 
00489   static void        MLTYPE_multWithInt           (const MLTypeData *p, MLint q,             MLTypeData *r)   { value_cast(r) = static_cast<VTYP>(value_cast(p) * q);   }
00491   static void        MLTYPE_multWithDouble        (const MLTypeData *p, MLdouble q,          MLTypeData *r)   { value_cast(r) = static_cast<VTYP>(value_cast(p) * q);   }
00493   static void        MLTYPE_multWithType          (const MLTypeData *p, const MLTypeData *q, MLTypeData *r)   { value_cast(r) = value_cast(p)*value_cast(q); }
00495   static void        MLTYPE_multWithOtherType     (const MLTypeInfos *otherInfos, const MLTypeData *otherData, 
00496                                                    const MLTypeData *myData, MLTypeData *r)                   { MLTypeMultWithOtherType(_myInfos, myData, otherInfos, otherData, r); }
00497   
00499   static void        MLTYPE_plusInt          (const MLTypeData *p, MLint q,             MLTypeData *r)   { value_cast(r) = value_cast(p) + q;                     }
00501   static void        MLTYPE_plusDouble       (const MLTypeData *p, MLdouble q,          MLTypeData *r)   { value_cast(r) = static_cast<VTYP>(value_cast(p) + q);  }
00503   static void        MLTYPE_plusType         (const MLTypeData *p, const MLTypeData *q, MLTypeData *r)   { value_cast(r) = value_cast(p) + value_cast(q);         }
00504   
00506 };
00507 
00508 template<> inline MLfloat   MLTStdTypeInfos<MLfloat  >::castToType(MLdouble v) { return static_cast<MLfloat  >(v); }
00509 template<> inline MLdouble  MLTStdTypeInfos<MLdouble >::castToType(MLdouble v) { return v;                         }
00510 
00511 
00512 // For unsigned types implement specialized negations to avoid warning due to negations of unsigned types. We just copy instead of negating.
00513 template<> inline void MLTStdTypeInfos<MLuint8  >::MLTYPE_negate      (const MLTypeData *p, MLTypeData *q)                     { value_cast(q) = value_cast(p);   }
00514 template<> inline void MLTStdTypeInfos<MLuint16 >::MLTYPE_negate      (const MLTypeData *p, MLTypeData *q)                     { value_cast(q) = value_cast(p);   }
00515 template<> inline void MLTStdTypeInfos<MLuint32 >::MLTYPE_negate      (const MLTypeData *p, MLTypeData *q)                     { value_cast(q) = value_cast(p);   }
00516 template<> inline void MLTStdTypeInfos<MLuint64 >::MLTYPE_negate      (const MLTypeData *p, MLTypeData *q)                     { value_cast(q) = value_cast(p);   }
00517 
00518 
00519 // Member initializations for unspecialized class members.
00520 template <typename VTYP> const VTYP   MLTStdTypeInfos<VTYP>::_typeDefault                  = VTYP(0);
00521 template <typename VTYP> MLTypeInfos* MLTStdTypeInfos<VTYP>::_myInfos                      = NULL;
00522 template <typename VTYP> MLint32      MLTStdTypeInfos<VTYP>::_numInstances                 = 0;
00523 
00524 ML_END_NAMESPACE
00525 
00526 #ifdef _MSC_VER
00527 #pragma warning(pop)
00528 #endif
00529 
00530 
00531 #endif //of __mlStdTypeInfos_H
00532 
00533 
00534