source: trunk/src/corelib/global/qglobal.cpp@ 440

Last change on this file since 440 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 78.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43#include "qstring.h"
44#include "qvector.h"
45#include "qlist.h"
46#include "qthreadstorage.h"
47
48#ifndef QT_NO_QOBJECT
49#include <private/qthread_p.h>
50#endif
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <limits.h>
55#include <stdarg.h>
56#include <string.h>
57
58#if !defined(Q_OS_WINCE)
59# include <errno.h>
60# if defined(Q_CC_MSVC)
61# include <crtdbg.h>
62# endif
63#endif
64
65#ifdef Q_CC_MWERKS
66#include <CoreServices/CoreServices.h>
67#endif
68
69QT_BEGIN_NAMESPACE
70
71
72/*!
73 \class QFlag
74 \brief The QFlag class is a helper data type for QFlags.
75
76 It is equivalent to a plain \c int, except with respect to
77 function overloading and type conversions. You should never need
78 to use this class in your applications.
79
80 \sa QFlags
81*/
82
83/*!
84 \fn QFlag::QFlag(int value)
85
86 Constructs a QFlag object that stores the given \a value.
87*/
88
89/*!
90 \fn QFlag::operator int() const
91
92 Returns the value stored by the QFlag object.
93*/
94
95/*!
96 \class QFlags
97 \brief The QFlags class provides a type-safe way of storing
98 OR-combinations of enum values.
99
100 \mainclass
101 \ingroup tools
102
103 The QFlags<Enum> class is a template class, where Enum is an enum
104 type. QFlags is used throughout Qt for storing combinations of
105 enum values.
106
107 The traditional C++ approach for storing OR-combinations of enum
108 values is to use an \c int or \c uint variable. The inconvenience
109 with this approach is that there's no type checking at all; any
110 enum value can be OR'd with any other enum value and passed on to
111 a function that takes an \c int or \c uint.
112
113 Qt uses QFlags to provide type safety. For example, the
114 Qt::Alignment type is simply a typedef for
115 QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a
116 Qt::Alignment parameter, which means that any combination of
117 Qt::AlignmentFlag values,or 0, is legal:
118
119 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 0
120
121 If you try to pass a value from another enum or just a plain
122 integer other than 0, the compiler will report an error. If you
123 need to cast integer values to flags in a untyped fashion, you can
124 use the explicit QFlags constructor as cast operator.
125
126 If you want to use QFlags for your own enum types, use
127 the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
128 For example:
129
130 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 1
131
132 You can then use the \c MyClass::Options type to store
133 combinations of \c MyClass::Option values.
134
135 \section1 Flags and the Meta-Object System
136
137 The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object
138 system, so they cannot be used by Qt Script or edited in Qt Designer.
139 To make the flags available for these purposes, the Q_FLAGS() macro must
140 be used:
141
142 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp meta-object flags
143
144 \section1 Naming Convention
145
146 A sensible naming convention for enum types and associated QFlags
147 types is to give a singular name to the enum type (e.g., \c
148 Option) and a plural name to the QFlags type (e.g., \c Options).
149 When a singular name is desired for the QFlags type (e.g., \c
150 Alignment), you can use \c Flag as the suffix for the enum type
151 (e.g., \c AlignmentFlag).
152
153 \sa QFlag
154*/
155
156/*!
157 \typedef QFlags::enum_type
158
159 Typedef for the Enum template type.
160*/
161
162/*!
163 \fn QFlags::QFlags(const QFlags &other)
164
165 Constructs a copy of \a other.
166*/
167
168/*!
169 \fn QFlags::QFlags(Enum flag)
170
171 Constructs a QFlags object storing the given \a flag.
172*/
173
174/*!
175 \fn QFlags::QFlags(Zero zero)
176
177 Constructs a QFlags object with no flags set. \a zero must be a
178 literal 0 value.
179*/
180
181/*!
182 \fn QFlags::QFlags(QFlag value)
183
184 Constructs a QFlags object initialized with the given integer \a
185 value.
186
187 The QFlag type is a helper type. By using it here instead of \c
188 int, we effectively ensure that arbitrary enum values cannot be
189 cast to a QFlags, whereas untyped enum values (i.e., \c int
190 values) can.
191*/
192
193/*!
194 \fn QFlags &QFlags::operator=(const QFlags &other)
195
196 Assigns \a other to this object and returns a reference to this
197 object.
198*/
199
200/*!
201 \fn QFlags &QFlags::operator&=(int mask)
202
203 Performs a bitwise AND operation with \a mask and stores the
204 result in this QFlags object. Returns a reference to this object.
205
206 \sa operator&(), operator|=(), operator^=()
207*/
208
209/*!
210 \fn QFlags &QFlags::operator&=(uint mask)
211
212 \overload
213*/
214
215/*!
216 \fn QFlags &QFlags::operator|=(QFlags other)
217
218 Performs a bitwise OR operation with \a other and stores the
219 result in this QFlags object. Returns a reference to this object.
220
221 \sa operator|(), operator&=(), operator^=()
222*/
223
224/*!
225 \fn QFlags &QFlags::operator|=(Enum other)
226
227 \overload
228*/
229
230/*!
231 \fn QFlags &QFlags::operator^=(QFlags other)
232
233 Performs a bitwise XOR operation with \a other and stores the
234 result in this QFlags object. Returns a reference to this object.
235
236 \sa operator^(), operator&=(), operator|=()
237*/
238
239/*!
240 \fn QFlags &QFlags::operator^=(Enum other)
241
242 \overload
243*/
244
245/*!
246 \fn QFlags::operator int() const
247
248 Returns the value stored in the QFlags object as an integer.
249*/
250
251/*!
252 \fn QFlags QFlags::operator|(QFlags other) const
253
254 Returns a QFlags object containing the result of the bitwise OR
255 operation on this object and \a other.
256
257 \sa operator|=(), operator^(), operator&(), operator~()
258*/
259
260/*!
261 \fn QFlags QFlags::operator|(Enum other) const
262
263 \overload
264*/
265
266/*!
267 \fn QFlags QFlags::operator^(QFlags other) const
268
269 Returns a QFlags object containing the result of the bitwise XOR
270 operation on this object and \a other.
271
272 \sa operator^=(), operator&(), operator|(), operator~()
273*/
274
275/*!
276 \fn QFlags QFlags::operator^(Enum other) const
277
278 \overload
279*/
280
281/*!
282 \fn QFlags QFlags::operator&(int mask) const
283
284 Returns a QFlags object containing the result of the bitwise AND
285 operation on this object and \a mask.
286
287 \sa operator&=(), operator|(), operator^(), operator~()
288*/
289
290/*!
291 \fn QFlags QFlags::operator&(uint mask) const
292
293 \overload
294*/
295
296/*!
297 \fn QFlags QFlags::operator&(Enum mask) const
298
299 \overload
300*/
301
302/*!
303 \fn QFlags QFlags::operator~() const
304
305 Returns a QFlags object that contains the bitwise negation of
306 this object.
307
308 \sa operator&(), operator|(), operator^()
309*/
310
311/*!
312 \fn bool QFlags::operator!() const
313
314 Returns true if no flag is set (i.e., if the value stored by the
315 QFlags object is 0); otherwise returns false.
316*/
317
318/*!
319 \fn bool QFlags::testFlag(Enum flag) const
320 \since 4.2
321
322 Returns true if the \a flag is set, otherwise false.
323*/
324
325/*!
326 \macro Q_DISABLE_COPY(Class)
327 \relates QObject
328
329 Disables the use of copy constructors and assignment operators
330 for the given \a Class.
331
332 Instances of subclasses of QObject should not be thought of as
333 values that can be copied or assigned, but as unique identities.
334 This means that when you create your own subclass of QObject
335 (director or indirect), you should \e not give it a copy constructor
336 or an assignment operator. However, it may not enough to simply
337 omit them from your class, because, if you mistakenly write some code
338 that requires a copy constructor or an assignment operator (it's easy
339 to do), your compiler will thoughtfully create it for you. You must
340 do more.
341
342 The curious user will have seen that the Qt classes derived
343 from QObject typically include this macro in a private section:
344
345 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 43
346
347 It declares a copy constructor and an assignment operator in the
348 private section, so that if you use them by mistake, the compiler
349 will report an error.
350
351 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 44
352
353 But even this might not catch absolutely every case. You might be
354 tempted to do something like this:
355
356 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 45
357
358 First of all, don't do that. Most compilers will generate code that
359 uses the copy constructor, so the privacy violation error will be
360 reported, but your C++ compiler is not required to generate code for
361 this statement in a specific way. It could generate code using
362 \e{neither} the copy constructor \e{nor} the assignment operator we
363 made private. In that case, no error would be reported, but your
364 application would probably crash when you called a member function
365 of \c{w}.
366*/
367
368/*!
369 \macro Q_DECLARE_FLAGS(Flags, Enum)
370 \relates QFlags
371
372 The Q_DECLARE_FLAGS() macro expands to
373
374 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 2
375
376 \a Enum is the name of an existing enum type, whereas \a Flags is
377 the name of the QFlags<\e{Enum}> typedef.
378
379 See the QFlags documentation for details.
380
381 \sa Q_DECLARE_OPERATORS_FOR_FLAGS()
382*/
383
384/*!
385 \macro Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
386 \relates QFlags
387
388 The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global \c
389 operator|() functions for \a Flags, which is of type QFlags<T>.
390
391 See the QFlags documentation for details.
392
393 \sa Q_DECLARE_FLAGS()
394*/
395
396/*!
397 \headerfile <QtGlobal>
398 \title Global Qt Declarations
399 \ingroup architecture
400
401 \brief The <QtGlobal> header provides basic declarations and
402 is included by all other Qt headers.
403
404 The declarations include \l {types}, \l functions and
405 \l macros.
406
407 The type definitions are partly convenience definitions for basic
408 types (some of which guarantee certain bit-sizes on all platforms
409 supported by Qt), partly types related to Qt message handling. The
410 functions are related to generating messages, Qt version handling
411 and comparing and adjusting object values. And finally, some of
412 the declared macros enable programmers to add compiler or platform
413 specific code to their applications, while others are convenience
414 macros for larger operations.
415
416 \section1 Types
417
418 The header file declares several type definitions that guarantee a
419 specified bit-size on all platforms supported by Qt for various
420 basic types, for example \l qint8 which is a signed char
421 guaranteed to be 8-bit on all platforms supported by Qt. The
422 header file also declares the \l qlonglong type definition for \c
423 {long long int } (\c __int64 on Windows).
424
425 Several convenience type definitions are declared: \l qreal for \c
426 double, \l uchar for \c unsigned char, \l uint for \c unsigned
427 int, \l ulong for \c unsigned long and \l ushort for \c unsigned
428 short.
429
430 Finally, the QtMsgType definition identifies the various messages
431 that can be generated and sent to a Qt message handler;
432 QtMsgHandler is a type definition for a pointer to a function with
433 the signature \c {void myMsgHandler(QtMsgType, const char *)}.
434
435 \section1 Functions
436
437 The <QtGlobal> header file contains several functions comparing
438 and adjusting an object's value. These functions take a template
439 type as argument: You can retrieve the absolute value of an object
440 using the qAbs() function, and you can bound a given object's
441 value by given minimum and maximum values using the qBound()
442 function. You can retrieve the minimum and maximum of two given
443 objects using qMin() and qMax() respectively. All these functions
444 return a corresponding template type; the template types can be
445 replaced by any other type. For example:
446
447 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 3
448
449 <QtGlobal> also contains functions that generate messages from the
450 given string argument: qCritical(), qDebug(), qFatal() and
451 qWarning(). These functions call the message handler with the
452 given message. For example:
453
454 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 4
455
456 The remaining functions are qRound() and qRound64(), which both
457 accept a \l qreal value as their argument returning the value
458 rounded up to the nearest integer and 64-bit integer respectively,
459 the qInstallMsgHandler() function which installs the given
460 QtMsgHandler, and the qVersion() function which returns the
461 version number of Qt at run-time as a string.
462
463 \section1 Macros
464
465 The <QtGlobal> header file provides a range of macros (Q_CC_*)
466 that are defined if the application is compiled using the
467 specified platforms. For example, the Q_CC_SUN macro is defined if
468 the application is compiled using Forte Developer, or Sun Studio
469 C++. The header file also declares a range of macros (Q_OS_*)
470 that are defined for the specified platforms. For example,
471 Q_OS_X11 which is defined for the X Window System.
472
473 The purpose of these macros is to enable programmers to add
474 compiler or platform specific code to their application.
475
476 The remaining macros are convenience macros for larger operations:
477 The QT_TRANSLATE_NOOP() and QT_TR_NOOP() macros provide the
478 possibility of marking text for dynamic translation,
479 i.e. translation without changing the stored source text. The
480 Q_ASSERT() and Q_ASSERT_X() enables warning messages of various
481 level of refinement. The Q_FOREACH() and foreach() macros
482 implement Qt's foreach loop.
483
484 The Q_INT64_C() and Q_UINT64_C() macros wrap signed and unsigned
485 64-bit integer literals in a platform-independent way. The
486 Q_CHECK_PTR() macro prints a warning containing the source code's
487 file name and line number, saying that the program ran out of
488 memory, if the pointer is 0. The qPrintable() macro represent an
489 easy way of printing text.
490
491 Finally, the QT_POINTER_SIZE macro expands to the size of a
492 pointer in bytes, and the QT_VERSION and QT_VERSION_STR macros
493 expand to a numeric value or a string, respectively, specifying
494 Qt's version number, i.e the version the application is compiled
495 against.
496
497 \sa <QtAlgorithms>, QSysInfo
498*/
499
500/*!
501 \typedef qreal
502 \relates <QtGlobal>
503
504 Typedef for \c double on all platforms except for those using CPUs with
505 ARM architectures.
506 On ARM-based platforms, \c qreal is a typedef for \c float for performance
507 reasons.
508*/
509
510/*! \typedef uchar
511 \relates <QtGlobal>
512
513 Convenience typedef for \c{unsigned char}.
514*/
515
516/*!
517 \fn qt_set_sequence_auto_mnemonic(bool on)
518 \relates <QtGlobal>
519
520 Enables automatic mnemonics on Mac if \a on is true; otherwise
521 this feature is disabled.
522
523 Note that this function is only available on Mac where mnemonics
524 are disabled by default.
525
526 To access to this function, use an extern declaration:
527 extern void qt_set_sequence_auto_mnemonic(bool b);
528
529 \sa {QShortcut#mnemonic}{QShortcut}
530*/
531
532/*! \typedef ushort
533 \relates <QtGlobal>
534
535 Convenience typedef for \c{unsigned short}.
536*/
537
538/*! \typedef uint
539 \relates <QtGlobal>
540
541 Convenience typedef for \c{unsigned int}.
542*/
543
544/*! \typedef ulong
545 \relates <QtGlobal>
546
547 Convenience typedef for \c{unsigned long}.
548*/
549
550/*! \typedef qint8
551 \relates <QtGlobal>
552
553 Typedef for \c{signed char}. This type is guaranteed to be 8-bit
554 on all platforms supported by Qt.
555*/
556
557/*!
558 \typedef quint8
559 \relates <QtGlobal>
560
561 Typedef for \c{unsigned char}. This type is guaranteed to
562 be 8-bit on all platforms supported by Qt.
563*/
564
565/*! \typedef qint16
566 \relates <QtGlobal>
567
568 Typedef for \c{signed short}. This type is guaranteed to be
569 16-bit on all platforms supported by Qt.
570*/
571
572/*!
573 \typedef quint16
574 \relates <QtGlobal>
575
576 Typedef for \c{unsigned short}. This type is guaranteed to
577 be 16-bit on all platforms supported by Qt.
578*/
579
580/*! \typedef qint32
581 \relates <QtGlobal>
582
583 Typedef for \c{signed int}. This type is guaranteed to be 32-bit
584 on all platforms supported by Qt.
585*/
586
587/*!
588 \typedef quint32
589 \relates <QtGlobal>
590
591 Typedef for \c{unsigned int}. This type is guaranteed to
592 be 32-bit on all platforms supported by Qt.
593*/
594
595/*! \typedef qint64
596 \relates <QtGlobal>
597
598 Typedef for \c{long long int} (\c __int64 on Windows). This type
599 is guaranteed to be 64-bit on all platforms supported by Qt.
600
601 Literals of this type can be created using the Q_INT64_C() macro:
602
603 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 5
604
605 \sa Q_INT64_C(), quint64, qlonglong
606*/
607
608/*!
609 \typedef quint64
610 \relates <QtGlobal>
611
612 Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
613 Windows). This type is guaranteed to be 64-bit on all platforms
614 supported by Qt.
615
616 Literals of this type can be created using the Q_UINT64_C()
617 macro:
618
619 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 6
620
621 \sa Q_UINT64_C(), qint64, qulonglong
622*/
623
624/*!
625 \typedef quintptr
626 \relates <QtGlobal>
627
628 Integral type for representing a pointers (useful for hashing,
629 etc.).
630
631 Typedef for either quint32 or quint64. This type is guaranteed to
632 be the same size as a pointer on all platforms supported by Qt. On
633 a system with 32-bit pointers, quintptr is a typedef for quint32;
634 on a system with 64-bit pointers, quintptr is a typedef for
635 quint64.
636
637 Note that quintptr is unsigned. Use qptrdiff for signed values.
638
639 \sa qptrdiff, quint32, quint64
640*/
641
642/*!
643 \typedef qptrdiff
644 \relates <QtGlobal>
645
646 Integral type for representing pointer differences.
647
648 Typedef for either qint32 or qint64. This type is guaranteed to be
649 the same size as a pointer on all platforms supported by Qt. On a
650 system with 32-bit pointers, quintptr is a typedef for quint32; on
651 a system with 64-bit pointers, quintptr is a typedef for quint64.
652
653 Note that qptrdiff is signed. Use quintptr for unsigned values.
654
655 \sa quintptr, qint32, qint64
656*/
657
658/*!
659 \typedef QtMsgHandler
660 \relates <QtGlobal>
661
662 This is a typedef for a pointer to a function with the following
663 signature:
664
665 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 7
666
667 \sa QtMsgType, qInstallMsgHandler()
668*/
669
670/*!
671 \enum QtMsgType
672 \relates <QtGlobal>
673
674 This enum describes the messages that can be sent to a message
675 handler (QtMsgHandler). You can use the enum to identify and
676 associate the various message types with the appropriate
677 actions.
678
679 \value QtDebugMsg
680 A message generated by the qDebug() function.
681 \value QtWarningMsg
682 A message generated by the qWarning() function.
683 \value QtCriticalMsg
684 A message generated by the qCritical() function.
685 \value QtFatalMsg
686 A message generated by the qFatal() function.
687 \value QtSystemMsg
688
689
690 \sa QtMsgHandler, qInstallMsgHandler()
691*/
692
693/*! \macro qint64 Q_INT64_C(literal)
694 \relates <QtGlobal>
695
696 Wraps the signed 64-bit integer \a literal in a
697 platform-independent way. For example:
698
699 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 8
700
701 \sa qint64, Q_UINT64_C()
702*/
703
704/*! \macro quint64 Q_UINT64_C(literal)
705 \relates <QtGlobal>
706
707 Wraps the unsigned 64-bit integer \a literal in a
708 platform-independent way. For example:
709
710 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 9
711
712 \sa quint64, Q_INT64_C()
713*/
714
715/*! \typedef qlonglong
716 \relates <QtGlobal>
717
718 Typedef for \c{long long int} (\c __int64 on Windows). This is
719 the same as \l qint64.
720
721 \sa qulonglong, qint64
722*/
723
724/*!
725 \typedef qulonglong
726 \relates <QtGlobal>
727
728 Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
729 Windows). This is the same as \l quint64.
730
731 \sa quint64, qlonglong
732*/
733
734/*! \fn const T &qAbs(const T &value)
735 \relates <QtGlobal>
736
737 Returns the absolute value of \a value. For example:
738
739 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 10
740*/
741
742/*! \fn int qRound(qreal value)
743 \relates <QtGlobal>
744
745 Rounds \a value to the nearest integer. For example:
746
747 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 11
748*/
749
750/*! \fn qint64 qRound64(qreal value)
751 \relates <QtGlobal>
752
753 Rounds \a value to the nearest 64-bit integer. For example:
754
755 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 12
756*/
757
758/*! \fn const T &qMin(const T &value1, const T &value2)
759 \relates <QtGlobal>
760
761 Returns the minimum of \a value1 and \a value2. For example:
762
763 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 13
764
765 \sa qMax(), qBound()
766*/
767
768/*! \fn const T &qMax(const T &value1, const T &value2)
769 \relates <QtGlobal>
770
771 Returns the maximum of \a value1 and \a value2. For example:
772
773 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 14
774
775 \sa qMin(), qBound()
776*/
777
778/*! \fn const T &qBound(const T &min, const T &value, const T &max)
779 \relates <QtGlobal>
780
781 Returns \a value bounded by \a min and \a max. This is equivalent
782 to qMax(\a min, qMin(\a value, \a max)). For example:
783
784 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 15
785
786 \sa qMin(), qMax()
787*/
788
789/*!
790 \typedef Q_INT8
791 \relates <QtGlobal>
792 \compat
793
794 Use \l qint8 instead.
795*/
796
797/*!
798 \typedef Q_UINT8
799 \relates <QtGlobal>
800 \compat
801
802 Use \l quint8 instead.
803*/
804
805/*!
806 \typedef Q_INT16
807 \relates <QtGlobal>
808 \compat
809
810 Use \l qint16 instead.
811*/
812
813/*!
814 \typedef Q_UINT16
815 \relates <QtGlobal>
816 \compat
817
818 Use \l quint16 instead.
819*/
820
821/*!
822 \typedef Q_INT32
823 \relates <QtGlobal>
824 \compat
825
826 Use \l qint32 instead.
827*/
828
829/*!
830 \typedef Q_UINT32
831 \relates <QtGlobal>
832 \compat
833
834 Use \l quint32 instead.
835*/
836
837/*!
838 \typedef Q_INT64
839 \relates <QtGlobal>
840 \compat
841
842 Use \l qint64 instead.
843*/
844
845/*!
846 \typedef Q_UINT64
847 \relates <QtGlobal>
848 \compat
849
850 Use \l quint64 instead.
851*/
852
853/*!
854 \typedef Q_LLONG
855 \relates <QtGlobal>
856 \compat
857
858 Use \l qint64 instead.
859*/
860
861/*!
862 \typedef Q_ULLONG
863 \relates <QtGlobal>
864 \compat
865
866 Use \l quint64 instead.
867*/
868
869/*!
870 \typedef Q_LONG
871 \relates <QtGlobal>
872 \compat
873
874 Use \c{void *} instead.
875*/
876
877/*!
878 \typedef Q_ULONG
879 \relates <QtGlobal>
880 \compat
881
882 Use \c{void *} instead.
883*/
884
885/*! \fn bool qSysInfo(int *wordSize, bool *bigEndian)
886 \relates <QtGlobal>
887
888 Use QSysInfo::WordSize and QSysInfo::ByteOrder instead.
889*/
890
891/*!
892 \fn bool qt_winUnicode()
893 \relates <QtGlobal>
894
895 Use QSysInfo::WindowsVersion and QSysInfo::WV_DOS_based instead.
896
897 \sa QSysInfo
898*/
899
900/*!
901 \fn int qWinVersion()
902 \relates <QtGlobal>
903
904 Use QSysInfo::WindowsVersion instead.
905
906 \sa QSysInfo
907*/
908
909/*!
910 \fn int qMacVersion()
911 \relates <QtGlobal>
912
913 Use QSysInfo::MacintoshVersion instead.
914
915 \sa QSysInfo
916*/
917
918/*!
919 \macro QT_VERSION
920 \relates <QtGlobal>
921
922 This macro expands a numeric value of the form 0xMMNNPP (MM =
923 major, NN = minor, PP = patch) that specifies Qt's version
924 number. For example, if you compile your application against Qt
925 4.1.2, the QT_VERSION macro will expand to 0x040102.
926
927 You can use QT_VERSION to use the latest Qt features where
928 available. For example:
929
930 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 16
931
932 \sa QT_VERSION_STR, qVersion()
933*/
934
935/*!
936 \macro QT_VERSION_STR
937 \relates <QtGlobal>
938
939 This macro expands to a string that specifies Qt's version number
940 (for example, "4.1.2"). This is the version against which the
941 application is compiled.
942
943 \sa qVersion(), QT_VERSION
944*/
945
946/*!
947 \relates <QtGlobal>
948
949 Returns the version number of Qt at run-time as a string (for
950 example, "4.1.2"). This may be a different version than the
951 version the application was compiled against.
952
953 \sa QT_VERSION_STR
954*/
955
956const char *qVersion()
957{
958 return QT_VERSION_STR;
959}
960
961bool qSharedBuild()
962{
963#ifdef QT_SHARED
964 return true;
965#else
966 return false;
967#endif
968}
969
970/*****************************************************************************
971 System detection routines
972 *****************************************************************************/
973
974/*!
975 \class QSysInfo
976 \brief The QSysInfo class provides information about the system.
977
978 \list
979 \o \l WordSize specifies the size of a pointer for the platform
980 on which the application is compiled.
981 \o \l ByteOrder specifies whether the platform is big-endian or
982 little-endian.
983 \o \l WindowsVersion specifies the version of the Windows operating
984 system on which the application is run (Windows only)
985 \o \l MacintoshVersion specifies the version of the Macintosh
986 operating system on which the application is run (Mac only).
987 \endlist
988
989 Some constants are defined only on certain platforms. You can use
990 the preprocessor symbols Q_WS_WIN and Q_WS_MAC to test that
991 the application is compiled under Windows or Mac.
992
993 \sa QLibraryInfo
994*/
995
996/*!
997 \enum QSysInfo::Sizes
998
999 This enum provides platform-specific information about the sizes of data
1000 structures used by the underlying architecture.
1001
1002 \value WordSize The size in bits of a pointer for the platform on which
1003 the application is compiled (32 or 64).
1004*/
1005
1006/*!
1007 \variable QSysInfo::WindowsVersion
1008 \brief the version of the Windows operating system on which the
1009 application is run (Windows only)
1010*/
1011
1012/*!
1013 \fn QSysInfo::WindowsVersion QSysInfo::windowsVersion()
1014 \since 4.4
1015
1016 Returns the version of the Windows operating system on which the
1017 application is run (Windows only).
1018*/
1019
1020/*!
1021 \variable QSysInfo::MacintoshVersion
1022 \brief the version of the Macintosh operating system on which
1023 the application is run (Mac only).
1024*/
1025
1026/*!
1027 \enum QSysInfo::Endian
1028
1029 \value BigEndian Big-endian byte order (also called Network byte order)
1030 \value LittleEndian Little-endian byte order
1031 \value ByteOrder Equals BigEndian or LittleEndian, depending on
1032 the platform's byte order.
1033*/
1034
1035/*!
1036 \enum QSysInfo::WinVersion
1037
1038 This enum provides symbolic names for the various versions of the
1039 Windows operating system. On Windows, the
1040 QSysInfo::WindowsVersion variable gives the version of the system
1041 on which the application is run.
1042
1043 MS-DOS-based versions:
1044
1045 \value WV_32s Windows 3.1 with Win 32s
1046 \value WV_95 Windows 95
1047 \value WV_98 Windows 98
1048 \value WV_Me Windows Me
1049
1050 NT-based versions (note that each operating system version is only represented once rather than each Windows edition):
1051
1052 \value WV_NT Windows NT (operating system version 4.0)
1053 \value WV_2000 Windows 2000 (operating system version 5.0)
1054 \value WV_XP Windows XP (operating system version 5.1)
1055 \value WV_2003 Windows Server 2003, Windows Server 2003 R2, Windows Home Server, Windows XP Professional x64 Edition (operating system version 5.2)
1056 \value WV_VISTA Windows Vista, Windows Server 2008 (operating system version 6.0)
1057 \value WV_WINDOWS7 Windows 7 (operating system version 6.1)
1058
1059 Alternatively, you may use the following macros which correspond directly to the Windows operating system version number:
1060
1061 \value WV_4_0 Operating system version 4.0, corresponds to Windows NT
1062 \value WV_5_0 Operating system version 5.0, corresponds to Windows 2000
1063 \value WV_5_1 Operating system version 5.1, corresponds to Windows XP
1064 \value WV_5_2 Operating system version 5.2, corresponds to Windows Server 2003, Windows Server 2003 R2, Windows Home Server, and Windows XP Professional x64 Edition
1065 \value WV_6_0 Operating system version 6.0, corresponds to Windows Vista and Windows Server 2008
1066 \value WV_6_1 Operating system version 6.1, corresponds to Windows 7
1067
1068 CE-based versions:
1069
1070 \value WV_CE Windows CE
1071 \value WV_CENET Windows CE .NET
1072 \value WV_CE_5 Windows CE 5.x
1073 \value WV_CE_6 Windows CE 6.x
1074
1075 The following masks can be used for testing whether a Windows
1076 version is MS-DOS-based, NT-based, or CE-based:
1077
1078 \value WV_DOS_based MS-DOS-based version of Windows
1079 \value WV_NT_based NT-based version of Windows
1080 \value WV_CE_based CE-based version of Windows
1081
1082 \sa MacVersion
1083*/
1084
1085/*!
1086 \enum QSysInfo::MacVersion
1087
1088 This enum provides symbolic names for the various versions of the
1089 Macintosh operating system. On Mac, the
1090 QSysInfo::MacintoshVersion variable gives the version of the
1091 system on which the application is run.
1092
1093 \value MV_9 Mac OS 9 (unsupported)
1094 \value MV_10_0 Mac OS X 10.0 (unsupported)
1095 \value MV_10_1 Mac OS X 10.1 (unsupported)
1096 \value MV_10_2 Mac OS X 10.2 (unsupported)
1097 \value MV_10_3 Mac OS X 10.3
1098 \value MV_10_4 Mac OS X 10.4
1099 \value MV_10_5 Mac OS X 10.5
1100 \value MV_10_6 Mac OS X 10.6
1101 \value MV_Unknown An unknown and currently unsupported platform
1102
1103 \value MV_CHEETAH Apple codename for MV_10_0
1104 \value MV_PUMA Apple codename for MV_10_1
1105 \value MV_JAGUAR Apple codename for MV_10_2
1106 \value MV_PANTHER Apple codename for MV_10_3
1107 \value MV_TIGER Apple codename for MV_10_4
1108 \value MV_LEOPARD Apple codename for MV_10_5
1109 \value MV_SNOWLEOPARD Apple codename for MV_10_6
1110
1111 \sa WinVersion
1112*/
1113
1114/*!
1115 \macro Q_WS_MAC
1116 \relates <QtGlobal>
1117
1118 Defined on Mac OS X.
1119
1120 \sa Q_WS_WIN, Q_WS_X11, Q_WS_QWS
1121*/
1122
1123/*!
1124 \macro Q_WS_WIN
1125 \relates <QtGlobal>
1126
1127 Defined on Windows.
1128
1129 \sa Q_WS_MAC, Q_WS_X11, Q_WS_QWS
1130*/
1131
1132/*!
1133 \macro Q_WS_X11
1134 \relates <QtGlobal>
1135
1136 Defined on X11.
1137
1138 \sa Q_WS_MAC, Q_WS_WIN, Q_WS_QWS
1139*/
1140
1141/*!
1142 \macro Q_WS_QWS
1143 \relates <QtGlobal>
1144
1145 Defined on Qt for Embedded Linux.
1146
1147 \sa Q_WS_MAC, Q_WS_WIN, Q_WS_X11
1148*/
1149
1150/*!
1151 \macro Q_OS_DARWIN
1152 \relates <QtGlobal>
1153
1154 Defined on Darwin OS (synonym for Q_OS_MAC).
1155*/
1156
1157/*!
1158 \macro Q_OS_MSDOS
1159 \relates <QtGlobal>
1160
1161 Defined on MS-DOS and Windows.
1162*/
1163
1164/*!
1165 \macro Q_OS_OS2
1166 \relates <QtGlobal>
1167
1168 Defined on OS/2.
1169*/
1170
1171/*!
1172 \macro Q_OS_OS2EMX
1173 \relates <QtGlobal>
1174
1175 Defined on XFree86 on OS/2 (not PM).
1176*/
1177
1178/*!
1179 \macro Q_OS_WIN32
1180 \relates <QtGlobal>
1181
1182 Defined on all supported versions of Windows.
1183*/
1184
1185/*!
1186 \macro Q_OS_WINCE
1187 \relates <QtGlobal>
1188
1189 Defined on Windows CE.
1190*/
1191
1192/*!
1193 \macro Q_OS_CYGWIN
1194 \relates <QtGlobal>
1195
1196 Defined on Cygwin.
1197*/
1198
1199/*!
1200 \macro Q_OS_SOLARIS
1201 \relates <QtGlobal>
1202
1203 Defined on Sun Solaris.
1204*/
1205
1206/*!
1207 \macro Q_OS_HPUX
1208 \relates <QtGlobal>
1209
1210 Defined on HP-UX.
1211*/
1212
1213/*!
1214 \macro Q_OS_ULTRIX
1215 \relates <QtGlobal>
1216
1217 Defined on DEC Ultrix.
1218*/
1219
1220/*!
1221 \macro Q_OS_LINUX
1222 \relates <QtGlobal>
1223
1224 Defined on Linux.
1225*/
1226
1227/*!
1228 \macro Q_OS_FREEBSD
1229 \relates <QtGlobal>
1230
1231 Defined on FreeBSD.
1232*/
1233
1234/*!
1235 \macro Q_OS_NETBSD
1236 \relates <QtGlobal>
1237
1238 Defined on NetBSD.
1239*/
1240
1241/*!
1242 \macro Q_OS_OPENBSD
1243 \relates <QtGlobal>
1244
1245 Defined on OpenBSD.
1246*/
1247
1248/*!
1249 \macro Q_OS_BSDI
1250 \relates <QtGlobal>
1251
1252 Defined on BSD/OS.
1253*/
1254
1255/*!
1256 \macro Q_OS_IRIX
1257 \relates <QtGlobal>
1258
1259 Defined on SGI Irix.
1260*/
1261
1262/*!
1263 \macro Q_OS_OSF
1264 \relates <QtGlobal>
1265
1266 Defined on HP Tru64 UNIX.
1267*/
1268
1269/*!
1270 \macro Q_OS_SCO
1271 \relates <QtGlobal>
1272
1273 Defined on SCO OpenServer 5.
1274*/
1275
1276/*!
1277 \macro Q_OS_UNIXWARE
1278 \relates <QtGlobal>
1279
1280 Defined on UnixWare 7, Open UNIX 8.
1281*/
1282
1283/*!
1284 \macro Q_OS_AIX
1285 \relates <QtGlobal>
1286
1287 Defined on AIX.
1288*/
1289
1290/*!
1291 \macro Q_OS_HURD
1292 \relates <QtGlobal>
1293
1294 Defined on GNU Hurd.
1295*/
1296
1297/*!
1298 \macro Q_OS_DGUX
1299 \relates <QtGlobal>
1300
1301 Defined on DG/UX.
1302*/
1303
1304/*!
1305 \macro Q_OS_RELIANT
1306 \relates <QtGlobal>
1307
1308 Defined on Reliant UNIX.
1309*/
1310
1311/*!
1312 \macro Q_OS_DYNIX
1313 \relates <QtGlobal>
1314
1315 Defined on DYNIX/ptx.
1316*/
1317
1318/*!
1319 \macro Q_OS_QNX
1320 \relates <QtGlobal>
1321
1322 Defined on QNX.
1323*/
1324
1325/*!
1326 \macro Q_OS_QNX6
1327 \relates <QtGlobal>
1328
1329 Defined on QNX RTP 6.1.
1330*/
1331
1332/*!
1333 \macro Q_OS_LYNX
1334 \relates <QtGlobal>
1335
1336 Defined on LynxOS.
1337*/
1338
1339/*!
1340 \macro Q_OS_BSD4
1341 \relates <QtGlobal>
1342
1343 Defined on Any BSD 4.4 system.
1344*/
1345
1346/*!
1347 \macro Q_OS_UNIX
1348 \relates <QtGlobal>
1349
1350 Defined on Any UNIX BSD/SYSV system.
1351*/
1352
1353/*!
1354 \macro Q_CC_SYM
1355 \relates <QtGlobal>
1356
1357 Defined if the application is compiled using Digital Mars C/C++
1358 (used to be Symantec C++).
1359*/
1360
1361/*!
1362 \macro Q_CC_MWERKS
1363 \relates <QtGlobal>
1364
1365 Defined if the application is compiled using Metrowerks
1366 CodeWarrior.
1367*/
1368
1369/*!
1370 \macro Q_CC_MSVC
1371 \relates <QtGlobal>
1372
1373 Defined if the application is compiled using Microsoft Visual
1374 C/C++, Intel C++ for Windows.
1375*/
1376
1377/*!
1378 \macro Q_CC_BOR
1379 \relates <QtGlobal>
1380
1381 Defined if the application is compiled using Borland/Turbo C++.
1382*/
1383
1384/*!
1385 \macro Q_CC_WAT
1386 \relates <QtGlobal>
1387
1388 Defined if the application is compiled using Watcom C++.
1389*/
1390
1391/*!
1392 \macro Q_CC_GNU
1393 \relates <QtGlobal>
1394
1395 Defined if the application is compiled using GNU C++.
1396*/
1397
1398/*!
1399 \macro Q_CC_COMEAU
1400 \relates <QtGlobal>
1401
1402 Defined if the application is compiled using Comeau C++.
1403*/
1404
1405/*!
1406 \macro Q_CC_EDG
1407 \relates <QtGlobal>
1408
1409 Defined if the application is compiled using Edison Design Group
1410 C++.
1411*/
1412
1413/*!
1414 \macro Q_CC_OC
1415 \relates <QtGlobal>
1416
1417 Defined if the application is compiled using CenterLine C++.
1418*/
1419
1420/*!
1421 \macro Q_CC_SUN
1422 \relates <QtGlobal>
1423
1424 Defined if the application is compiled using Forte Developer, or
1425 Sun Studio C++.
1426*/
1427
1428/*!
1429 \macro Q_CC_MIPS
1430 \relates <QtGlobal>
1431
1432 Defined if the application is compiled using MIPSpro C++.
1433*/
1434
1435/*!
1436 \macro Q_CC_DEC
1437 \relates <QtGlobal>
1438
1439 Defined if the application is compiled using DEC C++.
1440*/
1441
1442/*!
1443 \macro Q_CC_HPACC
1444 \relates <QtGlobal>
1445
1446 Defined if the application is compiled using HP aC++.
1447*/
1448
1449/*!
1450 \macro Q_CC_USLC
1451 \relates <QtGlobal>
1452
1453 Defined if the application is compiled using SCO OUDK and UDK.
1454*/
1455
1456/*!
1457 \macro Q_CC_CDS
1458 \relates <QtGlobal>
1459
1460 Defined if the application is compiled using Reliant C++.
1461*/
1462
1463/*!
1464 \macro Q_CC_KAI
1465 \relates <QtGlobal>
1466
1467 Defined if the application is compiled using KAI C++.
1468*/
1469
1470/*!
1471 \macro Q_CC_INTEL
1472 \relates <QtGlobal>
1473
1474 Defined if the application is compiled using Intel C++ for Linux,
1475 Intel C++ for Windows.
1476*/
1477
1478/*!
1479 \macro Q_CC_HIGHC
1480 \relates <QtGlobal>
1481
1482 Defined if the application is compiled using MetaWare High C/C++.
1483*/
1484
1485/*!
1486 \macro Q_CC_PGI
1487 \relates <QtGlobal>
1488
1489 Defined if the application is compiled using Portland Group C++.
1490*/
1491
1492/*!
1493 \macro Q_CC_GHS
1494 \relates <QtGlobal>
1495
1496 Defined if the application is compiled using Green Hills
1497 Optimizing C++ Compilers.
1498*/
1499
1500#if defined(QT_BUILD_QMAKE)
1501// needed to bootstrap qmake
1502static const unsigned int qt_one = 1;
1503const int QSysInfo::ByteOrder = ((*((unsigned char *) &qt_one) == 0) ? BigEndian : LittleEndian);
1504#endif
1505
1506#if !defined(QWS) && defined(Q_OS_MAC)
1507
1508QT_BEGIN_INCLUDE_NAMESPACE
1509#include "private/qcore_mac_p.h"
1510#include "qnamespace.h"
1511QT_END_INCLUDE_NAMESPACE
1512
1513Q_CORE_EXPORT OSErr qt_mac_create_fsref(const QString &file, FSRef *fsref)
1514{
1515 return FSPathMakeRef(reinterpret_cast<const UInt8 *>(file.toUtf8().constData()), fsref, 0);
1516}
1517
1518// Don't use this function, it won't work in 10.5 (Leopard) and up
1519Q_CORE_EXPORT OSErr qt_mac_create_fsspec(const QString &file, FSSpec *spec)
1520{
1521 FSRef fsref;
1522 OSErr ret = qt_mac_create_fsref(file, &fsref);
1523 if (ret == noErr)
1524 ret = FSGetCatalogInfo(&fsref, kFSCatInfoNone, 0, 0, spec, 0);
1525 return ret;
1526}
1527
1528Q_CORE_EXPORT void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1)
1529{
1530 if(len == -1)
1531 len = s.length();
1532#if 0
1533 UnicodeMapping mapping;
1534 mapping.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
1535 kTextEncodingDefaultVariant,
1536 kUnicode16BitFormat);
1537 mapping.otherEncoding = (encoding ? encoding : );
1538 mapping.mappingVersion = kUnicodeUseLatestMapping;
1539
1540 UnicodeToTextInfo info;
1541 OSStatus err = CreateUnicodeToTextInfo(&mapping, &info);
1542 if(err != noErr) {
1543 qDebug("Qt: internal: Unable to create pascal string '%s'::%d [%ld]",
1544 s.left(len).latin1(), (int)encoding, err);
1545 return;
1546 }
1547 const int unilen = len * 2;
1548 const UniChar *unibuf = (UniChar *)s.unicode();
1549 ConvertFromUnicodeToPString(info, unilen, unibuf, str);
1550 DisposeUnicodeToTextInfo(&info);
1551#else
1552 Q_UNUSED(encoding);
1553 CFStringGetPascalString(QCFString(s), str, 256, CFStringGetSystemEncoding());
1554#endif
1555}
1556
1557Q_CORE_EXPORT QString qt_mac_from_pascal_string(const Str255 pstr) {
1558 return QCFString(CFStringCreateWithPascalString(0, pstr, CFStringGetSystemEncoding()));
1559}
1560
1561
1562
1563static QSysInfo::MacVersion macVersion()
1564{
1565 SInt32 gestalt_version;
1566 if (Gestalt(gestaltSystemVersion, &gestalt_version) == noErr) {
1567 return QSysInfo::MacVersion(((gestalt_version & 0x00F0) >> 4) + 2);
1568 }
1569 return QSysInfo::MV_Unknown;
1570}
1571const QSysInfo::MacVersion QSysInfo::MacintoshVersion = macVersion();
1572
1573#elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_WINCE)
1574
1575QT_BEGIN_INCLUDE_NAMESPACE
1576#include "qt_windows.h"
1577QT_END_INCLUDE_NAMESPACE
1578
1579QSysInfo::WinVersion QSysInfo::windowsVersion()
1580{
1581#ifndef VER_PLATFORM_WIN32s
1582#define VER_PLATFORM_WIN32s 0
1583#endif
1584#ifndef VER_PLATFORM_WIN32_WINDOWS
1585#define VER_PLATFORM_WIN32_WINDOWS 1
1586#endif
1587#ifndef VER_PLATFORM_WIN32_NT
1588#define VER_PLATFORM_WIN32_NT 2
1589#endif
1590#ifndef VER_PLATFORM_WIN32_CE
1591#define VER_PLATFORM_WIN32_CE 3
1592#endif
1593
1594 static QSysInfo::WinVersion winver;
1595 if (winver)
1596 return winver;
1597 winver = QSysInfo::WV_NT;
1598#ifndef Q_OS_WINCE
1599 OSVERSIONINFOA osver;
1600 osver.dwOSVersionInfoSize = sizeof(osver);
1601 GetVersionExA(&osver);
1602#else
1603 DWORD qt_cever = 0;
1604 OSVERSIONINFOW osver;
1605 osver.dwOSVersionInfoSize = sizeof(osver);
1606 GetVersionEx(&osver);
1607 qt_cever = osver.dwMajorVersion * 100;
1608 qt_cever += osver.dwMinorVersion * 10;
1609#endif
1610 switch (osver.dwPlatformId) {
1611 case VER_PLATFORM_WIN32s:
1612 winver = QSysInfo::WV_32s;
1613 break;
1614 case VER_PLATFORM_WIN32_WINDOWS:
1615 // We treat Windows Me (minor 90) the same as Windows 98
1616 if (osver.dwMinorVersion == 90)
1617 winver = QSysInfo::WV_Me;
1618 else if (osver.dwMinorVersion == 10)
1619 winver = QSysInfo::WV_98;
1620 else
1621 winver = QSysInfo::WV_95;
1622 break;
1623#ifdef Q_OS_WINCE
1624 case VER_PLATFORM_WIN32_CE:
1625 if (qt_cever >= 600)
1626 winver = QSysInfo::WV_CE_6;
1627 if (qt_cever >= 500)
1628 winver = QSysInfo::WV_CE_5;
1629 else if (qt_cever >= 400)
1630 winver = QSysInfo::WV_CENET;
1631 else
1632 winver = QSysInfo::WV_CE;
1633 break;
1634#endif
1635 default: // VER_PLATFORM_WIN32_NT
1636 if (osver.dwMajorVersion < 5) {
1637 winver = QSysInfo::WV_NT;
1638 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0) {
1639 winver = QSysInfo::WV_2000;
1640 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1) {
1641 winver = QSysInfo::WV_XP;
1642 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2) {
1643 winver = QSysInfo::WV_2003;
1644 } else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0) {
1645 winver = QSysInfo::WV_VISTA;
1646 } else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1) {
1647 winver = QSysInfo::WV_WINDOWS7;
1648 } else {
1649 qWarning("Qt: Untested Windows version %d.%d detected!",
1650 osver.dwMajorVersion, osver.dwMinorVersion);
1651 winver = QSysInfo::WV_NT_based;
1652 }
1653 }
1654
1655#ifdef QT_DEBUG
1656 {
1657 QByteArray override = qgetenv("QT_WINVER_OVERRIDE");
1658 if (override.isEmpty())
1659 return winver;
1660
1661 if (override == "Me")
1662 winver = QSysInfo::WV_Me;
1663 if (override == "95")
1664 winver = QSysInfo::WV_95;
1665 else if (override == "98")
1666 winver = QSysInfo::WV_98;
1667 else if (override == "NT")
1668 winver = QSysInfo::WV_NT;
1669 else if (override == "2000")
1670 winver = QSysInfo::WV_2000;
1671 else if (override == "2003")
1672 winver = QSysInfo::WV_2003;
1673 else if (override == "XP")
1674 winver = QSysInfo::WV_XP;
1675 else if (override == "VISTA")
1676 winver = QSysInfo::WV_VISTA;
1677 else if (override == "WINDOWS7")
1678 winver = QSysInfo::WV_WINDOWS7;
1679 }
1680#endif
1681
1682 return winver;
1683}
1684
1685const QSysInfo::WinVersion QSysInfo::WindowsVersion = QSysInfo::windowsVersion();
1686
1687#endif
1688
1689/*!
1690 \macro void Q_ASSERT(bool test)
1691 \relates <QtGlobal>
1692
1693 Prints a warning message containing the source code file name and
1694 line number if \a test is false.
1695
1696 Q_ASSERT() is useful for testing pre- and post-conditions
1697 during development. It does nothing if \c QT_NO_DEBUG was defined
1698 during compilation.
1699
1700 Example:
1701
1702 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 17
1703
1704 If \c b is zero, the Q_ASSERT statement will output the following
1705 message using the qFatal() function:
1706
1707 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 18
1708
1709 \sa Q_ASSERT_X(), qFatal(), {Debugging Techniques}
1710*/
1711
1712/*!
1713 \macro void Q_ASSERT_X(bool test, const char *where, const char *what)
1714 \relates <QtGlobal>
1715
1716 Prints the message \a what together with the location \a where,
1717 the source file name and line number if \a test is false.
1718
1719 Q_ASSERT_X is useful for testing pre- and post-conditions during
1720 development. It does nothing if \c QT_NO_DEBUG was defined during
1721 compilation.
1722
1723 Example:
1724
1725 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 19
1726
1727 If \c b is zero, the Q_ASSERT_X statement will output the following
1728 message using the qFatal() function:
1729
1730 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 20
1731
1732 \sa Q_ASSERT(), qFatal(), {Debugging Techniques}
1733*/
1734
1735/*!
1736 \macro void Q_CHECK_PTR(void *pointer)
1737 \relates <QtGlobal>
1738
1739 If \a pointer is 0, prints a warning message containing the source
1740 code's file name and line number, saying that the program ran out
1741 of memory.
1742
1743 Q_CHECK_PTR does nothing if \c QT_NO_DEBUG was defined during
1744 compilation.
1745
1746 Example:
1747
1748 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 21
1749
1750 \sa qWarning(), {Debugging Techniques}
1751*/
1752
1753/*!
1754 \macro const char* Q_FUNC_INFO()
1755 \relates <QtGlobal>
1756
1757 Expands to a string that describe the function the macro resides in. How this string looks
1758 more specifically is compiler dependent. With GNU GCC it is typically the function signature,
1759 while with other compilers it might be the line and column number.
1760
1761 Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
1762
1763 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 22
1764
1765 when instantiated with the integer type, will with the GCC compiler produce:
1766
1767 \tt{const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4}
1768
1769 If this macro is used outside a function, the behavior is undefined.
1770 */
1771
1772/*
1773 The Q_CHECK_PTR macro calls this function if an allocation check
1774 fails.
1775*/
1776void qt_check_pointer(const char *n, int l)
1777{
1778 qWarning("In file %s, line %d: Out of memory", n, l);
1779}
1780
1781/*
1782 The Q_ASSERT macro calls this function when the test fails.
1783*/
1784void qt_assert(const char *assertion, const char *file, int line)
1785{
1786 qFatal("ASSERT: \"%s\" in file %s, line %d", assertion, file, line);
1787}
1788
1789/*
1790 The Q_ASSERT_X macro calls this function when the test fails.
1791*/
1792void qt_assert_x(const char *where, const char *what, const char *file, int line)
1793{
1794 qFatal("ASSERT failure in %s: \"%s\", file %s, line %d", where, what, file, line);
1795}
1796
1797
1798/*
1799 Dijkstra's bisection algorithm to find the square root of an integer.
1800 Deliberately not exported as part of the Qt API, but used in both
1801 qsimplerichtext.cpp and qgfxraster_qws.cpp
1802*/
1803Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n)
1804{
1805 // n must be in the range 0...UINT_MAX/2-1
1806 if (n >= (UINT_MAX>>2)) {
1807 unsigned int r = 2 * qt_int_sqrt(n / 4);
1808 unsigned int r2 = r + 1;
1809 return (n >= r2 * r2) ? r2 : r;
1810 }
1811 uint h, p= 0, q= 1, r= n;
1812 while (q <= n)
1813 q <<= 2;
1814 while (q != 1) {
1815 q >>= 2;
1816 h= p + q;
1817 p >>= 1;
1818 if (r >= h) {
1819 p += q;
1820 r -= h;
1821 }
1822 }
1823 return p;
1824}
1825
1826#if defined(qMemCopy)
1827# undef qMemCopy
1828#endif
1829#if defined(qMemSet)
1830# undef qMemSet
1831#endif
1832
1833void *qMemCopy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); }
1834void *qMemSet(void *dest, int c, size_t n) { return memset(dest, c, n); }
1835
1836static QtMsgHandler handler = 0; // pointer to debug handler
1837
1838#ifdef Q_CC_MWERKS
1839extern bool qt_is_gui_used;
1840static void mac_default_handler(const char *msg)
1841{
1842 if (qt_is_gui_used) {
1843 Str255 pmsg;
1844 qt_mac_to_pascal_string(msg, pmsg);
1845 DebugStr(pmsg);
1846 } else {
1847 fprintf(stderr, msg);
1848 }
1849}
1850#endif // Q_CC_MWERKS
1851
1852
1853
1854QString qt_error_string(int errorCode)
1855{
1856 const char *s = 0;
1857 QString ret;
1858 if (errorCode == -1) {
1859#if defined(Q_OS_WIN)
1860 errorCode = GetLastError();
1861#else
1862 errorCode = errno;
1863#endif
1864 }
1865 switch (errorCode) {
1866 case 0:
1867 break;
1868 case EACCES:
1869 s = QT_TRANSLATE_NOOP("QIODevice", "Permission denied");
1870 break;
1871 case EMFILE:
1872 s = QT_TRANSLATE_NOOP("QIODevice", "Too many open files");
1873 break;
1874 case ENOENT:
1875 s = QT_TRANSLATE_NOOP("QIODevice", "No such file or directory");
1876 break;
1877 case ENOSPC:
1878 s = QT_TRANSLATE_NOOP("QIODevice", "No space left on device");
1879 break;
1880 default: {
1881#ifdef Q_OS_WIN
1882 QT_WA({
1883 unsigned short *string = 0;
1884 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1885 NULL,
1886 errorCode,
1887 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1888 (LPTSTR)&string,
1889 0,
1890 NULL);
1891 ret = QString::fromUtf16(string);
1892 LocalFree((HLOCAL)string);
1893 }, {
1894 char *string = 0;
1895 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
1896 NULL,
1897 errorCode,
1898 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1899 (LPSTR)&string,
1900 0,
1901 NULL);
1902 ret = QString::fromLocal8Bit(string);
1903 LocalFree((HLOCAL)string);
1904 });
1905
1906 if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
1907 ret = QString::fromLatin1("The specified module could not be found.");
1908
1909#elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L && !defined(Q_OS_INTEGRITY)
1910
1911 QByteArray buf(1024, '\0');
1912 strerror_r(errorCode, buf.data(), buf.size());
1913 ret = QString::fromLocal8Bit(buf.constData());
1914#else
1915 ret = QString::fromLocal8Bit(strerror(errorCode));
1916#endif
1917 break; }
1918 }
1919 if (s)
1920 // ######## this breaks moc build currently
1921// ret = QCoreApplication::translate("QIODevice", s);
1922 ret = QString::fromLatin1(s);
1923 return ret.trimmed();
1924}
1925
1926
1927/*!
1928 \fn QtMsgHandler qInstallMsgHandler(QtMsgHandler handler)
1929 \relates <QtGlobal>
1930
1931 Installs a Qt message \a handler which has been defined
1932 previously. Returns a pointer to the previous message handler
1933 (which may be 0).
1934
1935 The message handler is a function that prints out debug messages,
1936 warnings, critical and fatal error messages. The Qt library (debug
1937 mode) contains hundreds of warning messages that are printed
1938 when internal errors (usually invalid function arguments)
1939 occur. Qt built in release mode also contains such warnings unless
1940 QT_NO_WARNING_OUTPUT and/or QT_NO_DEBUG_OUTPUT have been set during
1941 compilation. If you implement your own message handler, you get total
1942 control of these messages.
1943
1944 The default message handler prints the message to the standard
1945 output under X11 or to the debugger under Windows. If it is a
1946 fatal message, the application aborts immediately.
1947
1948 Only one message handler can be defined, since this is usually
1949 done on an application-wide basis to control debug output.
1950
1951 To restore the message handler, call \c qInstallMsgHandler(0).
1952
1953 Example:
1954
1955 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 23
1956
1957 \sa qDebug(), qWarning(), qCritical(), qFatal(), QtMsgType,
1958 {Debugging Techniques}
1959*/
1960#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB)
1961extern bool usingWinMain;
1962extern Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char* str);
1963#endif
1964
1965QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
1966{
1967 QtMsgHandler old = handler;
1968 handler = h;
1969#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB)
1970 if (!handler && usingWinMain)
1971 handler = qWinMsgHandler;
1972#endif
1973 return old;
1974}
1975
1976/*!
1977 \internal
1978*/
1979void qt_message_output(QtMsgType msgType, const char *buf)
1980{
1981 if (handler) {
1982 (*handler)(msgType, buf);
1983 } else {
1984#if defined(Q_CC_MWERKS)
1985 mac_default_handler(buf);
1986#elif defined(Q_OS_WINCE)
1987 QString fstr = QString::fromLatin1(buf);
1988 fstr += QLatin1String("\n");
1989 OutputDebugString(reinterpret_cast<const wchar_t *> (fstr.utf16()));
1990#else
1991 fprintf(stderr, "%s\n", buf);
1992 fflush(stderr);
1993#endif
1994 }
1995
1996 if (msgType == QtFatalMsg
1997 || (msgType == QtWarningMsg
1998 && (!qgetenv("QT_FATAL_WARNINGS").isNull())) ) {
1999
2000#if defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
2001 // get the current report mode
2002 int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
2003 _CrtSetReportMode(_CRT_ERROR, reportMode);
2004#if !defined(Q_OS_WINCE)
2005 int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf);
2006#else
2007 int ret = _CrtDbgReportW(_CRT_ERROR, _CRT_WIDE(__FILE__),
2008 __LINE__, _CRT_WIDE(QT_VERSION_STR), reinterpret_cast<const wchar_t *> (QString::fromLatin1(buf).utf16()));
2009#endif
2010 if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW)
2011 return; // ignore
2012 else if (ret == 1)
2013 _CrtDbgBreak();
2014#endif
2015
2016#if (defined(Q_OS_UNIX) || defined(Q_CC_MINGW))
2017 abort(); // trap; generates core dump
2018#else
2019 exit(1); // goodbye cruel world
2020#endif
2021 }
2022}
2023
2024#undef qDebug
2025/*!
2026 \relates <QtGlobal>
2027
2028 Calls the message handler with the debug message \a msg. If no
2029 message handler has been installed, the message is printed to
2030 stderr. Under Windows, the message is sent to the console, if it is a
2031 console application; otherwise, it is sent to the debugger. This
2032 function does nothing if \c QT_NO_DEBUG_OUTPUT was defined
2033 during compilation.
2034
2035 If you pass the function a format string and a list of arguments,
2036 it works in similar way to the C printf() function. The format
2037 should be a Latin-1 string.
2038
2039 Example:
2040
2041 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 24
2042
2043 If you include \c <QtDebug>, a more convenient syntax is also
2044 available:
2045
2046 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 25
2047
2048 With this syntax, the function returns a QDebug object that is
2049 configured to use the QtDebugMsg message type. It automatically
2050 puts a single space between each item, and outputs a newline at
2051 the end. It supports many C++ and Qt types.
2052
2053 To suppress the output at run-time, install your own message handler
2054 with qInstallMsgHandler().
2055
2056 \sa qWarning(), qCritical(), qFatal(), qInstallMsgHandler(),
2057 {Debugging Techniques}
2058*/
2059void qDebug(const char *msg, ...)
2060{
2061 QString buf;
2062 va_list ap;
2063 va_start(ap, msg); // use variable arg list
2064 if (msg)
2065 buf.vsprintf(msg, ap);
2066 va_end(ap);
2067
2068 qt_message_output(QtDebugMsg, buf.toLocal8Bit().constData());
2069}
2070
2071#undef qWarning
2072/*!
2073 \relates <QtGlobal>
2074
2075 Calls the message handler with the warning message \a msg. If no
2076 message handler has been installed, the message is printed to
2077 stderr. Under Windows, the message is sent to the debugger. This
2078 function does nothing if \c QT_NO_WARNING_OUTPUT was defined
2079 during compilation; it exits if the environment variable \c
2080 QT_FATAL_WARNINGS is defined.
2081
2082 This function takes a format string and a list of arguments,
2083 similar to the C printf() function. The format should be a Latin-1
2084 string.
2085
2086 Example:
2087 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 26
2088
2089 If you include <QtDebug>, a more convenient syntax is
2090 also available:
2091
2092 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 27
2093
2094 This syntax inserts a space between each item, and
2095 appends a newline at the end.
2096
2097 To supress the output at runtime, install your own message handler
2098 with qInstallMsgHandler().
2099
2100 \sa qDebug(), qCritical(), qFatal(), qInstallMsgHandler(),
2101 {Debugging Techniques}
2102*/
2103void qWarning(const char *msg, ...)
2104{
2105 QString buf;
2106 va_list ap;
2107 va_start(ap, msg); // use variable arg list
2108 if (msg)
2109 buf.vsprintf(msg, ap);
2110 va_end(ap);
2111
2112 qt_message_output(QtWarningMsg, buf.toLocal8Bit().constData());
2113}
2114
2115/*!
2116 \relates <QtGlobal>
2117
2118 Calls the message handler with the critical message \a msg. If no
2119 message handler has been installed, the message is printed to
2120 stderr. Under Windows, the message is sent to the debugger.
2121
2122 This function takes a format string and a list of arguments,
2123 similar to the C printf() function. The format should be a Latin-1
2124 string.
2125
2126 Example:
2127 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 28
2128
2129 If you include <QtDebug>, a more convenient syntax is
2130 also available:
2131
2132 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 29
2133
2134 A space is inserted between the items, and a newline is
2135 appended at the end.
2136
2137 To supress the output at runtime, install your own message handler
2138 with qInstallMsgHandler().
2139
2140 \sa qDebug(), qWarning(), qFatal(), qInstallMsgHandler(),
2141 {Debugging Techniques}
2142*/
2143void qCritical(const char *msg, ...)
2144{
2145 QString buf;
2146 va_list ap;
2147 va_start(ap, msg); // use variable arg list
2148 if (msg)
2149 buf.vsprintf(msg, ap);
2150 va_end(ap);
2151
2152 qt_message_output(QtCriticalMsg, buf.toLocal8Bit().constData());
2153}
2154#ifdef QT3_SUPPORT
2155void qSystemWarning(const char *msg, int code)
2156 { qCritical("%s (%s)", msg, qt_error_string(code).toLocal8Bit().constData()); }
2157#endif // QT3_SUPPORT
2158
2159void qErrnoWarning(const char *msg, ...)
2160{
2161 QString buf;
2162 va_list ap;
2163 va_start(ap, msg);
2164 if (msg)
2165 buf.vsprintf(msg, ap);
2166 va_end(ap);
2167
2168 qCritical("%s (%s)", buf.toLocal8Bit().constData(), qt_error_string(-1).toLocal8Bit().constData());
2169}
2170
2171void qErrnoWarning(int code, const char *msg, ...)
2172{
2173 QString buf;
2174 va_list ap;
2175 va_start(ap, msg);
2176 if (msg)
2177 buf.vsprintf(msg, ap);
2178 va_end(ap);
2179
2180 qCritical("%s (%s)", buf.toLocal8Bit().constData(), qt_error_string(code).toLocal8Bit().constData());
2181}
2182
2183/*!
2184 \relates <QtGlobal>
2185
2186 Calls the message handler with the fatal message \a msg. If no
2187 message handler has been installed, the message is printed to
2188 stderr. Under Windows, the message is sent to the debugger.
2189
2190 If you are using the \bold{default message handler} this function will
2191 abort on Unix systems to create a core dump. On Windows, for debug builds,
2192 this function will report a _CRT_ERROR enabling you to connect a debugger
2193 to the application.
2194
2195 This function takes a format string and a list of arguments,
2196 similar to the C printf() function.
2197
2198 Example:
2199 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 30
2200
2201 To supress the output at runtime, install your own message handler
2202 with qInstallMsgHandler().
2203
2204 \sa qDebug(), qCritical(), qWarning(), qInstallMsgHandler(),
2205 {Debugging Techniques}
2206*/
2207void qFatal(const char *msg, ...)
2208{
2209 QString buf;
2210 va_list ap;
2211 va_start(ap, msg); // use variable arg list
2212 if (msg)
2213 buf.vsprintf(msg, ap);
2214 va_end(ap);
2215
2216 qt_message_output(QtFatalMsg, buf.toLocal8Bit().constData());
2217}
2218
2219// getenv is declared as deprecated in VS2005. This function
2220// makes use of the new secure getenv function.
2221QByteArray qgetenv(const char *varName)
2222{
2223#if defined(_MSC_VER) && _MSC_VER >= 1400
2224 size_t requiredSize = 0;
2225 QByteArray buffer;
2226 getenv_s(&requiredSize, 0, 0, varName);
2227 if (requiredSize == 0)
2228 return buffer;
2229 buffer.resize(int(requiredSize));
2230 getenv_s(&requiredSize, buffer.data(), requiredSize, varName);
2231 // requiredSize includes the terminating null, which we don't want.
2232 Q_ASSERT(buffer.endsWith('\0'));
2233 buffer.chop(1);
2234 return buffer;
2235#else
2236 return QByteArray(::getenv(varName));
2237#endif
2238}
2239
2240bool qputenv(const char *varName, const QByteArray& value)
2241{
2242#if defined(_MSC_VER) && _MSC_VER >= 1400
2243 return _putenv_s(varName, value.constData()) == 0;
2244#else
2245 QByteArray buffer(varName);
2246 buffer += "=";
2247 buffer += value;
2248 return putenv(qstrdup(buffer.constData())) == 0;
2249#endif
2250}
2251
2252#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD)
2253
2254# if defined(Q_OS_INTEGRITY) && defined(__GHS_VERSION_NUMBER) && (__GHS_VERSION_NUMBER < 500)
2255// older versions of INTEGRITY used a long instead of a uint for the seed.
2256typedef long SeedStorageType;
2257# else
2258typedef uint SeedStorageType;
2259# endif
2260
2261typedef QThreadStorage<SeedStorageType *> SeedStorage;
2262Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value
2263
2264#endif
2265
2266/*!
2267 \relates <QtGlobal>
2268 \since 4.2
2269
2270 Thread-safe version of the standard C++ \c srand() function.
2271
2272 Sets the argument \a seed to be used to generate a new random number sequence of
2273 pseudo random integers to be returned by qrand().
2274
2275 If no seed value is provided, qrand() is automatically seeded with a value of 1.
2276
2277 The sequence of random numbers generated is deterministic per thread. For example,
2278 if two threads call qsrand(1) and subsequently calls qrand(), the threads will get
2279 the same random number sequence.
2280
2281 \sa qrand()
2282*/
2283void qsrand(uint seed)
2284{
2285#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD)
2286 SeedStorageType *pseed = randTLS()->localData();
2287 if (!pseed)
2288 randTLS()->setLocalData(pseed = new SeedStorageType);
2289 *pseed = seed;
2290#else
2291 // On Windows srand() and rand() already use Thread-Local-Storage
2292 // to store the seed between calls
2293 srand(seed);
2294#endif
2295}
2296
2297/*!
2298 \relates <QtGlobal>
2299 \since 4.2
2300
2301 Thread-safe version of the standard C++ \c rand() function.
2302
2303 Returns a value between 0 and \c RAND_MAX (defined in \c <cstdlib> and
2304 \c <stdlib.h>), the next number in the current sequence of pseudo-random
2305 integers.
2306
2307 Use \c qsrand() to initialize the pseudo-random number generator with
2308 a seed value.
2309
2310 \sa qsrand()
2311*/
2312int qrand()
2313{
2314#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD)
2315 SeedStorageType *pseed = randTLS()->localData();
2316 if (!pseed) {
2317 randTLS()->setLocalData(pseed = new SeedStorageType);
2318 *pseed = 1;
2319 }
2320 return rand_r(pseed);
2321#else
2322 // On Windows srand() and rand() already use Thread-Local-Storage
2323 // to store the seed between calls
2324 return rand();
2325#endif
2326}
2327
2328/*!
2329 \macro forever
2330 \relates <QtGlobal>
2331
2332 This macro is provided for convenience for writing infinite
2333 loops.
2334
2335 Example:
2336
2337 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 31
2338
2339 It is equivalent to \c{for (;;)}.
2340
2341 If you're worried about namespace pollution, you can disable this
2342 macro by adding the following line to your \c .pro file:
2343
2344 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 32
2345
2346 \sa Q_FOREVER
2347*/
2348
2349/*!
2350 \macro Q_FOREVER
2351 \relates <QtGlobal>
2352
2353 Same as \l{forever}.
2354
2355 This macro is available even when \c no_keywords is specified
2356 using the \c .pro file's \c CONFIG variable.
2357
2358 \sa foreach()
2359*/
2360
2361/*!
2362 \macro foreach(variable, container)
2363 \relates <QtGlobal>
2364
2365 This macro is used to implement Qt's \c foreach loop. The \a
2366 variable parameter is a variable name or variable definition; the
2367 \a container parameter is a Qt container whose value type
2368 corresponds to the type of the variable. See \l{The foreach
2369 Keyword} for details.
2370
2371 If you're worried about namespace pollution, you can disable this
2372 macro by adding the following line to your \c .pro file:
2373
2374 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 33
2375
2376 \sa Q_FOREACH()
2377*/
2378
2379/*!
2380 \macro Q_FOREACH(variable, container)
2381 \relates <QtGlobal>
2382
2383 Same as foreach(\a variable, \a container).
2384
2385 This macro is available even when \c no_keywords is specified
2386 using the \c .pro file's \c CONFIG variable.
2387
2388 \sa foreach()
2389*/
2390
2391/*!
2392 \macro QT_TR_NOOP(sourceText)
2393 \relates <QtGlobal>
2394
2395 Marks the string literal \a sourceText for dynamic translation in
2396 the current context (class), i.e the stored \a sourceText will not
2397 be altered.
2398
2399 The macro expands to \a sourceText.
2400
2401 Example:
2402
2403 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 34
2404
2405 The macro QT_TR_NOOP_UTF8() is identical except that it tells lupdate
2406 that the source string is encoded in UTF-8. Corresponding variants
2407 exist in the QT_TRANSLATE_NOOP() family of macros, too. Note that
2408 using these macros is not required if \c CODECFORTR is already set to
2409 UTF-8 in the qmake project file.
2410
2411 \sa QT_TRANSLATE_NOOP(), {Internationalization with Qt}
2412*/
2413
2414/*!
2415 \macro QT_TRANSLATE_NOOP(context, sourceText)
2416 \relates <QtGlobal>
2417
2418 Marks the string literal \a sourceText for dynamic translation in
2419 the given \a context, i.e the stored \a sourceText will not be
2420 altered. The \a context is typically a class and also needs to
2421 be specified as string literal.
2422
2423 The macro expands to \a sourceText.
2424
2425 Example:
2426
2427 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 35
2428
2429 \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP3(), {Internationalization with Qt}
2430*/
2431
2432/*!
2433 \macro QT_TRANSLATE_NOOP3(context, sourceText, comment)
2434 \relates <QtGlobal>
2435 \since 4.4
2436
2437 Marks the string literal \a sourceText for dynamic translation in the
2438 given \a context and with \a comment, i.e the stored \a sourceText will
2439 not be altered. The \a context is typically a class and also needs to
2440 be specified as string literal. The string literal \a comment
2441 will be available for translators using e.g. Qt Linguist.
2442
2443 The macro expands to anonymous struct of the two string
2444 literals passed as \a sourceText and \a comment.
2445
2446 Example:
2447
2448 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 36
2449
2450 \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), {Internationalization with Qt}
2451*/
2452
2453/*!
2454 \macro QT_POINTER_SIZE
2455 \relates <QtGlobal>
2456
2457 Expands to the size of a pointer in bytes (4 or 8). This is
2458 equivalent to \c sizeof(void *) but can be used in a preprocessor
2459 directive.
2460*/
2461
2462/*!
2463 \macro TRUE
2464 \relates <QtGlobal>
2465 \obsolete
2466
2467 Synonym for \c true.
2468
2469 \sa FALSE
2470*/
2471
2472/*!
2473 \macro FALSE
2474 \relates <QtGlobal>
2475 \obsolete
2476
2477 Synonym for \c false.
2478
2479 \sa TRUE
2480*/
2481
2482/*!
2483 \macro QABS(n)
2484 \relates <QtGlobal>
2485 \obsolete
2486
2487 Use qAbs(\a n) instead.
2488
2489 \sa QMIN(), QMAX()
2490*/
2491
2492/*!
2493 \macro QMIN(x, y)
2494 \relates <QtGlobal>
2495 \obsolete
2496
2497 Use qMin(\a x, \a y) instead.
2498
2499 \sa QMAX(), QABS()
2500*/
2501
2502/*!
2503 \macro QMAX(x, y)
2504 \relates <QtGlobal>
2505 \obsolete
2506
2507 Use qMax(\a x, \a y) instead.
2508
2509 \sa QMIN(), QABS()
2510*/
2511
2512/*!
2513 \macro const char *qPrintable(const QString &str)
2514 \relates <QtGlobal>
2515
2516 Returns \a str as a \c{const char *}. This is equivalent to
2517 \a{str}.toLocal8Bit().constData().
2518
2519 The char pointer will be invalid after the statement in which
2520 qPrintable() is used. This is because the array returned by
2521 toLocal8Bit() will fall out of scope.
2522
2523 Example:
2524
2525 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 37
2526
2527
2528 \sa qDebug(), qWarning(), qCritical(), qFatal()
2529*/
2530
2531/*!
2532 \macro Q_DECLARE_TYPEINFO(Type, Flags)
2533 \relates <QtGlobal>
2534
2535 You can use this macro to specify information about a custom type
2536 \a Type. With accurate type information, Qt's \l{generic
2537 containers} can choose appropriate storage methods and algorithms.
2538
2539 \a Flags can be one of the following:
2540
2541 \list
2542 \o \c Q_PRIMITIVE_TYPE specifies that \a Type is a POD (plain old
2543 data) type with no constructor or destructor.
2544 \o \c Q_MOVABLE_TYPE specifies that \a Type has a constructor
2545 and/or a destructor but can be moved in memory using \c
2546 memcpy().
2547 \o \c Q_COMPLEX_TYPE (the default) specifies that \a Type has
2548 constructors and/or a destructor and that it may not be moved
2549 in memory.
2550 \endlist
2551
2552 Example of a "primitive" type:
2553
2554 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 38
2555
2556 Example of a movable type:
2557
2558 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 39
2559*/
2560
2561/*!
2562 \macro Q_UNUSED(name)
2563 \relates <QtGlobal>
2564
2565 Indicates to the compiler that the parameter with the specified
2566 \a name is not used in the body of a function. This can be used to
2567 suppress compiler warnings while allowing functions to be defined
2568 with meaningful parameter names in their signatures.
2569*/
2570
2571#if defined(QT3_SUPPORT) && !defined(QT_NO_SETTINGS)
2572QT_BEGIN_INCLUDE_NAMESPACE
2573#include <qlibraryinfo.h>
2574QT_END_INCLUDE_NAMESPACE
2575
2576static const char *qInstallLocation(QLibraryInfo::LibraryLocation loc)
2577{
2578 static QByteArray ret;
2579 ret = QLibraryInfo::location(loc).toLatin1();
2580 return ret.constData();
2581}
2582const char *qInstallPath()
2583{
2584 return qInstallLocation(QLibraryInfo::PrefixPath);
2585}
2586const char *qInstallPathDocs()
2587{
2588 return qInstallLocation(QLibraryInfo::DocumentationPath);
2589}
2590const char *qInstallPathHeaders()
2591{
2592 return qInstallLocation(QLibraryInfo::HeadersPath);
2593}
2594const char *qInstallPathLibs()
2595{
2596 return qInstallLocation(QLibraryInfo::LibrariesPath);
2597}
2598const char *qInstallPathBins()
2599{
2600 return qInstallLocation(QLibraryInfo::BinariesPath);
2601}
2602const char *qInstallPathPlugins()
2603{
2604 return qInstallLocation(QLibraryInfo::PluginsPath);
2605}
2606const char *qInstallPathData()
2607{
2608 return qInstallLocation(QLibraryInfo::DataPath);
2609}
2610const char *qInstallPathTranslations()
2611{
2612 return qInstallLocation(QLibraryInfo::TranslationsPath);
2613}
2614const char *qInstallPathSysconf()
2615{
2616 return qInstallLocation(QLibraryInfo::SettingsPath);
2617}
2618#endif
2619
2620struct QInternal_CallBackTable {
2621 QVector<QList<qInternalCallback> > callbacks;
2622};
2623
2624Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
2625
2626bool QInternal::registerCallback(Callback cb, qInternalCallback callback)
2627{
2628 if (cb >= 0 && cb < QInternal::LastCallback) {
2629 QInternal_CallBackTable *cbt = global_callback_table();
2630 cbt->callbacks.resize(cb + 1);
2631 cbt->callbacks[cb].append(callback);
2632 return true;
2633 }
2634 return false;
2635}
2636
2637bool QInternal::unregisterCallback(Callback cb, qInternalCallback callback)
2638{
2639 if (cb >= 0 && cb < QInternal::LastCallback) {
2640 QInternal_CallBackTable *cbt = global_callback_table();
2641 return (bool) cbt->callbacks[cb].removeAll(callback);
2642 }
2643 return false;
2644}
2645
2646bool QInternal::activateCallbacks(Callback cb, void **parameters)
2647{
2648 Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()", "Callback id must be a valid id");
2649
2650 QInternal_CallBackTable *cbt = global_callback_table();
2651 if (cbt && cb < cbt->callbacks.size()) {
2652 QList<qInternalCallback> callbacks = cbt->callbacks[cb];
2653 bool ret = false;
2654 for (int i=0; i<callbacks.size(); ++i)
2655 ret |= (callbacks.at(i))(parameters);
2656 return ret;
2657 }
2658 return false;
2659}
2660
2661extern void qt_set_current_thread_to_main_thread();
2662
2663bool QInternal::callFunction(InternalFunction func, void **args)
2664{
2665 Q_ASSERT_X(func >= 0,
2666 "QInternal::callFunction()", "Callback id must be a valid id");
2667#ifndef QT_NO_QOBJECT
2668 switch (func) {
2669#ifndef QT_NO_THREAD
2670 case QInternal::CreateThreadForAdoption:
2671 *args = QAdoptedThread::createThreadForAdoption();
2672 return true;
2673#endif
2674 case QInternal::RefAdoptedThread:
2675 QThreadData::get2((QThread *) *args)->ref();
2676 return true;
2677 case QInternal::DerefAdoptedThread:
2678 QThreadData::get2((QThread *) *args)->deref();
2679 return true;
2680 case QInternal::SetCurrentThreadToMainThread:
2681 qt_set_current_thread_to_main_thread();
2682 return true;
2683 case QInternal::SetQObjectSender: {
2684 QObject *receiver = (QObject *) args[0];
2685 QObjectPrivate::Sender *sender = new QObjectPrivate::Sender;
2686 sender->sender = (QObject *) args[1];
2687 sender->signal = *(int *) args[2];
2688 sender->ref = 1;
2689
2690 // Store the old sender as "return value"
2691 args[3] = QObjectPrivate::setCurrentSender(receiver, sender);
2692 args[4] = sender;
2693 return true;
2694 }
2695 case QInternal::GetQObjectSender: {
2696 QObject *receiver = (QObject *) args[0];
2697 QObjectPrivate *d = QObjectPrivate::get(receiver);
2698 args[1] = d->currentSender ? d->currentSender->sender : 0;
2699 return true;
2700 }
2701 case QInternal::ResetQObjectSender: {
2702 QObject *receiver = (QObject *) args[0];
2703 QObjectPrivate::Sender *oldSender = (QObjectPrivate::Sender *) args[1];
2704 QObjectPrivate::Sender *sender = (QObjectPrivate::Sender *) args[2];
2705 QObjectPrivate::resetCurrentSender(receiver, sender, oldSender);
2706 delete sender;
2707 return true;
2708 }
2709
2710 default:
2711 break;
2712 }
2713#else
2714 Q_UNUSED(args);
2715 Q_UNUSED(func);
2716#endif
2717
2718 return false;
2719}
2720
2721/*!
2722 \macro Q_BYTE_ORDER
2723 \relates <QtGlobal>
2724
2725 This macro can be used to determine the byte order your system
2726 uses for storing data in memory. i.e., whether your system is
2727 little-endian or big-endian. It is set by Qt to one of the macros
2728 Q_LITTLE_ENDIAN or Q_BIG_ENDIAN. You normally won't need to worry
2729 about endian-ness, but you might, for example if you need to know
2730 which byte of an integer or UTF-16 character is stored in the
2731 lowest address. Endian-ness is important in networking, where
2732 computers with different values for Q_BYTE_ORDER must pass data
2733 back and forth.
2734
2735 Use this macro as in the following examples.
2736
2737 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 40
2738
2739 \sa Q_BIG_ENDIAN, Q_LITTLE_ENDIAN
2740*/
2741
2742/*!
2743 \macro Q_LITTLE_ENDIAN
2744 \relates <QtGlobal>
2745
2746 This macro represents a value you can compare to the macro
2747 Q_BYTE_ORDER to determine the endian-ness of your system. In a
2748 little-endian system, the least significant byte is stored at the
2749 lowest address. The other bytes follow in increasing order of
2750 significance.
2751
2752 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 41
2753
2754 \sa Q_BYTE_ORDER, Q_BIG_ENDIAN
2755*/
2756
2757/*!
2758 \macro Q_BIG_ENDIAN
2759 \relates <QtGlobal>
2760
2761 This macro represents a value you can compare to the macro
2762 Q_BYTE_ORDER to determine the endian-ness of your system. In a
2763 big-endian system, the most significant byte is stored at the
2764 lowest address. The other bytes follow in decreasing order of
2765 significance.
2766
2767 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 42
2768
2769 \sa Q_BYTE_ORDER, Q_LITTLE_ENDIAN
2770*/
2771
2772/*!
2773 \macro Q_GLOBAL_STATIC(type, name)
2774 \internal
2775
2776 Declares a global static variable with the given \a type and \a name.
2777
2778 Use this macro to instantiate an object in a thread-safe way, creating
2779 a global pointer that can be used to refer to it.
2780
2781 \warning This macro is subject to a race condition that can cause the object
2782 to be constructed twice. However, if this occurs, the second instance will
2783 be immediately deleted.
2784
2785 See also
2786 \l{http://www.aristeia.com/publications.html}{"C++ and the perils of Double-Checked Locking"}
2787 by Scott Meyers and Andrei Alexandrescu.
2788*/
2789
2790/*!
2791 \macro Q_GLOBAL_STATIC_WITH_ARGS(type, name, arguments)
2792 \internal
2793
2794 Declares a global static variable with the specified \a type and \a name.
2795
2796 Use this macro to instantiate an object using the \a arguments specified
2797 in a thread-safe way, creating a global pointer that can be used to refer
2798 to it.
2799
2800 \warning This macro is subject to a race condition that can cause the object
2801 to be constructed twice. However, if this occurs, the second instance will
2802 be immediately deleted.
2803
2804 See also
2805 \l{http://www.aristeia.com/publications.html}{"C++ and the perils of Double-Checked Locking"}
2806 by Scott Meyers and Andrei Alexandrescu.
2807*/
2808
2809/*!
2810 \macro QT_NAMESPACE
2811 \internal
2812
2813 If this macro is defined to \c ns all Qt classes are put in a namespace
2814 called \c ns. Also, moc will output code putting metaobjects etc.
2815 into namespace \c ns.
2816
2817 \sa QT_BEGIN_NAMESPACE, QT_END_NAMESPACE,
2818 QT_PREPEND_NAMESPACE, QT_USE_NAMESPACE,
2819 QT_BEGIN_INCLUDE_NAMESPACE, QT_END_INCLUDE_NAMESPACE,
2820 QT_BEGIN_MOC_NAMESPACE, QT_END_MOC_NAMESPACE,
2821*/
2822
2823/*!
2824 \macro QT_PREPEND_NAMESPACE(identifier)
2825 \internal
2826
2827 This macro qualifies \a identifier with the full namespace.
2828 It expands to \c{::QT_NAMESPACE::identifier} if \c QT_NAMESPACE is defined
2829 and only \a identifier otherwise.
2830
2831 \sa QT_NAMESPACE
2832*/
2833
2834/*!
2835 \macro QT_USE_NAMESPACE
2836 \internal
2837
2838 This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined
2839 and nothing otherwise.
2840
2841 \sa QT_NAMESPACE
2842*/
2843
2844/*!
2845 \macro QT_BEGIN_NAMESPACE
2846 \internal
2847
2848 This macro expands to
2849
2850 \snippet snippets/code/src_corelib_global_qglobal.cpp begin namespace macro
2851
2852 if \c QT_NAMESPACE is defined and nothing otherwise. If should always
2853 appear in the file-level scope and be followed by \c QT_END_NAMESPACE
2854 at the same logical level with respect to preprocessor conditionals
2855 in the same file.
2856
2857 As a rule of thumb, \c QT_BEGIN_NAMESPACE should appear in all Qt header
2858 and Qt source files after the last \c{#include} line and before the first
2859 declaration. In Qt headers using \c QT_BEGIN_HEADER, \c QT_BEGIN_NAMESPACE
2860 follows \c QT_BEGIN_HEADER immediately.
2861
2862 If that rule can't be followed because, e.g., \c{#include} lines and
2863 declarations are wildly mixed, place \c QT_BEGIN_NAMESPACE before
2864 the first declaration and wrap the \c{#include} lines in
2865 \c QT_BEGIN_INCLUDE_NAMESPACE and \c QT_END_INCLUDE_NAMESPACE.
2866
2867 When using the \c QT_NAMESPACE feature in user code
2868 (e.g., when building plugins statically linked to Qt) where
2869 the user code is not intended to go into the \c QT_NAMESPACE
2870 namespace, all forward declarations of Qt classes need to
2871 be wrapped in \c QT_BEGIN_NAMESPACE and \c QT_END_NAMESPACE.
2872 After that, a \c QT_USE_NAMESPACE should follow.
2873 No further changes should be needed.
2874
2875 \sa QT_NAMESPACE
2876*/
2877
2878/*!
2879 \macro QT_END_NAMESPACE
2880 \internal
2881
2882 This macro expands to
2883
2884 \snippet snippets/code/src_corelib_global_qglobal.cpp end namespace macro
2885
2886 if \c QT_NAMESPACE is defined and nothing otherwise. It is used to cancel
2887 the effect of \c QT_BEGIN_NAMESPACE.
2888
2889 If a source file ends with a \c{#include} directive that includes a moc file,
2890 \c QT_END_NAMESPACE should be placed before that \c{#include}.
2891
2892 \sa QT_NAMESPACE
2893*/
2894
2895/*!
2896 \macro QT_BEGIN_INCLUDE_NAMESPACE
2897 \internal
2898
2899 This macro is equivalent to \c QT_END_NAMESPACE.
2900 It only serves as syntactic sugar and is intended
2901 to be used before #include lines within a
2902 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
2903
2904 \sa QT_NAMESPACE
2905*/
2906
2907/*!
2908 \macro QT_END_INCLUDE_NAMESPACE
2909 \internal
2910
2911 This macro is equivalent to \c QT_BEGIN_NAMESPACE.
2912 It only serves as syntactic sugar and is intended
2913 to be used after #include lines within a
2914 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
2915
2916 \sa QT_NAMESPACE
2917*/
2918
2919/*!
2920 \macro QT_BEGIN_MOC_NAMESPACE
2921 \internal
2922
2923 This macro is output by moc at the beginning of
2924 moc files. It is equivalent to \c QT_USE_NAMESPACE.
2925
2926 \sa QT_NAMESPACE
2927*/
2928
2929/*!
2930 \macro QT_END_MOC_NAMESPACE
2931 \internal
2932
2933 This macro is output by moc at the beginning of
2934 moc files. It expands to nothing.
2935
2936 \sa QT_NAMESPACE
2937*/
2938
2939/*!
2940 \fn bool qFuzzyCompare(double p1, double p2)
2941 \relates <QtGlobal>
2942 \since 4.4
2943 \threadsafe
2944
2945 Compares the floating point value \a p1 and \a p2 and
2946 returns \c true if they are considered equal, otherwise \c false.
2947
2948 Note that comparing values where either \a p1 or \a p2 is 0.0 will not work.
2949 The solution to this is to compare against values greater than or equal to 1.0.
2950
2951 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 46
2952
2953 The two numbers are compared in a relative way, where the
2954 exactness is stronger the smaller the numbers are.
2955 */
2956
2957/*!
2958 \fn bool qFuzzyCompare(float p1, float p2)
2959 \relates <QtGlobal>
2960 \since 4.4
2961 \threadsafe
2962 \overload
2963 */
2964
2965/*!
2966 \macro QT_REQUIRE_VERSION(int argc, char **argv, const char *version)
2967 \relates <QtGlobal>
2968
2969 This macro can be used to ensure that the application is run
2970 against a recent enough version of Qt. This is especially useful
2971 if your application depends on a specific bug fix introduced in a
2972 bug-fix release (e.g., 4.0.2).
2973
2974 The \a argc and \a argv parameters are the \c main() function's
2975 \c argc and \c argv parameters. The \a version parameter is a
2976 string literal that specifies which version of Qt the application
2977 requires (e.g., "4.0.2").
2978
2979 Example:
2980
2981 \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 4
2982*/
2983
2984/*!
2985 \macro Q_DECL_EXPORT
2986 \relates <QtGlobal>
2987
2988 This macro marks a symbol for shared library export (see
2989 \l{sharedlibrary.html}{Creating Shared Libraries}).
2990
2991 \sa Q_DECL_IMPORT
2992*/
2993
2994/*!
2995 \macro Q_DECL_IMPORT
2996 \relates <QtGlobal>
2997
2998 This macro declares a symbol to be an import from a shared library (see
2999 \l{sharedlibrary.html}{Creating Shared Libraries}).
3000
3001 \sa Q_DECL_EXPORT
3002*/
3003
3004QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.