moab
|
00001 00017 #ifndef SPARSE_TAG_HPP 00018 #define SPARSE_TAG_HPP 00019 00020 #ifndef IS_BUILDING_MB 00021 #error "SparseTag.hpp isn't supposed to be included into an application" 00022 #endif 00023 00024 #ifdef WIN32 00025 #pragma warning(disable : 4786) 00026 #endif 00027 00028 00029 #define STRINGIFY_(X) #X 00030 #define STRINGIFY(X) STRINGIFY_(X) 00031 #ifdef HAVE_UNORDERED_MAP 00032 # include STRINGIFY(HAVE_UNORDERED_MAP) 00033 #else 00034 # include <map> 00035 #endif 00036 #include <vector> 00037 00038 #include "TagInfo.hpp" 00039 #include <stdlib.h> 00040 00041 namespace moab { 00042 00044 class SparseTagDataAllocator 00045 { 00046 public: 00048 SparseTagDataAllocator(){} 00050 ~SparseTagDataAllocator(){} 00052 void* allocate(size_t data_size) { return malloc(data_size); } 00054 void destroy(void* p){ free(p); } 00055 }; 00056 00057 00059 class SparseTag : public TagInfo 00060 { 00061 public: 00062 00063 SparseTag( const char* name, 00064 int size, 00065 DataType type, 00066 const void* default_value ); 00067 00068 ~SparseTag(); 00069 00070 virtual TagType get_storage_type() const; 00071 00085 virtual ErrorCode release_all_data( SequenceManager* seqman, 00086 Error* error_handler, 00087 bool delete_pending ); 00088 00089 00101 virtual 00102 ErrorCode get_data( const SequenceManager* seqman, 00103 Error* error_handler, 00104 const EntityHandle* entities, 00105 size_t num_entities, 00106 void* data ) const; 00107 00118 virtual 00119 ErrorCode get_data( const SequenceManager* seqman, 00120 Error* error_handler, 00121 const Range& entities, 00122 void* data ) const; 00123 00137 virtual 00138 ErrorCode get_data( const SequenceManager* seqman, 00139 Error* error_handler, 00140 const EntityHandle* entities, 00141 size_t num_entities, 00142 const void** data_ptrs, 00143 int* data_lengths ) const ; 00144 00145 00158 virtual 00159 ErrorCode get_data( const SequenceManager* seqman, 00160 Error* error_handler, 00161 const Range& entities, 00162 const void** data_ptrs, 00163 int* data_lengths ) const; 00164 00175 virtual 00176 ErrorCode set_data( SequenceManager* seqman, 00177 Error* error_handler, 00178 const EntityHandle* entities, 00179 size_t num_entities, 00180 const void* data ); 00181 00191 virtual 00192 ErrorCode set_data( SequenceManager* seqman, 00193 Error* error_handler, 00194 const Range& entities, 00195 const void* data ); 00196 00211 virtual 00212 ErrorCode set_data( SequenceManager* seqman, 00213 Error* error_handler, 00214 const EntityHandle* entities, 00215 size_t num_entities, 00216 void const* const* data_ptrs, 00217 const int* data_lengths ); 00218 00219 00233 virtual 00234 ErrorCode set_data( SequenceManager* seqman, 00235 Error* error_handler, 00236 const Range& entities, 00237 void const* const* data_ptrs, 00238 const int* data_lengths ); 00239 00253 virtual 00254 ErrorCode clear_data( SequenceManager* seqman, 00255 Error* error_handler, 00256 const EntityHandle* entities, 00257 size_t num_entities, 00258 const void* value_ptr, 00259 int value_len = 0 ); 00260 00273 virtual 00274 ErrorCode clear_data( SequenceManager* seqman, 00275 Error* error_handler, 00276 const Range& entities, 00277 const void* value_ptr, 00278 int value_len = 0 ); 00279 00288 virtual ErrorCode remove_data( SequenceManager* seqman, 00289 Error* error_handler, 00290 const EntityHandle* entities, 00291 size_t num_entities ); 00292 00300 virtual ErrorCode remove_data( SequenceManager* seqman, 00301 Error* error_handler, 00302 const Range& entities ); 00303 00327 virtual 00328 ErrorCode tag_iterate( SequenceManager* seqman, 00329 Error* error_handler, 00330 Range::iterator& iter, 00331 const Range::iterator& end, 00332 void*& data_ptr, 00333 bool allocate = true); 00334 00348 virtual 00349 ErrorCode get_tagged_entities( const SequenceManager* seqman, 00350 Range& output_entities, 00351 EntityType type = MBMAXTYPE, 00352 const Range* intersect = 0 ) const; 00353 00367 virtual 00368 ErrorCode num_tagged_entities( const SequenceManager* seqman, 00369 size_t& output_count, 00370 EntityType type = MBMAXTYPE, 00371 const Range* intersect = 0 ) const; 00372 00386 virtual 00387 ErrorCode find_entities_with_value( const SequenceManager* seqman, 00388 Error* error_handler, 00389 Range& output_entities, 00390 const void* value, 00391 int value_bytes = 0, 00392 EntityType type = MBMAXTYPE, 00393 const Range* intersect_entities = 0 ) const; 00394 00396 virtual bool is_tagged( const SequenceManager*, EntityHandle h ) const; 00397 00401 virtual 00402 ErrorCode get_memory_use( const SequenceManager* seqman, 00403 unsigned long& total, 00404 unsigned long& per_entity ) const; 00405 00407 unsigned long get_number_entities() 00408 { return mData.size(); } 00409 00410 00412 #ifdef HAVE_UNORDERED_MAP 00413 typedef UNORDERED_MAP_NS::unordered_map<EntityHandle,void*> MapType; 00414 #else 00415 typedef std::map<EntityHandle,void*> MapType; 00416 #endif 00417 00418 private: 00419 00420 SparseTag( const SparseTag& ); 00421 SparseTag& operator=( const SparseTag& ); 00422 00424 inline void *allocate_data(EntityHandle h, MapType::const_iterator iter, bool copy_default = true); 00425 00429 inline 00430 ErrorCode set_data(Error*, EntityHandle entity_handle, const void* data); 00431 00435 inline 00436 ErrorCode get_data(Error*, EntityHandle entity_handle, void* data) const; 00437 00439 inline 00440 ErrorCode get_data_ptr(EntityHandle entity_handle, const void*& data, bool allocate = true) const; 00441 00443 inline 00444 ErrorCode remove_data(Error*, EntityHandle entity_handle); 00445 00447 SparseTagDataAllocator mAllocator; 00448 00449 MapType mData; 00450 }; 00451 00452 inline void *SparseTag::allocate_data(EntityHandle h, MapType::const_iterator iter, bool copy_default) 00453 { 00454 void* new_data = mAllocator.allocate(get_size()); 00455 #ifdef HAVE_UNORDERED_MAP 00456 mData.insert(iter, std::pair<const EntityHandle,void*>(h, new_data)); 00457 #else 00458 mData[h] = new_data; 00459 #endif 00460 if (copy_default) 00461 memcpy(new_data, get_default_value(), get_size()); 00462 return new_data; 00463 } 00464 00465 } // namespace moab 00466 00467 #endif // SPARSE_TAG_HPP 00468 00469 00470 00471