sdk: const correctness
[sdk] / ecere / src / com / containers / BuiltInContainer.ec
1 namespace com;
2
3 import "instance"
4 import "Container"
5
6 public class IteratorPointer : struct;
7
8 default:
9 extern int __ecereVMethodID_class_OnCompare;
10 extern int __ecereVMethodID_class_OnFree;
11 extern int __ecereVMethodID_class_OnGetString;
12 private:
13
14 public struct BuiltInContainer
15 {
16 public:
17    void * _vTbl;
18    Class _class;
19    int _refCount;
20 public:
21    void * data;
22    int count;
23    Class type;
24
25    property Container { get { return this; } }
26
27    virtual IteratorPointer GetFirst() { return data; }
28    virtual IteratorPointer GetLast()  { return (IteratorPointer)(data ? ((byte *)data + (count * type.typeSize) - 1) : null); }
29    virtual IteratorPointer GetPrev(IteratorPointer pointer)
30    {
31       return (IteratorPointer)((pointer && (byte *)pointer > (byte *)data) ?
32          ((byte *)pointer - ((type.type == noHeadClass || type.type == normalClass) ? sizeof(void *) : type.typeSize)) : null);
33    }
34    virtual IteratorPointer GetNext(IteratorPointer pointer)
35    {
36       return (IteratorPointer)((pointer && (byte *)pointer < (byte *)data + (count - 1) *
37          ((type.type == noHeadClass || type.type == normalClass) ? sizeof(void *) : type.typeSize)) ?
38          ((byte *)pointer + ((type.type == noHeadClass || type.type == normalClass) ? sizeof(void *) : type.typeSize)) : null);
39    }
40    virtual uint64 GetData(IteratorPointer pointer)
41    {
42       uint64 * item = (uint64 *)pointer;
43       return ((((type.type == structClass) ? ((uint64)item) :
44          (type.type == normalClass || type.type == noHeadClass) ? (uint64)*((void **)item) :
45             ((type.typeSize == 1) ? *((unsigned char *)item) :
46                ((type.typeSize == 2) ? *((unsigned short *)item) :
47                   ((type.typeSize == 4) ? *((unsigned int *)item) : *((uint64 *)item)))))));
48    }
49    virtual bool SetData(IteratorPointer pointer, uint64 data)
50    {
51       return false;
52    }
53    virtual IteratorPointer GetAtPosition(const uint64 pos, bool create)
54    {
55       return data ? (IteratorPointer)((byte *)data +
56          ((type.type == noHeadClass || type.type == normalClass) ? sizeof(void *) : type.typeSize)) : null;
57    }
58    virtual IteratorPointer Insert(IteratorPointer after, uint64 value)
59    {
60       return null;
61    }
62    virtual IteratorPointer Add(uint64 value)
63    {
64       return null;
65    }
66    virtual void Remove(IteratorPointer it)
67    {
68    }
69    virtual void Move(IteratorPointer it, IteratorPointer after)
70    {
71    }
72    virtual void RemoveAll()
73    {
74       IteratorPointer i;
75       for(i = GetFirst(); i; i = GetNext(i))
76          Remove(i);
77    }
78
79    virtual void Copy(Container source)
80    {
81    }
82
83    virtual IteratorPointer Find(uint64 value)
84    {
85       IteratorPointer i;
86       for(i = GetFirst(); i; i = GetNext(i))
87       {
88          uint64 data = GetData(i);
89          Class Dclass = type;
90          int result = ((int (*)(void *, void *, void *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnCompare])(Dclass,
91             ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &value : (void *)value,
92             ((Dclass.type == systemClass && !Dclass.byValueSystemClass) || Dclass.type == bitClass || Dclass.type == enumClass || Dclass.type == unitClass) ? &data : (void *)data);
93          if(!result)
94             return i;
95       }
96       return null;
97    }
98
99    virtual void FreeIterator(IteratorPointer it);
100    virtual int GetCount() { return count; }
101
102    virtual void Free()
103    {
104       IteratorPointer i;
105       for(i = GetFirst(); i; i = GetNext(i))
106          ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, (void *)GetData(i));
107    }
108
109    virtual void Delete(IteratorPointer it) { }
110
111    const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
112    {
113       if(this)
114       {
115          Class Dclass = type;
116          char itemString[4096];
117          bool first = true;
118          byte * data = this.data;
119          int i;
120          tempString[0] = '\0';
121          for(i = 0; i < count; i++)
122          {
123             const char * result;
124             itemString[0] = '\0';
125             result = ((const char *(*)(void *, void *, char *, void *, bool *))(void *)Dclass._vTbl[__ecereVMethodID_class_OnGetString])(
126                Dclass, (type.type == normalClass || type.type == noHeadClass) ? *(void **)data : data, itemString, null, null);
127             if(!first) strcat(tempString, ", ");
128             strcat(tempString, result);
129             first = false;
130             data += Dclass.typeSize;
131          }
132       }
133       else
134          tempString[0] = 0;
135       return tempString;
136    }
137 };