/////////////////////////////////////////////////////////////////////////////////////// // // array.cpp KSH 22 Oct 2001 // // CArray class member functions. // /////////////////////////////////////////////////////////////////////////////////////// #include DefinePoolableClass(Script::CArray); namespace Script { CArray::CArray() { // Initialise everything. CArray is not derived from CClass so we don't get // the auro-zeroing. m_type=ESYMBOLTYPE_NONE; mp_array_data=NULL; m_size=0; } CArray::~CArray() { Clear(); } bool CArray::operator==( const CArray& v ) const { // TODO ... #ifdef __NOPT_ASSERT__ printf("CArray comparisons are not supported yet ... implement when needed\n"); #endif return false; } bool CArray::operator!=( const CArray& v ) const { return !(*this==v); } // Deletes the array buffer if it exists, asserting if it contains any non-NULL pointers. // Sets type to NONE and size to 0. void CArray::Clear() { if (m_size==1) { // Memory optimization: // Special case for size 1. In this case, no memory block has been allocated. if (m_union) { // The element is not zero ... #ifdef __NOPT_ASSERT__ // Check that no references to things remain in the array. switch (m_type) { case ESYMBOLTYPE_INTEGER: case ESYMBOLTYPE_FLOAT: case ESYMBOLTYPE_NAME: // No need for the user to have zeroed these. break; case ESYMBOLTYPE_STRING: case ESYMBOLTYPE_LOCALSTRING: case ESYMBOLTYPE_PAIR: case ESYMBOLTYPE_VECTOR: case ESYMBOLTYPE_STRUCTURE: case ESYMBOLTYPE_ARRAY: { // The array contains a pointer to something. // The CArray cannot delete it itself because this would cause cyclic dependencies. Dbg_MsgAssert(0,("Tried to delete a CArray that still contains non-NULL data: size=%d type='%s'",m_size,GetTypeName(m_type))); break; } default: Dbg_MsgAssert(0,("Bad CArray::m_type of '%s'",GetTypeName(m_type))); break; } #endif m_union=0; } } else { if (mp_array_data) { #ifdef __NOPT_ASSERT__ // Check that no references to things remain in the array. switch (m_type) { case ESYMBOLTYPE_INTEGER: case ESYMBOLTYPE_FLOAT: case ESYMBOLTYPE_NAME: // No need for the user to have zeroed these. break; case ESYMBOLTYPE_STRING: case ESYMBOLTYPE_LOCALSTRING: case ESYMBOLTYPE_PAIR: case ESYMBOLTYPE_VECTOR: case ESYMBOLTYPE_STRUCTURE: case ESYMBOLTYPE_ARRAY: { // The array is of pointers, so make sure that the user of CArray has deleted and zeroed these before deleting the array. // The CArray cannot delete them itself because this would cause cyclic dependencies. for (uint32 i=0; im_size,("Tried to resize CArray to a smaller size, not supported yet ...")); // TODO: Make it able to make the CArray smaller, if a need arises. To do, factor out some of the // code from CleanUpArray so that the leftover bit can be cleaned up. Dbg_MsgAssert(newSize>1,("Resizing arrays to size 1 not supported yet ...")); // TODO: Support the above if need be. Need to not allocate a new buffer in that case. // Allocate the new buffer. Mem::Manager::sHandle().PushContext(Mem::Manager::sHandle().ScriptHeap()); uint32 *p_new_buffer=(uint32*)Mem::Malloc(newSize*sizeof(uint32)); Mem::Manager::sHandle().PopContext(); // Copy the contents of the old buffer into the new. uint32 *p_source=GetArrayPointer(); // Note: Does not support resizing zero size arrays because it does not know what the type of the // new array should be. Dbg_MsgAssert(p_source,("NULL array pointer ?")); uint32 *p_dest=p_new_buffer; uint32 i; for (i=0; i 1) { Mem::Free(mp_array_data); } mp_array_data=p_new_buffer; m_size=newSize; Dbg_MsgAssert(m_size>1,("Expected array size to be > 1 ??")); // Just to be sure } uint32 *CArray::GetArrayPointer() const { if (m_size==1) { return (uint32*)&m_union; } return mp_array_data; } void CArray::SetString(uint32 index, char *p_string) { Dbg_MsgAssert(m_type==ESYMBOLTYPE_STRING,("Called CArray::SetString when m_type was '%s'",GetTypeName(m_type))); Dbg_MsgAssert(index