MOAB: Mesh Oriented datABase  (version 5.4.1)
MeshTag.cpp
Go to the documentation of this file.
00001 /** \file   MeshTag.cpp
00002  *  \author Jason Kraftcheck
00003  *  \date   2010-12-14
00004  */
00005 
00006 #include "moab/Interface.hpp"
00007 #include "MeshTag.hpp"
00008 #include "SysUtil.hpp"
00009 #include "moab/Error.hpp"
00010 #include "moab/CN.hpp"
00011 #include "Internals.hpp"
00012 
00013 namespace moab
00014 {
00015 
00016 inline static ErrorCode not_root_set( const std::string& /*name*/, EntityHandle /*h*/ )
00017 {
00018     // MB_TAG_NOT_FOUND could be a non-error condition, do not call MB_SET_ERR on it
00019     // Print warning messages for debugging only
00020 #if 0
00021   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));
00022 #endif
00023 
00024     return MB_TAG_NOT_FOUND;
00025 }
00026 
00027 inline static bool all_root_set( std::string name, const EntityHandle* array, size_t len )
00028 {
00029     for( size_t i = 0; i < len; ++i )
00030     {
00031         if( array[i] )
00032         {
00033             not_root_set( name, array[i] );
00034             return false;
00035         }
00036     }
00037 
00038     return true;
00039 }
00040 
00041 inline static ErrorCode not_found( const std::string& /*name*/ )
00042 {
00043     // MB_TAG_NOT_FOUND could be a non-error condition, do not call MB_SET_ERR on it
00044 #if 0
00045   fprintf(stderr, "[Warning]: No mesh tag %s value for global/mesh tag\n", name.c_str());
00046 #endif
00047 
00048     return MB_TAG_NOT_FOUND;
00049 }
00050 
00051 MeshTag::MeshTag( const char* name, int size, DataType type, const void* default_value, int default_value_size )
00052     : TagInfo( name, size, type, default_value, default_value_size )
00053 {
00054 }
00055 
00056 MeshTag::~MeshTag() {}
00057 
00058 TagType MeshTag::get_storage_type() const
00059 {
00060     return MB_TAG_MESH;
00061 }
00062 
00063 ErrorCode MeshTag::release_all_data( SequenceManager*, Error*, bool )
00064 {
00065     return MB_SUCCESS;
00066 }
00067 
00068 ErrorCode MeshTag::get_data( const SequenceManager*,
00069                              Error* /* error */,
00070                              const EntityHandle* entities,
00071                              size_t num_entities,
00072                              void* data ) const
00073 {
00074     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00075 
00076     const void* ptr;
00077     int len;
00078 
00079     if( !mValue.empty() )
00080     {
00081         ptr = &mValue[0];
00082         len = mValue.size();
00083     }
00084     else if( get_default_value() )
00085     {
00086         ptr = get_default_value();
00087         len = get_default_value_size();
00088     }
00089     else
00090     {
00091         return not_found( get_name() );
00092     }
00093 
00094     SysUtil::setmem( data, ptr, len, num_entities );
00095     return MB_SUCCESS;
00096 }
00097 
00098 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& r, void* ) const
00099 {
00100     if( variable_length() )
00101     {
00102         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00103     }
00104     else if( r.empty() )
00105         return MB_SUCCESS;
00106     else
00107         return not_root_set( get_name(), r.front() );
00108 }
00109 
00110 ErrorCode MeshTag::get_data( const SequenceManager*,
00111                              Error* /* error */,
00112                              const EntityHandle* entities,
00113                              size_t num_entities,
00114                              const void** data_ptrs,
00115                              int* data_lengths ) const
00116 {
00117     const void* ptr;
00118     int len;
00119 
00120     if( !mValue.empty() )
00121     {
00122         ptr = &mValue[0];
00123         len = mValue.size();
00124     }
00125     else if( get_default_value() )
00126     {
00127         ptr = get_default_value();
00128         len = get_default_value_size();
00129     }
00130     else
00131     {
00132         return not_found( get_name() );
00133     }
00134 
00135     for( size_t i = 0; i < num_entities; ++i )
00136     {
00137         if( entities[i] ) return not_root_set( get_name(), entities[i] );  // Not root set
00138         data_ptrs[i] = ptr;
00139         if( data_lengths ) data_lengths[i] = len;
00140     }
00141 
00142     return MB_SUCCESS;
00143 }
00144 
00145 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& range, const void**, int* ) const
00146 {
00147     if( range.empty() )
00148         return MB_SUCCESS;
00149     else
00150         return not_root_set( get_name(), range.front() );
00151 }
00152 
00153 ErrorCode MeshTag::set_data( SequenceManager*,
00154                              Error* /* error */,
00155                              const EntityHandle* entities,
00156                              size_t num_entities,
00157                              const void* data )
00158 {
00159     if( variable_length() )
00160     {
00161         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00162     }
00163     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00164 
00165     if( num_entities > 0 )
00166     {
00167         mValue.resize( get_size() );
00168         const unsigned char* bytes = reinterpret_cast< const unsigned char* >( data );
00169         memcpy( &mValue[0], bytes + get_size() * ( num_entities - 1 ), get_size() );
00170     }
00171 
00172     return MB_SUCCESS;
00173 }
00174 
00175 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, const void* )
00176 {
00177     if( variable_length() )
00178     {
00179         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00180     }
00181     else if( range.empty() )
00182         return MB_SUCCESS;
00183     else
00184         return not_root_set( get_name(), range.front() );
00185 }
00186 
00187 ErrorCode MeshTag::set_data( SequenceManager*,
00188                              Error* /* error */,
00189                              const EntityHandle* entities,
00190                              size_t num_entities,
00191                              void const* const* data_ptrs,
00192                              const int* data_lengths )
00193 {
00194     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00195 
00196     ErrorCode valid = validate_lengths( NULL, data_lengths, num_entities );MB_CHK_ERR( valid );
00197 
00198     if( num_entities > 0 )
00199     {
00200         mValue.resize( data_lengths[num_entities - 1] );
00201         memcpy( &mValue[0], data_ptrs[num_entities - 1], mValue.size() );
00202     }
00203 
00204     return MB_SUCCESS;
00205 }
00206 
00207 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, void const* const*, const int* )
00208 {
00209     if( range.empty() )
00210         return MB_SUCCESS;
00211     else
00212         return not_root_set( get_name(), range.front() );
00213 }
00214 
00215 ErrorCode MeshTag::clear_data( SequenceManager*,
00216                                Error* /* error */,
00217                                const EntityHandle* entities,
00218                                size_t num_entities,
00219                                const void* value_ptr,
00220                                int value_len )
00221 {
00222     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00223 
00224     ErrorCode valid = validate_lengths( NULL, value_len ? &value_len : 0, 1 );MB_CHK_ERR( valid );
00225 
00226     if( num_entities > 0 )
00227     {
00228         mValue.resize( value_len );
00229         memcpy( &mValue[0], value_ptr, value_len );
00230     }
00231 
00232     return MB_SUCCESS;
00233 }
00234 
00235 ErrorCode MeshTag::clear_data( SequenceManager*, Error* /* error */, const Range& range, const void*, int )
00236 {
00237     if( range.empty() )
00238         return MB_SUCCESS;
00239     else
00240         return not_root_set( get_name(), range.front() );
00241 }
00242 
00243 ErrorCode MeshTag::remove_data( SequenceManager*,
00244                                 Error* /* error */,
00245                                 const EntityHandle* entities,
00246                                 size_t num_entities )
00247 {
00248     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00249 
00250     if( num_entities ) mValue.clear();
00251 
00252     return MB_SUCCESS;
00253 }
00254 
00255 ErrorCode MeshTag::remove_data( SequenceManager*, Error* /* error */, const Range& range )
00256 {
00257     if( range.empty() )
00258         return MB_SUCCESS;
00259     else
00260         return not_root_set( get_name(), range.front() );
00261 }
00262 
00263 ErrorCode MeshTag::tag_iterate( SequenceManager*,
00264                                 Error* /* error */,
00265                                 Range::iterator& beg,
00266                                 const Range::iterator& end,
00267                                 void*&,
00268                                 bool )
00269 {
00270     if( beg == end )
00271         return MB_SUCCESS;
00272     else
00273         return not_root_set( get_name(), *beg );
00274 }
00275 
00276 ErrorCode MeshTag::get_tagged_entities( const SequenceManager*, Range&, EntityType, const Range* ) const
00277 {
00278     return MB_SUCCESS;
00279 }
00280 
00281 ErrorCode MeshTag::num_tagged_entities( const SequenceManager*, size_t&, EntityType, const Range* ) const
00282 {
00283     return MB_SUCCESS;
00284 }
00285 
00286 ErrorCode MeshTag::find_entities_with_value( const SequenceManager*,
00287                                              Error*,
00288                                              Range&,
00289                                              const void*,
00290                                              int,
00291                                              EntityType,
00292                                              const Range* ) const
00293 {
00294     return MB_SUCCESS;
00295 }
00296 
00297 bool MeshTag::is_tagged( const SequenceManager*, EntityHandle h ) const
00298 {
00299     return ( 0 == h ) && ( !mValue.empty() );
00300 }
00301 
00302 ErrorCode MeshTag::get_memory_use( const SequenceManager*, unsigned long& total, unsigned long& per_entity ) const
00303 {
00304     total      = TagInfo::get_memory_use() + sizeof( *this ) + mValue.size();
00305     per_entity = 0;
00306     return MB_SUCCESS;
00307 }
00308 
00309 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines