MOAB: Mesh Oriented datABase  (version 5.2.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*, Error* /* error */, const EntityHandle* entities,
00069                              size_t num_entities, void* data ) const
00070 {
00071     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00072 
00073     const void* ptr;
00074     int len;
00075 
00076     if( !mValue.empty() )
00077     {
00078         ptr = &mValue[0];
00079         len = mValue.size();
00080     }
00081     else if( get_default_value() )
00082     {
00083         ptr = get_default_value();
00084         len = get_default_value_size();
00085     }
00086     else
00087     {
00088         return not_found( get_name() );
00089     }
00090 
00091     SysUtil::setmem( data, ptr, len, num_entities );
00092     return MB_SUCCESS;
00093 }
00094 
00095 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& r, void* ) const
00096 {
00097     if( variable_length() )
00098     {
00099         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00100     }
00101     else if( r.empty() )
00102         return MB_SUCCESS;
00103     else
00104         return not_root_set( get_name(), r.front() );
00105 }
00106 
00107 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const EntityHandle* entities,
00108                              size_t num_entities, const void** data_ptrs, int* data_lengths ) const
00109 {
00110     const void* ptr;
00111     int len;
00112 
00113     if( !mValue.empty() )
00114     {
00115         ptr = &mValue[0];
00116         len = mValue.size();
00117     }
00118     else if( get_default_value() )
00119     {
00120         ptr = get_default_value();
00121         len = get_default_value_size();
00122     }
00123     else
00124     {
00125         return not_found( get_name() );
00126     }
00127 
00128     for( size_t i = 0; i < num_entities; ++i )
00129     {
00130         if( entities[i] ) return not_root_set( get_name(), entities[i] );  // Not root set
00131         data_ptrs[i] = ptr;
00132         if( data_lengths ) data_lengths[i] = len;
00133     }
00134 
00135     return MB_SUCCESS;
00136 }
00137 
00138 ErrorCode MeshTag::get_data( const SequenceManager*, Error* /* error */, const Range& range, const void**, int* ) const
00139 {
00140     if( range.empty() )
00141         return MB_SUCCESS;
00142     else
00143         return not_root_set( get_name(), range.front() );
00144 }
00145 
00146 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const EntityHandle* entities, size_t num_entities,
00147                              const void* data )
00148 {
00149     if( variable_length() )
00150     {
00151         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00152     }
00153     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00154 
00155     if( num_entities > 0 )
00156     {
00157         mValue.resize( get_size() );
00158         const unsigned char* bytes = reinterpret_cast< const unsigned char* >( data );
00159         memcpy( &mValue[0], bytes + get_size() * ( num_entities - 1 ), get_size() );
00160     }
00161 
00162     return MB_SUCCESS;
00163 }
00164 
00165 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, const void* )
00166 {
00167     if( variable_length() )
00168     {
00169         MB_SET_ERR( MB_VARIABLE_DATA_LENGTH, "No length specified for variable-length tag " << get_name() << " value" );
00170     }
00171     else if( range.empty() )
00172         return MB_SUCCESS;
00173     else
00174         return not_root_set( get_name(), range.front() );
00175 }
00176 
00177 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const EntityHandle* entities, size_t num_entities,
00178                              void const* const* data_ptrs, const int* data_lengths )
00179 {
00180     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00181 
00182     ErrorCode valid = validate_lengths( NULL, data_lengths, num_entities );MB_CHK_ERR( valid );
00183 
00184     if( num_entities > 0 )
00185     {
00186         mValue.resize( data_lengths[num_entities - 1] );
00187         memcpy( &mValue[0], data_ptrs[num_entities - 1], mValue.size() );
00188     }
00189 
00190     return MB_SUCCESS;
00191 }
00192 
00193 ErrorCode MeshTag::set_data( SequenceManager*, Error* /* error */, const Range& range, void const* const*, const int* )
00194 {
00195     if( range.empty() )
00196         return MB_SUCCESS;
00197     else
00198         return not_root_set( get_name(), range.front() );
00199 }
00200 
00201 ErrorCode MeshTag::clear_data( SequenceManager*, Error* /* error */, const EntityHandle* entities, size_t num_entities,
00202                                const void* value_ptr, int value_len )
00203 {
00204     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00205 
00206     ErrorCode valid = validate_lengths( NULL, value_len ? &value_len : 0, 1 );MB_CHK_ERR( valid );
00207 
00208     if( num_entities > 0 )
00209     {
00210         mValue.resize( value_len );
00211         memcpy( &mValue[0], value_ptr, value_len );
00212     }
00213 
00214     return MB_SUCCESS;
00215 }
00216 
00217 ErrorCode MeshTag::clear_data( SequenceManager*, Error* /* error */, const Range& range, const void*, int )
00218 {
00219     if( range.empty() )
00220         return MB_SUCCESS;
00221     else
00222         return not_root_set( get_name(), range.front() );
00223 }
00224 
00225 ErrorCode MeshTag::remove_data( SequenceManager*, Error* /* error */, const EntityHandle* entities,
00226                                 size_t num_entities )
00227 {
00228     if( !all_root_set( get_name(), entities, num_entities ) ) return MB_TAG_NOT_FOUND;
00229 
00230     if( num_entities ) mValue.clear();
00231 
00232     return MB_SUCCESS;
00233 }
00234 
00235 ErrorCode MeshTag::remove_data( SequenceManager*, Error* /* error */, const Range& range )
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::tag_iterate( SequenceManager*, Error* /* error */, Range::iterator& beg, const Range::iterator& end,
00244                                 void*&, bool )
00245 {
00246     if( beg == end )
00247         return MB_SUCCESS;
00248     else
00249         return not_root_set( get_name(), *beg );
00250 }
00251 
00252 ErrorCode MeshTag::get_tagged_entities( const SequenceManager*, Range&, EntityType, const Range* ) const
00253 {
00254     return MB_SUCCESS;
00255 }
00256 
00257 ErrorCode MeshTag::num_tagged_entities( const SequenceManager*, size_t&, EntityType, const Range* ) const
00258 {
00259     return MB_SUCCESS;
00260 }
00261 
00262 ErrorCode MeshTag::find_entities_with_value( const SequenceManager*, Error*, Range&, const void*, int, EntityType,
00263                                              const Range* ) const
00264 {
00265     return MB_SUCCESS;
00266 }
00267 
00268 bool MeshTag::is_tagged( const SequenceManager*, EntityHandle h ) const
00269 {
00270     return ( 0 == h ) && ( !mValue.empty() );
00271 }
00272 
00273 ErrorCode MeshTag::get_memory_use( const SequenceManager*, unsigned long& total, unsigned long& per_entity ) const
00274 {
00275     total      = TagInfo::get_memory_use() + sizeof( *this ) + mValue.size();
00276     per_entity = 0;
00277     return MB_SUCCESS;
00278 }
00279 
00280 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines