MOAB: Mesh Oriented datABase  (version 5.4.1)
ArrayManager.hpp
Go to the documentation of this file.
00001 #ifndef ARRAYMANAGER_HPP
00002 #define ARRAYMANAGER_HPP
00003 
00004 #include <cstdlib>
00005 
00006 // Check the array size, and allocate the array if necessary.
00007 // Free the array upon leaving scope unless KEEP_ARRAY
00008 // is invoked.
00009 #define ALLOC_CHECK_ARRAY( array, this_size )                                                                     \
00010     ArrayManager array##_manager( reinterpret_cast< void** >( array ), *( array##_allocated ), *( array##_size ), \
00011                                   this_size, sizeof( **( array ) ), err );                                        \
00012     if( iBase_SUCCESS != *err ) return
00013 
00014 #define ALLOC_CHECK_TAG_ARRAY( array, this_size )                                                                 \
00015     ArrayManager array##_manager( reinterpret_cast< void** >( array ), *( array##_allocated ), *( array##_size ), \
00016                                   this_size, 1, err );                                                            \
00017     if( iBase_SUCCESS != *err ) return
00018 
00019 #define KEEP_ARRAY( array ) array##_manager.keep_array()
00020 
00021 // Check the array size, and allocate the array if necessary.
00022 // Do NOT free the array upon leaving scope.
00023 #define ALLOC_CHECK_ARRAY_NOFAIL( array, this_size ) \
00024     ALLOC_CHECK_ARRAY( array, this_size );           \
00025     KEEP_ARRAY( array )
00026 
00027 // Implement RAII pattern for allocated arrays, stolen from iMesh
00028 class ArrayManager
00029 {
00030     void** arrayPtr;
00031 
00032   public:
00033     ArrayManager( void** array_ptr, int& array_allocated_space, int& array_size, int count, int val_size, int* err )
00034         : arrayPtr( 0 )
00035     {
00036         if( !*array_ptr || !array_allocated_space )
00037         {
00038             *array_ptr            = std::malloc( val_size * count );
00039             array_allocated_space = array_size = count;
00040             if( !*array_ptr )
00041             {
00042                 *err = iBase_MEMORY_ALLOCATION_FAILED;
00043                 return;
00044             }
00045             arrayPtr = array_ptr;
00046         }
00047         else
00048         {
00049             array_size = count;
00050             if( array_allocated_space < count )
00051             {
00052                 *err = iBase_BAD_ARRAY_DIMENSION;
00053                 return;
00054             }
00055         }
00056 
00057         *err = iBase_SUCCESS;
00058     }
00059 
00060     ~ArrayManager()
00061     {
00062         if( arrayPtr )
00063         {
00064             std::free( *arrayPtr );
00065             *arrayPtr = 0;
00066         }
00067     }
00068 
00069     void keep_array()
00070     {
00071         arrayPtr = 0;
00072     }
00073 };
00074 
00075 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines