Simplify system locale management

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 <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
bb10
Edward Welbourne 2022-08-10 11:49:59 +02:00
parent 753bfdf6a1
commit ebe9aca900
3 changed files with 29 additions and 14 deletions

View File

@ -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,
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.
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.
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;
}
@ -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;
}
}
}

View File

@ -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();

View File

@ -3258,9 +3258,6 @@ private:
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");