Sierra Toolkit Version of the Day
DataTraits.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #include <cstddef>
00011 #include <stdexcept>
00012 
00013 #include <stk_util/environment/ReportHandler.hpp>
00014 
00015 #include <stk_mesh/base/DataTraits.hpp>
00016 #include <stk_mesh/base/DataTraitsEnum.hpp>
00017 #include <stk_mesh/base/DataTraitsClass.hpp>
00018 
00019 namespace stk {
00020 namespace mesh {
00021 
00022 //----------------------------------------------------------------------
00023 
00024 namespace {
00025 
00026 std::size_t stride( std::size_t size , std::size_t align )
00027 {
00028   if ( align && size % align ) { size += align - size % align ; }
00029   return size ;
00030 }
00031 
00032 }
00033 
00034 DataTraits::~DataTraits() {}
00035 
00036 DataTraits::DataTraits( const std::type_info & arg_type ,
00037                         const char * const     arg_name ,
00038                         std::size_t            arg_size ,
00039                         std::size_t            arg_align )
00040   : type_info(         arg_type ),
00041     size_of(           arg_size ),
00042     is_void(           false ),
00043     is_integral(       false ),
00044     is_floating_point( false ),
00045     is_pointer(        false ),
00046     is_enum(           false ),
00047     is_class(          false ),
00048     is_pod(            false ),
00049     is_signed(         false ),
00050     is_unsigned(       false ),
00051     alignment_of(      arg_align ),
00052     stride_of(         stride( arg_size , arg_align ) ),
00053     remove_pointer(    NULL ),
00054     name(              arg_name ),
00055     enum_info(),
00056     class_info()
00057 {}
00058 
00059 DataTraits::DataTraits( const std::type_info & arg_type ,
00060                         const DataTraits     & arg_traits )
00061   : type_info(          arg_type ),
00062     size_of(            sizeof(void*) ),
00063     is_void(            false ),
00064     is_integral(        false ),
00065     is_floating_point(  false ),
00066     is_pointer(         true ),
00067     is_enum(            false ),
00068     is_class(           false ),
00069     is_pod(             false ),
00070     is_signed(          false ),
00071     is_unsigned(        false ),
00072     alignment_of(       sizeof(void*) ),
00073     stride_of(          sizeof(void*) ),
00074     remove_pointer( & arg_traits ),
00075     name(),
00076     enum_info(),
00077     class_info()
00078 {
00079   name.assign( arg_traits.name ).append("*");
00080 }
00081 
00082 //----------------------------------------------------------------------
00083 
00084 namespace {
00085 
00086 class DataTraitsVoid : public DataTraits {
00087 public:
00088 
00089   DataTraitsVoid()
00090     : DataTraits( typeid(void) , "void" , 0 , 0 )
00091     { is_void = true ; }
00092 
00093   void construct( void * , std::size_t ) const
00094   { ThrowErrorMsg( "not supported" ); }
00095 
00096   void destroy( void * , std::size_t ) const
00097   { ThrowErrorMsg( "not supported" ); }
00098 
00099   void pack( CommBuffer & , const void * , std::size_t ) const
00100   { ThrowErrorMsg( "not supported" ); }
00101 
00102   void unpack( CommBuffer & , void * , std::size_t ) const
00103   { ThrowErrorMsg( "not supported" ); }
00104 
00105   void print( std::ostream & , const void * , std::size_t ) const
00106   { ThrowErrorMsg( "not supported" ); }
00107 
00108   void copy( void * , const void * , std::size_t ) const
00109   { ThrowErrorMsg( "not supported" ); }
00110 
00111   void sum( void * , const void * , std::size_t ) const
00112   { ThrowErrorMsg( "not supported" ); }
00113 
00114   void max( void * , const void * , std::size_t ) const
00115   { ThrowErrorMsg( "not supported" ); }
00116 
00117   void min( void * , const void * , std::size_t ) const
00118   { ThrowErrorMsg( "not supported" ); }
00119 
00120   virtual void bit_and( void * , const void * , std::size_t ) const
00121   { ThrowErrorMsg( "not supported" ); }
00122 
00123   virtual void bit_or( void * , const void * , std::size_t ) const
00124   { ThrowErrorMsg( "not supported" ); }
00125 
00126   virtual void bit_xor( void * , const void * , std::size_t ) const
00127   { ThrowErrorMsg( "not supported" ); }
00128 };
00129 
00130 }
00131 
00132 template<> const DataTraits & data_traits<void>()
00133 { static const DataTraitsVoid traits ; return traits ; }
00134 
00135 //----------------------------------------------------------------------
00136 
00137 namespace {
00138 
00139 template< typename A , typename B >
00140 struct IsSameType { enum { value = false }; };
00141 
00142 template< typename A >
00143 struct IsSameType<A,A> { enum { value = true }; };
00144 
00145 
00146 template< typename T >
00147 class DataTraitsCommon : public DataTraits {
00148 public:
00149 
00150   explicit DataTraitsCommon( const char * arg_name )
00151     : DataTraits( typeid(T) , arg_name , sizeof(T) , sizeof(T) )
00152   {
00153     is_pod            = true ;
00154 
00155     is_integral       = IsSameType<T,char>::value ||
00156                         IsSameType<T,unsigned char>::value ||
00157                         IsSameType<T,short>::value ||
00158                         IsSameType<T,unsigned short>::value ||
00159                         IsSameType<T,int>::value ||
00160                         IsSameType<T,unsigned int>::value ||
00161                         IsSameType<T,long>::value ||
00162                         IsSameType<T,unsigned long>::value ;
00163 
00164     is_signed         = IsSameType<T,char>::value ||
00165                         IsSameType<T,short>::value ||
00166                         IsSameType<T,int>::value ||
00167                         IsSameType<T,long>::value ;
00168 
00169     is_unsigned       = IsSameType<T,unsigned char>::value ||
00170                         IsSameType<T,unsigned short>::value ||
00171                         IsSameType<T,unsigned int>::value ||
00172                         IsSameType<T,unsigned long>::value ;
00173 
00174     is_floating_point = IsSameType<T,double>::value ||
00175                         IsSameType<T,float>::value ;
00176   }
00177 
00178   void construct( void * v , std::size_t n ) const
00179   {
00180     T * x = reinterpret_cast<T*>( v );
00181     T * const x_end = x + n ;
00182     while ( x_end != x ) { *x++ = 0 ; }
00183   }
00184 
00185   void destroy( void * v , std::size_t n ) const {}
00186 
00187   void pack( CommBuffer & buf , const void * v , std::size_t n ) const
00188   {
00189     const T * x = reinterpret_cast<const T*>( v );
00190     buf.pack<T>( x , n );
00191   }
00192 
00193   void unpack( CommBuffer & buf , void * v , std::size_t n ) const
00194   {
00195     T * x = reinterpret_cast<T*>( v );
00196     buf.unpack<T>( x , n );
00197   }
00198 
00199   void copy( void * vx , const void * vy , std::size_t n ) const
00200   {
00201     const T * y = reinterpret_cast<const T*>( vy );
00202     T * x = reinterpret_cast<T*>( vx );
00203     T * const x_end = x + n ;
00204     while ( x_end != x ) { *x++ = *y++ ; };
00205   }
00206 
00207   void sum( void * vx , const void * vy , std::size_t n ) const
00208   {
00209     const T * y = reinterpret_cast<const T*>( vy );
00210     T * x = reinterpret_cast<T*>( vx );
00211     T * const x_end = x + n ;
00212     while ( x_end != x ) { *x++ += *y++ ; };
00213   }
00214 
00215   virtual void print( std::ostream & s , const void * v , std::size_t n ) const
00216   {
00217     if ( n ) {
00218       const T * x = reinterpret_cast<const T*>( v );
00219       const T * const x_end = x + n ;
00220       s << *x++ ;
00221       while ( x_end != x ) { s << " " << *x++ ; }
00222     }
00223   }
00224 
00225   virtual void max( void * vx , const void * vy , std::size_t n ) const
00226   { ThrowErrorMsg( "not supported" ); }
00227 
00228   virtual void min( void * vx , const void * vy , std::size_t n ) const
00229   { ThrowErrorMsg( "not supported" ); }
00230 
00231   virtual void bit_and( void * , const void * , std::size_t ) const
00232   { ThrowErrorMsg( "not supported" ); }
00233 
00234   virtual void bit_or( void * , const void * , std::size_t ) const
00235   { ThrowErrorMsg( "not supported" ); }
00236 
00237   virtual void bit_xor( void * , const void * , std::size_t ) const
00238   { ThrowErrorMsg( "not supported" ); }
00239 };
00240 
00241 template< typename T >
00242 class DataTraitsNumeric : public DataTraitsCommon<T> {
00243 public:
00244 
00245   explicit DataTraitsNumeric( const char * arg_name )
00246     : DataTraitsCommon<T>( arg_name )  {}
00247 
00248   virtual void max( void * vx , const void * vy , std::size_t n ) const
00249   {
00250     const T * y = reinterpret_cast<const T*>( vy );
00251     T * x = reinterpret_cast<T*>( vx );
00252     T * const x_end = x + n ;
00253     for ( ; x_end != x ; ++x , ++y ) { if ( *x < *y ) { *x = *y ; } }
00254   }
00255 
00256   virtual void min( void * vx , const void * vy , std::size_t n ) const
00257   {
00258     const T * y = reinterpret_cast<const T*>( vy );
00259     T * x = reinterpret_cast<T*>( vx );
00260     T * const x_end = x + n ;
00261     for ( ; x_end != x ; ++x , ++y ) { if ( *x > *y ) { *x = *y ; } }
00262   }
00263 };
00264 
00265 template< typename T >
00266 class DataTraitsComplex : public DataTraitsCommon<T> {
00267 public:
00268 
00269   explicit DataTraitsComplex( const char * arg_name )
00270     : DataTraitsCommon<T>( arg_name ) {}
00271 };
00272 
00273 template< typename T >
00274 class DataTraitsIntegral : public DataTraitsNumeric<T> {
00275 public:
00276   DataTraitsIntegral( const char * name ) : DataTraitsNumeric<T>( name ) {}
00277 
00278   virtual void bit_and( void * vx , const void * vy , std::size_t n ) const
00279   {
00280     const T * y = reinterpret_cast<const T*>( vy );
00281     T * x = reinterpret_cast<T*>( vx );
00282     T * const x_end = x + n ;
00283     while ( x_end != x ) { *x++ &= *y++ ; }
00284   }
00285 
00286   virtual void bit_or( void * vx , const void * vy , std::size_t n ) const
00287   {
00288     const T * y = reinterpret_cast<const T*>( vy );
00289     T * x = reinterpret_cast<T*>( vx );
00290     T * const x_end = x + n ;
00291     while ( x_end != x ) { *x++ |= *y++ ; }
00292   }
00293 
00294   virtual void bit_xor( void * vx , const void * vy , std::size_t n ) const
00295   {
00296     const T * y = reinterpret_cast<const T*>( vy );
00297     T * x = reinterpret_cast<T*>( vx );
00298     T * const x_end = x + n ;
00299     while ( x_end != x ) { *x++ ^= *y++ ; }
00300   }
00301 };
00302 
00303 class DataTraitsChar : public DataTraitsIntegral<char> {
00304 public:
00305   DataTraitsChar() : DataTraitsIntegral<char>( "char" ) {}
00306 
00307   virtual void print( std::ostream & s , const void * v , std::size_t n ) const
00308   {
00309     if ( n ) {
00310       const char * x = reinterpret_cast<const char*>( v );
00311       const char * const x_end = x + n ;
00312       s << int(*x++) ;
00313       while ( x_end != x ) { s << " " << int(*x++) ; }
00314     }
00315   }
00316 };
00317 
00318 class DataTraitsUnsignedChar : public DataTraitsIntegral<unsigned char> {
00319 public:
00320   DataTraitsUnsignedChar()
00321     : DataTraitsIntegral<unsigned char>( "unsigned char" ) {}
00322 
00323   virtual void print( std::ostream & s , const void * v , std::size_t n ) const
00324   {
00325     if ( n ) {
00326       const unsigned char * x = reinterpret_cast<const unsigned char*>( v );
00327       const unsigned char * const x_end = x + n ;
00328       s << unsigned(*x++) ;
00329       while ( x_end != x ) { s << " " << unsigned(*x++) ; }
00330     }
00331   }
00332 };
00333 
00334 }
00335 
00336 #define DATA_TRAITS_NUMERIC( T )        \
00337 template<>      \
00338 const DataTraits & data_traits<T>()     \
00339 { static const DataTraitsNumeric<T> traits( #T ); return traits ; }
00340 
00341 #define DATA_TRAITS_COMPLEX( T )        \
00342 template<>      \
00343 const DataTraits & data_traits<T>()     \
00344 { static const DataTraitsComplex<T> traits( #T ); return traits ; }
00345 
00346 #define DATA_TRAITS_INTEGRAL( T )        \
00347 template<>      \
00348 const DataTraits & data_traits<T>()     \
00349 { static const DataTraitsIntegral<T> traits( #T ); return traits ; }
00350 
00351 template<>
00352 const DataTraits & data_traits<char>()
00353 { static const DataTraitsChar traits ; return traits ; }
00354 
00355 template<>
00356 const DataTraits & data_traits<unsigned char>()
00357 { static const DataTraitsUnsignedChar traits ; return traits ; }
00358 
00359 DATA_TRAITS_INTEGRAL( short )
00360 DATA_TRAITS_INTEGRAL( unsigned short )
00361 DATA_TRAITS_INTEGRAL( int )
00362 DATA_TRAITS_INTEGRAL( unsigned int )
00363 DATA_TRAITS_INTEGRAL( long )
00364 DATA_TRAITS_INTEGRAL( unsigned long )
00365 DATA_TRAITS_NUMERIC( float )
00366 DATA_TRAITS_NUMERIC( double )
00367 DATA_TRAITS_COMPLEX( std::complex<float> ) // TODO: Probably not right
00368 DATA_TRAITS_COMPLEX( std::complex<double> ) // TODO: Probably not right
00369 
00370 //----------------------------------------------------------------------
00371 //----------------------------------------------------------------------
00372 
00373 namespace {
00374 
00375 template< typename T >
00376 class DataTraitsPointerToFundamental : public DataTraits {
00377 public:
00378 
00379   DataTraitsPointerToFundamental()
00380     : DataTraits( typeid(T*) , data_traits<T>() ) {}
00381 
00382   void construct( void * v , std::size_t n ) const
00383   {
00384     void ** x = reinterpret_cast<void**>(v);
00385     void ** const x_end = x + n ;
00386     while ( x_end != x ) { *x++ = NULL ; }
00387   }
00388 
00389   void destroy( void * v , std::size_t n ) const
00390   {
00391     void ** x = reinterpret_cast<void**>(v);
00392     void ** const x_end = x + n ;
00393     while ( x_end != x ) { *x++ = NULL ; }
00394   }
00395 
00396   void copy( void * vx , const void * vy , std::size_t n ) const
00397   {
00398     void * const * y = reinterpret_cast<void* const *>(vy);
00399     void ** x = reinterpret_cast<void**>(vx);
00400     void ** const x_end = x + n ;
00401     while ( x_end != x ) { *x++ = *y++ ; }
00402   }
00403 
00404   void pack( CommBuffer & , const void * , std::size_t ) const
00405   { ThrowErrorMsg( "not supported" ); }
00406 
00407   void unpack( CommBuffer & , void * , std::size_t ) const
00408   { ThrowErrorMsg( "not supported" ); }
00409 
00410   void print( std::ostream & , const void * , std::size_t ) const
00411   { ThrowErrorMsg( "not supported" ); }
00412 
00413   void sum( void * , const void * , std::size_t ) const
00414   { ThrowErrorMsg( "not supported" ); }
00415 
00416   void max( void * , const void * , std::size_t ) const
00417   { ThrowErrorMsg( "not supported" ); }
00418 
00419   void min( void * , const void * , std::size_t ) const
00420   { ThrowErrorMsg( "not supported" ); }
00421 
00422   virtual void bit_and( void * , const void * , std::size_t ) const
00423   { ThrowErrorMsg( "not supported" ); }
00424 
00425   virtual void bit_or( void * , const void * , std::size_t ) const
00426   { ThrowErrorMsg( "not supported" ); }
00427 
00428   virtual void bit_xor( void * , const void * , std::size_t ) const
00429   { ThrowErrorMsg( "not supported" ); }
00430 };
00431 
00432 }
00433 
00434 #define DATA_TRAITS_POINTER( T )        \
00435 template<> const DataTraits & data_traits<T*>()  \
00436 { static const DataTraitsPointerToFundamental<T> traits ; return traits ; }
00437 
00438 DATA_TRAITS_POINTER( char )
00439 DATA_TRAITS_POINTER( unsigned char )
00440 DATA_TRAITS_POINTER( short )
00441 DATA_TRAITS_POINTER( unsigned short )
00442 DATA_TRAITS_POINTER( int )
00443 DATA_TRAITS_POINTER( unsigned int )
00444 DATA_TRAITS_POINTER( long )
00445 DATA_TRAITS_POINTER( unsigned long )
00446 DATA_TRAITS_POINTER( float )
00447 DATA_TRAITS_POINTER( double )
00448 DATA_TRAITS_POINTER( void )
00449 DATA_TRAITS_POINTER( std::complex<float> )
00450 DATA_TRAITS_POINTER( std::complex<double> )
00451 
00452 //----------------------------------------------------------------------
00453 
00454 }
00455 }
00456 
00457 
00458 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends