inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/realloc
Go to the documentation of this file.
00001 #ifndef inlib_realloc
00002 #define inlib_realloc
00003 
00004 #include "typedefs"
00005 
00006 #include <cstring> //memcpy
00007 
00008 namespace inlib {
00009 
00010 template <class T>
00011 inline bool realloc(T*& a_pointer,uint32 a_new_size,uint32 a_old_size,bool a_init = false) {
00012   if(!a_new_size) { 
00013     delete [] a_pointer; 
00014     a_pointer = 0;
00015     return true;
00016   }
00017   if(!a_pointer) {
00018     a_pointer = new T[a_new_size];
00019     return true;
00020   }
00021   if(a_old_size==a_new_size) return true;
00022   T* pointer = new T[a_new_size];
00023   if(!pointer) {
00024     delete [] a_pointer; 
00025     a_pointer = 0;
00026     return false;
00027   }
00028   if(a_new_size>a_old_size) {
00029     ::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
00030     if(a_init){
00031       uint32 num = a_new_size-a_old_size;   
00032       T* pos = pointer+a_old_size;
00033       for(uint32 i=0;i<num;i++,pos++) *pos = T();
00034     }
00035   } else {
00036     ::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
00037   }
00038   delete [] a_pointer;
00039   a_pointer = pointer;
00040   return true;
00041 }
00042 
00043 }
00044 
00045 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines