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

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

OS/2: Make qFatal() call LIBC abort() instead of exit(1).

In kLIBC, this will cause "INT 3" to be generated if
LIBC_BREAKPOINT_ABORT is set which is useful for debugging.

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