Mercurial > projects > qtd
annotate qt/QGlobal.d @ 191:a212c97edcd2
one more example
author | eldar |
---|---|
date | Sun, 12 Jul 2009 21:03:57 +0000 |
parents | 7dd099050621 |
children | ba7abb5c9e03 |
rev | line source |
---|---|
1 | 1 module qt.QGlobal; |
2 | |
3 public import qt.qtd.Str; | |
4 | |
5 version (Tango) | |
6 { | |
7 import tango.io.Stdout; | |
8 void writeln(string s) | |
9 { | |
10 Stdout(s).newline; | |
11 } | |
188 | 12 package import tango.stdc.stdlib, |
13 tango.core.Memory; | |
1 | 14 } |
15 else | |
188 | 16 { |
1 | 17 import std.stdio; |
188 | 18 package import std.c.stdlib, |
19 core.memory; | |
20 } | |
1 | 21 |
22 template QT_BEGIN_NAMESPACE() { | |
23 } | |
24 | |
25 template QT_END_NAMESPACE() { | |
26 } | |
27 | |
28 template QT_BEGIN_HEADER() { | |
29 } | |
30 | |
31 template QT_END_HEADER() { | |
32 } | |
33 | |
34 mixin QT_BEGIN_HEADER; | |
35 mixin QT_BEGIN_NAMESPACE; | |
36 | |
125 | 37 extern(C) void __qtd_dummy() {} |
38 | |
39 version(cpp_shared) | |
1 | 40 { |
41 extern (C) void __qtd_qtd_core_initCallBacks(void* toUtf8, void* dummy); | |
42 static this() { | |
125 | 43 __qtd_qtd_core_initCallBacks(&_d_toUtf8, &__qtd_dummy); |
1 | 44 } |
45 } | |
46 | |
191 | 47 string tr(string arg) { |
1 | 48 return arg; |
49 } | |
50 | |
51 // TODO(katrina) get this from the C++ side | |
52 const char[] QT_VERSION_STR = "4.5.0"; | |
53 /* | |
54 QT_VERSION is (major << 16) + (minor << 8) + patch. | |
55 //TODO(katrina) get this from the C++ side | |
56 */ | |
57 const int QT_VERSION = 0x040403; | |
58 /* | |
59 can be used like #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0)) | |
60 */ | |
61 bool QT_VERSION_CHECK( ushort major, ushort minor, ushort patch ) | |
62 { | |
63 return cast(bool)((major<<16)|(minor<<8)|(patch)); | |
64 } | |
65 //TODO(katrina) get this from the C++ side | |
66 const char[] QT_PACKAGEDATE_STR = "2008-09-27"; | |
67 //TODO(katrina) get this from the C++ side | |
68 const char[] QT_PACKAGE_TAG = "gc9953de622c6a0f655322e0d9f5bd6dc2803b470"; | |
69 | |
70 /* | |
71 Size-dependent types (architechture-dependent byte order) | |
72 | |
73 Make sure to update QMetaType when changing these typedefs | |
74 */ | |
75 | |
76 alias char qint8; /* 8 bit signed */ | |
77 alias char quint8; /* 8 bit unsigned */ | |
78 alias short qint16; /* 16 bit signed */ | |
79 alias ushort quint16; /* 16 bit unsigned */ | |
80 alias int qint32; /* 32 bit signed */ | |
81 alias uint quint32; /* 32 bit unsigned */ | |
82 alias long qint64; /* 64 bit signed */ | |
83 alias ulong quint64; /* 64 bit unsigned */ | |
84 | |
39 | 85 version (X86) |
86 { | |
87 alias quint32 quintptr; | |
88 alias qint32 qptrdiff; | |
89 } | |
90 else version (X86_64) | |
91 { | |
92 alias quint64 quintptr; | |
93 alias qint64 qptrdiff; | |
94 } | |
95 | |
1 | 96 const byte QT_POINTER_SIZE = 8; |
97 | |
98 alias int QNoImplicitBoolCast; | |
99 | |
100 alias double qreal; | |
101 | |
102 | |
103 /* | |
104 Utility macros and inline functions | |
105 TODO(katrina) see if we need to do anything to make these | |
106 able to be evaluated at compile time | |
107 */ | |
108 | |
109 T qAbs(T)(T t) { return t >= 0 ? t : -t; } | |
110 | |
111 int qRound(qreal d) | |
112 { return d >= 0.0 ? cast(int)(d + 0.5) : cast(int)(d - cast(int)(d-1) + 0.5) + cast(int)(d-1); } | |
113 | |
114 qint64 qRound64(qreal d) | |
115 { return d >= 0.0 ? cast(qint64)(d + 0.5) : cast(qint64)(d - cast(qint64)(d-1) + 0.5) + cast(qint64)(d-1); } | |
116 | |
117 T qMin(T)(T a,T b) { if (a < b) return a; return b; } | |
118 T qMax(T)(T a, T b) { if (a < b) return b; return a; } | |
119 T qBound(T)(T min, T val,T max) { return qMax(min, qMin(max, val)); } | |
120 | |
121 /* | |
122 Data stream functions are provided by many classes (defined in qdatastream.h) | |
123 */ | |
124 | |
125 //class QDataStream; | |
126 | |
127 /* | |
128 System information | |
129 */ | |
130 | |
131 class QSysInfo { | |
132 public: | |
133 enum Sizes { | |
134 WordSize = ((void *).sizeof<<3) | |
135 }; | |
136 | |
137 enum Endian { | |
138 BigEndian, | |
139 LittleEndian, | |
140 ByteOrder = BigEndian | |
141 }; | |
142 /* needed to bootstrap qmake */ | |
143 static const int ByteOrder; | |
144 | |
145 enum WinVersion { | |
146 WV_32s = 0x0001, | |
147 WV_95 = 0x0002, | |
148 WV_98 = 0x0003, | |
149 WV_Me = 0x0004, | |
150 WV_DOS_based= 0x000f, | |
151 | |
152 WV_NT = 0x0010, | |
153 WV_2000 = 0x0020, | |
154 WV_XP = 0x0030, | |
155 WV_2003 = 0x0040, | |
156 WV_VISTA = 0x0080, | |
157 WV_NT_based = 0x00f0, | |
158 | |
159 WV_CE = 0x0100, | |
160 WV_CENET = 0x0200, | |
161 WV_CE_5 = 0x0300, | |
162 WV_CE_6 = 0x0400, | |
163 WV_CE_based = 0x0f00 | |
164 }; | |
165 static const WinVersion WindowsVersion; | |
166 static WinVersion windowsVersion(); | |
167 | |
168 enum MacVersion { | |
169 MV_Unknown = 0x0000, | |
170 | |
171 /* version */ | |
172 MV_9 = 0x0001, | |
173 MV_10_0 = 0x0002, | |
174 MV_10_1 = 0x0003, | |
175 MV_10_2 = 0x0004, | |
176 MV_10_3 = 0x0005, | |
177 MV_10_4 = 0x0006, | |
178 MV_10_5 = 0x0007, | |
179 | |
180 /* codenames */ | |
181 MV_CHEETAH = MV_10_0, | |
182 MV_PUMA = MV_10_1, | |
183 MV_JAGUAR = MV_10_2, | |
184 MV_PANTHER = MV_10_3, | |
185 MV_TIGER = MV_10_4, | |
186 MV_LEOPARD = MV_10_5 | |
187 }; | |
188 static const MacVersion MacintoshVersion; | |
189 }; | |
190 | |
191 char[] qVersion(); | |
192 bool qSharedBuild(); | |
193 | |
194 int qMacVersion() { return QSysInfo.MacintoshVersion; } | |
195 | |
196 void qUnused(T)(T x) { cast(void) x; } | |
197 void Q_UNUSED(T)(T x) { qUnused(x); } | |
198 | |
199 /* | |
200 Debugging and error handling | |
201 */ | |
202 | |
203 //class QString; | |
204 //char[] qPrintable(QString string) { string.toLocal8Bit().constData(); } | |
205 //TODO(katrina) These should probably actually call into the c++ functions | |
206 void qDebug( char[] str ) /* print debug message */ | |
207 { writeln(str); } | |
208 | |
209 extern (C) void Qt_qWarning( char * ); | |
210 | |
211 void qWarning(char[] str) /* print warning message */ | |
212 { writeln(str); } | |
213 | |
214 //QString qt_error_string(int errorCode = -1); | |
215 void qCritical(char[] str) /* print critical message */ | |
216 { writeln(str); } | |
217 | |
218 /* | |
219 Forward declarations only. | |
220 | |
221 In order to use the qDebug() stream, you must #include<QDebug> | |
222 */ | |
223 //class QDebug; | |
224 //class QNoDebug; | |
225 //QDebug qDebug(); | |
226 //QDebug qWarning(); | |
227 //QDebug qCritical(); | |
228 | |
229 void qt_noop() {} | |
230 //TODO(katrina) Implement these | |
231 void qt_assert(char[] assertion, char[] file, int line); | |
232 | |
233 void qt_assert_x(char[] where, char[] what, char[] file, int line); | |
234 | |
235 void qt_check_pointer(char[], int); | |
236 | |
237 enum QtMsgType { QtDebugMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtSystemMsg = QtCriticalMsg }; | |
238 | |
239 void qt_message_output(QtMsgType, char[] buf); | |
240 //class QtMsgHandler; | |
241 //QtMsgHandler qInstallMsgHandler(QtMsgHandler); | |
242 | |
243 // forward declaration, since qatomic.h needs qglobal.h | |
244 class QBasicAtomicPointer(T); | |
245 | |
246 // POD for Q_GLOBAL_STATIC | |
247 class QGlobalStatic(T) | |
248 { | |
249 public: | |
250 QBasicAtomicPointer!(T) pointer; | |
251 bool destroyed; | |
252 }; | |
253 | |
254 // Created as a function-local static to delete a QGlobalStatic<T> | |
255 class QGlobalStaticDeleter(T) | |
256 { | |
257 public: | |
258 QGlobalStatic!(T) globalStatic; | |
259 this(QGlobalStatic!(T) _globalStatic) { | |
260 globalStatic(_globalStatic); | |
261 } | |
262 | |
263 ~this() | |
264 { | |
265 delete globalStatic.pointer; | |
266 globalStatic.pointer = 0; | |
267 globalStatic.destroyed = true; | |
268 } | |
269 }; | |
270 | |
271 class QBool | |
272 { | |
273 bool b; | |
274 | |
275 public: | |
276 this(bool B) { b = B; } | |
277 // void *() const | |
278 // { return b ? static_cast<const void *>(this) : static_cast<const void *>(0); } | |
279 } | |
280 | |
281 bool qFuzzyCompare(double p1, double p2) | |
282 { | |
283 return (qAbs(p1 - p2) <= 0.000000000001 * qMin(qAbs(p1), qAbs(p2))); | |
284 } | |
285 | |
286 bool qFuzzyCompare(float p1, float p2) | |
287 { | |
288 return (qAbs(p1 - p2) <= 0.00001f * qMin(qAbs(p1), qAbs(p2))); | |
289 } | |
290 | |
291 /* | |
292 This function tests a double for a null value. It doesn't | |
293 check whether the actual value is 0 or close to 0, but whether | |
294 it is binary 0. | |
295 */ | |
296 bool qIsNull(double d) | |
297 { | |
298 union U { | |
299 double d; | |
300 quint64 u; | |
301 }; | |
302 U val; | |
303 val.d = d; | |
304 return val.u == cast(quint64)(0); | |
305 } | |
306 | |
307 /* | |
308 This function tests a float for a null value. It doesn't | |
309 check whether the actual value is 0 or close to 0, but whether | |
310 it is binary 0. | |
311 */ | |
312 bool qIsNull(float f) | |
313 { | |
314 union U { | |
315 float f; | |
316 quint32 u; | |
317 }; | |
318 U val; | |
319 val.f = f; | |
320 return val.u == 0u; | |
321 } | |
322 | |
323 /* | |
324 Compilers which follow outdated template instantiation rules | |
325 require a class to have a comparison operator to exist when | |
326 a QList of this type is instantiated. It's not actually | |
327 used in the list, though. Hence the dummy implementation. | |
328 Just in case other code relies on it we better trigger a warning | |
329 mandating a real implementation. | |
330 */ | |
331 | |
332 | |
333 /* | |
334 QTypeInfo - type trait functionality | |
335 qIsDetached - data sharing functionality | |
336 */ | |
337 | |
338 /* | |
339 The catch-all template. | |
340 */ | |
341 | |
342 bool qIsDetached(T)(T) { return true; } | |
343 | |
344 class QTypeInfossss(T) | |
345 { | |
346 public: | |
347 enum { | |
348 isPointer = false, | |
349 isComplex = true, | |
350 isStatic = true, | |
351 isLarge = ((T).sizeof>(void*).sizeof), | |
352 isDummy = false | |
353 }; | |
354 }; | |
355 | |
356 class QTypeInfo(T) | |
357 { | |
358 public: | |
359 enum { | |
360 isPointer = true, | |
361 isComplex = false, | |
362 isStatic = false, | |
363 isLarge = false, | |
364 isDummy = false | |
365 }; | |
366 }; | |
367 | |
368 | |
369 /* | |
370 Specialize a specific type with: | |
371 | |
372 Q_DECLARE_TYPEINFO(type, flags); | |
373 | |
374 where 'type' is the name of the type to specialize and 'flags' is | |
375 logically-OR'ed combination of the flags below. | |
376 */ | |
377 enum { /* TYPEINFO flags */ | |
378 Q_COMPLEX_TYPE = 0, | |
379 Q_PRIMITIVE_TYPE = 0x1, | |
380 Q_STATIC_TYPE = 0, | |
381 Q_MOVABLE_TYPE = 0x2, | |
382 Q_DUMMY_TYPE = 0x4 | |
383 }; | |
384 | |
385 /* | |
386 Specialize a shared type with: | |
387 | |
388 Q_DECLARE_SHARED(type); | |
389 | |
390 where 'type' is the name of the type to specialize. NOTE: shared | |
391 types must declare a 'bool isDetached(void) const;' member for this | |
392 to work. | |
393 */ | |
394 void qSwap_helper(T)(ref T value1, ref T value2, T*) | |
395 { | |
396 T t = value1; | |
397 value1 = value2; | |
398 value2 = t; | |
399 } | |
400 bool qIsDetached(T)(ref T t) { return t.isDetached(); } | |
401 void qSwap_helper(T)(ref T value1, ref T value2, T*) | |
402 { | |
403 const T.DataPtr t = value1.data_ptr(); | |
404 value1.data_ptr() = value2.data_ptr(); | |
405 value2.data_ptr() = t; | |
406 } | |
155 | 407 |
1 | 408 void qSwap(T)(ref T value1, ref T value2) |
409 { | |
155 | 410 T t = value1; |
411 value1 = value2; | |
412 value2 = t; | |
1 | 413 } |
414 | |
415 /* | |
416 QTypeInfo primitive specializations | |
417 TODO(katrina) Find out what we need to do here | |
418 */ | |
419 /* | |
420 Q_DECLARE_TYPEINFO(bool, Q_PRIMITIVE_TYPE); | |
421 Q_DECLARE_TYPEINFO(char, Q_PRIMITIVE_TYPE); | |
422 Q_DECLARE_TYPEINFO(signed char, Q_PRIMITIVE_TYPE); | |
423 Q_DECLARE_TYPEINFO(uchar, Q_PRIMITIVE_TYPE); | |
424 Q_DECLARE_TYPEINFO(short, Q_PRIMITIVE_TYPE); | |
425 Q_DECLARE_TYPEINFO(ushort, Q_PRIMITIVE_TYPE); | |
426 Q_DECLARE_TYPEINFO(int, Q_PRIMITIVE_TYPE); | |
427 Q_DECLARE_TYPEINFO(uint, Q_PRIMITIVE_TYPE); | |
428 Q_DECLARE_TYPEINFO(long, Q_PRIMITIVE_TYPE); | |
429 Q_DECLARE_TYPEINFO(ulong, Q_PRIMITIVE_TYPE); | |
430 Q_DECLARE_TYPEINFO(qint64, Q_PRIMITIVE_TYPE); | |
431 Q_DECLARE_TYPEINFO(quint64, Q_PRIMITIVE_TYPE); | |
432 Q_DECLARE_TYPEINFO(float, Q_PRIMITIVE_TYPE); | |
433 Q_DECLARE_TYPEINFO(double, Q_PRIMITIVE_TYPE); | |
434 #ifndef Q_OS_DARWIN | |
435 Q_DECLARE_TYPEINFO(long double, Q_PRIMITIVE_TYPE); | |
436 #endif | |
437 */ | |
438 /* | |
439 These functions make it possible to use standard C++ functions with | |
440 a similar name from Qt header files (especially template classes). | |
441 TODO(katrina) Implement these | |
442 */ | |
443 void * qMalloc(size_t size); | |
444 void qFree(void * ptr); | |
445 void * qRealloc(void * ptr, size_t size); | |
446 void * qMemCopy(void * dest, void * src, size_t n); | |
447 void * qMemSet(void * dest, int c, size_t n); | |
448 | |
449 struct QFlags(Enum) | |
450 { | |
451 private: | |
452 alias void **Zero; | |
453 int i; | |
454 | |
455 public: | |
456 alias Enum enum_type; | |
457 | |
458 public static QFlags!(Enum) opCall(Enum)(QFlags f) { | |
459 QFlags!(Enum) res; | |
460 res.i = f.i; | |
461 return res; | |
462 } | |
463 | |
464 public static QFlags opCall(Enum)(Enum f) { | |
465 QFlags!(Enum) res; | |
466 res.i = f; | |
467 return res; | |
468 } | |
469 | |
470 public static QFlags opCall(Enum)(int f) { | |
471 QFlags!(Enum) res; | |
472 res.i = cast(Enum) f; | |
473 return res; | |
474 } | |
475 | |
476 // this(Zero = 0) : i(0) {} | |
477 // this(QFlag f) : i(f) {} | |
478 | |
479 // QFlags!(Enum) opAssign(QFlags f) { i = f.i; return *this; } | |
480 QFlags!(Enum) opAssign(int f) { i = f; return *this; } | |
481 QFlags!(Enum) opAndAssign(int mask) { i &= mask; return *this; } | |
482 QFlags!(Enum) opAndAssign(uint mask) { i &= mask; return *this; } | |
483 QFlags!(Enum) opOrAssign(QFlags f) { i |= f.i; return *this; } | |
484 QFlags!(Enum) opOrAssign(Enum f) { i |= f; return *this; } | |
485 QFlags!(Enum) opXorAssign(QFlags f) { i ^= f.i; return *this; } | |
486 QFlags!(Enum) opXorAssign(Enum f) { i ^= f; return *this; } | |
487 | |
488 int toInt() { return i; } | |
489 | |
490 QFlags!(Enum) opOr(QFlags f) { QFlags g; g.i = i | f.i; return g; } | |
491 QFlags!(Enum) opOr(Enum f) { QFlags g; g.i = i | f; return g; } | |
492 QFlags!(Enum) opXor(QFlags f) { QFlags g; g.i = i ^ f.i; return g; } | |
493 QFlags!(Enum) opXor(Enum f) { QFlags g; g.i = i ^ f; return g; } | |
494 QFlags!(Enum) opAnd(int mask) { QFlags g; g.i = i & mask; return g; } | |
495 QFlags!(Enum) opAnd(uint mask) { QFlags g; g.i = i & mask; return g; } | |
496 QFlags!(Enum) opAnd(Enum f) { QFlags g; g.i = i & f; return g; } | |
497 QFlags!(Enum) opCom() { QFlags g; g.i = ~i; return g; } | |
498 | |
499 // bool operator!() { return !i; } | |
500 | |
501 // bool testFlag(Enum f) { return i & f; } | |
502 } | |
503 | |
504 /* TODO typesafety | |
505 #define Q_DECLARE_FLAGS(Flags, Enum)\ | |
506 typedef QFlags<Enum> Flags; | |
507 #define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \ | |
508 QFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) \ | |
509 { return QFlags<Flags::enum_type>(f1) | f2; } \ | |
510 QFlags<Flags::enum_type> operator|(Flags::enum_type f1, QFlags<Flags::enum_type> f2) \ | |
511 { return f2 | f1; } | |
512 */ | |
513 | |
514 char[] QT_TR_NOOP(char[] x) { return x; } | |
515 char[] QT_TRANSLATE_NOOP(char[] s, char[] x) { return x; } | |
516 char[] QT_TRANSLATE_NOOP3(char[] s, char[] x, char[] comment) { return x; } | |
517 | |
518 //class QByteArray; | |
519 //QByteArray qgetenv(char[] varName); | |
520 //bool qputenv(char[] varName, QByteArray value); | |
521 | |
522 int qIntCast(double f) { return cast(int)(f); } | |
523 int qIntCast(float f) { return cast(int)(f); } | |
524 | |
525 /* | |
526 Reentrant versions of basic rand() functions for random number generation | |
527 */ | |
528 void qsrand(uint seed); | |
529 int qrand(); | |
530 | |
531 | |
532 /* | |
533 This gives us the possibility to check which modules the user can | |
534 use. These are purely compile time checks and will generate no code. | |
535 */ | |
536 | |
537 /* Qt modules */ | |
538 | |
539 const ushort QT_MODULE_CORE = 0x0001; | |
540 const ushort QT_MODULE_GUI = 0x0002; | |
541 const ushort QT_MODULE_NETWORK = 0x0004; | |
542 const ushort QT_MODULE_OPENGL = 0x0008; | |
543 const ushort QT_MODULE_SQL = 0x0010; | |
544 const ushort QT_MODULE_XML = 0x0020; | |
545 const ushort QT_MODULE_QT3SUPPORTLIGHT = 0x0040; | |
546 const ushort QT_MODULE_QT3SUPPORT = 0x0080; | |
547 const ushort QT_MODULE_SVG = 0x0100; | |
548 const ushort QT_MODULE_ACTIVEQT = 0x0200; | |
549 const ushort QT_MODULE_GRAPHICSVIEW = 0x0400; | |
550 const ushort QT_MODULE_SCRIPT = 0x0800; | |
551 const ushort QT_MODULE_XMLPATTERNS = 0x1000; | |
552 const ushort QT_MODULE_HELP = 0x2000; | |
553 const ushort QT_MODULE_TEST = 0x4000; | |
554 const ushort QT_MODULE_DBUS = 0x8000; | |
555 | |
556 /* Qt editions */ | |
557 | |
558 const ushort QT_EDITION_CONSOLE = (QT_MODULE_CORE | |
559 | QT_MODULE_NETWORK | |
560 | QT_MODULE_SQL | |
561 | QT_MODULE_SCRIPT | |
562 | QT_MODULE_XML | |
563 | QT_MODULE_XMLPATTERNS | |
564 | QT_MODULE_TEST | |
565 | QT_MODULE_DBUS); | |
566 const ushort QT_EDITION_DESKTOPLIGHT = (QT_MODULE_CORE | |
567 | QT_MODULE_GUI | |
568 | QT_MODULE_QT3SUPPORTLIGHT | |
569 | QT_MODULE_TEST | |
570 | QT_MODULE_DBUS); | |
571 const ushort QT_EDITION_OPENSOURCE = (QT_MODULE_CORE | |
572 | QT_MODULE_GUI | |
573 | QT_MODULE_NETWORK | |
574 | QT_MODULE_OPENGL | |
575 | QT_MODULE_SQL | |
576 | QT_MODULE_XML | |
577 | QT_MODULE_XMLPATTERNS | |
578 | QT_MODULE_SCRIPT | |
579 | QT_MODULE_QT3SUPPORTLIGHT | |
580 | QT_MODULE_QT3SUPPORT | |
581 | QT_MODULE_SVG | |
582 | QT_MODULE_GRAPHICSVIEW | |
583 | QT_MODULE_HELP | |
584 | QT_MODULE_TEST | |
585 | QT_MODULE_DBUS); | |
586 const ushort QT_EDITION_DESKTOP = (QT_EDITION_OPENSOURCE | |
587 | QT_MODULE_ACTIVEQT); | |
588 const ushort QT_EDITION_UNIVERSAL = QT_EDITION_DESKTOP; | |
589 const ushort QT_EDITION_ACADEMIC = QT_EDITION_DESKTOP; | |
590 const ushort QT_EDITION_EDUCATIONAL = QT_EDITION_DESKTOP; | |
591 const ushort QT_EDITION_EVALUATION = QT_EDITION_DESKTOP; | |
592 | |
593 mixin QT_END_NAMESPACE; | |
183 | 594 |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
595 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
596 struct Align |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
597 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
598 ubyte a; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
599 void* b; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
600 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
601 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
602 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
603 const PTR_ALIGN = Align.tupleof[1].alignof; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
604 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
605 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
606 template AlignPad(size_t base, size_t aligned) |
183 | 607 { |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
608 static if( aligned == 0 ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
609 const AlignPad = base; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
610 else |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
611 const AlignPad = ((base+PTR_ALIGN-1)/PTR_ALIGN)*PTR_ALIGN |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
612 + aligned; |
183 | 613 } |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
614 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
615 template InstanceSize(T) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
616 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
617 static if( is( T == Object ) ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
618 const InstanceSize = 2*(void*).sizeof; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
619 else |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
620 const InstanceSize = Max!( |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
621 AlignPad!( |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
622 InstanceSize!(Super!(T)), |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
623 InterfaceCount!(T)*(void*).sizeof), |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
624 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
625 AlignPad!( |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
626 InstanceSizeImpl!(T, 0), |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
627 + InterfaceCount!(T)*(void*).sizeof)); |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
628 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
629 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
630 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
631 template Super(T) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
632 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
633 static if( is( T S == super ) ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
634 alias First!(S) Super; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
635 else |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
636 static assert(false, "Can't get super of "~T.mangleof); |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
637 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
638 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
639 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
640 template First(T) |
183 | 641 { |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
642 alias T First; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
643 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
644 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
645 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
646 template First(T, Ts...) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
647 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
648 alias T First; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
649 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
650 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
651 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
652 template InstanceSizeImpl(T, size_t i) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
653 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
654 static if( i < T.tupleof.length ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
655 const InstanceSizeImpl = Max!( |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
656 T.tupleof[i].offsetof + T.tupleof[i].sizeof, |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
657 InstanceSizeImpl!(T, i+1)); |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
658 else |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
659 // This is necessary to account for classes without member |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
660 // variables. |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
661 const InstanceSizeImpl = 2*(void*).sizeof; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
662 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
663 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
664 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
665 template Max(size_t a, size_t b) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
666 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
667 static if( a > b ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
668 const Max = a; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
669 else |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
670 const Max = b; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
671 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
672 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
673 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
674 template InterfaceCount(T) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
675 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
676 static if( is( T == Object ) ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
677 const InterfaceCount = 0u; |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
678 else static if( is( T S == super ) ) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
679 const InterfaceCount = InterfaceCountImpl!(S); |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
680 } |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
681 |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
682 private |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
683 template InterfaceCountImpl(TBase, TInterfaces...) |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
684 { |
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
685 const InterfaceCountImpl = TInterfaces.length; |
183 | 686 } |
687 | |
688 scope class StackObject(C) | |
689 { | |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
690 byte[InstanceSize!(C)] data; |
183 | 691 bool constructed; |
692 | |
693 C opCall(A...)(A args) | |
694 { | |
695 assert(!constructed); | |
696 | |
697 auto r = new(&data)C(args); | |
698 r.__stackAllocated = true; | |
699 constructed = true; | |
700 | |
701 return r; | |
702 } | |
703 | |
704 ~this() | |
705 { | |
706 if (constructed) | |
707 { | |
708 auto obj = cast(C)&data; | |
709 delete obj; | |
710 } | |
711 } | |
712 } | |
713 | |
1 | 714 mixin QT_END_HEADER; |
183 | 715 |