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

Last change on this file since 798 was 798, checked in by Dmitry A. Kuminov, 15 years ago

corelib: Typo in r797.

File size: 101.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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#include "qdir.h"
48#include "qstringlist.h"
49#include "qdatetime.h"
50
51#ifndef QT_NO_QOBJECT
52#include <private/qthread_p.h>
53#endif
54
55#include <stdio.h>
56#include <stdlib.h>
57#include <limits.h>
58#include <stdarg.h>
59#include <string.h>
60
61#if !defined(Q_OS_WINCE)
62# include <errno.h>
63# if defined(Q_CC_MSVC)
64# include <crtdbg.h>
65# endif
66#endif
67
68#if defined(Q_OS_VXWORKS)
69# include <envLib.h>
70#endif
71
72#if defined(Q_CC_MWERKS) && defined(Q_OS_MACX)
73#include <CoreServices/CoreServices.h>
74#endif
75
76#if defined(Q_OS_SYMBIAN)
77#include <e32def.h>
78#include <e32debug.h>
79#include <f32file.h>
80#include <e32math.h>
81# include "private/qcore_symbian_p.h"
82
83_LIT(qt_S60Filter, "Series60v?.*.sis");
84_LIT(qt_S60SystemInstallDir, "z:\\system\\install\\");
85#endif
86
87QT_BEGIN_NAMESPACE
88
89
90/*!
91 \class QFlag
92 \brief The QFlag class is a helper data type for QFlags.
93
94 It is equivalent to a plain \c int, except with respect to
95 function overloading and type conversions. You should never need
96 to use this class in your applications.
97
98 \sa QFlags
99*/
100
101/*!
102 \fn QFlag::QFlag(int value)
103
104 Constructs a QFlag object that stores the given \a value.
105*/
106
107/*!
108 \fn QFlag::operator int() const
109
110 Returns the value stored by the QFlag object.
111*/
112
113/*!
114 \class QFlags
115 \brief The QFlags class provides a type-safe way of storing
116 OR-combinations of enum values.
117
118
119 \ingroup tools
120
121 The QFlags<Enum> class is a template class, where Enum is an enum
122 type. QFlags is used throughout Qt for storing combinations of
123 enum values.
124
125 The traditional C++ approach for storing OR-combinations of enum
126 values is to use an \c int or \c uint variable. The inconvenience
127 with this approach is that there's no type checking at all; any
128 enum value can be OR'd with any other enum value and passed on to
129 a function that takes an \c int or \c uint.
130
131 Qt uses QFlags to provide type safety. For example, the
132 Qt::Alignment type is simply a typedef for
133 QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a
134 Qt::Alignment parameter, which means that any combination of
135 Qt::AlignmentFlag values,or 0, is legal:
136
137 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 0
138
139 If you try to pass a value from another enum or just a plain
140 integer other than 0, the compiler will report an error. If you
141 need to cast integer values to flags in a untyped fashion, you can
142 use the explicit QFlags constructor as cast operator.
143
144 If you want to use QFlags for your own enum types, use
145 the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().
146
147 Example:
148
149 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 1
150
151 You can then use the \c MyClass::Options type to store
152 combinations of \c MyClass::Option values.
153
154 \section1 Flags and the Meta-Object System
155
156 The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object
157 system, so they cannot be used by Qt Script or edited in Qt Designer.
158 To make the flags available for these purposes, the Q_FLAGS() macro must
159 be used:
160
161 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp meta-object flags
162
163 \section1 Naming Convention
164
165 A sensible naming convention for enum types and associated QFlags
166 types is to give a singular name to the enum type (e.g., \c
167 Option) and a plural name to the QFlags type (e.g., \c Options).
168 When a singular name is desired for the QFlags type (e.g., \c
169 Alignment), you can use \c Flag as the suffix for the enum type
170 (e.g., \c AlignmentFlag).
171
172 \sa QFlag
173*/
174
175/*!
176 \typedef QFlags::enum_type
177
178 Typedef for the Enum template type.
179*/
180
181/*!
182 \fn QFlags::QFlags(const QFlags &other)
183
184 Constructs a copy of \a other.
185*/
186
187/*!
188 \fn QFlags::QFlags(Enum flag)
189
190 Constructs a QFlags object storing the given \a flag.
191*/
192
193/*!
194 \fn QFlags::QFlags(Zero zero)
195
196 Constructs a QFlags object with no flags set. \a zero must be a
197 literal 0 value.
198*/
199
200/*!
201 \fn QFlags::QFlags(QFlag value)
202
203 Constructs a QFlags object initialized with the given integer \a
204 value.
205
206 The QFlag type is a helper type. By using it here instead of \c
207 int, we effectively ensure that arbitrary enum values cannot be
208 cast to a QFlags, whereas untyped enum values (i.e., \c int
209 values) can.
210*/
211
212/*!
213 \fn QFlags &QFlags::operator=(const QFlags &other)
214
215 Assigns \a other to this object and returns a reference to this
216 object.
217*/
218
219/*!
220 \fn QFlags &QFlags::operator&=(int mask)
221
222 Performs a bitwise AND operation with \a mask and stores the
223 result in this QFlags object. Returns a reference to this object.
224
225 \sa operator&(), operator|=(), operator^=()
226*/
227
228/*!
229 \fn QFlags &QFlags::operator&=(uint mask)
230
231 \overload
232*/
233
234/*!
235 \fn QFlags &QFlags::operator|=(QFlags other)
236
237 Performs a bitwise OR operation with \a other and stores the
238 result in this QFlags object. Returns a reference to this object.
239
240 \sa operator|(), operator&=(), operator^=()
241*/
242
243/*!
244 \fn QFlags &QFlags::operator|=(Enum other)
245
246 \overload
247*/
248
249/*!
250 \fn QFlags &QFlags::operator^=(QFlags other)
251
252 Performs a bitwise XOR operation with \a other and stores the
253 result in this QFlags object. Returns a reference to this object.
254
255 \sa operator^(), operator&=(), operator|=()
256*/
257
258/*!
259 \fn QFlags &QFlags::operator^=(Enum other)
260
261 \overload
262*/
263
264/*!
265 \fn QFlags::operator int() const
266
267 Returns the value stored in the QFlags object as an integer.
268*/
269
270/*!
271 \fn QFlags QFlags::operator|(QFlags other) const
272
273 Returns a QFlags object containing the result of the bitwise OR
274 operation on this object and \a other.
275
276 \sa operator|=(), operator^(), operator&(), operator~()
277*/
278
279/*!
280 \fn QFlags QFlags::operator|(Enum other) const
281
282 \overload
283*/
284
285/*!
286 \fn QFlags QFlags::operator^(QFlags other) const
287
288 Returns a QFlags object containing the result of the bitwise XOR
289 operation on this object and \a other.
290
291 \sa operator^=(), operator&(), operator|(), operator~()
292*/
293
294/*!
295 \fn QFlags QFlags::operator^(Enum other) const
296
297 \overload
298*/
299
300/*!
301 \fn QFlags QFlags::operator&(int mask) const
302
303 Returns a QFlags object containing the result of the bitwise AND
304 operation on this object and \a mask.
305
306 \sa operator&=(), operator|(), operator^(), operator~()
307*/
308
309/*!
310 \fn QFlags QFlags::operator&(uint mask) const
311
312 \overload
313*/
314
315/*!
316 \fn QFlags QFlags::operator&(Enum mask) const
317
318 \overload
319*/
320
321/*!
322 \fn QFlags QFlags::operator~() const
323
324 Returns a QFlags object that contains the bitwise negation of
325 this object.
326
327 \sa operator&(), operator|(), operator^()
328*/
329
330/*!
331 \fn bool QFlags::operator!() const
332
333 Returns true if no flag is set (i.e., if the value stored by the
334 QFlags object is 0); otherwise returns false.
335*/
336
337/*!
338 \fn bool QFlags::testFlag(Enum flag) const
339 \since 4.2
340
341 Returns true if the \a flag is set, otherwise false.
342*/
343
344/*!
345 \macro Q_DISABLE_COPY(Class)
346 \relates QObject
347
348 Disables the use of copy constructors and assignment operators
349 for the given \a Class.
350
351 Instances of subclasses of QObject should not be thought of as
352 values that can be copied or assigned, but as unique identities.
353 This means that when you create your own subclass of QObject
354 (director or indirect), you should \e not give it a copy constructor
355 or an assignment operator. However, it may not enough to simply
356 omit them from your class, because, if you mistakenly write some code
357 that requires a copy constructor or an assignment operator (it's easy
358 to do), your compiler will thoughtfully create it for you. You must
359 do more.
360
361 The curious user will have seen that the Qt classes derived
362 from QObject typically include this macro in a private section:
363
364 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 43
365
366 It declares a copy constructor and an assignment operator in the
367 private section, so that if you use them by mistake, the compiler
368 will report an error.
369
370 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 44
371
372 But even this might not catch absolutely every case. You might be
373 tempted to do something like this:
374
375 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 45
376
377 First of all, don't do that. Most compilers will generate code that
378 uses the copy constructor, so the privacy violation error will be
379 reported, but your C++ compiler is not required to generate code for
380 this statement in a specific way. It could generate code using
381 \e{neither} the copy constructor \e{nor} the assignment operator we
382 made private. In that case, no error would be reported, but your
383 application would probably crash when you called a member function
384 of \c{w}.
385*/
386
387/*!
388 \macro Q_DECLARE_FLAGS(Flags, Enum)
389 \relates QFlags
390
391 The Q_DECLARE_FLAGS() macro expands to
392
393 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 2
394
395 \a Enum is the name of an existing enum type, whereas \a Flags is
396 the name of the QFlags<\e{Enum}> typedef.
397
398 See the QFlags documentation for details.
399
400 \sa Q_DECLARE_OPERATORS_FOR_FLAGS()
401*/
402
403/*!
404 \macro Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
405 \relates QFlags
406
407 The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global \c
408 operator|() functions for \a Flags, which is of type QFlags<T>.
409
410 See the QFlags documentation for details.
411
412 \sa Q_DECLARE_FLAGS()
413*/
414
415/*!
416 \headerfile <QtGlobal>
417 \title Global Qt Declarations
418 \ingroup funclists
419
420 \brief The <QtGlobal> header file includes the fundamental global
421 declarations. It is included by most other Qt header files.
422
423 The global declarations include \l{types}, \l{functions} and
424 \l{macros}.
425
426 The type definitions are partly convenience definitions for basic
427 types (some of which guarantee certain bit-sizes on all platforms
428 supported by Qt), partly types related to Qt message handling. The
429 functions are related to generating messages, Qt version handling
430 and comparing and adjusting object values. And finally, some of
431 the declared macros enable programmers to add compiler or platform
432 specific code to their applications, while others are convenience
433 macros for larger operations.
434
435 \section1 Types
436
437 The header file declares several type definitions that guarantee a
438 specified bit-size on all platforms supported by Qt for various
439 basic types, for example \l qint8 which is a signed char
440 guaranteed to be 8-bit on all platforms supported by Qt. The
441 header file also declares the \l qlonglong type definition for \c
442 {long long int } (\c __int64 on Windows).
443
444 Several convenience type definitions are declared: \l qreal for \c
445 double, \l uchar for \c unsigned char, \l uint for \c unsigned
446 int, \l ulong for \c unsigned long and \l ushort for \c unsigned
447 short.
448
449 Finally, the QtMsgType definition identifies the various messages
450 that can be generated and sent to a Qt message handler;
451 QtMsgHandler is a type definition for a pointer to a function with
452 the signature \c {void myMsgHandler(QtMsgType, const char *)}.
453
454 \section1 Functions
455
456 The <QtGlobal> header file contains several functions comparing
457 and adjusting an object's value. These functions take a template
458 type as argument: You can retrieve the absolute value of an object
459 using the qAbs() function, and you can bound a given object's
460 value by given minimum and maximum values using the qBound()
461 function. You can retrieve the minimum and maximum of two given
462 objects using qMin() and qMax() respectively. All these functions
463 return a corresponding template type; the template types can be
464 replaced by any other type.
465
466 Example:
467
468 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 3
469
470 <QtGlobal> also contains functions that generate messages from the
471 given string argument: qCritical(), qDebug(), qFatal() and
472 qWarning(). These functions call the message handler with the
473 given message.
474
475 Example:
476
477 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 4
478
479 The remaining functions are qRound() and qRound64(), which both
480 accept a \l qreal value as their argument returning the value
481 rounded up to the nearest integer and 64-bit integer respectively,
482 the qInstallMsgHandler() function which installs the given
483 QtMsgHandler, and the qVersion() function which returns the
484 version number of Qt at run-time as a string.
485
486 \section1 Macros
487
488 The <QtGlobal> header file provides a range of macros (Q_CC_*)
489 that are defined if the application is compiled using the
490 specified platforms. For example, the Q_CC_SUN macro is defined if
491 the application is compiled using Forte Developer, or Sun Studio
492 C++. The header file also declares a range of macros (Q_OS_*)
493 that are defined for the specified platforms. For example,
494 Q_OS_X11 which is defined for the X Window System.
495
496 The purpose of these macros is to enable programmers to add
497 compiler or platform specific code to their application.
498
499 The remaining macros are convenience macros for larger operations:
500 The QT_TRANSLATE_NOOP() and QT_TR_NOOP() macros provide the
501 possibility of marking text for dynamic translation,
502 i.e. translation without changing the stored source text. The
503 Q_ASSERT() and Q_ASSERT_X() enables warning messages of various
504 level of refinement. The Q_FOREACH() and foreach() macros
505 implement Qt's foreach loop.
506
507 The Q_INT64_C() and Q_UINT64_C() macros wrap signed and unsigned
508 64-bit integer literals in a platform-independent way. The
509 Q_CHECK_PTR() macro prints a warning containing the source code's
510 file name and line number, saying that the program ran out of
511 memory, if the pointer is 0. The qPrintable() macro represent an
512 easy way of printing text.
513
514 Finally, the QT_POINTER_SIZE macro expands to the size of a
515 pointer in bytes, and the QT_VERSION and QT_VERSION_STR macros
516 expand to a numeric value or a string, respectively, specifying
517 Qt's version number, i.e the version the application is compiled
518 against.
519
520 \sa <QtAlgorithms>, QSysInfo
521*/
522
523/*!
524 \typedef qreal
525 \relates <QtGlobal>
526
527 Typedef for \c double on all platforms except for those using CPUs with
528 ARM architectures.
529 On ARM-based platforms, \c qreal is a typedef for \c float for performance
530 reasons.
531*/
532
533/*! \typedef uchar
534 \relates <QtGlobal>
535
536 Convenience typedef for \c{unsigned char}.
537*/
538
539/*!
540 \fn qt_set_sequence_auto_mnemonic(bool on)
541 \relates <QtGlobal>
542
543 Enables automatic mnemonics on Mac if \a on is true; otherwise
544 this feature is disabled.
545
546 Note that this function is only available on Mac where mnemonics
547 are disabled by default.
548
549 To access to this function, use an extern declaration:
550 extern void qt_set_sequence_auto_mnemonic(bool b);
551
552 \sa {QShortcut#mnemonic}{QShortcut}
553*/
554
555/*! \typedef ushort
556 \relates <QtGlobal>
557
558 Convenience typedef for \c{unsigned short}.
559*/
560
561/*! \typedef uint
562 \relates <QtGlobal>
563
564 Convenience typedef for \c{unsigned int}.
565*/
566
567/*! \typedef ulong
568 \relates <QtGlobal>
569
570 Convenience typedef for \c{unsigned long}.
571*/
572
573/*! \typedef qint8
574 \relates <QtGlobal>
575
576 Typedef for \c{signed char}. This type is guaranteed to be 8-bit
577 on all platforms supported by Qt.
578*/
579
580/*!
581 \typedef quint8
582 \relates <QtGlobal>
583
584 Typedef for \c{unsigned char}. This type is guaranteed to
585 be 8-bit on all platforms supported by Qt.
586*/
587
588/*! \typedef qint16
589 \relates <QtGlobal>
590
591 Typedef for \c{signed short}. This type is guaranteed to be
592 16-bit on all platforms supported by Qt.
593*/
594
595/*!
596 \typedef quint16
597 \relates <QtGlobal>
598
599 Typedef for \c{unsigned short}. This type is guaranteed to
600 be 16-bit on all platforms supported by Qt.
601*/
602
603/*! \typedef qint32
604 \relates <QtGlobal>
605
606 Typedef for \c{signed int}. This type is guaranteed to be 32-bit
607 on all platforms supported by Qt.
608*/
609
610/*!
611 \typedef quint32
612 \relates <QtGlobal>
613
614 Typedef for \c{unsigned int}. This type is guaranteed to
615 be 32-bit on all platforms supported by Qt.
616*/
617
618/*! \typedef qint64
619 \relates <QtGlobal>
620
621 Typedef for \c{long long int} (\c __int64 on Windows). This type
622 is guaranteed to be 64-bit on all platforms supported by Qt.
623
624 Literals of this type can be created using the Q_INT64_C() macro:
625
626 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 5
627
628 \sa Q_INT64_C(), quint64, qlonglong
629*/
630
631/*!
632 \typedef quint64
633 \relates <QtGlobal>
634
635 Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
636 Windows). This type is guaranteed to be 64-bit on all platforms
637 supported by Qt.
638
639 Literals of this type can be created using the Q_UINT64_C()
640 macro:
641
642 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 6
643
644 \sa Q_UINT64_C(), qint64, qulonglong
645*/
646
647/*!
648 \typedef quintptr
649 \relates <QtGlobal>
650
651 Integral type for representing a pointers (useful for hashing,
652 etc.).
653
654 Typedef for either quint32 or quint64. This type is guaranteed to
655 be the same size as a pointer on all platforms supported by Qt. On
656 a system with 32-bit pointers, quintptr is a typedef for quint32;
657 on a system with 64-bit pointers, quintptr is a typedef for
658 quint64.
659
660 Note that quintptr is unsigned. Use qptrdiff for signed values.
661
662 \sa qptrdiff, quint32, quint64
663*/
664
665/*!
666 \typedef qptrdiff
667 \relates <QtGlobal>
668
669 Integral type for representing pointer differences.
670
671 Typedef for either qint32 or qint64. This type is guaranteed to be
672 the same size as a pointer on all platforms supported by Qt. On a
673 system with 32-bit pointers, quintptr is a typedef for quint32; on
674 a system with 64-bit pointers, quintptr is a typedef for quint64.
675
676 Note that qptrdiff is signed. Use quintptr for unsigned values.
677
678 \sa quintptr, qint32, qint64
679*/
680
681/*!
682 \typedef QtMsgHandler
683 \relates <QtGlobal>
684
685 This is a typedef for a pointer to a function with the following
686 signature:
687
688 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 7
689
690 \sa QtMsgType, qInstallMsgHandler()
691*/
692
693/*!
694 \enum QtMsgType
695 \relates <QtGlobal>
696
697 This enum describes the messages that can be sent to a message
698 handler (QtMsgHandler). You can use the enum to identify and
699 associate the various message types with the appropriate
700 actions.
701
702 \value QtDebugMsg
703 A message generated by the qDebug() function.
704 \value QtWarningMsg
705 A message generated by the qWarning() function.
706 \value QtCriticalMsg
707 A message generated by the qCritical() function.
708 \value QtFatalMsg
709 A message generated by the qFatal() function.
710 \value QtSystemMsg
711
712
713 \sa QtMsgHandler, qInstallMsgHandler()
714*/
715
716/*! \macro qint64 Q_INT64_C(literal)
717 \relates <QtGlobal>
718
719 Wraps the signed 64-bit integer \a literal in a
720 platform-independent way.
721
722 Example:
723
724 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 8
725
726 \sa qint64, Q_UINT64_C()
727*/
728
729/*! \macro quint64 Q_UINT64_C(literal)
730 \relates <QtGlobal>
731
732 Wraps the unsigned 64-bit integer \a literal in a
733 platform-independent way.
734
735 Example:
736
737 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 9
738
739 \sa quint64, Q_INT64_C()
740*/
741
742/*! \typedef qlonglong
743 \relates <QtGlobal>
744
745 Typedef for \c{long long int} (\c __int64 on Windows). This is
746 the same as \l qint64.
747
748 \sa qulonglong, qint64
749*/
750
751/*!
752 \typedef qulonglong
753 \relates <QtGlobal>
754
755 Typedef for \c{unsigned long long int} (\c{unsigned __int64} on
756 Windows). This is the same as \l quint64.
757
758 \sa quint64, qlonglong
759*/
760
761/*! \fn const T &qAbs(const T &value)
762 \relates <QtGlobal>
763
764 Compares \a value to the 0 of type T and returns the absolute
765 value. Thus if T is \e {double}, then \a value is compared to
766 \e{(double) 0}.
767
768 Example:
769
770 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 10
771*/
772
773/*! \fn int qRound(qreal value)
774 \relates <QtGlobal>
775
776 Rounds \a value to the nearest integer.
777
778 Example:
779
780 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 11
781*/
782
783/*! \fn qint64 qRound64(qreal value)
784 \relates <QtGlobal>
785
786 Rounds \a value to the nearest 64-bit integer.
787
788 Example:
789
790 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 12
791*/
792
793/*! \fn const T &qMin(const T &value1, const T &value2)
794 \relates <QtGlobal>
795
796 Returns the minimum of \a value1 and \a value2.
797
798 Example:
799
800 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 13
801
802 \sa qMax(), qBound()
803*/
804
805/*! \fn const T &qMax(const T &value1, const T &value2)
806 \relates <QtGlobal>
807
808 Returns the maximum of \a value1 and \a value2.
809
810 Example:
811
812 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 14
813
814 \sa qMin(), qBound()
815*/
816
817/*! \fn const T &qBound(const T &min, const T &value, const T &max)
818 \relates <QtGlobal>
819
820 Returns \a value bounded by \a min and \a max. This is equivalent
821 to qMax(\a min, qMin(\a value, \a max)).
822
823 Example:
824
825 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 15
826
827 \sa qMin(), qMax()
828*/
829
830/*!
831 \typedef Q_INT8
832 \relates <QtGlobal>
833 \compat
834
835 Use \l qint8 instead.
836*/
837
838/*!
839 \typedef Q_UINT8
840 \relates <QtGlobal>
841 \compat
842
843 Use \l quint8 instead.
844*/
845
846/*!
847 \typedef Q_INT16
848 \relates <QtGlobal>
849 \compat
850
851 Use \l qint16 instead.
852*/
853
854/*!
855 \typedef Q_UINT16
856 \relates <QtGlobal>
857 \compat
858
859 Use \l quint16 instead.
860*/
861
862/*!
863 \typedef Q_INT32
864 \relates <QtGlobal>
865 \compat
866
867 Use \l qint32 instead.
868*/
869
870/*!
871 \typedef Q_UINT32
872 \relates <QtGlobal>
873 \compat
874
875 Use \l quint32 instead.
876*/
877
878/*!
879 \typedef Q_INT64
880 \relates <QtGlobal>
881 \compat
882
883 Use \l qint64 instead.
884*/
885
886/*!
887 \typedef Q_UINT64
888 \relates <QtGlobal>
889 \compat
890
891 Use \l quint64 instead.
892*/
893
894/*!
895 \typedef Q_LLONG
896 \relates <QtGlobal>
897 \compat
898
899 Use \l qint64 instead.
900*/
901
902/*!
903 \typedef Q_ULLONG
904 \relates <QtGlobal>
905 \compat
906
907 Use \l quint64 instead.
908*/
909
910/*!
911 \typedef Q_LONG
912 \relates <QtGlobal>
913 \compat
914
915 Use \c{void *} instead.
916*/
917
918/*!
919 \typedef Q_ULONG
920 \relates <QtGlobal>
921 \compat
922
923 Use \c{void *} instead.
924*/
925
926/*! \fn bool qSysInfo(int *wordSize, bool *bigEndian)
927 \relates <QtGlobal>
928
929 Use QSysInfo::WordSize and QSysInfo::ByteOrder instead.
930*/
931
932/*!
933 \fn bool qt_winUnicode()
934 \relates <QtGlobal>
935
936 This function always returns true.
937
938 \sa QSysInfo
939*/
940
941/*!
942 \fn int qWinVersion()
943 \relates <QtGlobal>
944
945 Use QSysInfo::WindowsVersion instead.
946
947 \sa QSysInfo
948*/
949
950/*!
951 \fn int qMacVersion()
952 \relates <QtGlobal>
953
954 Use QSysInfo::MacintoshVersion instead.
955
956 \sa QSysInfo
957*/
958
959/*!
960 \macro QT_VERSION_CHECK
961 \relates <QtGlobal>
962
963 Turns the major, minor and patch numbers of a version into an
964 integer, 0xMMNNPP (MM = major, NN = minor, PP = patch). This can
965 be compared with another similarly processed version id.
966
967 \sa QT_VERSION
968*/
969
970/*!
971 \macro QT_VERSION
972 \relates <QtGlobal>
973
974 This macro expands a numeric value of the form 0xMMNNPP (MM =
975 major, NN = minor, PP = patch) that specifies Qt's version
976 number. For example, if you compile your application against Qt
977 4.1.2, the QT_VERSION macro will expand to 0x040102.
978
979 You can use QT_VERSION to use the latest Qt features where
980 available.
981
982 Example:
983
984 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 16
985
986 \sa QT_VERSION_STR, qVersion()
987*/
988
989/*!
990 \macro QT_VERSION_STR
991 \relates <QtGlobal>
992
993 This macro expands to a string that specifies Qt's version number
994 (for example, "4.1.2"). This is the version against which the
995 application is compiled.
996
997 \sa qVersion(), QT_VERSION
998*/
999
1000/*!
1001 \relates <QtGlobal>
1002
1003 Returns the version number of Qt at run-time as a string (for
1004 example, "4.1.2"). This may be a different version than the
1005 version the application was compiled against.
1006
1007 \sa QT_VERSION_STR
1008*/
1009
1010const char *qVersion()
1011{
1012 return QT_VERSION_STR;
1013}
1014
1015bool qSharedBuild()
1016{
1017#ifdef QT_SHARED
1018 return true;
1019#else
1020 return false;
1021#endif
1022}
1023
1024/*****************************************************************************
1025 System detection routines
1026 *****************************************************************************/
1027
1028/*!
1029 \class QSysInfo
1030 \brief The QSysInfo class provides information about the system.
1031
1032 \list
1033 \o \l WordSize specifies the size of a pointer for the platform
1034 on which the application is compiled.
1035 \o \l ByteOrder specifies whether the platform is big-endian or
1036 little-endian.
1037 \o \l WindowsVersion specifies the version of the Windows operating
1038 system on which the application is run (Windows only)
1039 \o \l MacintoshVersion specifies the version of the Macintosh
1040 operating system on which the application is run (Mac only).
1041 \o \l os2Version() specifies the version of the OS/2
1042 operating system on which the application is run (OS/2 only).
1043 \endlist
1044
1045 Some constants are defined only on certain platforms. You can use
1046 the preprocessor symbols Q_WS_WIN, Q_OS_MAC and Q_OS_OS2 to test that the
1047 application is compiled under Windows, Mac or OS/2.
1048
1049 \sa QLibraryInfo
1050*/
1051
1052/*!
1053 \enum QSysInfo::Sizes
1054
1055 This enum provides platform-specific information about the sizes of data
1056 structures used by the underlying architecture.
1057
1058 \value WordSize The size in bits of a pointer for the platform on which
1059 the application is compiled (32 or 64).
1060*/
1061
1062/*!
1063 \variable QSysInfo::WindowsVersion
1064 \brief the version of the Windows operating system on which the
1065 application is run (Windows only)
1066*/
1067
1068/*!
1069 \fn QSysInfo::WindowsVersion QSysInfo::windowsVersion()
1070 \since 4.4
1071
1072 Returns the version of the Windows operating system on which the
1073 application is run (Windows only).
1074*/
1075
1076/*!
1077 \variable QSysInfo::MacintoshVersion
1078 \brief the version of the Macintosh operating system on which
1079 the application is run (Mac only).
1080*/
1081
1082/*!
1083 \fn QSysInfo::SymbianVersion QSysInfo::symbianVersion()
1084 \since 4.6
1085
1086 Returns the version of the Symbian operating system on which the
1087 application is run (Symbian only).
1088*/
1089
1090/*!
1091 \fn QSysInfo::S60Version QSysInfo::s60Version()
1092 \since 4.6
1093
1094 Returns the version of the S60 SDK system on which the
1095 application is run (S60 only).
1096*/
1097
1098/*!
1099 \fn QSysInfo::Os2Version QSysInfo::os2Version()
1100 \since 4.6
1101
1102 Returns the version of the OS/2 operating system on which the
1103 application is run (OS/2 only).
1104*/
1105
1106/*!
1107 \enum QSysInfo::Endian
1108
1109 \value BigEndian Big-endian byte order (also called Network byte order)
1110 \value LittleEndian Little-endian byte order
1111 \value ByteOrder Equals BigEndian or LittleEndian, depending on
1112 the platform's byte order.
1113*/
1114
1115/*!
1116 \enum QSysInfo::WinVersion
1117
1118 This enum provides symbolic names for the various versions of the
1119 Windows operating system. On Windows, the
1120 QSysInfo::WindowsVersion variable gives the version of the system
1121 on which the application is run.
1122
1123 MS-DOS-based versions:
1124
1125 \value WV_32s Windows 3.1 with Win 32s
1126 \value WV_95 Windows 95
1127 \value WV_98 Windows 98
1128 \value WV_Me Windows Me
1129
1130 NT-based versions (note that each operating system version is only represented once rather than each Windows edition):
1131
1132 \value WV_NT Windows NT (operating system version 4.0)
1133 \value WV_2000 Windows 2000 (operating system version 5.0)
1134 \value WV_XP Windows XP (operating system version 5.1)
1135 \value WV_2003 Windows Server 2003, Windows Server 2003 R2, Windows Home Server, Windows XP Professional x64 Edition (operating system version 5.2)
1136 \value WV_VISTA Windows Vista, Windows Server 2008 (operating system version 6.0)
1137 \value WV_WINDOWS7 Windows 7, Windows Server 2008 R2 (operating system version 6.1)
1138
1139 Alternatively, you may use the following macros which correspond directly to the Windows operating system version number:
1140
1141 \value WV_4_0 Operating system version 4.0, corresponds to Windows NT
1142 \value WV_5_0 Operating system version 5.0, corresponds to Windows 2000
1143 \value WV_5_1 Operating system version 5.1, corresponds to Windows XP
1144 \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
1145 \value WV_6_0 Operating system version 6.0, corresponds to Windows Vista and Windows Server 2008
1146 \value WV_6_1 Operating system version 6.1, corresponds to Windows 7 and Windows Server 2008 R2
1147
1148 CE-based versions:
1149
1150 \value WV_CE Windows CE
1151 \value WV_CENET Windows CE .NET
1152 \value WV_CE_5 Windows CE 5.x
1153 \value WV_CE_6 Windows CE 6.x
1154
1155 The following masks can be used for testing whether a Windows
1156 version is MS-DOS-based, NT-based, or CE-based:
1157
1158 \value WV_DOS_based MS-DOS-based version of Windows
1159 \value WV_NT_based NT-based version of Windows
1160 \value WV_CE_based CE-based version of Windows
1161
1162 \sa MacVersion, SymbianVersion
1163*/
1164
1165/*!
1166 \enum QSysInfo::MacVersion
1167
1168 This enum provides symbolic names for the various versions of the
1169 Macintosh operating system. On Mac, the
1170 QSysInfo::MacintoshVersion variable gives the version of the
1171 system on which the application is run.
1172
1173 \value MV_9 Mac OS 9 (unsupported)
1174 \value MV_10_0 Mac OS X 10.0 (unsupported)
1175 \value MV_10_1 Mac OS X 10.1 (unsupported)
1176 \value MV_10_2 Mac OS X 10.2 (unsupported)
1177 \value MV_10_3 Mac OS X 10.3
1178 \value MV_10_4 Mac OS X 10.4
1179 \value MV_10_5 Mac OS X 10.5
1180 \value MV_10_6 Mac OS X 10.6
1181 \value MV_Unknown An unknown and currently unsupported platform
1182
1183 \value MV_CHEETAH Apple codename for MV_10_0
1184 \value MV_PUMA Apple codename for MV_10_1
1185 \value MV_JAGUAR Apple codename for MV_10_2
1186 \value MV_PANTHER Apple codename for MV_10_3
1187 \value MV_TIGER Apple codename for MV_10_4
1188 \value MV_LEOPARD Apple codename for MV_10_5
1189 \value MV_SNOWLEOPARD Apple codename for MV_10_6
1190
1191 \sa WinVersion, SymbianVersion
1192*/
1193
1194/*!
1195 \enum QSysInfo::SymbianVersion
1196
1197 This enum provides symbolic names for the various versions of the
1198 Symbian operating system. On Symbian, the
1199 QSysInfo::symbianVersion() function gives the version of the
1200 system on which the application is run.
1201
1202 \value SV_9_2 Symbian OS v9.2
1203 \value SV_9_3 Symbian OS v9.3
1204 \value SV_9_4 Symbian OS v9.4
1205 \value SV_SF_1 Symbian^1
1206 \value SV_SF_2 Symbian^2
1207 \value SV_SF_3 Symbian^3
1208 \value SV_SF_4 Symbian^4
1209 \value SV_Unknown An unknown and currently unsupported platform
1210
1211 \sa S60Version, WinVersion, MacVersion
1212*/
1213
1214/*!
1215 \enum QSysInfo::S60Version
1216
1217 This enum provides symbolic names for the various versions of the
1218 S60 SDK. On S60, the
1219 QSysInfo::s60Version() function gives the version of the
1220 SDK on which the application is run.
1221
1222 \value SV_S60_3_1 S60 3rd Edition Feature Pack 1
1223 \value SV_S60_3_2 S60 3rd Edition Feature Pack 2
1224 \value SV_S60_5_0 S60 5th Edition
1225 \value SV_S60_5_1 S60 5th Edition Feature Pack 1
1226 \value SV_S60_5_2 S60 5th Edition Feature Pack 2
1227 \value SV_S60_Unknown An unknown and currently unsupported platform
1228 \omitvalue SV_S60_None
1229
1230 \sa SymbianVersion, WinVersion, MacVersion
1231*/
1232
1233/*!
1234 \enum QSysInfo::Os2Version
1235
1236 This enum provides symbolic names for the various versions of the
1237 OS/2 operating system. On OS/2, the
1238 QSysInfo::os2Version() function gives the version of the system on
1239 which the application is run.
1240
1241 Original IBM OS/2 systems:
1242
1243 \value OV_Unknown An unknown and currently unsupported OS/2 version
1244 \value OV_2_0 OS/2 2.0 (unsupported)
1245 \value OV_2_1 OS/2 2.1 (unsupported)
1246 \value OV_2_11 OS/2 2.11 (unsupported)
1247 \value OV_3_0 OS/2 3.0, corresponds to Warp 3 (unsupported)
1248 \value OV_4_0 OS/2 4.0, corresponds to Warp 4 also known as Merlin
1249 \value OV_4_5 OS/2 4.5, corresponds to Warp 4.5 also known as Aurora
1250 \value OV_4_52 OS/2 4.52, corresponds to Warp 4.52 also known as WSeB
1251
1252 \value OV_MERLIN Codename for OV_4_0
1253 \value OV_AURORA Codename for OV_4_5
1254 \value OV_WSEB Codename for OV_4_52
1255
1256 Serenity eComStation systems:
1257
1258 \value OV_ECS_Unknown An unknown and currently unsupported
1259 eComStation version
1260 \value OV_ECS_1_0 eComStation 1.0
1261 \value OV_ECS_1_1 eComStation 1.1
1262 \value OV_ECS_1_2 eComStation 1.2
1263 \value OV_ECS_2_0 eComStation 2.0 (either any of RCs or GA)
1264
1265 The following masks can be used for detecting properties common to a group
1266 of versions:
1267
1268 \value OV_WARP3 Set if the version represents an OS/2 Warp 3 system
1269 \value OV_WARP4 Set if the version represents an OS/2 Warp 4 system
1270 \value OV_WARP Set if the version is either of the OS/2 Warp systems
1271 \value OV_ECS Set if the version represents an eComStation system
1272
1273 \sa WinVersion, SymbianVersion, MacVersion
1274*/
1275
1276/*!
1277 \macro Q_WS_MAC
1278 \relates <QtGlobal>
1279
1280 Defined on Mac OS X.
1281
1282 \sa Q_WS_WIN, Q_WS_X11, Q_WS_QWS, Q_WS_S60
1283*/
1284
1285/*!
1286 \macro Q_WS_WIN
1287 \relates <QtGlobal>
1288
1289 Defined on Windows.
1290
1291 \sa Q_WS_MAC, Q_WS_X11, Q_WS_QWS, Q_WS_S60
1292*/
1293
1294/*!
1295 \macro Q_WS_X11
1296 \relates <QtGlobal>
1297
1298 Defined on X11.
1299
1300 \sa Q_WS_MAC, Q_WS_WIN, Q_WS_QWS, Q_WS_S60
1301*/
1302
1303/*!
1304 \macro Q_WS_QWS
1305 \relates <QtGlobal>
1306
1307 Defined on Qt for Embedded Linux.
1308
1309 \sa Q_WS_MAC, Q_WS_WIN, Q_WS_X11, Q_WS_S60
1310*/
1311
1312/*!
1313 \macro Q_OS_DARWIN
1314 \relates <QtGlobal>
1315
1316 Defined on Darwin OS (synonym for Q_OS_MAC).
1317*/
1318
1319/*!
1320 \macro Q_OS_MSDOS
1321 \relates <QtGlobal>
1322
1323 Defined on MS-DOS and Windows.
1324*/
1325
1326/*!
1327 \macro Q_OS_OS2
1328 \relates <QtGlobal>
1329
1330 Defined on OS/2.
1331*/
1332
1333/*!
1334 \macro Q_OS_OS2EMX
1335 \relates <QtGlobal>
1336
1337 Defined on XFree86 on OS/2 (not PM).
1338*/
1339
1340/*!
1341 \macro Q_OS_WIN32
1342 \relates <QtGlobal>
1343
1344 Defined on all supported versions of Windows.
1345*/
1346
1347/*!
1348 \macro Q_OS_WINCE
1349 \relates <QtGlobal>
1350
1351 Defined on Windows CE.
1352*/
1353
1354/*!
1355 \macro Q_OS_CYGWIN
1356 \relates <QtGlobal>
1357
1358 Defined on Cygwin.
1359*/
1360
1361/*!
1362 \macro Q_OS_SOLARIS
1363 \relates <QtGlobal>
1364
1365 Defined on Sun Solaris.
1366*/
1367
1368/*!
1369 \macro Q_OS_HPUX
1370 \relates <QtGlobal>
1371
1372 Defined on HP-UX.
1373*/
1374
1375/*!
1376 \macro Q_OS_ULTRIX
1377 \relates <QtGlobal>
1378
1379 Defined on DEC Ultrix.
1380*/
1381
1382/*!
1383 \macro Q_OS_LINUX
1384 \relates <QtGlobal>
1385
1386 Defined on Linux.
1387*/
1388
1389/*!
1390 \macro Q_OS_FREEBSD
1391 \relates <QtGlobal>
1392
1393 Defined on FreeBSD.
1394*/
1395
1396/*!
1397 \macro Q_OS_NETBSD
1398 \relates <QtGlobal>
1399
1400 Defined on NetBSD.
1401*/
1402
1403/*!
1404 \macro Q_OS_OPENBSD
1405 \relates <QtGlobal>
1406
1407 Defined on OpenBSD.
1408*/
1409
1410/*!
1411 \macro Q_OS_BSDI
1412 \relates <QtGlobal>
1413
1414 Defined on BSD/OS.
1415*/
1416
1417/*!
1418 \macro Q_OS_IRIX
1419 \relates <QtGlobal>
1420
1421 Defined on SGI Irix.
1422*/
1423
1424/*!
1425 \macro Q_OS_OSF
1426 \relates <QtGlobal>
1427
1428 Defined on HP Tru64 UNIX.
1429*/
1430
1431/*!
1432 \macro Q_OS_SCO
1433 \relates <QtGlobal>
1434
1435 Defined on SCO OpenServer 5.
1436*/
1437
1438/*!
1439 \macro Q_OS_UNIXWARE
1440 \relates <QtGlobal>
1441
1442 Defined on UnixWare 7, Open UNIX 8.
1443*/
1444
1445/*!
1446 \macro Q_OS_AIX
1447 \relates <QtGlobal>
1448
1449 Defined on AIX.
1450*/
1451
1452/*!
1453 \macro Q_OS_HURD
1454 \relates <QtGlobal>
1455
1456 Defined on GNU Hurd.
1457*/
1458
1459/*!
1460 \macro Q_OS_DGUX
1461 \relates <QtGlobal>
1462
1463 Defined on DG/UX.
1464*/
1465
1466/*!
1467 \macro Q_OS_RELIANT
1468 \relates <QtGlobal>
1469
1470 Defined on Reliant UNIX.
1471*/
1472
1473/*!
1474 \macro Q_OS_DYNIX
1475 \relates <QtGlobal>
1476
1477 Defined on DYNIX/ptx.
1478*/
1479
1480/*!
1481 \macro Q_OS_QNX
1482 \relates <QtGlobal>
1483
1484 Defined on QNX Neutrino.
1485*/
1486
1487/*!
1488 \macro Q_OS_LYNX
1489 \relates <QtGlobal>
1490
1491 Defined on LynxOS.
1492*/
1493
1494/*!
1495 \macro Q_OS_BSD4
1496 \relates <QtGlobal>
1497
1498 Defined on Any BSD 4.4 system.
1499*/
1500
1501/*!
1502 \macro Q_OS_UNIX
1503 \relates <QtGlobal>
1504
1505 Defined on Any UNIX BSD/SYSV system.
1506*/
1507
1508/*!
1509 \macro Q_CC_SYM
1510 \relates <QtGlobal>
1511
1512 Defined if the application is compiled using Digital Mars C/C++
1513 (used to be Symantec C++).
1514*/
1515
1516/*!
1517 \macro Q_CC_MWERKS
1518 \relates <QtGlobal>
1519
1520 Defined if the application is compiled using Metrowerks
1521 CodeWarrior.
1522*/
1523
1524/*!
1525 \macro Q_CC_MSVC
1526 \relates <QtGlobal>
1527
1528 Defined if the application is compiled using Microsoft Visual
1529 C/C++, Intel C++ for Windows.
1530*/
1531
1532/*!
1533 \macro Q_CC_BOR
1534 \relates <QtGlobal>
1535
1536 Defined if the application is compiled using Borland/Turbo C++.
1537*/
1538
1539/*!
1540 \macro Q_CC_WAT
1541 \relates <QtGlobal>
1542
1543 Defined if the application is compiled using Watcom C++.
1544*/
1545
1546/*!
1547 \macro Q_CC_GNU
1548 \relates <QtGlobal>
1549
1550 Defined if the application is compiled using GNU C++.
1551*/
1552
1553/*!
1554 \macro Q_CC_COMEAU
1555 \relates <QtGlobal>
1556
1557 Defined if the application is compiled using Comeau C++.
1558*/
1559
1560/*!
1561 \macro Q_CC_EDG
1562 \relates <QtGlobal>
1563
1564 Defined if the application is compiled using Edison Design Group
1565 C++.
1566*/
1567
1568/*!
1569 \macro Q_CC_OC
1570 \relates <QtGlobal>
1571
1572 Defined if the application is compiled using CenterLine C++.
1573*/
1574
1575/*!
1576 \macro Q_CC_SUN
1577 \relates <QtGlobal>
1578
1579 Defined if the application is compiled using Forte Developer, or
1580 Sun Studio C++.
1581*/
1582
1583/*!
1584 \macro Q_CC_MIPS
1585 \relates <QtGlobal>
1586
1587 Defined if the application is compiled using MIPSpro C++.
1588*/
1589
1590/*!
1591 \macro Q_CC_DEC
1592 \relates <QtGlobal>
1593
1594 Defined if the application is compiled using DEC C++.
1595*/
1596
1597/*!
1598 \macro Q_CC_HPACC
1599 \relates <QtGlobal>
1600
1601 Defined if the application is compiled using HP aC++.
1602*/
1603
1604/*!
1605 \macro Q_CC_USLC
1606 \relates <QtGlobal>
1607
1608 Defined if the application is compiled using SCO OUDK and UDK.
1609*/
1610
1611/*!
1612 \macro Q_CC_CDS
1613 \relates <QtGlobal>
1614
1615 Defined if the application is compiled using Reliant C++.
1616*/
1617
1618/*!
1619 \macro Q_CC_KAI
1620 \relates <QtGlobal>
1621
1622 Defined if the application is compiled using KAI C++.
1623*/
1624
1625/*!
1626 \macro Q_CC_INTEL
1627 \relates <QtGlobal>
1628
1629 Defined if the application is compiled using Intel C++ for Linux,
1630 Intel C++ for Windows.
1631*/
1632
1633/*!
1634 \macro Q_CC_HIGHC
1635 \relates <QtGlobal>
1636
1637 Defined if the application is compiled using MetaWare High C/C++.
1638*/
1639
1640/*!
1641 \macro Q_CC_PGI
1642 \relates <QtGlobal>
1643
1644 Defined if the application is compiled using Portland Group C++.
1645*/
1646
1647/*!
1648 \macro Q_CC_GHS
1649 \relates <QtGlobal>
1650
1651 Defined if the application is compiled using Green Hills
1652 Optimizing C++ Compilers.
1653*/
1654
1655/*!
1656 \macro Q_OS_MAC
1657 \relates <QtGlobal>
1658
1659 Defined on MAC OS (synonym for Darwin).
1660 */
1661
1662/*!
1663 \macro Q_OS_SYMBIAN
1664 \relates <QtGlobal>
1665
1666 Defined on Symbian.
1667 */
1668
1669/*!
1670 \macro Q_WS_S60
1671 \relates <QtGlobal>
1672
1673 Defined on S60.
1674
1675 \sa Q_WS_MAC, Q_WS_WIN, Q_WS_X11, Q_WS_QWS
1676 */
1677
1678#if defined(QT_BUILD_QMAKE)
1679// needed to bootstrap qmake
1680static const unsigned int qt_one = 1;
1681const int QSysInfo::ByteOrder = ((*((unsigned char *) &qt_one) == 0) ? BigEndian : LittleEndian);
1682#endif
1683
1684#if !defined(QWS) && defined(Q_OS_MAC)
1685
1686QT_BEGIN_INCLUDE_NAMESPACE
1687#include "private/qcore_mac_p.h"
1688#include "qnamespace.h"
1689QT_END_INCLUDE_NAMESPACE
1690
1691Q_CORE_EXPORT OSErr qt_mac_create_fsref(const QString &file, FSRef *fsref)
1692{
1693 return FSPathMakeRef(reinterpret_cast<const UInt8 *>(file.toUtf8().constData()), fsref, 0);
1694}
1695
1696// Don't use this function, it won't work in 10.5 (Leopard) and up
1697Q_CORE_EXPORT OSErr qt_mac_create_fsspec(const QString &file, FSSpec *spec)
1698{
1699 FSRef fsref;
1700 OSErr ret = qt_mac_create_fsref(file, &fsref);
1701 if (ret == noErr)
1702 ret = FSGetCatalogInfo(&fsref, kFSCatInfoNone, 0, 0, spec, 0);
1703 return ret;
1704}
1705
1706Q_CORE_EXPORT void qt_mac_to_pascal_string(QString s, Str255 str, TextEncoding encoding=0, int len=-1)
1707{
1708 if(len == -1)
1709 len = s.length();
1710#if 0
1711 UnicodeMapping mapping;
1712 mapping.unicodeEncoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
1713 kTextEncodingDefaultVariant,
1714 kUnicode16BitFormat);
1715 mapping.otherEncoding = (encoding ? encoding : );
1716 mapping.mappingVersion = kUnicodeUseLatestMapping;
1717
1718 UnicodeToTextInfo info;
1719 OSStatus err = CreateUnicodeToTextInfo(&mapping, &info);
1720 if(err != noErr) {
1721 qDebug("Qt: internal: Unable to create pascal string '%s'::%d [%ld]",
1722 s.left(len).latin1(), (int)encoding, err);
1723 return;
1724 }
1725 const int unilen = len * 2;
1726 const UniChar *unibuf = (UniChar *)s.unicode();
1727 ConvertFromUnicodeToPString(info, unilen, unibuf, str);
1728 DisposeUnicodeToTextInfo(&info);
1729#else
1730 Q_UNUSED(encoding);
1731 CFStringGetPascalString(QCFString(s), str, 256, CFStringGetSystemEncoding());
1732#endif
1733}
1734
1735Q_CORE_EXPORT QString qt_mac_from_pascal_string(const Str255 pstr) {
1736 return QCFString(CFStringCreateWithPascalString(0, pstr, CFStringGetSystemEncoding()));
1737}
1738
1739
1740
1741static QSysInfo::MacVersion macVersion()
1742{
1743 SInt32 gestalt_version;
1744 if (Gestalt(gestaltSystemVersion, &gestalt_version) == noErr) {
1745 return QSysInfo::MacVersion(((gestalt_version & 0x00F0) >> 4) + 2);
1746 }
1747 return QSysInfo::MV_Unknown;
1748}
1749const QSysInfo::MacVersion QSysInfo::MacintoshVersion = macVersion();
1750
1751#elif defined(Q_OS_WIN32) || defined(Q_OS_CYGWIN) || defined(Q_OS_WINCE)
1752
1753QT_BEGIN_INCLUDE_NAMESPACE
1754#include "qt_windows.h"
1755QT_END_INCLUDE_NAMESPACE
1756
1757QSysInfo::WinVersion QSysInfo::windowsVersion()
1758{
1759#ifndef VER_PLATFORM_WIN32s
1760#define VER_PLATFORM_WIN32s 0
1761#endif
1762#ifndef VER_PLATFORM_WIN32_WINDOWS
1763#define VER_PLATFORM_WIN32_WINDOWS 1
1764#endif
1765#ifndef VER_PLATFORM_WIN32_NT
1766#define VER_PLATFORM_WIN32_NT 2
1767#endif
1768#ifndef VER_PLATFORM_WIN32_CE
1769#define VER_PLATFORM_WIN32_CE 3
1770#endif
1771
1772 static QSysInfo::WinVersion winver;
1773 if (winver)
1774 return winver;
1775 winver = QSysInfo::WV_NT;
1776 OSVERSIONINFOW osver;
1777 osver.dwOSVersionInfoSize = sizeof(osver);
1778 GetVersionEx(&osver);
1779#ifdef Q_OS_WINCE
1780 DWORD qt_cever = 0;
1781 qt_cever = osver.dwMajorVersion * 100;
1782 qt_cever += osver.dwMinorVersion * 10;
1783#endif
1784 switch (osver.dwPlatformId) {
1785 case VER_PLATFORM_WIN32s:
1786 winver = QSysInfo::WV_32s;
1787 break;
1788 case VER_PLATFORM_WIN32_WINDOWS:
1789 // We treat Windows Me (minor 90) the same as Windows 98
1790 if (osver.dwMinorVersion == 90)
1791 winver = QSysInfo::WV_Me;
1792 else if (osver.dwMinorVersion == 10)
1793 winver = QSysInfo::WV_98;
1794 else
1795 winver = QSysInfo::WV_95;
1796 break;
1797#ifdef Q_OS_WINCE
1798 case VER_PLATFORM_WIN32_CE:
1799 if (qt_cever >= 600)
1800 winver = QSysInfo::WV_CE_6;
1801 if (qt_cever >= 500)
1802 winver = QSysInfo::WV_CE_5;
1803 else if (qt_cever >= 400)
1804 winver = QSysInfo::WV_CENET;
1805 else
1806 winver = QSysInfo::WV_CE;
1807 break;
1808#endif
1809 default: // VER_PLATFORM_WIN32_NT
1810 if (osver.dwMajorVersion < 5) {
1811 winver = QSysInfo::WV_NT;
1812 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0) {
1813 winver = QSysInfo::WV_2000;
1814 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1) {
1815 winver = QSysInfo::WV_XP;
1816 } else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2) {
1817 winver = QSysInfo::WV_2003;
1818 } else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0) {
1819 winver = QSysInfo::WV_VISTA;
1820 } else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1) {
1821 winver = QSysInfo::WV_WINDOWS7;
1822 } else {
1823 qWarning("Qt: Untested Windows version %d.%d detected!",
1824 int(osver.dwMajorVersion), int(osver.dwMinorVersion));
1825 winver = QSysInfo::WV_NT_based;
1826 }
1827 }
1828
1829#ifdef QT_DEBUG
1830 {
1831 QByteArray override = qgetenv("QT_WINVER_OVERRIDE");
1832 if (override.isEmpty())
1833 return winver;
1834
1835 if (override == "Me")
1836 winver = QSysInfo::WV_Me;
1837 if (override == "95")
1838 winver = QSysInfo::WV_95;
1839 else if (override == "98")
1840 winver = QSysInfo::WV_98;
1841 else if (override == "NT")
1842 winver = QSysInfo::WV_NT;
1843 else if (override == "2000")
1844 winver = QSysInfo::WV_2000;
1845 else if (override == "2003")
1846 winver = QSysInfo::WV_2003;
1847 else if (override == "XP")
1848 winver = QSysInfo::WV_XP;
1849 else if (override == "VISTA")
1850 winver = QSysInfo::WV_VISTA;
1851 else if (override == "WINDOWS7")
1852 winver = QSysInfo::WV_WINDOWS7;
1853 }
1854#endif
1855
1856 return winver;
1857}
1858
1859const QSysInfo::WinVersion QSysInfo::WindowsVersion = QSysInfo::windowsVersion();
1860
1861#endif
1862
1863#ifdef Q_OS_SYMBIAN
1864# ifdef Q_WS_S60
1865static QSysInfo::S60Version cachedS60Version = QSysInfo::S60Version(-1);
1866
1867QSysInfo::S60Version QSysInfo::s60Version()
1868{
1869 if (cachedS60Version != -1)
1870 return cachedS60Version;
1871
1872 // Use pure Symbian code, because if done using QDir, there will be a call back
1873 // to this method, resulting doing this expensive operation twice before the cache kicks in.
1874 // Pure Symbian code also makes this method ~10x faster, speeding up the application launch.
1875 RFs rfs = qt_s60GetRFs();
1876 TFindFile fileFinder(rfs);
1877 CDir* contents;
1878 TInt err = fileFinder.FindWildByDir(qt_S60Filter, qt_S60SystemInstallDir, contents);
1879 if (err == KErrNone) {
1880 err = contents->Sort(EDescending|ESortByName);
1881 if (err == KErrNone && contents->Count() > 0 && (*contents)[0].iName.Length() >= 12) {
1882 TInt major = (*contents)[0].iName[9] - '0';
1883 TInt minor = (*contents)[0].iName[11] - '0';
1884 if (major == 3) {
1885 if (minor == 1) {
1886 return cachedS60Version = SV_S60_3_1;
1887 } else if (minor == 2) {
1888 return cachedS60Version = SV_S60_3_2;
1889 }
1890 } else if (major == 5) {
1891 if (minor == 0) {
1892 return cachedS60Version = SV_S60_5_0;
1893 }
1894 else if (minor == 1) {
1895 return cachedS60Version = SV_S60_5_1;
1896 }
1897 else if (minor == 2) {
1898 return cachedS60Version = SV_S60_5_2;
1899 }
1900 }
1901 }
1902 delete contents;
1903 }
1904
1905# ifdef Q_CC_NOKIAX86
1906 // Some emulator environments may not contain the version specific .sis files, so
1907 // simply hardcode the version on those environments.
1908# if defined(__SERIES60_31__)
1909 return cachedS60Version = SV_S60_3_1;
1910# elif defined(__S60_32__)
1911 return cachedS60Version = SV_S60_3_2;
1912# elif defined(__S60_50__)
1913 return cachedS60Version = SV_S60_5_0;
1914# endif
1915# endif
1916 //If reaching here, it was not possible to determine the version
1917 return cachedS60Version = SV_S60_Unknown;
1918}
1919QSysInfo::SymbianVersion QSysInfo::symbianVersion()
1920{
1921 switch (s60Version()) {
1922 case SV_S60_3_1:
1923 return SV_9_2;
1924 case SV_S60_3_2:
1925 return SV_9_3;
1926 case SV_S60_5_0:
1927 return SV_9_4;
1928 case SV_S60_5_1:
1929 return SV_9_4;
1930 case SV_S60_5_2:
1931 return SV_9_4;
1932 default:
1933 return SV_Unknown;
1934 }
1935}
1936#else
1937QSysInfo::S60Version QSysInfo::s60Version()
1938{
1939 return SV_S60_None;
1940}
1941
1942QSysInfo::SymbianVersion QSysInfo::symbianVersion()
1943{
1944 return SV_Unknown;
1945}
1946# endif // ifdef Q_WS_S60
1947#endif // ifdef Q_OS_SYMBIAN
1948
1949#ifdef Q_OS_OS2
1950
1951// Returns a version number of the component taken directly from its SYSLEVEL
1952// file (see http://www.edm2.com/index.php/Using_SYSLEVEL_Files_in_Your_Applications
1953// for more information about SYSLEVEL file internals)
1954static bool getSyslevelVersion(const QByteArray &fileName, char (*version)[2])
1955{
1956 bool success = false;
1957 FILE *f = fopen(fileName, "rb");
1958 if (!f)
1959 return success;
1960 // go to the char d_version[2] field of the SYSLEVEL data structure
1961 if ((success = fseek(f, 0x28, SEEK_SET) == 0)) {
1962 // read in two bytes
1963 success = fread(*version, 1, 2, f) == 2;
1964 }
1965 fclose(f);
1966 return success;
1967}
1968
1969QSysInfo::Os2Version QSysInfo::os2Version()
1970{
1971 static QSysInfo::Os2Version os2ver = OV_Unknown;
1972 static bool os2VerSet = false;
1973
1974 if (os2VerSet)
1975 return os2ver;
1976 os2VerSet = true;
1977
1978 // detect the basic version number
1979 ULONG buf[3];
1980 if (DosQuerySysInfo(QSV_VERSION_MAJOR, QSV_VERSION_REVISION,
1981 &buf, sizeof(buf)) != NO_ERROR)
1982 return os2ver;
1983
1984 if (buf[0] != 20) // QSV_VERSION_MAJOR
1985 return os2ver;
1986
1987 switch (buf[1]) { // QSV_VERSION_MINOR
1988 case 00: os2ver = OV_2_0; return os2ver;
1989 case 10: os2ver = OV_2_1; return os2ver;
1990 case 11: os2ver = OV_2_11; return os2ver;
1991 case 30: os2ver = OV_3_0; return os2ver;
1992 case 40: os2ver = OV_4_0; return os2ver;
1993 case 45: break;
1994 default: return os2ver;
1995 }
1996
1997 if (DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE,
1998 &buf, sizeof(buf)) != NO_ERROR)
1999 return os2ver;
2000
2001 // See http://ewiki.ecomstation.nl/HowToDetectVersion on how to detect
2002 // an eComStation version
2003
2004 // read SYSLEVEL.OS2
2005 QByteArray fn = QByteArray("A:\\OS2\\INSTALL\\SYSLEVEL.OS2");
2006 fn.data()[0] += buf[0] - 1;
2007 char sys_os2_ver[2];
2008 if (!getSyslevelVersion(fn, &sys_os2_ver))
2009 return os2ver;
2010
2011 // read SYSLEVEL.ECS
2012 fn = QByteArray("A:\\OS2\\INSTALL\\SYSLEVEL.ECS");
2013 fn.data()[0] += buf[0] - 1;
2014 char sys_ecs_ver[2];
2015 if (!getSyslevelVersion(fn, &sys_ecs_ver)) {
2016 // no or broken SYSLEVEL.ECS, it's either eCS 1.1 or below or non-eCS
2017 bool have_ecsreg = false;
2018 bool have_ecsreg11 = false;
2019 fn = QByteArray("A:\\OS2\\ECSREG.INI");
2020 fn.data()[0] += buf[0] - 1;
2021 have_ecsreg = access(fn, F_OK) == 0;
2022 fn = QByteArray("A:\\OS2\\ECSREG11.INI");
2023 fn.data()[0] += buf[0] - 1;
2024 have_ecsreg11 = access(fn, F_OK) == 0;
2025 // detect eCS 1.1
2026 if (have_ecsreg && have_ecsreg11)
2027 return os2ver = OV_ECS_1_1;
2028 // detect eCS 1.0
2029 if (have_ecsreg)
2030 return os2ver = OV_ECS_1_0;
2031 // detect WSeB (4.52)
2032 if (sys_os2_ver[0] == 0x45 &&sys_os2_ver[1] == 0x02)
2033 return os2ver = OV_4_52;
2034 // fallback to Aurora (QSV_VERSION_MINOR is 45 here)
2035 return os2ver = OV_4_5;
2036 }
2037
2038 // detect eCS >= 1.2
2039 if (sys_ecs_ver[0] == 0x12 && sys_ecs_ver[1] == 0x00)
2040 return os2ver = OV_ECS_1_2;
2041 if (sys_ecs_ver[0] == 0x20 && sys_ecs_ver[1] == 0x00)
2042 return os2ver = OV_ECS_2_0;
2043
2044 return os2ver = OV_ECS_Unknown;
2045}
2046
2047static QSysInfo::Os2Version os2verDummy = QSysInfo::os2Version();
2048
2049#endif // ifdef Q_OS_OS2
2050
2051/*!
2052 \macro void Q_ASSERT(bool test)
2053 \relates <QtGlobal>
2054
2055 Prints a warning message containing the source code file name and
2056 line number if \a test is false.
2057
2058 Q_ASSERT() is useful for testing pre- and post-conditions
2059 during development. It does nothing if \c QT_NO_DEBUG was defined
2060 during compilation.
2061
2062 Example:
2063
2064 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 17
2065
2066 If \c b is zero, the Q_ASSERT statement will output the following
2067 message using the qFatal() function:
2068
2069 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 18
2070
2071 \sa Q_ASSERT_X(), qFatal(), {Debugging Techniques}
2072*/
2073
2074/*!
2075 \macro void Q_ASSERT_X(bool test, const char *where, const char *what)
2076 \relates <QtGlobal>
2077
2078 Prints the message \a what together with the location \a where,
2079 the source file name and line number if \a test is false.
2080
2081 Q_ASSERT_X is useful for testing pre- and post-conditions during
2082 development. It does nothing if \c QT_NO_DEBUG was defined during
2083 compilation.
2084
2085 Example:
2086
2087 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 19
2088
2089 If \c b is zero, the Q_ASSERT_X statement will output the following
2090 message using the qFatal() function:
2091
2092 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 20
2093
2094 \sa Q_ASSERT(), qFatal(), {Debugging Techniques}
2095*/
2096
2097/*!
2098 \macro void Q_CHECK_PTR(void *pointer)
2099 \relates <QtGlobal>
2100
2101 If \a pointer is 0, prints a warning message containing the source
2102 code's file name and line number, saying that the program ran out
2103 of memory.
2104
2105 Q_CHECK_PTR does nothing if \c QT_NO_DEBUG was defined during
2106 compilation.
2107
2108 Example:
2109
2110 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 21
2111
2112 \sa qWarning(), {Debugging Techniques}
2113*/
2114
2115/*!
2116 \fn T *q_check_ptr(T *pointer)
2117 \relates <QtGlobal>
2118
2119 Users Q_CHECK_PTR on \a pointer, then returns \a pointer.
2120
2121 This can be used as an inline version of Q_CHECK_PTR.
2122*/
2123
2124/*!
2125 \macro const char* Q_FUNC_INFO()
2126 \relates <QtGlobal>
2127
2128 Expands to a string that describe the function the macro resides in. How this string looks
2129 more specifically is compiler dependent. With GNU GCC it is typically the function signature,
2130 while with other compilers it might be the line and column number.
2131
2132 Q_FUNC_INFO can be conveniently used with qDebug(). For example, this function:
2133
2134 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 22
2135
2136 when instantiated with the integer type, will with the GCC compiler produce:
2137
2138 \tt{const TInputType& myMin(const TInputType&, const TInputType&) [with TInputType = int] was called with value1: 3 value2: 4}
2139
2140 If this macro is used outside a function, the behavior is undefined.
2141 */
2142
2143/*
2144 The Q_CHECK_PTR macro calls this function if an allocation check
2145 fails.
2146*/
2147void qt_check_pointer(const char *n, int l)
2148{
2149 qWarning("In file %s, line %d: Out of memory", n, l);
2150}
2151
2152/* \internal
2153 Allows you to throw an exception without including <new>
2154 Called internally from Q_CHECK_PTR on certain OS combinations
2155*/
2156void qBadAlloc()
2157{
2158 QT_THROW(std::bad_alloc());
2159}
2160
2161/*
2162 The Q_ASSERT macro calls this function when the test fails.
2163*/
2164void qt_assert(const char *assertion, const char *file, int line)
2165{
2166 qFatal("ASSERT: \"%s\" in file %s, line %d", assertion, file, line);
2167}
2168
2169/*
2170 The Q_ASSERT_X macro calls this function when the test fails.
2171*/
2172void qt_assert_x(const char *where, const char *what, const char *file, int line)
2173{
2174 qFatal("ASSERT failure in %s: \"%s\", file %s, line %d", where, what, file, line);
2175}
2176
2177
2178/*
2179 Dijkstra's bisection algorithm to find the square root of an integer.
2180 Deliberately not exported as part of the Qt API, but used in both
2181 qsimplerichtext.cpp and qgfxraster_qws.cpp
2182*/
2183Q_CORE_EXPORT unsigned int qt_int_sqrt(unsigned int n)
2184{
2185 // n must be in the range 0...UINT_MAX/2-1
2186 if (n >= (UINT_MAX>>2)) {
2187 unsigned int r = 2 * qt_int_sqrt(n / 4);
2188 unsigned int r2 = r + 1;
2189 return (n >= r2 * r2) ? r2 : r;
2190 }
2191 uint h, p= 0, q= 1, r= n;
2192 while (q <= n)
2193 q <<= 2;
2194 while (q != 1) {
2195 q >>= 2;
2196 h= p + q;
2197 p >>= 1;
2198 if (r >= h) {
2199 p += q;
2200 r -= h;
2201 }
2202 }
2203 return p;
2204}
2205
2206#if defined(qMemCopy)
2207# undef qMemCopy
2208#endif
2209#if defined(qMemSet)
2210# undef qMemSet
2211#endif
2212
2213void *qMemCopy(void *dest, const void *src, size_t n) { return memcpy(dest, src, n); }
2214void *qMemSet(void *dest, int c, size_t n) { return memset(dest, c, n); }
2215
2216static QtMsgHandler handler = 0; // pointer to debug handler
2217
2218#if defined(Q_CC_MWERKS) && defined(Q_OS_MACX)
2219extern bool qt_is_gui_used;
2220static void mac_default_handler(const char *msg)
2221{
2222 if (qt_is_gui_used) {
2223 Str255 pmsg;
2224 qt_mac_to_pascal_string(msg, pmsg);
2225 DebugStr(pmsg);
2226 } else {
2227 fprintf(stderr, msg);
2228 }
2229}
2230#endif // Q_CC_MWERKS && Q_OS_MACX
2231
2232#if !defined(Q_OS_WIN) && !defined(QT_NO_THREAD) && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX) && \
2233 defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L
2234namespace {
2235 // There are two incompatible versions of strerror_r:
2236 // a) the XSI/POSIX.1 version, which returns an int,
2237 // indicating success or not
2238 // b) the GNU version, which returns a char*, which may or may not
2239 // be the beginning of the buffer we used
2240 // The GNU libc manpage for strerror_r says you should use the the XSI
2241 // version in portable code. However, it's impossible to do that if
2242 // _GNU_SOURCE is defined so we use C++ overloading to decide what to do
2243 // depending on the return type
2244 static inline QString fromstrerror_helper(int, const QByteArray &buf)
2245 {
2246 return QString::fromLocal8Bit(buf);
2247 }
2248 static inline QString fromstrerror_helper(const char *str, const QByteArray &)
2249 {
2250 return QString::fromLocal8Bit(str);
2251 }
2252}
2253#endif
2254
2255QString qt_error_string(int errorCode)
2256{
2257 const char *s = 0;
2258 QString ret;
2259 if (errorCode == -1) {
2260#if defined(Q_OS_WIN)
2261 errorCode = GetLastError();
2262#else
2263 errorCode = errno;
2264#endif
2265 }
2266 switch (errorCode) {
2267 case 0:
2268 break;
2269 case EACCES:
2270 s = QT_TRANSLATE_NOOP("QIODevice", "Permission denied");
2271 break;
2272 case EMFILE:
2273 s = QT_TRANSLATE_NOOP("QIODevice", "Too many open files");
2274 break;
2275 case ENOENT:
2276 s = QT_TRANSLATE_NOOP("QIODevice", "No such file or directory");
2277 break;
2278 case ENOSPC:
2279 s = QT_TRANSLATE_NOOP("QIODevice", "No space left on device");
2280 break;
2281 default: {
2282#ifdef Q_OS_WIN
2283 wchar_t *string = 0;
2284 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
2285 NULL,
2286 errorCode,
2287 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
2288 (LPWSTR)&string,
2289 0,
2290 NULL);
2291 ret = QString::fromWCharArray(string);
2292 LocalFree((HLOCAL)string);
2293
2294 if (ret.isEmpty() && errorCode == ERROR_MOD_NOT_FOUND)
2295 ret = QString::fromLatin1("The specified module could not be found.");
2296#elif !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L && !defined(Q_OS_INTEGRITY) && !defined(Q_OS_QNX)
2297 QByteArray buf(1024, '\0');
2298 ret = fromstrerror_helper(strerror_r(errorCode, buf.data(), buf.size()), buf);
2299#else
2300 ret = QString::fromLocal8Bit(strerror(errorCode));
2301#endif
2302 break; }
2303 }
2304 if (s)
2305 // ######## this breaks moc build currently
2306// ret = QCoreApplication::translate("QIODevice", s);
2307 ret = QString::fromLatin1(s);
2308 return ret.trimmed();
2309}
2310
2311
2312/*!
2313 \fn QtMsgHandler qInstallMsgHandler(QtMsgHandler handler)
2314 \relates <QtGlobal>
2315
2316 Installs a Qt message \a handler which has been defined
2317 previously. Returns a pointer to the previous message handler
2318 (which may be 0).
2319
2320 The message handler is a function that prints out debug messages,
2321 warnings, critical and fatal error messages. The Qt library (debug
2322 mode) contains hundreds of warning messages that are printed
2323 when internal errors (usually invalid function arguments)
2324 occur. Qt built in release mode also contains such warnings unless
2325 QT_NO_WARNING_OUTPUT and/or QT_NO_DEBUG_OUTPUT have been set during
2326 compilation. If you implement your own message handler, you get total
2327 control of these messages.
2328
2329 The default message handler prints the message to the standard
2330 output under X11 or to the debugger under Windows. If it is a
2331 fatal message, the application aborts immediately.
2332
2333 Only one message handler can be defined, since this is usually
2334 done on an application-wide basis to control debug output.
2335
2336 To restore the message handler, call \c qInstallMsgHandler(0).
2337
2338 Example:
2339
2340 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 23
2341
2342 \sa qDebug(), qWarning(), qCritical(), qFatal(), QtMsgType,
2343 {Debugging Techniques}
2344*/
2345#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB)
2346extern bool usingWinMain;
2347extern Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char* str);
2348#endif
2349
2350QtMsgHandler qInstallMsgHandler(QtMsgHandler h)
2351{
2352 QtMsgHandler old = handler;
2353 handler = h;
2354#if defined(Q_OS_WIN) && defined(QT_BUILD_CORE_LIB)
2355 if (!handler && usingWinMain)
2356 handler = qWinMsgHandler;
2357#endif
2358 return old;
2359}
2360
2361/*!
2362 \internal
2363*/
2364void qt_message_output(QtMsgType msgType, const char *buf)
2365{
2366 if (handler) {
2367 (*handler)(msgType, buf);
2368 } else {
2369#if defined(Q_CC_MWERKS) && defined(Q_OS_MACX)
2370 mac_default_handler(buf);
2371#elif defined(Q_OS_WINCE)
2372 QString fstr = QString::fromLatin1(buf);
2373 fstr += QLatin1Char('\n');
2374 OutputDebugString(reinterpret_cast<const wchar_t *> (fstr.utf16()));
2375#elif defined(Q_OS_SYMBIAN)
2376 // RDebug::Print has a cap of 256 characters so break it up
2377 _LIT(format, "[Qt Message] %S");
2378 const int maxBlockSize = 256 - ((const TDesC &)format).Length();
2379 const TPtrC8 ptr(reinterpret_cast<const TUint8*>(buf));
2380 HBufC* hbuffer = q_check_ptr(HBufC::New(qMin(maxBlockSize, ptr.Length())));
2381 for (int i = 0; i < ptr.Length(); i += hbuffer->Length()) {
2382 hbuffer->Des().Copy(ptr.Mid(i, qMin(maxBlockSize, ptr.Length()-i)));
2383 RDebug::Print(format, hbuffer);
2384 }
2385 delete hbuffer;
2386#else
2387 fprintf(stderr, "%s\n", buf);
2388 fflush(stderr);
2389#endif
2390 }
2391
2392 if (msgType == QtFatalMsg
2393 || (msgType == QtWarningMsg
2394 && (!qgetenv("QT_FATAL_WARNINGS").isNull())) ) {
2395
2396#if defined(Q_CC_MSVC) && defined(QT_DEBUG) && defined(_DEBUG) && defined(_CRT_ERROR)
2397 // get the current report mode
2398 int reportMode = _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_WNDW);
2399 _CrtSetReportMode(_CRT_ERROR, reportMode);
2400#if !defined(Q_OS_WINCE)
2401 int ret = _CrtDbgReport(_CRT_ERROR, __FILE__, __LINE__, QT_VERSION_STR, buf);
2402#else
2403 int ret = _CrtDbgReportW(_CRT_ERROR, _CRT_WIDE(__FILE__),
2404 __LINE__, _CRT_WIDE(QT_VERSION_STR), reinterpret_cast<const wchar_t *> (QString::fromLatin1(buf).utf16()));
2405#endif
2406 if (ret == 0 && reportMode & _CRTDBG_MODE_WNDW)
2407 return; // ignore
2408 else if (ret == 1)
2409 _CrtDbgBreak();
2410#endif
2411
2412#if defined(Q_OS_SYMBIAN)
2413 __DEBUGGER(); // on the emulator, get the debugger to kick in if there's one around
2414 TBuf<256> tmp;
2415 TPtrC8 ptr(reinterpret_cast<const TUint8*>(buf));
2416 TInt len = Min(tmp.MaxLength(), ptr.Length());
2417 tmp.Copy(ptr.Left(len));
2418 // Panic the current thread. We don't use real panic codes, so 0 has no special meaning.
2419 User::Panic(tmp, 0);
2420#elif (defined(Q_OS_UNIX) || defined(Q_CC_MINGW))
2421 abort(); // trap; generates core dump
2422#else
2423 exit(1); // goodbye cruel world
2424#endif
2425 }
2426}
2427
2428#if !defined(QT_NO_EXCEPTIONS)
2429/*!
2430 \internal
2431 Uses a local buffer to output the message. Not locale safe + cuts off
2432 everything after character 255, but will work in out of memory situations.
2433*/
2434static void qEmergencyOut(QtMsgType msgType, const char *msg, va_list ap)
2435{
2436 char emergency_buf[256] = { '\0' };
2437 emergency_buf[255] = '\0';
2438 if (msg)
2439 qvsnprintf(emergency_buf, 255, msg, ap);
2440 qt_message_output(msgType, emergency_buf);
2441}
2442#endif
2443
2444/*!
2445 \internal
2446*/
2447static void qt_message(QtMsgType msgType, const char *msg, va_list ap)
2448{
2449#if !defined(QT_NO_EXCEPTIONS)
2450 if (std::uncaught_exception()) {
2451 qEmergencyOut(msgType, msg, ap);
2452 return;
2453 }
2454#endif
2455 QByteArray buf;
2456 if (msg) {
2457 QT_TRY {
2458 buf = QString().vsprintf(msg, ap).toLocal8Bit();
2459 } QT_CATCH(const std::bad_alloc &) {
2460#if !defined(QT_NO_EXCEPTIONS)
2461 qEmergencyOut(msgType, msg, ap);
2462 // don't rethrow - we use qWarning and friends in destructors.
2463 return;
2464#endif
2465 }
2466 }
2467 qt_message_output(msgType, buf.constData());
2468}
2469
2470#undef qDebug
2471/*!
2472 \relates <QtGlobal>
2473
2474 Calls the message handler with the debug message \a msg. If no
2475 message handler has been installed, the message is printed to
2476 stderr. Under Windows, the message is sent to the console, if it is a
2477 console application; otherwise, it is sent to the debugger. This
2478 function does nothing if \c QT_NO_DEBUG_OUTPUT was defined
2479 during compilation.
2480
2481 If you pass the function a format string and a list of arguments,
2482 it works in similar way to the C printf() function. The format
2483 should be a Latin-1 string.
2484
2485 Example:
2486
2487 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 24
2488
2489 If you include \c <QtDebug>, a more convenient syntax is also
2490 available:
2491
2492 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 25
2493
2494 With this syntax, the function returns a QDebug object that is
2495 configured to use the QtDebugMsg message type. It automatically
2496 puts a single space between each item, and outputs a newline at
2497 the end. It supports many C++ and Qt types.
2498
2499 To suppress the output at run-time, install your own message handler
2500 with qInstallMsgHandler().
2501
2502 \sa qWarning(), qCritical(), qFatal(), qInstallMsgHandler(),
2503 {Debugging Techniques}
2504*/
2505void qDebug(const char *msg, ...)
2506{
2507 va_list ap;
2508 va_start(ap, msg); // use variable arg list
2509 qt_message(QtDebugMsg, msg, ap);
2510 va_end(ap);
2511}
2512
2513#undef qWarning
2514/*!
2515 \relates <QtGlobal>
2516
2517 Calls the message handler with the warning message \a msg. If no
2518 message handler has been installed, the message is printed to
2519 stderr. Under Windows, the message is sent to the debugger. This
2520 function does nothing if \c QT_NO_WARNING_OUTPUT was defined
2521 during compilation; it exits if the environment variable \c
2522 QT_FATAL_WARNINGS is defined.
2523
2524 This function takes a format string and a list of arguments,
2525 similar to the C printf() function. The format should be a Latin-1
2526 string.
2527
2528 Example:
2529 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 26
2530
2531 If you include <QtDebug>, a more convenient syntax is
2532 also available:
2533
2534 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 27
2535
2536 This syntax inserts a space between each item, and
2537 appends a newline at the end.
2538
2539 To supress the output at runtime, install your own message handler
2540 with qInstallMsgHandler().
2541
2542 \sa qDebug(), qCritical(), qFatal(), qInstallMsgHandler(),
2543 {Debugging Techniques}
2544*/
2545void qWarning(const char *msg, ...)
2546{
2547 va_list ap;
2548 va_start(ap, msg); // use variable arg list
2549 qt_message(QtWarningMsg, msg, ap);
2550 va_end(ap);
2551}
2552
2553/*!
2554 \relates <QtGlobal>
2555
2556 Calls the message handler with the critical message \a msg. If no
2557 message handler has been installed, the message is printed to
2558 stderr. Under Windows, the message is sent to the debugger.
2559
2560 This function takes a format string and a list of arguments,
2561 similar to the C printf() function. The format should be a Latin-1
2562 string.
2563
2564 Example:
2565 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 28
2566
2567 If you include <QtDebug>, a more convenient syntax is
2568 also available:
2569
2570 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 29
2571
2572 A space is inserted between the items, and a newline is
2573 appended at the end.
2574
2575 To supress the output at runtime, install your own message handler
2576 with qInstallMsgHandler().
2577
2578 \sa qDebug(), qWarning(), qFatal(), qInstallMsgHandler(),
2579 {Debugging Techniques}
2580*/
2581void qCritical(const char *msg, ...)
2582{
2583 va_list ap;
2584 va_start(ap, msg); // use variable arg list
2585 qt_message(QtCriticalMsg, msg, ap);
2586 va_end(ap);
2587}
2588
2589#ifdef QT3_SUPPORT
2590void qSystemWarning(const char *msg, int code)
2591 { qCritical("%s (%s)", msg, qt_error_string(code).toLocal8Bit().constData()); }
2592#endif // QT3_SUPPORT
2593
2594void qErrnoWarning(const char *msg, ...)
2595{
2596 // qt_error_string() will allocate anyway, so we don't have
2597 // to be careful here (like we do in plain qWarning())
2598 QString buf;
2599 va_list ap;
2600 va_start(ap, msg);
2601 if (msg)
2602 buf.vsprintf(msg, ap);
2603 va_end(ap);
2604
2605 qCritical("%s (%s)", buf.toLocal8Bit().constData(), qt_error_string(-1).toLocal8Bit().constData());
2606}
2607
2608void qErrnoWarning(int code, const char *msg, ...)
2609{
2610 // qt_error_string() will allocate anyway, so we don't have
2611 // to be careful here (like we do in plain qWarning())
2612 QString buf;
2613 va_list ap;
2614 va_start(ap, msg);
2615 if (msg)
2616 buf.vsprintf(msg, ap);
2617 va_end(ap);
2618
2619 qCritical("%s (%s)", buf.toLocal8Bit().constData(), qt_error_string(code).toLocal8Bit().constData());
2620}
2621
2622/*!
2623 \relates <QtGlobal>
2624
2625 Calls the message handler with the fatal message \a msg. If no
2626 message handler has been installed, the message is printed to
2627 stderr. Under Windows, the message is sent to the debugger.
2628
2629 If you are using the \bold{default message handler} this function will
2630 abort on Unix systems to create a core dump. On Windows, for debug builds,
2631 this function will report a _CRT_ERROR enabling you to connect a debugger
2632 to the application.
2633
2634 This function takes a format string and a list of arguments,
2635 similar to the C printf() function.
2636
2637 Example:
2638 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 30
2639
2640 To supress the output at runtime, install your own message handler
2641 with qInstallMsgHandler().
2642
2643 \sa qDebug(), qCritical(), qWarning(), qInstallMsgHandler(),
2644 {Debugging Techniques}
2645*/
2646void qFatal(const char *msg, ...)
2647{
2648 va_list ap;
2649 va_start(ap, msg); // use variable arg list
2650 qt_message(QtFatalMsg, msg, ap);
2651 va_end(ap);
2652}
2653
2654// getenv is declared as deprecated in VS2005. This function
2655// makes use of the new secure getenv function.
2656QByteArray qgetenv(const char *varName)
2657{
2658#if defined(_MSC_VER) && _MSC_VER >= 1400
2659 size_t requiredSize = 0;
2660 QByteArray buffer;
2661 getenv_s(&requiredSize, 0, 0, varName);
2662 if (requiredSize == 0)
2663 return buffer;
2664 buffer.resize(int(requiredSize));
2665 getenv_s(&requiredSize, buffer.data(), requiredSize, varName);
2666 // requiredSize includes the terminating null, which we don't want.
2667 Q_ASSERT(buffer.endsWith('\0'));
2668 buffer.chop(1);
2669 return buffer;
2670#else
2671 return QByteArray(::getenv(varName));
2672#endif
2673}
2674
2675bool qputenv(const char *varName, const QByteArray& value)
2676{
2677#if defined(_MSC_VER) && _MSC_VER >= 1400
2678 return _putenv_s(varName, value.constData()) == 0;
2679#else
2680 QByteArray buffer(varName);
2681 buffer += '=';
2682 buffer += value;
2683 char* envVar = qstrdup(buffer.constData());
2684 int result = putenv(envVar);
2685 if (result != 0) // error. we have to delete the string.
2686 delete[] envVar;
2687 return result == 0;
2688#endif
2689}
2690
2691#if (defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD)
2692
2693# if defined(Q_OS_INTEGRITY) && defined(__GHS_VERSION_NUMBER) && (__GHS_VERSION_NUMBER < 500)
2694// older versions of INTEGRITY used a long instead of a uint for the seed.
2695typedef long SeedStorageType;
2696# else
2697typedef uint SeedStorageType;
2698# endif
2699
2700typedef QThreadStorage<SeedStorageType *> SeedStorage;
2701Q_GLOBAL_STATIC(SeedStorage, randTLS) // Thread Local Storage for seed value
2702
2703#endif
2704
2705/*!
2706 \relates <QtGlobal>
2707 \since 4.2
2708
2709 Thread-safe version of the standard C++ \c srand() function.
2710
2711 Sets the argument \a seed to be used to generate a new random number sequence of
2712 pseudo random integers to be returned by qrand().
2713
2714 The sequence of random numbers generated is deterministic per thread. For example,
2715 if two threads call qsrand(1) and subsequently calls qrand(), the threads will get
2716 the same random number sequence.
2717
2718 \sa qrand()
2719*/
2720void qsrand(uint seed)
2721{
2722#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN)
2723 SeedStorage *seedStorage = randTLS();
2724 if (seedStorage) {
2725 SeedStorageType *pseed = seedStorage->localData();
2726 if (!pseed)
2727 seedStorage->setLocalData(pseed = new SeedStorageType);
2728 *pseed = seed;
2729 } else {
2730 //golbal static seed storage should always exist,
2731 //except after being deleted by QGlobalStaticDeleter.
2732 //But since it still can be called from destructor of another
2733 //global static object, fallback to sqrand(seed)
2734 srand(seed);
2735 }
2736#else
2737 // On Windows and Symbian srand() and rand() already use Thread-Local-Storage
2738 // to store the seed between calls
2739 // this is also valid for QT_NO_THREAD
2740 srand(seed);
2741#endif
2742}
2743
2744/*! \internal
2745 \relates <QtGlobal>
2746 \since 4.6
2747
2748 Seed the PRNG, but only if it has not already been seeded.
2749
2750 The default seed is a combination of current time, a stack address and a
2751 serial counter (since thread stack addresses are re-used).
2752*/
2753void qsrand()
2754{
2755#if (defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD)
2756 SeedStorage *seedStorage = randTLS();
2757 if (seedStorage) {
2758 SeedStorageType *pseed = seedStorage->localData();
2759 if (pseed) {
2760 // already seeded
2761 return;
2762 }
2763 seedStorage->setLocalData(pseed = new SeedStorageType);
2764 // start beyond 1 to avoid the sequence reset
2765 static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2);
2766 *pseed = QDateTime::currentDateTime().toTime_t()
2767 + quintptr(&pseed)
2768 + serial.fetchAndAddRelaxed(1);
2769#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
2770 // for Windows and Symbian the srand function must still be called.
2771 srand(*pseed);
2772#endif
2773 }
2774
2775//QT_NO_THREAD implementations
2776#else
2777 static unsigned int seed = 0;
2778
2779 if (seed)
2780 return;
2781
2782#if defined(Q_OS_SYMBIAN)
2783 seed = Math::Random();
2784#elif defined(Q_OS_WIN)
2785 seed = GetTickCount();
2786#else
2787 seed = quintptr(&seed) + QDateTime::currentDateTime().toTime_t();
2788#endif
2789 srand(seed);
2790#endif // defined(Q_OS_UNIX) || defined(Q_OS_WIN)) && !defined(QT_NO_THREAD)
2791}
2792
2793/*!
2794 \relates <QtGlobal>
2795 \since 4.2
2796
2797 Thread-safe version of the standard C++ \c rand() function.
2798
2799 Returns a value between 0 and \c RAND_MAX (defined in \c <cstdlib> and
2800 \c <stdlib.h>), the next number in the current sequence of pseudo-random
2801 integers.
2802
2803 Use \c qsrand() to initialize the pseudo-random number generator with
2804 a seed value.
2805
2806 \sa qsrand()
2807*/
2808int qrand()
2809{
2810#if defined(Q_OS_UNIX) && !defined(QT_NO_THREAD) && !defined(Q_OS_SYMBIAN)
2811 SeedStorage *seedStorage = randTLS();
2812 if (seedStorage) {
2813 SeedStorageType *pseed = seedStorage->localData();
2814 if (!pseed) {
2815 seedStorage->setLocalData(pseed = new SeedStorageType);
2816 *pseed = 1;
2817 }
2818 return rand_r(pseed);
2819 } else {
2820 //golbal static seed storage should always exist,
2821 //except after being deleted by QGlobalStaticDeleter.
2822 //But since it still can be called from destructor of another
2823 //global static object, fallback to qrand()
2824 return rand();
2825 }
2826#else
2827 // On Windows and Symbian srand() and rand() already use Thread-Local-Storage
2828 // to store the seed between calls
2829 // this is also valid for QT_NO_THREAD
2830 return rand();
2831#endif
2832}
2833
2834/*!
2835 \macro forever
2836 \relates <QtGlobal>
2837
2838 This macro is provided for convenience for writing infinite
2839 loops.
2840
2841 Example:
2842
2843 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 31
2844
2845 It is equivalent to \c{for (;;)}.
2846
2847 If you're worried about namespace pollution, you can disable this
2848 macro by adding the following line to your \c .pro file:
2849
2850 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 32
2851
2852 \sa Q_FOREVER
2853*/
2854
2855/*!
2856 \macro Q_FOREVER
2857 \relates <QtGlobal>
2858
2859 Same as \l{forever}.
2860
2861 This macro is available even when \c no_keywords is specified
2862 using the \c .pro file's \c CONFIG variable.
2863
2864 \sa foreach()
2865*/
2866
2867/*!
2868 \macro foreach(variable, container)
2869 \relates <QtGlobal>
2870
2871 This macro is used to implement Qt's \c foreach loop. The \a
2872 variable parameter is a variable name or variable definition; the
2873 \a container parameter is a Qt container whose value type
2874 corresponds to the type of the variable. See \l{The foreach
2875 Keyword} for details.
2876
2877 If you're worried about namespace pollution, you can disable this
2878 macro by adding the following line to your \c .pro file:
2879
2880 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 33
2881
2882 \sa Q_FOREACH()
2883*/
2884
2885/*!
2886 \macro Q_FOREACH(variable, container)
2887 \relates <QtGlobal>
2888
2889 Same as foreach(\a variable, \a container).
2890
2891 This macro is available even when \c no_keywords is specified
2892 using the \c .pro file's \c CONFIG variable.
2893
2894 \sa foreach()
2895*/
2896
2897/*!
2898 \macro QT_TR_NOOP(sourceText)
2899 \relates <QtGlobal>
2900
2901 Marks the string literal \a sourceText for dynamic translation in
2902 the current context (class), i.e the stored \a sourceText will not
2903 be altered.
2904
2905 The macro expands to \a sourceText.
2906
2907 Example:
2908
2909 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 34
2910
2911 The macro QT_TR_NOOP_UTF8() is identical except that it tells lupdate
2912 that the source string is encoded in UTF-8. Corresponding variants
2913 exist in the QT_TRANSLATE_NOOP() family of macros, too. Note that
2914 using these macros is not required if \c CODECFORTR is already set to
2915 UTF-8 in the qmake project file.
2916
2917 \sa QT_TRANSLATE_NOOP(), {Internationalization with Qt}
2918*/
2919
2920/*!
2921 \macro QT_TRANSLATE_NOOP(context, sourceText)
2922 \relates <QtGlobal>
2923
2924 Marks the string literal \a sourceText for dynamic translation in
2925 the given \a context; i.e, the stored \a sourceText will not be
2926 altered. The \a context is typically a class and also needs to
2927 be specified as string literal.
2928
2929 The macro expands to \a sourceText.
2930
2931 Example:
2932
2933 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 35
2934
2935 \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP3(), {Internationalization with Qt}
2936*/
2937
2938/*!
2939 \macro QT_TRANSLATE_NOOP3(context, sourceText, comment)
2940 \relates <QtGlobal>
2941 \since 4.4
2942
2943 Marks the string literal \a sourceText for dynamic translation in the
2944 given \a context and with \a comment, i.e the stored \a sourceText will
2945 not be altered. The \a context is typically a class and also needs to
2946 be specified as string literal. The string literal \a comment
2947 will be available for translators using e.g. Qt Linguist.
2948
2949 The macro expands to anonymous struct of the two string
2950 literals passed as \a sourceText and \a comment.
2951
2952 Example:
2953
2954 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 36
2955
2956 \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), {Internationalization with Qt}
2957*/
2958
2959/*!
2960 \fn QString qtTrId(const char *id, int n = -1)
2961 \relates <QtGlobal>
2962 \reentrant
2963 \since 4.6
2964
2965 \brief The qtTrId function finds and returns a translated string.
2966
2967 Returns a translated string identified by \a id.
2968 If no matching string is found, the id itself is returned. This
2969 should not happen under normal conditions.
2970
2971 If \a n >= 0, all occurrences of \c %n in the resulting string
2972 are replaced with a decimal representation of \a n. In addition,
2973 depending on \a n's value, the translation text may vary.
2974
2975 Meta data and comments can be passed as documented for QObject::tr().
2976 In addition, it is possible to supply a source string template like that:
2977
2978 \tt{//% <C string>}
2979
2980 or
2981
2982 \tt{\begincomment% <C string> \endcomment}
2983
2984 Example:
2985
2986 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp qttrid
2987
2988 Creating QM files suitable for use with this function requires passing
2989 the \c -idbased option to the \c lrelease tool.
2990
2991 \warning This method is reentrant only if all translators are
2992 installed \e before calling this method. Installing or removing
2993 translators while performing translations is not supported. Doing
2994 so will probably result in crashes or other undesirable behavior.
2995
2996 \sa QObject::tr(), QCoreApplication::translate(), {Internationalization with Qt}
2997*/
2998
2999/*!
3000 \macro QT_TRID_NOOP(id)
3001 \relates <QtGlobal>
3002 \since 4.6
3003
3004 \brief The QT_TRID_NOOP macro marks an id for dynamic translation.
3005
3006 The only purpose of this macro is to provide an anchor for attaching
3007 meta data like to qtTrId().
3008
3009 The macro expands to \a id.
3010
3011 Example:
3012
3013 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp qttrid_noop
3014
3015 \sa qtTrId(), {Internationalization with Qt}
3016*/
3017
3018/*!
3019 \macro QT_POINTER_SIZE
3020 \relates <QtGlobal>
3021
3022 Expands to the size of a pointer in bytes (4 or 8). This is
3023 equivalent to \c sizeof(void *) but can be used in a preprocessor
3024 directive.
3025*/
3026
3027/*!
3028 \macro TRUE
3029 \relates <QtGlobal>
3030 \obsolete
3031
3032 Synonym for \c true.
3033
3034 \sa FALSE
3035*/
3036
3037/*!
3038 \macro FALSE
3039 \relates <QtGlobal>
3040 \obsolete
3041
3042 Synonym for \c false.
3043
3044 \sa TRUE
3045*/
3046
3047/*!
3048 \macro QABS(n)
3049 \relates <QtGlobal>
3050 \obsolete
3051
3052 Use qAbs(\a n) instead.
3053
3054 \sa QMIN(), QMAX()
3055*/
3056
3057/*!
3058 \macro QMIN(x, y)
3059 \relates <QtGlobal>
3060 \obsolete
3061
3062 Use qMin(\a x, \a y) instead.
3063
3064 \sa QMAX(), QABS()
3065*/
3066
3067/*!
3068 \macro QMAX(x, y)
3069 \relates <QtGlobal>
3070 \obsolete
3071
3072 Use qMax(\a x, \a y) instead.
3073
3074 \sa QMIN(), QABS()
3075*/
3076
3077/*!
3078 \macro const char *qPrintable(const QString &str)
3079 \relates <QtGlobal>
3080
3081 Returns \a str as a \c{const char *}. This is equivalent to
3082 \a{str}.toLocal8Bit().constData().
3083
3084 The char pointer will be invalid after the statement in which
3085 qPrintable() is used. This is because the array returned by
3086 toLocal8Bit() will fall out of scope.
3087
3088 Example:
3089
3090 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 37
3091
3092
3093 \sa qDebug(), qWarning(), qCritical(), qFatal()
3094*/
3095
3096/*!
3097 \macro Q_DECLARE_TYPEINFO(Type, Flags)
3098 \relates <QtGlobal>
3099
3100 You can use this macro to specify information about a custom type
3101 \a Type. With accurate type information, Qt's \l{generic
3102 containers} can choose appropriate storage methods and algorithms.
3103
3104 \a Flags can be one of the following:
3105
3106 \list
3107 \o \c Q_PRIMITIVE_TYPE specifies that \a Type is a POD (plain old
3108 data) type with no constructor or destructor.
3109 \o \c Q_MOVABLE_TYPE specifies that \a Type has a constructor
3110 and/or a destructor but can be moved in memory using \c
3111 memcpy().
3112 \o \c Q_COMPLEX_TYPE (the default) specifies that \a Type has
3113 constructors and/or a destructor and that it may not be moved
3114 in memory.
3115 \endlist
3116
3117 Example of a "primitive" type:
3118
3119 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 38
3120
3121 Example of a movable type:
3122
3123 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 39
3124*/
3125
3126/*!
3127 \macro Q_UNUSED(name)
3128 \relates <QtGlobal>
3129
3130 Indicates to the compiler that the parameter with the specified
3131 \a name is not used in the body of a function. This can be used to
3132 suppress compiler warnings while allowing functions to be defined
3133 with meaningful parameter names in their signatures.
3134*/
3135
3136#if defined(QT3_SUPPORT) && !defined(QT_NO_SETTINGS)
3137QT_BEGIN_INCLUDE_NAMESPACE
3138#include <qlibraryinfo.h>
3139QT_END_INCLUDE_NAMESPACE
3140
3141static const char *qInstallLocation(QLibraryInfo::LibraryLocation loc)
3142{
3143 static QByteArray ret;
3144 ret = QLibraryInfo::location(loc).toLatin1();
3145 return ret.constData();
3146}
3147const char *qInstallPath()
3148{
3149 return qInstallLocation(QLibraryInfo::PrefixPath);
3150}
3151const char *qInstallPathDocs()
3152{
3153 return qInstallLocation(QLibraryInfo::DocumentationPath);
3154}
3155const char *qInstallPathHeaders()
3156{
3157 return qInstallLocation(QLibraryInfo::HeadersPath);
3158}
3159const char *qInstallPathLibs()
3160{
3161 return qInstallLocation(QLibraryInfo::LibrariesPath);
3162}
3163const char *qInstallPathBins()
3164{
3165 return qInstallLocation(QLibraryInfo::BinariesPath);
3166}
3167const char *qInstallPathPlugins()
3168{
3169 return qInstallLocation(QLibraryInfo::PluginsPath);
3170}
3171const char *qInstallPathData()
3172{
3173 return qInstallLocation(QLibraryInfo::DataPath);
3174}
3175const char *qInstallPathTranslations()
3176{
3177 return qInstallLocation(QLibraryInfo::TranslationsPath);
3178}
3179const char *qInstallPathSysconf()
3180{
3181 return qInstallLocation(QLibraryInfo::SettingsPath);
3182}
3183#endif
3184
3185struct QInternal_CallBackTable {
3186 QVector<QList<qInternalCallback> > callbacks;
3187};
3188
3189Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
3190
3191bool QInternal::registerCallback(Callback cb, qInternalCallback callback)
3192{
3193 if (cb >= 0 && cb < QInternal::LastCallback) {
3194 QInternal_CallBackTable *cbt = global_callback_table();
3195 cbt->callbacks.resize(cb + 1);
3196 cbt->callbacks[cb].append(callback);
3197 return true;
3198 }
3199 return false;
3200}
3201
3202bool QInternal::unregisterCallback(Callback cb, qInternalCallback callback)
3203{
3204 if (cb >= 0 && cb < QInternal::LastCallback) {
3205 QInternal_CallBackTable *cbt = global_callback_table();
3206 return (bool) cbt->callbacks[cb].removeAll(callback);
3207 }
3208 return false;
3209}
3210
3211bool QInternal::activateCallbacks(Callback cb, void **parameters)
3212{
3213 Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()", "Callback id must be a valid id");
3214
3215 QInternal_CallBackTable *cbt = global_callback_table();
3216 if (cbt && cb < cbt->callbacks.size()) {
3217 QList<qInternalCallback> callbacks = cbt->callbacks[cb];
3218 bool ret = false;
3219 for (int i=0; i<callbacks.size(); ++i)
3220 ret |= (callbacks.at(i))(parameters);
3221 return ret;
3222 }
3223 return false;
3224}
3225
3226extern void qt_set_current_thread_to_main_thread();
3227
3228bool QInternal::callFunction(InternalFunction func, void **args)
3229{
3230 Q_ASSERT_X(func >= 0,
3231 "QInternal::callFunction()", "Callback id must be a valid id");
3232#ifndef QT_NO_QOBJECT
3233 switch (func) {
3234#ifndef QT_NO_THREAD
3235 case QInternal::CreateThreadForAdoption:
3236 *args = QAdoptedThread::createThreadForAdoption();
3237 return true;
3238#endif
3239 case QInternal::RefAdoptedThread:
3240 QThreadData::get2((QThread *) *args)->ref();
3241 return true;
3242 case QInternal::DerefAdoptedThread:
3243 QThreadData::get2((QThread *) *args)->deref();
3244 return true;
3245 case QInternal::SetCurrentThreadToMainThread:
3246 qt_set_current_thread_to_main_thread();
3247 return true;
3248 case QInternal::SetQObjectSender: {
3249 QObject *receiver = (QObject *) args[0];
3250 QObjectPrivate::Sender *sender = new QObjectPrivate::Sender;
3251 sender->sender = (QObject *) args[1];
3252 sender->signal = *(int *) args[2];
3253 sender->ref = 1;
3254
3255 // Store the old sender as "return value"
3256 args[3] = QObjectPrivate::setCurrentSender(receiver, sender);
3257 args[4] = sender;
3258 return true;
3259 }
3260 case QInternal::GetQObjectSender: {
3261 QObject *receiver = (QObject *) args[0];
3262 QObjectPrivate *d = QObjectPrivate::get(receiver);
3263 args[1] = d->currentSender ? d->currentSender->sender : 0;
3264 return true;
3265 }
3266 case QInternal::ResetQObjectSender: {
3267 QObject *receiver = (QObject *) args[0];
3268 QObjectPrivate::Sender *oldSender = (QObjectPrivate::Sender *) args[1];
3269 QObjectPrivate::Sender *sender = (QObjectPrivate::Sender *) args[2];
3270 QObjectPrivate::resetCurrentSender(receiver, sender, oldSender);
3271 delete sender;
3272 return true;
3273 }
3274
3275 default:
3276 break;
3277 }
3278#else
3279 Q_UNUSED(args);
3280 Q_UNUSED(func);
3281#endif
3282
3283 return false;
3284}
3285
3286/*!
3287 \macro Q_BYTE_ORDER
3288 \relates <QtGlobal>
3289
3290 This macro can be used to determine the byte order your system
3291 uses for storing data in memory. i.e., whether your system is
3292 little-endian or big-endian. It is set by Qt to one of the macros
3293 Q_LITTLE_ENDIAN or Q_BIG_ENDIAN. You normally won't need to worry
3294 about endian-ness, but you might, for example if you need to know
3295 which byte of an integer or UTF-16 character is stored in the
3296 lowest address. Endian-ness is important in networking, where
3297 computers with different values for Q_BYTE_ORDER must pass data
3298 back and forth.
3299
3300 Use this macro as in the following examples.
3301
3302 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 40
3303
3304 \sa Q_BIG_ENDIAN, Q_LITTLE_ENDIAN
3305*/
3306
3307/*!
3308 \macro Q_LITTLE_ENDIAN
3309 \relates <QtGlobal>
3310
3311 This macro represents a value you can compare to the macro
3312 Q_BYTE_ORDER to determine the endian-ness of your system. In a
3313 little-endian system, the least significant byte is stored at the
3314 lowest address. The other bytes follow in increasing order of
3315 significance.
3316
3317 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 41
3318
3319 \sa Q_BYTE_ORDER, Q_BIG_ENDIAN
3320*/
3321
3322/*!
3323 \macro Q_BIG_ENDIAN
3324 \relates <QtGlobal>
3325
3326 This macro represents a value you can compare to the macro
3327 Q_BYTE_ORDER to determine the endian-ness of your system. In a
3328 big-endian system, the most significant byte is stored at the
3329 lowest address. The other bytes follow in decreasing order of
3330 significance.
3331
3332 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 42
3333
3334 \sa Q_BYTE_ORDER, Q_LITTLE_ENDIAN
3335*/
3336
3337/*!
3338 \macro Q_GLOBAL_STATIC(type, name)
3339 \internal
3340
3341 Declares a global static variable with the given \a type and \a name.
3342
3343 Use this macro to instantiate an object in a thread-safe way, creating
3344 a global pointer that can be used to refer to it.
3345
3346 \warning This macro is subject to a race condition that can cause the object
3347 to be constructed twice. However, if this occurs, the second instance will
3348 be immediately deleted.
3349
3350 See also
3351 \l{http://www.aristeia.com/publications.html}{"C++ and the perils of Double-Checked Locking"}
3352 by Scott Meyers and Andrei Alexandrescu.
3353*/
3354
3355/*!
3356 \macro Q_GLOBAL_STATIC_WITH_ARGS(type, name, arguments)
3357 \internal
3358
3359 Declares a global static variable with the specified \a type and \a name.
3360
3361 Use this macro to instantiate an object using the \a arguments specified
3362 in a thread-safe way, creating a global pointer that can be used to refer
3363 to it.
3364
3365 \warning This macro is subject to a race condition that can cause the object
3366 to be constructed twice. However, if this occurs, the second instance will
3367 be immediately deleted.
3368
3369 See also
3370 \l{http://www.aristeia.com/publications.html}{"C++ and the perils of Double-Checked Locking"}
3371 by Scott Meyers and Andrei Alexandrescu.
3372*/
3373
3374/*!
3375 \macro QT_NAMESPACE
3376 \internal
3377
3378 If this macro is defined to \c ns all Qt classes are put in a namespace
3379 called \c ns. Also, moc will output code putting metaobjects etc.
3380 into namespace \c ns.
3381
3382 \sa QT_BEGIN_NAMESPACE, QT_END_NAMESPACE,
3383 QT_PREPEND_NAMESPACE, QT_USE_NAMESPACE,
3384 QT_BEGIN_INCLUDE_NAMESPACE, QT_END_INCLUDE_NAMESPACE,
3385 QT_BEGIN_MOC_NAMESPACE, QT_END_MOC_NAMESPACE,
3386*/
3387
3388/*!
3389 \macro QT_PREPEND_NAMESPACE(identifier)
3390 \internal
3391
3392 This macro qualifies \a identifier with the full namespace.
3393 It expands to \c{::QT_NAMESPACE::identifier} if \c QT_NAMESPACE is defined
3394 and only \a identifier otherwise.
3395
3396 \sa QT_NAMESPACE
3397*/
3398
3399/*!
3400 \macro QT_USE_NAMESPACE
3401 \internal
3402
3403 This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined
3404 and nothing otherwise.
3405
3406 \sa QT_NAMESPACE
3407*/
3408
3409/*!
3410 \macro QT_BEGIN_NAMESPACE
3411 \internal
3412
3413 This macro expands to
3414
3415 \snippet snippets/code/src_corelib_global_qglobal.cpp begin namespace macro
3416
3417 if \c QT_NAMESPACE is defined and nothing otherwise. If should always
3418 appear in the file-level scope and be followed by \c QT_END_NAMESPACE
3419 at the same logical level with respect to preprocessor conditionals
3420 in the same file.
3421
3422 As a rule of thumb, \c QT_BEGIN_NAMESPACE should appear in all Qt header
3423 and Qt source files after the last \c{#include} line and before the first
3424 declaration. In Qt headers using \c QT_BEGIN_HEADER, \c QT_BEGIN_NAMESPACE
3425 follows \c QT_BEGIN_HEADER immediately.
3426
3427 If that rule can't be followed because, e.g., \c{#include} lines and
3428 declarations are wildly mixed, place \c QT_BEGIN_NAMESPACE before
3429 the first declaration and wrap the \c{#include} lines in
3430 \c QT_BEGIN_INCLUDE_NAMESPACE and \c QT_END_INCLUDE_NAMESPACE.
3431
3432 When using the \c QT_NAMESPACE feature in user code
3433 (e.g., when building plugins statically linked to Qt) where
3434 the user code is not intended to go into the \c QT_NAMESPACE
3435 namespace, all forward declarations of Qt classes need to
3436 be wrapped in \c QT_BEGIN_NAMESPACE and \c QT_END_NAMESPACE.
3437 After that, a \c QT_USE_NAMESPACE should follow.
3438 No further changes should be needed.
3439
3440 \sa QT_NAMESPACE
3441*/
3442
3443/*!
3444 \macro QT_END_NAMESPACE
3445 \internal
3446
3447 This macro expands to
3448
3449 \snippet snippets/code/src_corelib_global_qglobal.cpp end namespace macro
3450
3451 if \c QT_NAMESPACE is defined and nothing otherwise. It is used to cancel
3452 the effect of \c QT_BEGIN_NAMESPACE.
3453
3454 If a source file ends with a \c{#include} directive that includes a moc file,
3455 \c QT_END_NAMESPACE should be placed before that \c{#include}.
3456
3457 \sa QT_NAMESPACE
3458*/
3459
3460/*!
3461 \macro QT_BEGIN_INCLUDE_NAMESPACE
3462 \internal
3463
3464 This macro is equivalent to \c QT_END_NAMESPACE.
3465 It only serves as syntactic sugar and is intended
3466 to be used before #include lines within a
3467 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
3468
3469 \sa QT_NAMESPACE
3470*/
3471
3472/*!
3473 \macro QT_END_INCLUDE_NAMESPACE
3474 \internal
3475
3476 This macro is equivalent to \c QT_BEGIN_NAMESPACE.
3477 It only serves as syntactic sugar and is intended
3478 to be used after #include lines within a
3479 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
3480
3481 \sa QT_NAMESPACE
3482*/
3483
3484/*!
3485 \macro QT_BEGIN_MOC_NAMESPACE
3486 \internal
3487
3488 This macro is output by moc at the beginning of
3489 moc files. It is equivalent to \c QT_USE_NAMESPACE.
3490
3491 \sa QT_NAMESPACE
3492*/
3493
3494/*!
3495 \macro QT_END_MOC_NAMESPACE
3496 \internal
3497
3498 This macro is output by moc at the beginning of
3499 moc files. It expands to nothing.
3500
3501 \sa QT_NAMESPACE
3502*/
3503
3504/*!
3505 \fn bool qFuzzyCompare(double p1, double p2)
3506 \relates <QtGlobal>
3507 \since 4.4
3508 \threadsafe
3509
3510 Compares the floating point value \a p1 and \a p2 and
3511 returns \c true if they are considered equal, otherwise \c false.
3512
3513 Note that comparing values where either \a p1 or \a p2 is 0.0 will not work.
3514 The solution to this is to compare against values greater than or equal to 1.0.
3515
3516 \snippet doc/src/snippets/code/src_corelib_global_qglobal.cpp 46
3517
3518 The two numbers are compared in a relative way, where the
3519 exactness is stronger the smaller the numbers are.
3520 */
3521
3522/*!
3523 \fn bool qFuzzyCompare(float p1, float p2)
3524 \relates <QtGlobal>
3525 \since 4.4
3526 \threadsafe
3527
3528 Compares the floating point value \a p1 and \a p2 and
3529 returns \c true if they are considered equal, otherwise \c false.
3530
3531 The two numbers are compared in a relative way, where the
3532 exactness is stronger the smaller the numbers are.
3533 */
3534
3535/*!
3536 \macro QT_REQUIRE_VERSION(int argc, char **argv, const char *version)
3537 \relates <QtGlobal>
3538
3539 This macro can be used to ensure that the application is run
3540 against a recent enough version of Qt. This is especially useful
3541 if your application depends on a specific bug fix introduced in a
3542 bug-fix release (e.g., 4.0.2).
3543
3544 The \a argc and \a argv parameters are the \c main() function's
3545 \c argc and \c argv parameters. The \a version parameter is a
3546 string literal that specifies which version of Qt the application
3547 requires (e.g., "4.0.2").
3548
3549 Example:
3550
3551 \snippet doc/src/snippets/code/src_gui_dialogs_qmessagebox.cpp 4
3552*/
3553
3554/*!
3555 \macro Q_DECL_EXPORT
3556 \relates <QtGlobal>
3557
3558 This macro marks a symbol for shared library export (see
3559 \l{sharedlibrary.html}{Creating Shared Libraries}).
3560
3561 \sa Q_DECL_IMPORT
3562*/
3563
3564/*!
3565 \macro Q_DECL_IMPORT
3566 \relates <QtGlobal>
3567
3568 This macro declares a symbol to be an import from a shared library (see
3569 \l{sharedlibrary.html}{Creating Shared Libraries}).
3570
3571 \sa Q_DECL_EXPORT
3572*/
3573
3574#if defined(Q_OS_SYMBIAN)
3575
3576#include <typeinfo>
3577
3578/*! \macro QT_TRAP_THROWING(function)
3579 \relates <QtGlobal>
3580 \ingroup qts60
3581
3582 TRAP leaves from Symbian \a function and throws an appropriate
3583 standard C++ exception instead.
3584 This must be used when calling Symbian OS leaving functions
3585 from inside Qt or standard C++ code, so that the code can respond
3586 correctly to the exception.
3587
3588 \warning This macro is only available on Symbian.
3589
3590 Example:
3591
3592 \code
3593 // A Symbian leaving function is being called within a Qt function.
3594 // Any leave must be converted to an exception
3595 CAknTitlePane* titlePane = S60->titlePane();
3596 if (titlePane) {
3597 TPtrC captionPtr(qt_QString2TPtrC(caption));
3598 QT_TRAP_THROWING(titlePane->SetTextL(captionPtr));
3599 }
3600 \endcode
3601
3602 \sa QT_TRYCATCH_ERROR(), QT_TRYCATCH_LEAVING()
3603*/
3604
3605/*! \macro QT_TRYCATCH_ERROR(error, function)
3606 \relates <QtGlobal>
3607 \ingroup qts60
3608
3609 Catch standard C++ exceptions from a \a function and convert them to a Symbian OS
3610 \a error code, or \c KErrNone if there is no exception.
3611 This must be used inside Qt or standard C++ code when using exception throwing
3612 code (practically anything) and returning an error code to Symbian OS.
3613
3614 \warning This macro is only available on Symbian.
3615
3616 Example:
3617
3618 \code
3619 // An exception might be thrown in this Symbian TInt error returning function.
3620 // It is caught and translated to an error code
3621 TInt QServerApp::Connect(const QString &serverName)
3622 {
3623 TPtrC name;
3624 TInt err;
3625 QT_TRYCATCH_ERROR(err, name.Set(qt_QString2TPtrC(serverName)));
3626 if (err != KErrNone)
3627 return err;
3628 return iServer.Connect(name);
3629 }
3630 \endcode
3631}
3632
3633 \sa QT_TRYCATCH_LEAVING(), QT_TRAP_THROWING()
3634*/
3635
3636/*! \macro QT_TRYCATCH_LEAVING(function)
3637 \relates <QtGlobal>
3638 \ingroup qts60
3639
3640 Catch standard C++ exceptions from \a function and convert them to Symbian OS
3641 leaves. This must be used inside Qt or standard C++ code when using exception
3642 throwing code (practically anything) and returning to Symbian OS from a leaving function.
3643 For example inside a Symbian active object's \c RunL function implemented with Qt code.
3644
3645 \warning This macro is only available on Symbian.
3646
3647 Example:
3648
3649 \code
3650 // This active object signals Qt code
3651 // Exceptions from the Qt code must be converted to Symbian OS leaves for the active scheduler
3652 void QWakeUpActiveObject::RunL()
3653 {
3654 iStatus = KRequestPending;
3655 SetActive();
3656 QT_TRYCATCH_LEAVING(m_dispatcher->wakeUpWasCalled());
3657 }
3658 \endcode
3659
3660 \sa QT_TRAP_THROWING(), QT_TRYCATCH_ERROR()
3661*/
3662
3663#include <stdexcept>
3664
3665class QSymbianLeaveException : public std::exception
3666{
3667public:
3668 inline QSymbianLeaveException(int err) : error(err) {}
3669 inline const char* what() const throw() { return "Symbian leave exception"; }
3670
3671public:
3672 int error;
3673};
3674
3675/*! \relates <QtGlobal>
3676 \ingroup qts60
3677
3678 Throws an exception if the \a error parameter is a symbian error code.
3679 This is the exception throwing equivalent of Symbian's User::LeaveIfError.
3680
3681 \warning This function is only available on Symbian.
3682
3683 \sa qt_symbian_exception2LeaveL(), qt_symbian_exception2Error()
3684*/
3685void qt_symbian_throwIfError(int error)
3686{
3687 if (error >= KErrNone)
3688 return; // do nothing - not an exception
3689 switch (error) {
3690 case KErrNoMemory:
3691 throw std::bad_alloc();
3692 case KErrArgument:
3693 throw std::invalid_argument("from Symbian error");
3694 case KErrOverflow:
3695 throw std::overflow_error("from Symbian error");
3696 case KErrUnderflow:
3697 throw std::underflow_error("from Symbian error");
3698 default:
3699 throw QSymbianLeaveException(error);
3700 }
3701}
3702
3703/*! \relates <QtGlobal>
3704 \ingroup qts60
3705
3706 Convert a caught standard C++ exception \a aThrow to a Symbian leave
3707
3708 \warning This function is only available on Symbian.
3709
3710 \sa qt_symbian_throwIfError(), qt_symbian_exception2Error()
3711*/
3712void qt_symbian_exception2LeaveL(const std::exception& aThrow)
3713{
3714 User::Leave(qt_symbian_exception2Error(aThrow));
3715}
3716
3717/*! \relates <QtGlobal>
3718 \ingroup qts60
3719
3720 Convert a caught standard C++ exception \a aThrow to a Symbian error code
3721
3722 \warning This function is only available on Symbian.
3723
3724 \sa qt_symbian_throwIfError(), qt_symbian_exception2LeaveL()
3725*/
3726int qt_symbian_exception2Error(const std::exception& aThrow)
3727{
3728 const std::type_info& atype = typeid(aThrow);
3729 int err = KErrGeneral;
3730
3731 if(atype == typeid (std::bad_alloc))
3732 err = KErrNoMemory;
3733 else if(atype == typeid(QSymbianLeaveException))
3734 err = static_cast<const QSymbianLeaveException&>(aThrow).error;
3735 else {
3736 if(atype == typeid(std::invalid_argument))
3737 err = KErrArgument;
3738 else if(atype == typeid(std::out_of_range))
3739 // std::out_of_range is of type logic_error which by definition means that it is
3740 // "presumably detectable before the program executes".
3741 // std::out_of_range is used to report an argument is not within the expected range.
3742 // The description of KErrArgument says an argument is out of range. Hence the mapping.
3743 err = KErrArgument;
3744 else if(atype == typeid(std::overflow_error))
3745 err = KErrOverflow;
3746 else if(atype == typeid(std::underflow_error))
3747 err = KErrUnderflow;
3748 qWarning("translation from std exception \"%s\" to %d", aThrow.what(), err);
3749 }
3750
3751 return err;
3752}
3753#endif
3754
3755QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.