MOAB: Mesh Oriented datABase  (version 5.5.0)
MBiMesh.hpp
Go to the documentation of this file.
1 #ifndef MBIMESH_HPP
2 #define MBIMESH_HPP
3 
4 #include "moab/Core.hpp"
5 #include <vector>
6 #include <algorithm>
7 #include <cstring>
8 
9 using namespace moab;
10 
11 /* map from MOAB's ErrorCode to tstt's */
12 extern "C" const iBase_ErrorType iBase_ERROR_MAP[MB_FAILURE + 1];
13 
14 class MBiMesh
15 {
16  private:
19  std::vector< Tag > setHandleTags, entHandleTags;
20 
21  public:
22  MBiMesh( moab::Interface* mbImpl = NULL );
23 
24  virtual ~MBiMesh();
25  bool have_deleted_ents( bool reset )
26  {
27  bool result = haveDeletedEntities;
28  if( reset ) haveDeletedEntities = false;
29  return result;
30  }
31 
32  virtual ErrorCode delete_mesh();
33  virtual ErrorCode delete_entities( const EntityHandle*, const int );
34  virtual ErrorCode delete_entities( const Range& );
35  iBase_AdjacencyCost AdjTable[16];
38  char lastErrorDescription[120];
39 
40  inline void note_set_handle_tag( Tag );
41  inline void note_ent_handle_tag( Tag );
42  inline void note_tag_destroyed( Tag );
43  inline bool is_set_handle_tag( Tag ) const;
44  inline bool is_ent_handle_tag( Tag ) const;
45 
46  inline int set_last_error( int, const char* );
47  inline int set_last_error( ErrorCode, const char* );
48 };
49 
50 static inline MBiMesh* mbimeshi_instance( iMesh_Instance instance )
51 {
52  return reinterpret_cast< MBiMesh* >( instance );
53 }
54 #define MBIMESHI mbimeshi_instance( instance )
55 #define MOABI MBIMESHI->mbImpl
56 
57 inline MBiMesh::MBiMesh( Interface* impl )
58  : haveDeletedEntities( false ), iCreatedInterface( false ), mbImpl( impl ), lastErrorType( iBase_SUCCESS )
59 {
60  lastErrorDescription[0] = '\0';
61 
68  memcpy( AdjTable, tmp_table, 16 * sizeof( iBase_AdjacencyCost ) );
69 
70  if( !mbImpl )
71  {
72  mbImpl = new Core();
73  iCreatedInterface = true;
74  }
75 }
76 
78 {
79  if( iCreatedInterface ) delete mbImpl;
80 }
81 
83 {
84  haveDeletedEntities = true;
85  return mbImpl->delete_mesh();
86 }
87 
88 inline ErrorCode MBiMesh::delete_entities( const EntityHandle* a, const int n )
89 {
90  if( n > 0 ) haveDeletedEntities = true;
91  return mbImpl->delete_entities( a, n );
92 }
93 
95 {
96  if( !r.empty() ) haveDeletedEntities = true;
97  return mbImpl->delete_entities( r );
98 }
99 
101 {
102  std::vector< Tag >::iterator i;
103  i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
104  if( i != entHandleTags.end() && *i == t ) entHandleTags.erase( i );
105  i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
106  if( i == setHandleTags.end() || *i != t ) setHandleTags.insert( i, t );
107 }
108 
110 {
111  std::vector< Tag >::iterator i;
112  i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
113  if( i != setHandleTags.end() && *i == t ) setHandleTags.erase( i );
114  i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
115  if( i == entHandleTags.end() || *i != t ) entHandleTags.insert( i, t );
116 }
117 
119 {
120  std::vector< Tag >::iterator i;
121  i = std::lower_bound( setHandleTags.begin(), setHandleTags.end(), t );
122  if( i != setHandleTags.end() && *i == t ) setHandleTags.erase( i );
123  i = std::lower_bound( entHandleTags.begin(), entHandleTags.end(), t );
124  if( i != entHandleTags.end() && *i == t ) entHandleTags.erase( i );
125 }
126 
128 {
129  return std::binary_search( setHandleTags.begin(), setHandleTags.end(), t );
130 }
131 
133 {
134  return std::binary_search( entHandleTags.begin(), entHandleTags.end(), t );
135 }
136 
137 int MBiMesh::set_last_error( int code, const char* msg )
138 {
139  std::strncpy( lastErrorDescription, msg, sizeof( lastErrorDescription ) );
140  lastErrorDescription[sizeof( lastErrorDescription ) - 1] = '\0';
141  return ( lastErrorType = static_cast< iBase_ErrorType >( code ) );
142 }
143 
144 int MBiMesh::set_last_error( ErrorCode code, const char* msg )
145 {
146  std::string message( msg );
147  message += " (MOAB Error Code: ";
148  message += mbImpl->get_error_string( code );
149  message += ")";
150  return set_last_error( iBase_ERROR_MAP[code], message.c_str() );
151 }
152 
153 #endif