Skip to content

Commit

Permalink
Simplify system locale management
Browse files Browse the repository at this point in the history
Have QSystemLocale manage a stack, so that tests can install an
over-ride for the actual system-specific one reliably and restore the
system-specific one when finished. Leave the QSystemLocaleSingleton
out of the stack, all the same. In the process, mark the QDoc comments
for QSystemLocale all as \internal, since this is not public API.

Change-Id: I8faed49780215e42f32be10cf936c32bb46105bf
Reviewed-by: Thiago Macieira <[email protected]>
Reviewed-by: Qt CI Bot <[email protected]>
  • Loading branch information
ediosyncratic committed Sep 7, 2022
1 parent 753bfdf commit ebe9aca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
39 changes: 28 additions & 11 deletions src/corelib/text/qlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,28 @@ static QLocalePrivate *c_private()
*/

/*!
Constructs a QSystemLocale object.
\internal
Constructs a QSystemLocale object.
The constructor will automatically install this object as the system locale.
It and the destructor maintain a stack of system locales, with the
most-recently-created instance (that hasn't yet been deleted) used as the
system locale. This is only intended as a way to let a platform plugin
install its own system locale, overriding what might otherwise be provided
for its class of platform (as Android does, differing from Linux), and to
let tests transiently over-ride the system or plugin-supplied one. As such,
there should not be diverse threads creating and destroying QSystemLocale
instances concurrently, so no attempt is made at thread-safety in managing
the stack.
The constructor will automatically install this object as the system locale,
if there's not one active. It also resets the flag that'll prompt
QLocale::system() to re-initialize its data, so that instantiating a
QSystemLocale transiently (doesn't install the transient as system locale if
there was one already and) triggers an update to the system locale's data.
This constructor also resets the flag that'll prompt QLocale::system() to
re-initialize its data, so that instantiating a QSystemLocale (even
transiently) triggers a refresh of the system locale's data. This is
exploited by some test code.
*/
QSystemLocale::QSystemLocale()
QSystemLocale::QSystemLocale() : next(_systemLocale)
{
if (!_systemLocale)
_systemLocale = this;
_systemLocale = this;

systemLocaleData.m_language_id = 0;
}
Expand All @@ -718,14 +728,21 @@ QSystemLocale::QSystemLocale(bool)
{ }

/*!
Deletes the object.
\internal
Deletes the object.
*/
QSystemLocale::~QSystemLocale()
{
if (_systemLocale == this) {
_systemLocale = nullptr;
_systemLocale = next;

// Change to system locale => force refresh.
systemLocaleData.m_language_id = 0;
} else {
for (QSystemLocale *p = _systemLocale; p; p = p->next) {
if (p->next == this)
p->next = next;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/corelib/text/qlocale_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct QLocaleData;
// Subclassed by Android platform plugin:
class Q_CORE_EXPORT QSystemLocale
{
QSystemLocale *next = nullptr; // Maintains a stack.
public:
QSystemLocale();
virtual ~QSystemLocale();
Expand Down
3 changes: 0 additions & 3 deletions tests/auto/corelib/text/qlocale/tst_qlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3258,9 +3258,6 @@ class MySystemLocale : public QSystemLocale

void tst_QLocale::systemLocale_data()
{
#ifdef Q_OS_ANDROID
QSKIP("Android already has a QSystemLocale installed, we can't override it");
#endif
// Test uses MySystemLocale, so is platform-independent.
QTest::addColumn<QString>("name");
QTest::addColumn<QLocale::Language>("language");
Expand Down

0 comments on commit ebe9aca

Please sign in to comment.