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