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