Sierra Toolkit  Version of the Day
DataTraits.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #include <cstddef>
11 #include <stdexcept>
12 
13 #include <stk_util/environment/ReportHandler.hpp>
14 
15 #include <stk_mesh/base/DataTraits.hpp>
16 #include <stk_mesh/base/DataTraitsEnum.hpp>
17 #include <stk_mesh/base/DataTraitsClass.hpp>
18 
19 namespace stk_classic {
20 namespace mesh {
21 
22 //----------------------------------------------------------------------
23 
24 namespace {
25 
26 std::size_t stride( std::size_t size , std::size_t align )
27 {
28  if ( align && size % align ) { size += align - size % align ; }
29  return size ;
30 }
31 
32 }
33 
34 DataTraits::DataTraits( const std::type_info & arg_type ,
35  const char * const arg_name ,
36  std::size_t arg_size ,
37  std::size_t arg_align )
38  : type_info( arg_type ),
39  size_of( arg_size ),
40  is_void( false ),
41  is_integral( false ),
42  is_floating_point( false ),
43  is_pointer( false ),
44  is_enum( false ),
45  is_class( false ),
46  is_pod( false ),
47  is_signed( false ),
48  is_unsigned( false ),
49  alignment_of( arg_align ),
50  stride_of( stride( arg_size , arg_align ) ),
51  remove_pointer( NULL ),
52  name( arg_name ),
53  enum_info(),
54  class_info()
55 {}
56 
57 DataTraits::DataTraits( const std::type_info & arg_type ,
58  const DataTraits & arg_traits )
59  : type_info( arg_type ),
60  size_of( sizeof(void*) ),
61  is_void( false ),
62  is_integral( false ),
63  is_floating_point( false ),
64  is_pointer( true ),
65  is_enum( false ),
66  is_class( false ),
67  is_pod( false ),
68  is_signed( false ),
69  is_unsigned( false ),
70  alignment_of( sizeof(void*) ),
71  stride_of( sizeof(void*) ),
72  remove_pointer( & arg_traits ),
73  name(),
74  enum_info(),
75  class_info()
76 {
77  name.assign( arg_traits.name ).append("*");
78 }
79 
80 //----------------------------------------------------------------------
81 
82 namespace {
83 
84 class DataTraitsVoid : public DataTraits {
85 public:
86 
87  DataTraitsVoid()
88  : DataTraits( typeid(void) , "void" , 0 , 0 )
89  { is_void = true ; }
90 
91  void construct( void * , std::size_t ) const
92  { ThrowErrorMsg( "not supported" ); }
93 
94  void destroy( void * , std::size_t ) const
95  { ThrowErrorMsg( "not supported" ); }
96 
97  void pack( CommBuffer & , const void * , std::size_t ) const
98  { ThrowErrorMsg( "not supported" ); }
99 
100  void unpack( CommBuffer & , void * , std::size_t ) const
101  { ThrowErrorMsg( "not supported" ); }
102 
103  void print( std::ostream & , const void * , std::size_t ) const
104  { ThrowErrorMsg( "not supported" ); }
105 
106  void copy( void * , const void * , std::size_t ) const
107  { ThrowErrorMsg( "not supported" ); }
108 
109  void sum( void * , const void * , std::size_t ) const
110  { ThrowErrorMsg( "not supported" ); }
111 
112  void max( void * , const void * , std::size_t ) const
113  { ThrowErrorMsg( "not supported" ); }
114 
115  void min( void * , const void * , std::size_t ) const
116  { ThrowErrorMsg( "not supported" ); }
117 
118  virtual void bit_and( void * , const void * , std::size_t ) const
119  { ThrowErrorMsg( "not supported" ); }
120 
121  virtual void bit_or( void * , const void * , std::size_t ) const
122  { ThrowErrorMsg( "not supported" ); }
123 
124  virtual void bit_xor( void * , const void * , std::size_t ) const
125  { ThrowErrorMsg( "not supported" ); }
126 };
127 
128 }
129 
130 template<> const DataTraits & data_traits<void>()
131 { static const DataTraitsVoid traits ; return traits ; }
132 
133 //----------------------------------------------------------------------
134 
135 namespace {
136 
137 template< typename A , typename B >
138 struct IsSameType { enum { value = false }; };
139 
140 template< typename A >
141 struct IsSameType<A,A> { enum { value = true }; };
142 
143 
144 template< typename T >
145 class DataTraitsCommon : public DataTraits {
146 public:
147 
148  explicit DataTraitsCommon( const char * arg_name )
149  : DataTraits( typeid(T) , arg_name , sizeof(T) , sizeof(T) )
150  {
151  is_pod = true ;
152 
153  is_integral = IsSameType<T,char>::value ||
154  IsSameType<T,unsigned char>::value ||
155  IsSameType<T,short>::value ||
156  IsSameType<T,unsigned short>::value ||
157  IsSameType<T,int>::value ||
158  IsSameType<T,unsigned int>::value ||
159  IsSameType<T,long>::value ||
160  IsSameType<T,unsigned long>::value ;
161 
162  is_signed = IsSameType<T,char>::value ||
163  IsSameType<T,short>::value ||
164  IsSameType<T,int>::value ||
165  IsSameType<T,long>::value ;
166 
167  is_unsigned = IsSameType<T,unsigned char>::value ||
168  IsSameType<T,unsigned short>::value ||
169  IsSameType<T,unsigned int>::value ||
170  IsSameType<T,unsigned long>::value ;
171 
172  is_floating_point = IsSameType<T,double>::value ||
173  IsSameType<T,float>::value ;
174  }
175 
176  void construct( void * v , std::size_t n ) const
177  {
178  T * x = reinterpret_cast<T*>( v );
179  T * const x_end = x + n ;
180  while ( x_end != x ) { *x++ = 0 ; }
181  }
182 
183  void destroy( void * v , std::size_t n ) const {}
184 
185  void pack( CommBuffer & buf , const void * v , std::size_t n ) const
186  {
187  const T * x = reinterpret_cast<const T*>( v );
188  buf.pack<T>( x , n );
189  }
190 
191  void unpack( CommBuffer & buf , void * v , std::size_t n ) const
192  {
193  T * x = reinterpret_cast<T*>( v );
194  buf.unpack<T>( x , n );
195  }
196 
197  void copy( void * vx , const void * vy , std::size_t n ) const
198  {
199  const T * y = reinterpret_cast<const T*>( vy );
200  T * x = reinterpret_cast<T*>( vx );
201  T * const x_end = x + n ;
202  while ( x_end != x ) { *x++ = *y++ ; };
203  }
204 
205  void sum( void * vx , const void * vy , std::size_t n ) const
206  {
207  const T * y = reinterpret_cast<const T*>( vy );
208  T * x = reinterpret_cast<T*>( vx );
209  T * const x_end = x + n ;
210  while ( x_end != x ) { *x++ += *y++ ; };
211  }
212 
213  virtual void print( std::ostream & s , const void * v , std::size_t n ) const
214  {
215  if ( n ) {
216  const T * x = reinterpret_cast<const T*>( v );
217  const T * const x_end = x + n ;
218  s << *x++ ;
219  while ( x_end != x ) { s << " " << *x++ ; }
220  }
221  }
222 
223  virtual void max( void * vx , const void * vy , std::size_t n ) const
224  { ThrowErrorMsg( "not supported" ); }
225 
226  virtual void min( void * vx , const void * vy , std::size_t n ) const
227  { ThrowErrorMsg( "not supported" ); }
228 
229  virtual void bit_and( void * , const void * , std::size_t ) const
230  { ThrowErrorMsg( "not supported" ); }
231 
232  virtual void bit_or( void * , const void * , std::size_t ) const
233  { ThrowErrorMsg( "not supported" ); }
234 
235  virtual void bit_xor( void * , const void * , std::size_t ) const
236  { ThrowErrorMsg( "not supported" ); }
237 };
238 
239 template< typename T >
240 class DataTraitsNumeric : public DataTraitsCommon<T> {
241 public:
242 
243  explicit DataTraitsNumeric( const char * arg_name )
244  : DataTraitsCommon<T>( arg_name ) {}
245 
246  virtual void max( void * vx , const void * vy , std::size_t n ) const
247  {
248  const T * y = reinterpret_cast<const T*>( vy );
249  T * x = reinterpret_cast<T*>( vx );
250  T * const x_end = x + n ;
251  for ( ; x_end != x ; ++x , ++y ) { if ( *x < *y ) { *x = *y ; } }
252  }
253 
254  virtual void min( void * vx , const void * vy , std::size_t n ) const
255  {
256  const T * y = reinterpret_cast<const T*>( vy );
257  T * x = reinterpret_cast<T*>( vx );
258  T * const x_end = x + n ;
259  for ( ; x_end != x ; ++x , ++y ) { if ( *x > *y ) { *x = *y ; } }
260  }
261 };
262 
263 template< typename T >
264 class DataTraitsComplex : public DataTraitsCommon<T> {
265 public:
266 
267  explicit DataTraitsComplex( const char * arg_name )
268  : DataTraitsCommon<T>( arg_name ) {}
269 };
270 
271 template< typename T >
272 class DataTraitsIntegral : public DataTraitsNumeric<T> {
273 public:
274  DataTraitsIntegral( const char * name ) : DataTraitsNumeric<T>( name ) {}
275 
276  virtual void bit_and( void * vx , const void * vy , std::size_t n ) const
277  {
278  const T * y = reinterpret_cast<const T*>( vy );
279  T * x = reinterpret_cast<T*>( vx );
280  T * const x_end = x + n ;
281  while ( x_end != x ) { *x++ &= *y++ ; }
282  }
283 
284  virtual void bit_or( void * vx , const void * vy , std::size_t n ) const
285  {
286  const T * y = reinterpret_cast<const T*>( vy );
287  T * x = reinterpret_cast<T*>( vx );
288  T * const x_end = x + n ;
289  while ( x_end != x ) { *x++ |= *y++ ; }
290  }
291 
292  virtual void bit_xor( void * vx , const void * vy , std::size_t n ) const
293  {
294  const T * y = reinterpret_cast<const T*>( vy );
295  T * x = reinterpret_cast<T*>( vx );
296  T * const x_end = x + n ;
297  while ( x_end != x ) { *x++ ^= *y++ ; }
298  }
299 };
300 
301 class DataTraitsChar : public DataTraitsIntegral<char> {
302 public:
303  DataTraitsChar() : DataTraitsIntegral<char>( "char" ) {}
304 
305  virtual void print( std::ostream & s , const void * v , std::size_t n ) const
306  {
307  if ( n ) {
308  const char * x = reinterpret_cast<const char*>( v );
309  const char * const x_end = x + n ;
310  s << int(*x++) ;
311  while ( x_end != x ) { s << " " << int(*x++) ; }
312  }
313  }
314 };
315 
316 class DataTraitsUnsignedChar : public DataTraitsIntegral<unsigned char> {
317 public:
318  DataTraitsUnsignedChar()
319  : DataTraitsIntegral<unsigned char>( "unsigned char" ) {}
320 
321  virtual void print( std::ostream & s , const void * v , std::size_t n ) const
322  {
323  if ( n ) {
324  const unsigned char * x = reinterpret_cast<const unsigned char*>( v );
325  const unsigned char * const x_end = x + n ;
326  s << unsigned(*x++) ;
327  while ( x_end != x ) { s << " " << unsigned(*x++) ; }
328  }
329  }
330 };
331 
332 }
333 
334 #define DATA_TRAITS_NUMERIC( T ) \
335 template<> \
336 const DataTraits & data_traits<T>() \
337 { static const DataTraitsNumeric<T> traits( #T ); return traits ; }
338 
339 #define DATA_TRAITS_COMPLEX( T ) \
340 template<> \
341 const DataTraits & data_traits<T>() \
342 { static const DataTraitsComplex<T> traits( #T ); return traits ; }
343 
344 #define DATA_TRAITS_INTEGRAL( T ) \
345 template<> \
346 const DataTraits & data_traits<T>() \
347 { static const DataTraitsIntegral<T> traits( #T ); return traits ; }
348 
349 template<>
350 const DataTraits & data_traits<char>()
351 { static const DataTraitsChar traits ; return traits ; }
352 
353 template<>
354 const DataTraits & data_traits<unsigned char>()
355 { static const DataTraitsUnsignedChar traits ; return traits ; }
356 
357 DATA_TRAITS_INTEGRAL( short )
358 DATA_TRAITS_INTEGRAL( unsigned short )
359 DATA_TRAITS_INTEGRAL( int )
360 DATA_TRAITS_INTEGRAL( unsigned int )
361 DATA_TRAITS_INTEGRAL( long )
362 DATA_TRAITS_INTEGRAL( unsigned long )
363 DATA_TRAITS_NUMERIC( float )
364 DATA_TRAITS_NUMERIC( double )
365 DATA_TRAITS_COMPLEX( std::complex<float> ) // TODO: Probably not right
366 DATA_TRAITS_COMPLEX( std::complex<double> ) // TODO: Probably not right
367 
368 //----------------------------------------------------------------------
369 //----------------------------------------------------------------------
370 
371 namespace {
372 
373 template< typename T >
374 class DataTraitsPointerToFundamental : public DataTraits {
375 public:
376 
377  DataTraitsPointerToFundamental()
378  : DataTraits( typeid(T*) , data_traits<T>() ) {}
379 
380  void construct( void * v , std::size_t n ) const
381  {
382  void ** x = reinterpret_cast<void**>(v);
383  void ** const x_end = x + n ;
384  while ( x_end != x ) { *x++ = NULL ; }
385  }
386 
387  void destroy( void * v , std::size_t n ) const
388  {
389  void ** x = reinterpret_cast<void**>(v);
390  void ** const x_end = x + n ;
391  while ( x_end != x ) { *x++ = NULL ; }
392  }
393 
394  void copy( void * vx , const void * vy , std::size_t n ) const
395  {
396  void * const * y = reinterpret_cast<void* const *>(vy);
397  void ** x = reinterpret_cast<void**>(vx);
398  void ** const x_end = x + n ;
399  while ( x_end != x ) { *x++ = *y++ ; }
400  }
401 
402  void pack( CommBuffer & , const void * , std::size_t ) const
403  { ThrowErrorMsg( "not supported" ); }
404 
405  void unpack( CommBuffer & , void * , std::size_t ) const
406  { ThrowErrorMsg( "not supported" ); }
407 
408  void print( std::ostream & , const void * , std::size_t ) const
409  { ThrowErrorMsg( "not supported" ); }
410 
411  void sum( void * , const void * , std::size_t ) const
412  { ThrowErrorMsg( "not supported" ); }
413 
414  void max( void * , const void * , std::size_t ) const
415  { ThrowErrorMsg( "not supported" ); }
416 
417  void min( void * , const void * , std::size_t ) const
418  { ThrowErrorMsg( "not supported" ); }
419 
420  virtual void bit_and( void * , const void * , std::size_t ) const
421  { ThrowErrorMsg( "not supported" ); }
422 
423  virtual void bit_or( void * , const void * , std::size_t ) const
424  { ThrowErrorMsg( "not supported" ); }
425 
426  virtual void bit_xor( void * , const void * , std::size_t ) const
427  { ThrowErrorMsg( "not supported" ); }
428 };
429 
430 }
431 
432 #define DATA_TRAITS_POINTER( T ) \
433 template<> const DataTraits & data_traits<T*>() \
434 { static const DataTraitsPointerToFundamental<T> traits ; return traits ; }
435 
436 DATA_TRAITS_POINTER( char )
437 DATA_TRAITS_POINTER( unsigned char )
438 DATA_TRAITS_POINTER( short )
439 DATA_TRAITS_POINTER( unsigned short )
440 DATA_TRAITS_POINTER( int )
441 DATA_TRAITS_POINTER( unsigned int )
442 DATA_TRAITS_POINTER( long )
443 DATA_TRAITS_POINTER( unsigned long )
444 DATA_TRAITS_POINTER( float )
445 DATA_TRAITS_POINTER( double )
446 DATA_TRAITS_POINTER( void )
447 DATA_TRAITS_POINTER( std::complex<float> )
448 DATA_TRAITS_POINTER( std::complex<double> )
449 
450 //----------------------------------------------------------------------
451 
452 }
453 }
454 
455 
456 
std::ostream & print(std::ostream &os, const std::string &indent, const Bucket &bucket)
Print the parts and entities of this bucket.
Definition: Bucket.cpp:259
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Sierra Toolkit.