MOAB: Mesh Oriented datABase  (version 5.5.0)
ArrayManager.hpp
Go to the documentation of this file.
1 #ifndef ARRAYMANAGER_HPP
2 #define ARRAYMANAGER_HPP
3 
4 #include <cstdlib>
5 
6 // Check the array size, and allocate the array if necessary.
7 // Free the array upon leaving scope unless KEEP_ARRAY
8 // is invoked.
9 #define ALLOC_CHECK_ARRAY( array, this_size ) \
10  ArrayManager array##_manager( reinterpret_cast< void** >( array ), *( array##_allocated ), *( array##_size ), \
11  this_size, sizeof( **( array ) ), err ); \
12  if( iBase_SUCCESS != *err ) return
13 
14 #define ALLOC_CHECK_TAG_ARRAY( array, this_size ) \
15  ArrayManager array##_manager( reinterpret_cast< void** >( array ), *( array##_allocated ), *( array##_size ), \
16  this_size, 1, err ); \
17  if( iBase_SUCCESS != *err ) return
18 
19 #define KEEP_ARRAY( array ) array##_manager.keep_array()
20 
21 // Check the array size, and allocate the array if necessary.
22 // Do NOT free the array upon leaving scope.
23 #define ALLOC_CHECK_ARRAY_NOFAIL( array, this_size ) \
24  ALLOC_CHECK_ARRAY( array, this_size ); \
25  KEEP_ARRAY( array )
26 
27 // Implement RAII pattern for allocated arrays, stolen from iMesh
29 {
30  void** arrayPtr;
31 
32  public:
33  ArrayManager( void** array_ptr, int& array_allocated_space, int& array_size, int count, int val_size, int* err )
34  : arrayPtr( 0 )
35  {
36  if( !*array_ptr || !array_allocated_space )
37  {
38  *array_ptr = std::malloc( val_size * count );
39  array_allocated_space = array_size = count;
40  if( !*array_ptr )
41  {
43  return;
44  }
45  arrayPtr = array_ptr;
46  }
47  else
48  {
49  array_size = count;
50  if( array_allocated_space < count )
51  {
53  return;
54  }
55  }
56 
57  *err = iBase_SUCCESS;
58  }
59 
61  {
62  if( arrayPtr )
63  {
64  std::free( *arrayPtr );
65  *arrayPtr = 0;
66  }
67  }
68 
69  void keep_array()
70  {
71  arrayPtr = 0;
72  }
73 };
74 
75 #endif