3 import "BuiltInContainer"
7 static __attribute__((unused)) void UnusedFunction()
12 a.OnGetString(null, null, null);
14 a.OnUnserialize(null);
17 extern int __ecereVMethodID_class_OnCompare;
18 extern int __ecereVMethodID_class_OnGetString;
19 extern int __ecereVMethodID_class_OnSerialize;
20 extern int __ecereVMethodID_class_OnUnserialize;
23 // CAUSES PROBLEM WHEN AFTER
24 public struct Iterator<class T, class IT = int>
26 Container<T, IT> container;
28 IteratorPointer pointer;
33 get { return container.GetData(pointer); }
34 set { container.SetData(pointer, value); }
39 if(pointer && container)
40 pointer = container.GetPrev(pointer);
42 pointer = container.GetLast();
43 return pointer != null;
48 if(pointer && container)
49 pointer = container.GetNext(pointer);
51 pointer = container.GetFirst();
52 return pointer != null;
57 return container.GetData(pointer);
62 return container.SetData(pointer, value);
65 bool Find(const T value)
70 pointer = container.Find(value);
72 return pointer != null;
78 container.Remove(pointer);
85 container.FreeIterator(pointer);
88 bool Index(const IT index, bool create)
92 bool justAdded = false;
94 pointer = container.GetAtPosition(index, create, &justAdded);
95 return !justAdded && pointer != null;
101 public class Container<class T, class I = int, class D = T>
105 public property Container<T> copySrc { set { if(value) Copy(value); } }
106 property Iterator<T> firstIterator { get { value = { (Container<T>)this, pointer = GetFirst() }; } }
107 property Iterator<T> lastIterator { get { value = { (Container<T>)this, pointer = GetLast() }; } }
109 virtual IteratorPointer GetFirst() { return null; }
110 virtual IteratorPointer GetLast() { return null; }
111 virtual IteratorPointer GetPrev(IteratorPointer pointer) { return null; }
112 virtual IteratorPointer GetNext(IteratorPointer pointer) { return null; }
113 virtual D GetData(IteratorPointer pointer) { return (D)0; }
114 virtual bool SetData(IteratorPointer pointer, D data);
115 virtual IteratorPointer GetAtPosition(const I pos, bool create, bool * justAdded) { return null; }
117 virtual IteratorPointer Insert(IteratorPointer after, T value);
118 virtual IteratorPointer Add(T value);
119 virtual void Remove(IteratorPointer it);
120 virtual void Move(IteratorPointer it, IteratorPointer after);
122 virtual void RemoveAll()
124 IteratorPointer i, next;
125 for(i = GetFirst(), next = i ? GetNext(i) : null; i; i = next, next = i ? GetNext(i) : null)
129 virtual void Copy(Container<T> source)
133 for(i = source.GetFirst(); i; i = source.GetNext(i))
135 D data = source.GetData(i);
136 // WARNING: This doesn't make a new copy of the elements it adds
150 int OnCompare(Container<T> b)
152 IteratorPointer ia, ib;
153 Class Dclass = class(D);
154 bool byRef = (Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass;
155 int (* onCompare)(void *, const void *, const void *) = (void *)Dclass._vTbl[__ecereVMethodID_class_OnCompare];
157 if(this && !b) return 1;
158 if(b && !this) return -1;
159 if(GetCount() > b.GetCount()) return 1;
160 if(GetCount() < b.GetCount()) return -1;
166 D dataA = GetData(ia);
167 D dataB = b.GetData(ib);
168 int r = onCompare(Dclass, byRef ? &dataA : (const void *)(uintptr)dataA, byRef ? &dataB : (const void *)(uintptr)dataB);
178 void OnCopy(Container<T> source)
182 // BUG IN this = SYNTAX
183 Container<T> container = eInstance_New(source._class);
184 // See WARNING in Copy()
185 container.Copy(source);
186 //*(void **)this = container;
195 virtual IteratorPointer Find(const D value)
198 Class Dclass = class(D);
199 bool byRef = (Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass;
200 int (* onCompare)(void *, const void *, const void *) = (void *)Dclass._vTbl[__ecereVMethodID_class_OnCompare];
204 for(i = GetFirst(); i; i = GetNext(i))
207 int result = onCompare(Dclass, &value, &data);
214 for(i = GetFirst(); i; i = GetNext(i))
217 int result = onCompare(Dclass, (const void *)(uintptr) value, (const void *)(uintptr) data);
225 virtual void FreeIterator(IteratorPointer it);
227 virtual int GetCount()
231 for(i = GetFirst(); i; i = GetNext(i)) count++;
238 while((i = GetFirst()))
242 virtual void Delete(IteratorPointer i)
249 const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
253 char itemString[4096];//1024];
256 tempString[0] = '\0';
257 for(i = GetFirst(); i; i = GetNext(i))
259 Class Dclass = class(D);
263 itemString[0] = '\0';
265 result = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnGetString])(Dclass,
266 ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, itemString, null, null);
267 if(!first) strcat(tempString, ", ");
269 strcat(tempString, result);
278 void TakeOut(const D d)
280 IteratorPointer i = Find(d);
289 void OnSerialize(IOChannel channel)
291 uint count = GetCount();
293 Class Dclass = class(D);
294 bool isNormalClass = (Dclass.type == normalClass) && Dclass.structSize;
297 for(i = GetFirst(); i; i = GetNext(i))
300 Class Eclass = isNormalClass ? ((Instance)data)._class : Dclass;
301 ((void (*)(void *, void *, void *))(void *)Eclass._vTbl[__ecereVMethodID_class_OnSerialize])(Eclass,
302 ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data, channel);
306 void OnUnserialize(IOChannel channel)
308 Container container = eInstance_New(_class.fullName);
310 Class Dclass = class(D);
312 bool isStruct = Dclass.type == structClass;
316 data = (D)(new byte[Dclass.structSize]);
317 for(c = 0; c < count; c++)
320 memset((char *)data, 0, Dclass.structSize);
323 ((void (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnUnserialize])
324 (Dclass, isStruct ? (void *)data : &data, channel);