annotate qt/core/QList.d @ 298:adae77fdc1ea signals

Native QList implementation is now used throughout QtD
author eldar
date Sun, 06 Dec 2009 17:26:37 +0000
parents bc783e20da2b
children 87db643519a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
1 module qt.core.QList;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
2
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
3 import qt.QGlobal;
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
4 import qt.QtdObject;
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
5 import qt.qtd.Atomic;
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
6 import qt.qtd.MetaMarshall;
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
7 import qt.core.QTypeInfo;
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
8 import qt.core.QString;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
9
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
10 import core.stdc.stdlib : qRealloc = realloc, qFree = free, qMalloc = malloc;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
11 import core.stdc.string : memcpy, memmove;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
12
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
13 import std.traits;
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
14
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
15 enum INT_MAX = int.max;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
16
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
17 bool isComplex(T)()
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
18 if (is(T.QTypeInfo))
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
19 {
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
20 return T.QTypeInfo.isComplex;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
21 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
22
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
23 bool isStatic(T)()
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
24 if (is(T.QTypeInfo))
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
25 {
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
26 return T.QTypeInfo.isStatic;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
27 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
28
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
29 bool isLarge(T)()
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
30 if (is(T.QTypeInfo))
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
31 {
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
32 return T.QTypeInfo.isLarge;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
33 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
34
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
35 template isQtReference(T)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
36 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
37 enum isQtReference = isQObjectType!T || isObjectType!T || isValueType!T || is(T == string);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
38 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
39
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
40 int qAllocMore(int alloc, int extra)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
41 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
42 if (alloc == 0 && extra == 0)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
43 return 0;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
44 const int page = 1 << 12;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
45 int nalloc;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
46 alloc += extra;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
47 if (alloc < 1<<6) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
48 nalloc = (1<<3) + ((alloc >>3) << 3);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
49 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
50 // don't do anything if the loop will overflow signed int.
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
51 if (alloc >= INT_MAX/2)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
52 return INT_MAX;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
53 nalloc = (alloc < page) ? 1 << 3 : page;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
54 while (nalloc < alloc) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
55 if (nalloc <= 0)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
56 return INT_MAX;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
57 nalloc *= 2;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
58 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
59 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
60 return nalloc - extra;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
61 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
62
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
63 void q_new_at(T)(T* ptr, const ref T t)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
64 {
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
65 memcpy(ptr, &t, T.sizeof);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
66 /* static if (__traits(compiles, ptr.__postblit())) DMD bug #3539
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
67 ptr.__postblit();*/
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
68 }
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
69
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
70 T* q_new(T)(const ref T t)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
71 {
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
72 T* ptr = cast(T*) qMalloc(T.sizeof);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
73 q_new_at!T(ptr, t);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
74 return ptr;
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
75 }
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
76
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
77 void q_delete(T)(T* t)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
78 {
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
79 static if (__traits(compiles, t.__dtor()))
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
80 t.__dtor();
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
81 qFree(t);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
82 }
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
83
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
84 private int grow(int size)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
85 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
86 // dear compiler: don't optimize me out.
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
87 // synchronized {
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
88 int x = qAllocMore(size * (void*).sizeof, QListData.DataHeaderSize) / (void*).sizeof;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
89 return x;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
90 // }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
91 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
92
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
93 struct QListData {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
94 struct Data {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
95 Atomic!int ref_;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
96 int alloc, begin, end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
97 uint sharable;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
98 void*[1] array;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
99 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
100
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
101 enum { DataHeaderSize = Data.sizeof - (void*).sizeof }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
102
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
103 static Data shared_null;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
104 Data *d;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
105
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
106 static this()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
107 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
108 shared_null = Data(Atomic!int(1), 0, 0, 0, true, [null]);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
109 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
110
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
111
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
112 // Data *detach(); // remove in 5.0
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
113
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
114 Data* detach2()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
115 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
116 Data* x = d;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
117 d = cast(Data*)(qMalloc(DataHeaderSize + x.alloc * (void*).sizeof));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
118 if (!d)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
119 qFatal("QList: Out of memory");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
120
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
121 memcpy(d, x, DataHeaderSize + x.alloc * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
122 d.alloc = x.alloc;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
123 d.ref_.store(1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
124 d.sharable = true;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
125 if (!d.alloc)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
126 d.begin = d.end = 0;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
127
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
128 return x;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
129 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
130
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
131 void realloc(int alloc)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
132 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
133 // assert(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
134 Data* x = cast(Data*)(qRealloc(d, DataHeaderSize + alloc * (void*).sizeof));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
135 if (!x)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
136 qFatal("QList: Out of memory");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
137
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
138 d = x;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
139 d.alloc = alloc;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
140 if (!alloc)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
141 d.begin = d.end = 0;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
142 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
143
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
144 void** append()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
145 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
146 // #TODO Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
147 if (d.end == d.alloc) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
148 int n = d.end - d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
149 if (d.begin > 2 * d.alloc / 3) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
150 memcpy(d.array.ptr + n, d.array.ptr + d.begin, n * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
151 d.begin = n;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
152 d.end = n * 2;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
153 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
154 realloc(grow(d.alloc + 1));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
155 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
156 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
157 return d.array.ptr + d.end++;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
158 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
159
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
160 void **append(const ref QListData l)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
161 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
162 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
163 int e = d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
164 int n = l.d.end - l.d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
165 if (n) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
166 if (e + n > d.alloc)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
167 realloc(grow(e + l.d.end - l.d.begin));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
168 memcpy(d.array.ptr + d.end, l.d.array.ptr + l.d.begin, n * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
169 d.end += n;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
170 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
171 return d.array.ptr + e;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
172 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
173
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
174 void **prepend()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
175 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
176 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
177 if (d.begin == 0) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
178 if (d.end >= d.alloc / 3)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
179 realloc(grow(d.alloc + 1));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
180
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
181 if (d.end < d.alloc / 3)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
182 d.begin = d.alloc - 2 * d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
183 else
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
184 d.begin = d.alloc - d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
185
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
186 memmove(d.array.ptr + d.begin, d.array.ptr, d.end * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
187 d.end += d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
188 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
189 return d.array.ptr + --d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
190 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
191
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
192 void **insert(int i)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
193 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
194 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
195 if (i <= 0)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
196 return prepend();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
197 if (i >= d.end - d.begin)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
198 return append();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
199
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
200 bool leftward = false;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
201 int size = d.end - d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
202
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
203 if (d.begin == 0) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
204 if (d.end == d.alloc) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
205 // If the array is full, we expand it and move some items rightward
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
206 realloc(grow(d.alloc + 1));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
207 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
208 // If there is free space at the end of the array, we move some items rightward
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
209 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
210 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
211 if (d.end == d.alloc) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
212 // If there is free space at the beginning of the array, we move some items leftward
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
213 leftward = true;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
214 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
215 // If there is free space at both ends, we move as few items as possible
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
216 leftward = (i < size - i);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
217 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
218 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
219
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
220 if (leftward) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
221 --d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
222 memmove(d.array.ptr + d.begin, d.array.ptr + d.begin + 1, i * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
223 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
224 memmove(d.array.ptr + d.begin + i + 1, d.array.ptr + d.begin + i,
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
225 (size - i) * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
226 ++d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
227 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
228 return d.array.ptr + d.begin + i;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
229 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
230
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
231 void remove(int i)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
232 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
233 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
234 i += d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
235 if (i - d.begin < d.end - i) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
236 if (int offset = i - d.begin)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
237 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
238 d.begin++;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
239 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
240 if (int offset = d.end - i - 1)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
241 memmove(d.array.ptr + i, d.array.ptr + i + 1, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
242 d.end--;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
243 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
244 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
245
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
246 void remove(int i, int n)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
247 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
248 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
249 i += d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
250 int middle = i + n/2;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
251 if (middle - d.begin < d.end - middle) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
252 memmove(d.array.ptr + d.begin + n, d.array.ptr + d.begin,
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
253 (i - d.begin) * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
254 d.begin += n;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
255 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
256 memmove(d.array.ptr + i, d.array.ptr + i + n,
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
257 (d.end - i - n) * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
258 d.end -= n;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
259 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
260 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
261
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
262 void move(int from, int to)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
263 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
264 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
265 if (from == to)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
266 return;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
267
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
268 from += d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
269 to += d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
270 void *t = d.array.ptr[from];
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
271
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
272 if (from < to) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
273 if (d.end == d.alloc || 3 * (to - from) < 2 * (d.end - d.begin)) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
274 memmove(d.array.ptr + from, d.array.ptr + from + 1, (to - from) * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
275 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
276 // optimization
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
277 if (int offset = from - d.begin)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
278 memmove(d.array.ptr + d.begin + 1, d.array.ptr + d.begin, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
279 if (int offset = d.end - (to + 1))
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
280 memmove(d.array.ptr + to + 2, d.array.ptr + to + 1, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
281 ++d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
282 ++d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
283 ++to;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
284 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
285 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
286 if (d.begin == 0 || 3 * (from - to) < 2 * (d.end - d.begin)) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
287 memmove(d.array.ptr + to + 1, d.array.ptr + to, (from - to) * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
288 } else {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
289 // optimization
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
290 if (int offset = to - d.begin)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
291 memmove(d.array.ptr + d.begin - 1, d.array.ptr + d.begin, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
292 if (int offset = d.end - (from + 1))
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
293 memmove(d.array.ptr + from, d.array.ptr + from + 1, offset * (void*).sizeof);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
294 --d.begin;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
295 --d.end;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
296 --to;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
297 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
298 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
299 d.array.ptr[to] = t;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
300 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
301
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
302 void **erase(void **xi)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
303 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
304 // Q_ASSERT(d.ref_ == 1);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
305 int i = xi - (d.array.ptr + d.begin);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
306 remove(i);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
307 return d.array.ptr + d.begin + i;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
308 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
309
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
310 int size() const { return d.end - d.begin; }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
311 bool isEmpty() const { return d.end == d.begin; }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
312 const (void*)* at(int i) const { return d.array.ptr + d.begin + i; }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
313 const (void*)* begin() const { return d.array.ptr + d.begin; }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
314 const (void*)* end() const { return d.array.ptr + d.end; }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
315 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
316
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
317 import std.stdio;
298
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
318 import std.conv;
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
319
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
320 alias void Dummy; // DMD bug #3538
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
321
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
322 struct QList(T, alias Default = Dummy)
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
323 {
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
324 static if (is(Default == Dummy))
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
325 alias QTypeInfo!T TI;
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
326 else
298
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
327 alias Default TI;
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
328
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
329 struct Node
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
330 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
331 void *v;
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
332
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
333 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string)) // binded Qt types
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
334 {
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
335 T t()
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
336 {
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
337 static if(is(T == string))
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
338 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
339 void* ptr = cast(void*)(TI.isLarge || TI.isStatic ? v : &this);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
340 return QString.toNativeString(ptr);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
341 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
342 else static if (isValueType!T)
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
343 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
344 void* ptr = cast(void*)(isLarge!T() || isStatic!T() ? v : &this);
8627891e4556 QList updates
eldar
parents: 292
diff changeset
345 return new T(ptr, QtdObjectFlags.nativeOwnership);
8627891e4556 QList updates
eldar
parents: 292
diff changeset
346 }
8627891e4556 QList updates
eldar
parents: 292
diff changeset
347 else
8627891e4556 QList updates
eldar
parents: 292
diff changeset
348 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
349 return T.__getObject( *cast(void**)(&this) );
8627891e4556 QList updates
eldar
parents: 292
diff changeset
350 }
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
351 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
352 }
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
353 else // native types
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
354 {
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
355 ref T t()
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
356 {
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
357 static if(TI.isLarge || TI.isStatic)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
358 return *cast(T*)(v);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
359 else
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
360 return *cast(T*)(&this);
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
361 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
362 }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
363 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
364
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
365 union {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
366 QListData p;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
367 QListData.Data* d;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
368 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
369
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
370 public:
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
371 void output()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
372 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
373 writeln("QList atomic ", d.ref_.load());
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
374 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
375
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
376 static QList!T opCall()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
377 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
378 QList!T res;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
379 writeln("QList opCall");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
380
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
381 res.d = &QListData.shared_null;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
382 res.d.ref_.increment();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
383
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
384 return res;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
385 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
386
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
387 this(this)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
388 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
389 writeln("QList postblit");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
390 d.ref_.increment();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
391 if (!d.sharable)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
392 detach_helper();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
393 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
394
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
395 ~this()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
396 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
397 writeln("QList ~this");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
398 if (d && !d.ref_.decrement())
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
399 free(d);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
400 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
401
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
402 ref QList!T opAssign(const ref QList!T l)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
403 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
404 writeln("QList opAssign");
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
405 if (d != l.d) {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
406 l.d.ref_.increment();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
407 if (!d.ref_.decrement())
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
408 free(d);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
409 d = cast(QListData.Data*)l.d;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
410 if (!d.sharable)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
411 detach_helper();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
412 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
413 return this;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
414 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
415
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
416 int length() const { return p.size(); }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
417 int size() const { return length; }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
418
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
419 void detach() { if (d.ref_.load() != 1) detach_helper(); }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
420
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
421 private void detach_helper()
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
422 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
423 Node *n = cast(Node*)(p.begin());
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
424 QListData.Data* x = p.detach2();
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
425 node_copy(cast(Node*)(p.begin()), cast(Node*)(p.end()), n);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
426 if (!x.ref_.decrement())
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
427 free(x);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
428 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
429
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
430
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
431 void append(const T t) // fix to const ref for complex types TODO
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
432 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
433 detach();
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
434 static if (isQObjectType!T || isObjectType!T || isValueType!T)
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
435 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
436 node_construct(cast(Node*)(p.append()), t);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
437 }
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
438 else
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
439 {
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
440 const T cpy = t;
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
441 node_construct(cast(Node*)(p.append()), cpy);
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
442 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
443 }
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
444
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
445 static if (isQObjectType!T || isObjectType!T || isValueType!T || is(T == string))
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
446 {
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
447 T at(int i) const
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
448 {
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
449 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
450 return (cast(Node*)(p.at(i))).t();
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
451 }
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
452 T opIndex(int i)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
453 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
454 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
455 return (cast(Node*)(p.at(i))).t();
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
456 }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
457 }
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
458 else
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
459 {
298
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
460 const (T) at(int i) const
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
461 {
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
462 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
463 return (cast(Node*)(p.at(i))).t();
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
464 }
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
465 ref T opIndex(int i)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
466 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
467 assert(i >= 0 && i < p.size(), "QList!T.at(): index out of range");
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
468 return (cast(Node*)(p.at(i))).t();
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
469 }
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
470 }
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
471
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
472 static if (isQObjectType!T || isObjectType!T || isValueType!T) //binded types
8627891e4556 QList updates
eldar
parents: 292
diff changeset
473 void node_construct(Node *n, const T t)
8627891e4556 QList updates
eldar
parents: 292
diff changeset
474 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
475 static if (isValueType!T)
8627891e4556 QList updates
eldar
parents: 292
diff changeset
476 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
477 if (isLarge!T() || isStatic!T()) // TODO should be static if
294
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
478 n.v = T.__constructNativeCopy(t.__nativeId); // n.v = new T(t);
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
479 else if (isComplex!T())
294
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
480 T.__constructPlacedNativeCopy(t.__nativeId, n); // new (n) T(t);
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
481 else
294
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
482 T.__constructPlacedNativeCopy(t.__nativeId, n); // TODO should be *cast(T*)(n) = cast(T)(t); as it is a primitive type. fix when they are implemented with structs
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
483 }
8627891e4556 QList updates
eldar
parents: 292
diff changeset
484 else // in case of QObject or Object Qt types we place a pointer to a native object in the node
8627891e4556 QList updates
eldar
parents: 292
diff changeset
485 n = cast(Node*) t.__nativeId;
8627891e4556 QList updates
eldar
parents: 292
diff changeset
486 }
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
487 else static if (is(T == string))
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
488 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
489 void node_construct(Node *n, T t)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
490 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
491 QString.__constructPlacedQString(n, t);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
492 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
493 }
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
494 else // native types
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
495 void node_construct(Node *n, const ref T t)
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
496 {
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
497 static if (TI.isLarge || TI.isStatic)
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
498 n.v = q_new!T(t); // n.v = new T(t);
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
499 else static if (TI.isComplex)
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
500 q_new_at(n, t); // new (n) T(t);
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
501 else
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
502 *cast(T*)(n) = cast(T)(t);
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
503 }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
504
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
505 void node_copy(Node *from, Node *to, Node *src)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
506 {
294
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
507 writeln("QList node_copy");
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
508 static if (isQObjectType!T || isObjectType!T)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
509 {} // ensure to do nothing. copying only a pointer
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
510 else static if(is(T == string))
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
511 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
512 while(from != to) // TODO when porting to Qt 5 ensure that QTypeInfo<QString>.isLarge and .isStatic == false
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
513 QString.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++));
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
514 }
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
515 else static if (isValueType!T)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
516 {
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
517 if (TI.isLarge || TI.isStatic) // TODO should be static if
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
518 while(from != to)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
519 (from++).v = T.__constructNativeCopy((src++).v); // (from++)->v = new T(*reinterpret_cast<T*>((src++)->v));
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
520 else if (TI.isComplex)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
521 while(from != to)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
522 T.__constructPlacedNativeCopy(src++, from++); // new (from++) T(*reinterpret_cast<T*>(src++));
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
523 }
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
524 else static if (TI.isLarge || TI.isStatic)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
525 while(from != to)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
526 (from++).v = q_new!T(*cast(T*)((src++).v));
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
527 else static if (TI.isComplex)
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
528 while(from != to)
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
529 q_new_at(from++, *cast(T*)(src++));
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
530 }
297
bc783e20da2b share strange dmd
eldar
parents: 296
diff changeset
531
298
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
532 T[] toArray()
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
533 {
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
534 T[] res;
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
535 res.length = this.length;
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
536 for(int i = 0; i < res.length; ++i)
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
537 {
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
538 static if (isValueType!T)
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
539 res[i] = new T(T.__constructNativeCopy(this.at(i).__nativeId)); // Node should probably provide a ptr method to directly extract pointer to the native value stored in the list to avoid creating a dummy D object in t()
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
540 else
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
541 res[i] = this.opIndex(i);
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
542 }
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
543 return res;
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
544 }
adae77fdc1ea Native QList implementation is now used throughout QtD
eldar
parents: 297
diff changeset
545
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
546 void free(QListData.Data* data)
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
547 {
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
548 writeln("QList data destroyed");
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
549 node_destruct(cast(Node*)(data.array.ptr + data.begin),
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
550 cast(Node*)(data.array.ptr + data.end));
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
551 if (data.ref_.load() == 0)
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
552 qFree(data);
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
553 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
554
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
555 void node_destruct(Node *from, Node *to)
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
556 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
557 static if (isQObjectType!T || isObjectType!T) //binded types
8627891e4556 QList updates
eldar
parents: 292
diff changeset
558 {} // removing just pointers, do nothing
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
559 else static if (is(T == string))
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
560 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
561 while (from != to)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
562 --to, QString.__callNativeDestructor(to);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
563 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
564 else static if (isValueType!T) //binded value types
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
565 {
8627891e4556 QList updates
eldar
parents: 292
diff changeset
566 if (isLarge!T() || isStatic!T()) // TODO should be static if
8627891e4556 QList updates
eldar
parents: 292
diff changeset
567 while (from != to)
294
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
568 --to, T.__deleteNativeObject(to.v);
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
569 else if (isComplex!T())
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
570 while (from != to)
bb37b0ed94c9 some more QList
eldar
parents: 293
diff changeset
571 --to, T.__callNativeDestructor(to);
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
572 }
8627891e4556 QList updates
eldar
parents: 292
diff changeset
573 else
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
574 {
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
575 static if (TI.isLarge || TI.isStatic)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
576 while (from != to) --to, q_delete(cast(T*)(to.v));
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
577 else static if (TI.isComplex)
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
578 while (from != to) --to, cast(T*)(to).__dtor();
293
8627891e4556 QList updates
eldar
parents: 292
diff changeset
579 }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
580 }
296
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
581
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
582 //iteration support
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
583 int opApply(int delegate(ref T) dg)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
584 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
585 int result = 0;
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
586 int sz = this.length;
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
587 for (int i = 0; i < sz; i++)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
588 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
589 static if (isQtReference!T)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
590 {
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
591 T t = this[i]; // hack to avoid "is not an lvalue" error, since dg accepts ref T
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
592 result = dg(t);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
593 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
594 else
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
595 result = dg(this[i]);
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
596
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
597 if (result)
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
598 break;
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
599 }
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
600 return result;
5173835bb372 iteration over QList with opApply
eldar
parents: 295
diff changeset
601 }
291
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
602 }
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
603
0d2094800bdb QList native implementation
eldar
parents:
diff changeset
604 extern(C) void qtd_create_QList(void *nativeId);
295
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
605 extern(C) void qtd_create_QList_double(void *nativeId);
463563fc9e17 more of QList. const functions in C++ are now const in D too. Drop of the store result feature, which was incompatible with const functions and introduced too much of the bloat in the generator.
eldar
parents: 294
diff changeset
606
292
19498f420252 more QList goodness
eldar
parents: 291
diff changeset
607 extern(C) void qtd_create_QList_QObject(void *nativeId);