45 lines
933 B
Plaintext
45 lines
933 B
Plaintext
#ifndef tools_realloc
|
|
#define tools_realloc
|
|
|
|
#include <cstring> //memcpy
|
|
|
|
namespace tools {
|
|
|
|
template <class T>
|
|
inline bool realloc(T*& a_pointer,size_t a_new_size,size_t a_old_size,bool a_init = false) {
|
|
if(!a_new_size) {
|
|
delete [] a_pointer;
|
|
a_pointer = 0;
|
|
return true;
|
|
}
|
|
if(!a_pointer) {
|
|
a_pointer = new T[a_new_size];
|
|
if(!a_pointer) return false;
|
|
return true;
|
|
}
|
|
if(a_old_size==a_new_size) return true;
|
|
T* pointer = new T[a_new_size];
|
|
if(!pointer) {
|
|
delete [] a_pointer;
|
|
a_pointer = 0;
|
|
return false;
|
|
}
|
|
if(a_new_size>a_old_size) {
|
|
::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
|
|
if(a_init){
|
|
size_t num = a_new_size-a_old_size;
|
|
T* pos = pointer+a_old_size;
|
|
for(size_t i=0;i<num;i++,pos++) *pos = T();
|
|
}
|
|
} else {
|
|
::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
|
|
}
|
|
delete [] a_pointer;
|
|
a_pointer = pointer;
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|