00001 #include "ObjectArray.h" 00002 00003 #include "Object.h" 00004 #include "ObjectLink.h" 00005 00006 #include <stdio.h> 00007 00008 ObjectArray::ObjectArray () : Object() 00009 { 00010 this->_first = 0; 00011 } 00012 00013 ObjectArray::~ObjectArray() { 00014 ObjectLink *help = 0; 00015 00016 while(this->_first != NULL) { 00017 help = this->_first->GetNext(); 00018 delete this->_first; 00019 this->_first = help; 00020 } 00021 this->_first = 0; 00022 } 00023 00024 void ObjectArray::FreeArray() { 00025 ObjectLink *help; help = this->_first; 00026 Object *object = 0; 00027 00028 while(this->_first != NULL) { 00029 help = this->_first->GetNext(); 00030 object = this->_first->GetValue(); 00031 delete object; 00032 delete this->_first; 00033 this->_first = help; 00034 } 00035 this->_first = 0; 00036 } 00037 00038 class Object *ObjectArray::GetAt(int pAt) { 00039 ObjectLink *link; 00040 link= this->_first; 00041 while(link && (link->GetIndex() < pAt)) { 00042 link = link->GetNext(); 00043 } 00044 if(link && (pAt == link->GetIndex())) { 00045 return link->GetValue(); 00046 } else { 00047 return 0; 00048 } 00049 } 00050 00051 class Object * ObjectArray::PutAt (int pAt, class Object *pO, bool replace) { 00052 ObjectLink *prevLink; 00053 ObjectLink *curLink; 00054 prevLink = 0; 00055 curLink = this->_first; 00056 while(curLink && (curLink->GetIndex() < pAt)) { 00057 prevLink = curLink; 00058 curLink = curLink->GetNext(); 00059 } 00060 if(curLink && (pAt == curLink->GetIndex())) { 00061 if(replace) { 00062 Object *o; o = curLink->GetValue(); 00063 curLink->SetValue(pO); 00064 return o; 00065 } else { 00066 return 0; 00067 } 00068 } else { 00069 ObjectLink *newLink = new ObjectLink(pAt,pO); 00070 newLink->SetNext(curLink); 00071 if(this->_first == curLink) { 00072 this->_first = newLink; 00073 } 00074 if(prevLink) { 00075 prevLink->SetNext(newLink); 00076 } 00077 return 0; 00078 } 00079 } 00080 00081 bool ObjectArray::IsEmpty () { 00082 return (this->_first == 0); 00083 } 00084 00085 void ObjectArray::Print(int indent) { 00086 ObjectLink *curLink; 00087 00088 PrintIndent(indent); 00089 printf("ObjectArray:\n"); 00090 00091 if(this->IsEmpty()) { 00092 PrintIndent(indent+2); 00093 printf("I contain no objects\n"); 00094 } else { 00095 PrintIndent(indent+2); 00096 printf("I contain following objects:\n"); 00097 00098 curLink = this->_first; 00099 while (curLink) { 00100 curLink->GetValue()->Print(indent+2); 00101 curLink = curLink->GetNext(); 00102 } 00103 } 00104 00105 Object::Print(indent+2); 00106 }