summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qglobal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/global/qglobal.cpp')
-rw-r--r--src/corelib/global/qglobal.cpp155
1 files changed, 100 insertions, 55 deletions
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp
index b30d690025..5c1665fa00 100644
--- a/src/corelib/global/qglobal.cpp
+++ b/src/corelib/global/qglobal.cpp
@@ -53,10 +53,6 @@
#include <qmutex.h>
-#ifndef QT_NO_QOBJECT
-#include <private/qthread_p.h>
-#endif
-
#include <stdlib.h>
#include <limits.h>
#include <stdarg.h>
@@ -650,7 +646,8 @@ Q_STATIC_ASSERT((std::is_same<qsizetype, qptrdiff>::value));
The remaining macros are convenience macros for larger operations:
The QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and QT_TRANSLATE_NOOP3()
macros provide the possibility of marking strings for delayed
- translation.
+ translation. QT_TR_N_NOOP(), QT_TRANSLATE_N_NOOP(), and
+ QT_TRANSLATE_N_NOOP3() are numerator dependent variants of these.
The Q_ASSERT() and Q_ASSERT_X() enables warning messages of various
level of refinement. The Q_FOREACH() and foreach() macros
implement Qt's foreach loop.
@@ -3379,9 +3376,7 @@ QString qEnvironmentVariable(const char *varName)
Returns whether the environment variable \a varName is empty.
Equivalent to
- \code
- qgetenv(varName).isEmpty()
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp is-empty
except that it's potentially much faster, and can't throw exceptions.
\sa qgetenv(), qEnvironmentVariable(), qEnvironmentVariableIsSet()
@@ -3411,9 +3406,7 @@ bool qEnvironmentVariableIsEmpty(const char *varName) Q_DECL_NOEXCEPT
on the success of the conversion.
Equivalent to
- \code
- qgetenv(varName).toInt(ok, 0)
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp to-int
except that it's much faster, and can't throw exceptions.
\note there's a limit on the length of the value, which is sufficient for
@@ -3450,7 +3443,27 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) Q_DECL_NOEXCEPT
bool ok_ = true;
const char *endptr;
const qlonglong value = qstrtoll(buffer, &endptr, 0, &ok_);
- if (int(value) != value || *endptr != '\0') { // this is the check in QByteArray::toInt(), keep it in sync
+
+ // Keep the following checks in sync with QByteArray::toInt()
+ if (!ok_) {
+ if (ok)
+ *ok = false;
+ return 0;
+ }
+
+ if (*endptr != '\0') {
+ while (ascii_isspace(*endptr))
+ ++endptr;
+ }
+
+ if (*endptr != '\0') {
+ // we stopped at a non-digit character after converting some digits
+ if (ok)
+ *ok = false;
+ return 0;
+ }
+
+ if (int(value) != value) {
if (ok)
*ok = false;
return 0;
@@ -3467,9 +3480,7 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) Q_DECL_NOEXCEPT
Returns whether the environment variable \a varName is set.
Equivalent to
- \code
- !qgetenv(varName).isNull()
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp is-null
except that it's potentially much faster, and can't throw exceptions.
\sa qgetenv(), qEnvironmentVariable(), qEnvironmentVariableIsEmpty()
@@ -3645,37 +3656,21 @@ bool qunsetenv(const char *varName)
Its main use in Qt is to prevent implicitly-shared Qt containers
from detaching:
- \code
- QString s = ...;
- for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
- process(ch);
- for (QChar ch : qAsConst(s)) // ok, no detach attempt
- process(ch);
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp as-const-0
Of course, in this case, you could (and probably should) have declared
\c s as \c const in the first place:
- \code
- const QString s = ...;
- for (QChar ch : s) // ok, no detach attempt on const objects
- process(ch);
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp as-const-1
but often that is not easily possible.
It is important to note that qAsConst() does not copy its argument,
it just performs a \c{const_cast<const T&>(t)}. This is also the reason
why it is designed to fail for rvalues: The returned reference would go
stale too soon. So while this works (but detaches the returned object):
- \code
- for (QChar ch : funcReturningQString())
- process(ch); // OK, the returned object is kept alive for the loop's duration
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp as-const-2
this would not:
- \code
- for (QChar ch : qAsConst(funcReturningQString()))
- process(ch); // ERROR: ch is copied from deleted memory
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp as-const-3
To prevent this construct from compiling (and failing at runtime), qAsConst() has
a second, deleted, overload which binds to rvalues.
@@ -3688,10 +3683,7 @@ bool qunsetenv(const char *varName)
\overload
This overload is deleted to prevent a dangling reference in code like
- \code
- for (QChar ch : qAsConst(funcReturningQString()))
- process(ch); // ERROR: ch is copied from deleted memory
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp as-const-4
*/
/*!
@@ -3755,6 +3747,71 @@ bool qunsetenv(const char *varName)
*/
/*!
+ \macro QT_TR_N_NOOP(sourceText)
+ \relates <QtGlobal>
+ \since 5.12
+
+ Marks the UTF-8 encoded string literal \a sourceText for numerator
+ dependent delayed translation in the current context (class).
+
+ The macro tells lupdate to collect the string, and expands to
+ \a sourceText itself.
+
+ The macro expands to \a sourceText.
+
+ Example:
+
+ \snippet code/src_corelib_global_qglobal.cpp qttrnnoop
+
+ \sa QT_TR_NOOP, {Internationalization with Qt}
+*/
+
+/*!
+ \macro QT_TRANSLATE_N_NOOP(context, sourceText)
+ \relates <QtGlobal>
+ \since 5.12
+
+ Marks the UTF-8 encoded string literal \a sourceText for numerator
+ dependent delayed translation in the given \a context.
+ The \a context is typically a class name and also needs to be
+ specified as a string literal.
+
+ The macro tells lupdate to collect the string, and expands to
+ \a sourceText itself.
+
+ Example:
+
+ \snippet code/src_corelib_global_qglobal.cpp qttranslatennoop
+
+ \sa QT_TRANSLATE_NOOP(), QT_TRANSLATE_N_NOOP3(),
+ {Internationalization with Qt}
+*/
+
+/*!
+ \macro QT_TRANSLATE_N_NOOP3(context, sourceText, comment)
+ \relates <QtGlobal>
+ \since 5.12
+
+ Marks the UTF-8 encoded string literal \a sourceText for numerator
+ dependent delayed translation in the given \a context with the given
+ \a comment.
+ The \a context is typically a class and also needs to be specified
+ as a string literal. The string literal \a disambiguation should be
+ a short semantic tag to tell apart otherwise identical strings.
+
+ The macro tells lupdate to collect the string, and expands to an
+ anonymous struct of the two string literals passed as \a sourceText
+ and \a comment.
+
+ Example:
+
+ \snippet code/src_corelib_global_qglobal.cpp qttranslatennoop3
+
+ \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), QT_TRANSLATE_NOOP3(),
+ {Internationalization with Qt}
+*/
+
+/*!
\fn QString qtTrId(const char *id, int n = -1)
\relates <QtGlobal>
\reentrant
@@ -4560,9 +4617,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
purpose. It either expands to \c expr (if Qt is compiled without
exception support or the compiler supports C++11 noexcept
semantics) or to
- \code
- try { expr; } catch(...) { qTerminate(); }
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp qterminate
otherwise.
Since this macro expands to just \c expr if the compiler supports
@@ -4632,10 +4687,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
The macro goes at the end of the function, usually after the
\c{const}, if any:
- \code
- // generate error if this doesn't actually override anything:
- virtual void MyWidget::paintEvent(QPaintEvent*) Q_DECL_OVERRIDE;
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp qdecloverride
\sa Q_DECL_FINAL
*/
@@ -4657,18 +4709,11 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
The macro goes at the end of the function, usually after the
\c{const}, if any:
- \code
- // more-derived classes no longer permitted to override this:
- virtual void MyWidget::paintEvent(QPaintEvent*) Q_DECL_FINAL;
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-1
For classes, it goes in front of the \c{:} in the class
definition, if any:
- \code
- class QRect Q_DECL_FINAL { // cannot be derived from
- // ...
- };
- \endcode
+ \snippet code/src_corelib_global_qglobal.cpp qdeclfinal-2
\sa Q_DECL_OVERRIDE
*/