Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
MeshTag.cpp
Go to the documentation of this file.
1 /** \file MeshTag.cpp
2  * \author Jason Kraftcheck
3  * \date 2010-12-14
4  */
5 
6 #include "moab/Interface.hpp"
7 #include "MeshTag.hpp"
8 #include "SysUtil.hpp"
9 #include "moab/Error.hpp"
10 #include "moab/CN.hpp"
11 #include "Internals.hpp"
12 
13 namespace moab
14 {
15 
16 inline static ErrorCode not_root_set( const std::string& /*name*/, EntityHandle /*h*/ )
17 {
18  // MB_TAG_NOT_FOUND could be a non-error condition, do not call MB_SET_ERR on it
19  // Print warning messages for debugging only
20 #if 0
21  MB_SET_ERR(MB_VARIABLE_DATA_LENGTH, "Cannot get/set mesh/global tag " << name << " on non-root-set " << CN::EntityTypeName(TYPE_FROM_HANDLE(h)) << " " << (unsigned long)ID_FROM_HANDLE(h));
22 #endif
23 
24  return MB_TAG_NOT_FOUND;
25 }
26 
27 inline static bool all_root_set( std::string name, const EntityHandle* array, size_t len )
28 {
29  for( size_t i = 0; i < len; ++i )
30  {
31  if( array[i] )
32  {
33  not_root_set( name, array[i] );
34  return false;
35  }
36  }
37 
38  return true;
39 }
40 
41 inline static ErrorCode not_found( const std::string& /*name*/ )
42 {
43  // MB_TAG_NOT_FOUND could be a non-error condition, do not call MB_SET_ERR on it
44 #if 0
45  fprintf(stderr, "[Warning]: No mesh tag %s value for global/mesh tag\n", name.c_str());
46 #endif
47 
48  return MB_TAG_NOT_FOUND;
49 }
50 
51 MeshTag::MeshTag( const char* name, int size, DataType type, const void* default_value, int default_value_size )
52  : TagInfo( name, size, type, default_value, default_value_size )
53 {
54 }
55 
57 
59 {
60  return MB_TAG_MESH;
61 }
62 
64 {
65  return MB_SUCCESS;
66 }
67 
69  Error* /* error */,
70  const EntityHandle* entities,
71  size_t num_entities,
72  void* data ) const
73 {
74  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
75 
76  const void* ptr;
77  int len;
78 
79  if( !mValue.empty() )
80  {
81  ptr = &mValue[0];
82  len = mValue.size();
83  }
84  else if( get_default_value() )
85  {
86  ptr = get_default_value();
87  len = get_default_value_size();
88  }
89  else
90  {
91  return not_found( get_name() );
92  }
93 
94  SysUtil::setmem( data, ptr, len, num_entities );
95  return MB_SUCCESS;
96 }
97 
98 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& r, void* ) const
99 {
100  if( variable_length() )
101  {
102  MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
103  }
104  else if( r.empty() )
105  return MB_SUCCESS;
106  else
107  return not_root_set( get_name(), r.front() );
108 }
109 
111  Error* /* error */,
112  const EntityHandle* entities,
113  size_t num_entities,
114  const void** data_ptrs,
115  int* data_lengths ) const
116 {
117  const void* ptr;
118  int len;
119 
120  if( !mValue.empty() )
121  {
122  ptr = &mValue[0];
123  len = mValue.size();
124  }
125  else if( get_default_value() )
126  {
127  ptr = get_default_value();
128  len = get_default_value_size();
129  }
130  else
131  {
132  return not_found( get_name() );
133  }
134 
135  for( size_t i = 0; i < num_entities; ++i )
136  {
137  if( entities[i] ) return not_root_set( get_name(), entities[i] ); // Not root set
138  data_ptrs[i] = ptr;
139  if( data_lengths ) data_lengths[i] = len;
140  }
141 
142  return MB_SUCCESS;
143 }
144 
145 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& range, const void**, int* ) const
146 {
147  if( range.empty() )
148  return MB_SUCCESS;
149  else
150  return not_root_set( get_name(), range.front() );
151 }
152 
154  Error* /* error */,
155  const EntityHandle* entities,
156  size_t num_entities,
157  const void* data )
158 {
159  if( variable_length() )
160  {
161  MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
162  }
163  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
164 
165  if( num_entities > 0 )
166  {
167  mValue.resize( get_size() );
168  const unsigned char* bytes = reinterpret_cast< const unsigned char* >( data );
169  memcpy( &mValue[0], bytes + get_size() * ( num_entities - 1 ), get_size() );
170  }
171 
172  return MB_SUCCESS;
173 }
174 
175 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, const void* )
176 {
177  if( variable_length() )
178  {
179  MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
180  }
181  else if( range.empty() )
182  return MB_SUCCESS;
183  else
184  return not_root_set( get_name(), range.front() );
185 }
186 
188  Error* /* error */,
189  const EntityHandle* entities,
190  size_t num_entities,
191  void const* const* data_ptrs,
192  const int* data_lengths )
193 {
194  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
195 
196  ErrorCode valid = validate_lengths( NULL, data_lengths, num_entities );
197  MB_CHK_ERR( valid );
198 
199  if( num_entities > 0 )
200  {
201  mValue.resize( data_lengths[num_entities - 1] );
202  memcpy( &mValue[0], data_ptrs[num_entities - 1], mValue.size() );
203  }
204 
205  return MB_SUCCESS;
206 }
207 
208 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, void const* const*, const int* )
209 {
210  if( range.empty() )
211  return MB_SUCCESS;
212  else
213  return not_root_set( get_name(), range.front() );
214 }
215 
217  Error* /* error */,
218  const EntityHandle* entities,
219  size_t num_entities,
220  const void* value_ptr,
221  int value_len )
222 {
223  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
224 
225  ErrorCode valid = validate_lengths( NULL, value_len ? &value_len : 0, 1 );
226  MB_CHK_ERR( valid );
227 
228  if( num_entities > 0 )
229  {
230  mValue.resize( value_len );
231  memcpy( &mValue[0], value_ptr, value_len );
232  }
233 
234  return MB_SUCCESS;
235 }
236 
237 ErrorCode MeshTag::clear_data( SequenceManager*, Error* /* error */, const Range& range, const void*, int )
238 {
239  if( range.empty() )
240  return MB_SUCCESS;
241  else
242  return not_root_set( get_name(), range.front() );
243 }
244 
246  Error* /* error */,
247  const EntityHandle* entities,
248  size_t num_entities )
249 {
250  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
251 
252  if( num_entities ) mValue.clear();
253 
254  return MB_SUCCESS;
255 }
256 
258 {
259  if( range.empty() )
260  return MB_SUCCESS;
261  else
262  return not_root_set( get_name(), range.front() );
263 }
264 
266  Error* /* error */,
267  Range::iterator& beg,
268  const Range::iterator& end,
269  void*&,
270  bool )
271 {
272  if( beg == end )
273  return MB_SUCCESS;
274  else
275  return not_root_set( get_name(), *beg );
276 }
277 
278 ErrorCode MeshTag::get_tagged_entities( const SequenceManager*, Range&, EntityType, const Range* ) const
279 {
280  return MB_SUCCESS;
281 }
282 
283 ErrorCode MeshTag::num_tagged_entities( const SequenceManager*, size_t&, EntityType, const Range* ) const
284 {
285  return MB_SUCCESS;
286 }
287 
289  Error*,
290  Range&,
291  const void*,
292  int,
293  EntityType,
294  const Range* ) const
295 {
296  return MB_SUCCESS;
297 }
298 
300 {
301  return ( 0 == h ) && ( !mValue.empty() );
302 }
303 
304 ErrorCode MeshTag::get_memory_use( const SequenceManager*, unsigned long& total, unsigned long& per_entity ) const
305 {
306  total = TagInfo::get_memory_use() + sizeof( *this ) + mValue.size();
307  per_entity = 0;
308  return MB_SUCCESS;
309 }
310 
311 } // namespace moab