Sierra Toolkit Version of the Day
DataTraitsClass.hpp
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 #include <stk_mesh/base/DataTraits.hpp>
00010 
00011 #include <stk_util/environment/ReportHandler.hpp>
00012 
00013 namespace stk {
00014 namespace mesh {
00015  
00016 //----------------------------------------------------------------------
00017 
00018 namespace {
00019 
00020 template< typename T >
00021 class DataTraitsClassPOD : public DataTraits {
00022 public:
00023   DataTraitsClassPOD( const char * name , std::size_t n )
00024   : DataTraits( typeid(T) , name , sizeof(T) , 1 )
00025   {
00026     is_pod = true ;
00027     is_class = true ;
00028     class_info.reserve( n );
00029   }
00030 
00031   void set_stride( const void * first , const void * second )
00032   {
00033     stride_of = reinterpret_cast<const unsigned char *>(second) -
00034                 reinterpret_cast<const unsigned char *>(first);
00035   }
00036 
00037   void add_member( const char * n , const DataTraits & t ,
00038                    const void * base , const void * member )
00039   {
00040     const std::size_t i = class_info.size();
00041     const std::size_t d = reinterpret_cast<const unsigned char *>(member) -
00042                           reinterpret_cast<const unsigned char *>(base);
00043     class_info.resize( i + 1 );
00044     class_info[i].name.assign( n );
00045     class_info[i].traits = & t ;
00046     class_info[i].offset = d ;
00047     if ( alignment_of < t.alignment_of ) { alignment_of = t.alignment_of ; }
00048   }
00049 
00050   void construct( void * v , std::size_t n ) const
00051   {
00052     T * x = reinterpret_cast<T*>( v ); 
00053     T * const x_end = x + n ;  
00054     for ( ; x_end != x ; ++x ) { new(x) T(); }
00055   }
00056      
00057   void destroy( void * v , std::size_t n ) const
00058   {
00059     T * x = reinterpret_cast<T*>( v ); 
00060     T * const x_end = x + n ;  
00061     for ( ; x_end != x ; ++x ) { x->~T(); }
00062   }
00063 
00064   void copy( void * vx , const void * vy , std::size_t n ) const
00065   {
00066     const T * y = reinterpret_cast<const T*>( vy );
00067     T * x = reinterpret_cast<T*>( vx ); 
00068     T * const x_end = x + n ;
00069     while ( x_end != x ) { *x++ = *y++ ; };
00070   }
00071 
00072   void pack( CommBuffer & buf , const void * v , std::size_t n ) const
00073   {  
00074     const T * x = reinterpret_cast<const T*>( v );
00075     buf.pack<T>( x , n ); 
00076   }  
00077      
00078   void unpack( CommBuffer & buf , void * v , std::size_t n ) const
00079   {  
00080     T * x = reinterpret_cast<T*>( v );
00081     buf.unpack<T>( x , n ); 
00082   }  
00083 
00084   void print( std::ostream & s , const void * v , std::size_t n ) const
00085   { ThrowErrorMsg( "not supported" ); }
00086 
00087   void sum( void * , const void * , std::size_t ) const
00088   { ThrowErrorMsg( "not supported" ); }
00089  
00090   void max( void * , const void * , std::size_t ) const
00091   { ThrowErrorMsg( "not supported" ); }
00092  
00093   void min( void * , const void * , std::size_t ) const
00094   { ThrowErrorMsg( "not supported" ); }
00095  
00096   void bit_and( void * , const void * , std::size_t ) const
00097   { ThrowErrorMsg( "not supported" ); }
00098  
00099   void bit_or( void * , const void * , std::size_t ) const
00100   { ThrowErrorMsg( "not supported" ); }
00101  
00102   void bit_xor( void * , const void * , std::size_t ) const
00103   { ThrowErrorMsg( "not supported" ); }
00104 };
00105 
00106 }
00107 
00108 //----------------------------------------------------------------------
00109 
00110 #define DATA_TRAITS_POD_CLASS_2( C , M1 , M2 )      \
00111 namespace {     \
00112 class DataTraitsClass ## C : public DataTraitsClassPOD<C> {        \
00113 public: \
00114   DataTraitsClass ## C () : DataTraitsClassPOD<C>( # C , 2 )       \
00115   {     \
00116     C tmp[1] ;     \
00117     set_stride( tmp , tmp + 1 ); \
00118     add_member( # M1 , data_traits( tmp->M1 ) , tmp , & tmp->M1 );      \
00119     add_member( # M2 , data_traits( tmp->M2 ) , tmp , & tmp->M2 );      \
00120   }     \
00121 };      \
00122 }       \
00123 template<> const DataTraits & data_traits< C >()   \
00124 { static const DataTraitsClass ## C traits ; return traits ; }
00125 
00126 //----------------------------------------------------------------------
00127 
00128 #define DATA_TRAITS_POD_CLASS_3( C , M1 , M2 , M3 )      \
00129 namespace {     \
00130 class DataTraitsClass ## C : public DataTraitsClassPOD<C> {        \
00131 public: \
00132   DataTraitsClass ## C () : DataTraitsClassPOD<C>( # C , 3 )       \
00133   {     \
00134     C tmp[1] ;     \
00135     set_stride( tmp , tmp + 1 ); \
00136     add_member( # M1 , data_traits( tmp->M1 ) , tmp , & tmp->M1 );      \
00137     add_member( # M2 , data_traits( tmp->M2 ) , tmp , & tmp->M2 );      \
00138     add_member( # M3 , data_traits( tmp->M3 ) , tmp , & tmp->M3 );      \
00139   }     \
00140 };      \
00141 }       \
00142 template<> const DataTraits & data_traits< C >()   \
00143 { static const DataTraitsClass ## C traits ; return traits ; }
00144 
00145 //----------------------------------------------------------------------
00146 
00147 #define DATA_TRAITS_POD_CLASS_4( C , M1 , M2 , M3 , M4 )      \
00148 namespace {     \
00149 class DataTraitsClass ## C : public DataTraitsClassPOD<C> {        \
00150 public: \
00151   DataTraitsClass ## C () : DataTraitsClassPOD<C>( # C , 4 )       \
00152   {     \
00153     C tmp[1] ;     \
00154     set_stride( tmp , tmp + 1 ); \
00155     add_member( # M1 , data_traits( tmp->M1 ) , tmp , & tmp->M1 );      \
00156     add_member( # M2 , data_traits( tmp->M2 ) , tmp , & tmp->M2 );      \
00157     add_member( # M3 , data_traits( tmp->M3 ) , tmp , & tmp->M3 );      \
00158     add_member( # M4 , data_traits( tmp->M4 ) , tmp , & tmp->M4 );      \
00159   }     \
00160 };      \
00161 }       \
00162 template<> const DataTraits & data_traits< C >()   \
00163 { static const DataTraitsClass ## C traits ; return traits ; }
00164 
00165 //----------------------------------------------------------------------
00166 
00167 #define DATA_TRAITS_POD_CLASS_5( C , M1 , M2 , M3 , M4 , M5 )      \
00168 namespace {     \
00169 class DataTraitsClass ## C : public DataTraitsClassPOD<C> {        \
00170 public: \
00171   DataTraitsClass ## C () : DataTraitsClassPOD<C>( # C , 5 )       \
00172   {     \
00173     C tmp[1] ;     \
00174     set_stride( tmp , tmp + 1 ); \
00175     add_member( # M1 , data_traits( tmp->M1 ) , tmp , & tmp->M1 );      \
00176     add_member( # M2 , data_traits( tmp->M2 ) , tmp , & tmp->M2 );      \
00177     add_member( # M3 , data_traits( tmp->M3 ) , tmp , & tmp->M3 );      \
00178     add_member( # M4 , data_traits( tmp->M4 ) , tmp , & tmp->M4 );      \
00179     add_member( # M5 , data_traits( tmp->M5 ) , tmp , & tmp->M5 );      \
00180   }     \
00181 };      \
00182 }       \
00183 template<> const DataTraits & data_traits< C >()   \
00184 { static const DataTraitsClass ## C traits ; return traits ; }
00185 
00186 //----------------------------------------------------------------------
00187 
00188 #define DATA_TRAITS_POD_CLASS_6( C , M1 , M2 , M3 , M4 , M5 , M6 )      \
00189 namespace {     \
00190 class DataTraitsClass ## C : public DataTraitsClassPOD<C> {        \
00191 public: \
00192   DataTraitsClass ## C () : DataTraitsClassPOD<C>( # C , 5 )       \
00193   {     \
00194     C tmp[1] ;     \
00195     set_stride( tmp , tmp + 1 ); \
00196     add_member( # M1 , data_traits( tmp->M1 ) , tmp , & tmp->M1 );      \
00197     add_member( # M2 , data_traits( tmp->M2 ) , tmp , & tmp->M2 );      \
00198     add_member( # M3 , data_traits( tmp->M3 ) , tmp , & tmp->M3 );      \
00199     add_member( # M4 , data_traits( tmp->M4 ) , tmp , & tmp->M4 );      \
00200     add_member( # M5 , data_traits( tmp->M5 ) , tmp , & tmp->M5 );      \
00201     add_member( # M6 , data_traits( tmp->M6 ) , tmp , & tmp->M6 );      \
00202   }     \
00203 };      \
00204 }       \
00205 template<> const DataTraits & data_traits< C >()   \
00206 { static const DataTraitsClass ## C traits ; return traits ; }
00207 
00208 //----------------------------------------------------------------------
00209 
00210 }
00211 }
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines