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