comparison qt/QGlobal.d @ 1:e78566595089

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