Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
TagInfo.cpp
Go to the documentation of this file.
1 #include "TagInfo.hpp"
2 #include "moab/Error.hpp"
3 #include "moab/ErrorHandler.hpp"
4 #include <cstring> /* memcpy */
5 #include <cstdlib> /* realloc & free */
6 #include <cassert>
7 
8 namespace moab
9 {
10 
11 TagInfo::TagInfo( const char* name, int size, DataType type, const void* default_value, int default_value_size )
12  : mDefaultValue( NULL ), mMeshValue( NULL ), mDefaultValueSize( default_value_size ), mMeshValueSize( 0 ),
13  mDataSize( size ), dataType( type )
14 {
15  if( default_value )
16  {
17  mDefaultValue = malloc( mDefaultValueSize );
18  memcpy( mDefaultValue, default_value, mDefaultValueSize );
19  }
20  if( name ) mTagName = name;
21 }
22 
24 {
25  free( mDefaultValue );
26  mDefaultValue = 0;
28 }
29 
31 {
32  static const int sizes[] = { 1, sizeof( int ), sizeof( double ), 1, sizeof( EntityHandle ), 0 };
33  return sizes[t];
34 }
35 
36 bool TagInfo::equals_default_value( const void* data, int size ) const
37 {
38  if( !get_default_value() ) return false;
39 
40  if( variable_length() && size != get_default_value_size() ) return false;
41 
42  if( !variable_length() && size >= 0 && size != get_size() ) return false;
43 
44  if( get_data_type() == MB_TYPE_BIT )
45  {
46  assert( get_size() <= 8 && get_default_value_size() == 1 );
47  unsigned char byte1 = *reinterpret_cast< const unsigned char* >( data );
48  unsigned char byte2 = *reinterpret_cast< const unsigned char* >( get_default_value() );
49  unsigned char mask = (unsigned char)( ( 1u << get_size() ) - 1 );
50  return ( byte1 & mask ) == ( byte2 & mask );
51  }
52  else
53  {
54  return !memcmp( data, get_default_value(), get_default_value_size() );
55  }
56 }
57 
58 // Check that all lengths are valid multiples of the type size.
59 // Returns true if all lengths are valid, false otherwise.
60 bool TagInfo::check_valid_sizes( const int* sizes, int num_sizes ) const
61 {
62  const unsigned size = size_from_data_type( get_data_type() );
63  if( 1 == size ) return true;
64 
65  unsigned sum = 0;
66  for( int i = 0; i < num_sizes; ++i )
67  sum |= ( (unsigned)sizes[i] ) % size;
68 
69  return ( 0 == sum );
70 }
71 
72 ErrorCode TagInfo::validate_lengths( Error* /* error_handler */, const int* lengths, size_t num_lengths ) const
73 {
74  int bits = 0;
75  if( variable_length() )
76  {
77  if( !lengths )
78  {
79  MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No size specified for variable-length tag" );
80  }
81  const unsigned type_size = size_from_data_type( get_data_type() );
82  if( type_size == 1 ) return MB_SUCCESS;
83  for( size_t i = 0; i < num_lengths; ++i )
84  bits |= lengths[i] % type_size;
85  }
86  else if( lengths )
87  {
88  for( size_t i = 0; i < num_lengths; ++i )
89  bits |= lengths[i] - get_size();
90  }
91  if( 0 == bits ) return MB_SUCCESS;
92 
93  MB_SET_ERR( MB_INVALID_SIZE, "Tag data with invalid size" );
94 }
95 
96 } // namespace moab