Mesh Oriented datABase  (version 5.5.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 );MB_CHK_ERR( valid );
197 
198  if( num_entities > 0 )
199  {
200  mValue.resize( data_lengths[num_entities - 1] );
201  memcpy( &mValue[0], data_ptrs[num_entities - 1], mValue.size() );
202  }
203 
204  return MB_SUCCESS;
205 }
206 
207 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, void const* const*, const int* )
208 {
209  if( range.empty() )
210  return MB_SUCCESS;
211  else
212  return not_root_set( get_name(), range.front() );
213 }
214 
216  Error* /* error */,
217  const EntityHandle* entities,
218  size_t num_entities,
219  const void* value_ptr,
220  int value_len )
221 {
222  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
223 
224  ErrorCode valid = validate_lengths( NULL, value_len ? &value_len : 0, 1 );MB_CHK_ERR( valid );
225 
226  if( num_entities > 0 )
227  {
228  mValue.resize( value_len );
229  memcpy( &mValue[0], value_ptr, value_len );
230  }
231 
232  return MB_SUCCESS;
233 }
234 
235 ErrorCode MeshTag::clear_data( SequenceManager*, Error* /* error */, const Range& range, const void*, int )
236 {
237  if( range.empty() )
238  return MB_SUCCESS;
239  else
240  return not_root_set( get_name(), range.front() );
241 }
242 
244  Error* /* error */,
245  const EntityHandle* entities,
246  size_t num_entities )
247 {
248  if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
249 
250  if( num_entities ) mValue.clear();
251 
252  return MB_SUCCESS;
253 }
254 
256 {
257  if( range.empty() )
258  return MB_SUCCESS;
259  else
260  return not_root_set( get_name(), range.front() );
261 }
262 
264  Error* /* error */,
265  Range::iterator& beg,
266  const Range::iterator& end,
267  void*&,
268  bool )
269 {
270  if( beg == end )
271  return MB_SUCCESS;
272  else
273  return not_root_set( get_name(), *beg );
274 }
275 
276 ErrorCode MeshTag::get_tagged_entities( const SequenceManager*, Range&, EntityType, const Range* ) const
277 {
278  return MB_SUCCESS;
279 }
280 
281 ErrorCode MeshTag::num_tagged_entities( const SequenceManager*, size_t&, EntityType, const Range* ) const
282 {
283  return MB_SUCCESS;
284 }
285 
287  Error*,
288  Range&,
289  const void*,
290  int,
291  EntityType,
292  const Range* ) const
293 {
294  return MB_SUCCESS;
295 }
296 
298 {
299  return ( 0 == h ) && ( !mValue.empty() );
300 }
301 
302 ErrorCode MeshTag::get_memory_use( const SequenceManager*, unsigned long& total, unsigned long& per_entity ) const
303 {
304  total = TagInfo::get_memory_use() + sizeof( *this ) + mValue.size();
305  per_entity = 0;
306  return MB_SUCCESS;
307 }
308 
309 } // namespace moab