Sierra Toolkit Version of the Day
DataTraitsEnum.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 
00010 #include <stk_mesh/base/DataTraits.hpp>
00011  
00012 //----------------------------------------------------------------------
00013 
00014 namespace stk {
00015 namespace mesh {
00016 
00017 template< typename EnumType > class DataTraitsEnum ;
00018 
00019 //----------------------------------------------------------------------
00020 //----------------------------------------------------------------------
00021  
00022 namespace {
00023 
00024 template< typename T >
00025 class DataTraitsEnum : public DataTraits {
00026 public:
00027   DataTraitsEnum( const char * name , std::size_t n )
00028   : DataTraits( typeid(T) , name , sizeof(T) , sizeof(T) )
00029   {
00030     is_pod  = true ;
00031     is_enum = true ;
00032     enum_info.reserve( n );
00033   }
00034 
00035   void add_member( const char * n , T v )
00036   {
00037     const std::size_t i = enum_info.size();
00038     enum_info.resize( i + 1 );
00039     enum_info[i].name.assign( n );
00040     enum_info[i].value = static_cast<long>( v );
00041   }
00042 
00043   void construct( void * v , std::size_t n ) const
00044   {
00045     const T init = static_cast<T>( enum_info.front().value );
00046     T * x = reinterpret_cast<T*>(v);
00047     T * const x_end = x + n ;
00048     while ( x_end != x ) { *x++ = init ; }
00049   }
00050 
00051   void destroy( void * v , std::size_t n ) const {}
00052 
00053   void copy( void * vx , const void * vy , std::size_t n ) const
00054   {
00055     const T * y = reinterpret_cast<const T*>(vy);
00056     T * x = reinterpret_cast<T*>(vx);
00057     T * const x_end = x + n ;
00058     while ( x_end != x ) { *x++ = *y++ ; }
00059   }
00060 
00061   void max( void * vx , const void * vy , std::size_t n ) const
00062   {
00063     const T * y = reinterpret_cast<const T*>(vy);
00064     T * x = reinterpret_cast<T*>(vx);
00065     T * const x_end = x + n ;
00066     for ( ; x_end != x ; ++x , ++y ) { if ( *x < *y ) { *x = *y ; } }
00067   }
00068 
00069   void min( void * vx , const void * vy , std::size_t n ) const
00070   {
00071     const T * y = reinterpret_cast<const T*>(vy);
00072     T * x = reinterpret_cast<T*>(vx);
00073     T * const x_end = x + n ;
00074     for ( ; x_end != x ; ++x , ++y ) { if ( *x > *y ) { *x = *y ; } }
00075   }
00076 
00077   void print_one( std::ostream & s , T v ) const
00078   {
00079     std::vector<EnumMember>::const_iterator i = enum_info.begin();
00080     for ( ; i != enum_info.end() && i->value != v ; ++i );
00081     if ( i != enum_info.end() ) {
00082       s << i->name ;
00083     }
00084     else {
00085       s << name << "( " << static_cast<long>( v ) << " VALUE_NOT_VALID )" ;
00086     }
00087   }
00088 
00089   void print( std::ostream & s , const void * v , std::size_t n ) const
00090   {
00091     if ( n ) {
00092       const T * x = reinterpret_cast<const T*>(v);
00093       const T * const x_end = x + n ;
00094       print_one( s , *x++ );
00095       while ( x_end != x ) { s << " " ; print_one( s , *x++ ); }
00096     }
00097   }
00098 
00099   void pack( CommBuffer & buf , const void * v , std::size_t n ) const
00100   {
00101     const T * x = reinterpret_cast<const T*>(v);
00102     buf.pack<T>( x , n );
00103   }
00104 
00105   void unpack( CommBuffer & buf , void * v , std::size_t n ) const
00106   {
00107     T * x = reinterpret_cast<T*>(v);
00108     buf.unpack<T>( x , n );
00109   }
00110 
00111   void sum( void * , const void * , std::size_t ) const
00112   { throw_not_supported( "sum" ); }
00113 
00114   void bit_and( void * , const void * , std::size_t ) const
00115   { throw_not_supported( "sum" ); }
00116 
00117   void bit_or( void * , const void * , std::size_t ) const
00118   { throw_not_supported( "sum" ); }
00119 
00120   void bit_xor( void * , const void * , std::size_t ) const
00121   { throw_not_supported( "sum" ); }
00122 };
00123 
00124 }
00125 
00126 //----------------------------------------------------------------------
00127 
00128 #define DATA_TRAITS_ENUM_1( T , V1 )    \
00129 namespace {     \
00130 class DataTraitsEnum ## T : public DataTraitsEnum<T> {      \
00131 public: \
00132   DataTraitsEnum ## T () : DataTraitsEnum<T>( # T , 1 ) \
00133   { add_member( # V1 , V1 ); }  \
00134 };      \
00135 }       \
00136 template<> const DataTraits & data_traits< T >()   \
00137 { static const DataTraitsEnum ## T traits ; return traits ; }
00138 
00139 //----------------------------------------------------------------------
00140 
00141 #define DATA_TRAITS_ENUM_2( T , V1 , V2 )    \
00142 namespace {     \
00143 class DataTraitsEnum ## T : public DataTraitsEnum<T> {      \
00144 public: \
00145   DataTraitsEnum ## T () : DataTraitsEnum<T>( # T , 2 ) \
00146   { \
00147     add_member( # V1 , V1 ); \
00148     add_member( # V2 , V2 ); \
00149   }  \
00150 };      \
00151 }       \
00152 template<> const DataTraits & data_traits< T >()   \
00153 { static const DataTraitsEnum ## T traits ; return traits ; }
00154 
00155 //----------------------------------------------------------------------
00156 
00157 #define DATA_TRAITS_ENUM_3( T , V1 , V2 , V3 )    \
00158 namespace {     \
00159 class DataTraitsEnum ## T : public DataTraitsEnum<T> {      \
00160 public: \
00161   DataTraitsEnum ## T () : DataTraitsEnum<T>( # T , 3 ) \
00162   { \
00163     add_member( # V1 , V1 ); \
00164     add_member( # V2 , V2 ); \
00165     add_member( # V3 , V3 ); \
00166   }  \
00167 };      \
00168 }       \
00169 template<> const DataTraits & data_traits< T >()   \
00170 { static const DataTraitsEnum ## T traits ; return traits ; }
00171 
00172 //----------------------------------------------------------------------
00173 
00174 } // namespace mesh
00175 } // namespace stk
00176 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends