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