Revision control
Copy as Markdown
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:trivial-impl-only
#ifndef QCOLLATOR_P_H
#define QCOLLATOR_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtCore/private/qglobal_p.h>
#include "qcollator.h"
#include "qlocale_p.h"
#include <QList>
#include <QtCore/qloggingcategory.h>
#if QT_CONFIG(icu) || defined(Q_OS_ANDROID)
#include <unicode/ucol.h>
#elif defined(Q_OS_MACOS)
#include <CoreServices/CoreServices.h>
#elif defined(Q_OS_WIN)
#include <qt_windows.h>
#elif defined(Q_OS_WASM)
#include <emscripten/val.h>
#endif
#ifdef Q_OS_ANDROID
#include "qjniobject.h"
#include "qjnitypes.h"
#endif
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcQCollator)
#if QT_CONFIG(icu) || defined(Q_OS_ANDROID)
typedef UCollator *CollatorType;
typedef QByteArray CollatorKeyType;
const CollatorType NoCollator = nullptr;
#elif defined(Q_OS_MACOS)
typedef CollatorRef CollatorType;
typedef QList<UCCollationValue> CollatorKeyType;
const CollatorType NoCollator = 0;
#elif defined(Q_OS_WIN)
typedef QByteArray CollatorKeyType;
typedef int CollatorType;
const CollatorType NoCollator = 0;
#elif defined(Q_OS_WASM)
typedef emscripten::val *CollatorType;
struct QCollatorSortKeyWasm
{
QString string;
emscripten::val collator = emscripten::val::undefined();
};
typedef QCollatorSortKeyWasm CollatorKeyType;
const CollatorType NoCollator = nullptr;
#else // posix - ignores CollatorType collator, only handles system locale
typedef QList<wchar_t> CollatorKeyType;
typedef bool CollatorType;
const CollatorType NoCollator = false;
#endif
#ifdef Q_OS_ANDROID
Q_DECLARE_JNI_CLASS(QtCollator, "org/qtproject/qt/android/QtCollator")
Q_DECLARE_JNI_CLASS(Collator, "android/icu/text/Collator")
#endif
class QCollatorPrivate
{
public:
using Opt = QCollator::CollationOption;
QAtomicInt ref = 1;
QLocale locale;
QCollator::CollationOptions options;
bool dirty = true;
CollatorType collator = NoCollator;
#ifdef Q_OS_ANDROID
QtJniTypes::Collator fallbackCollator = QJniObject(); // needs to be null rather than default-constructed
#endif
QCollatorPrivate(const QLocale &locale) : locale(locale) {}
~QCollatorPrivate() { cleanup(); }
bool isC() const { return locale.language() == QLocale::C; }
void clear() {
cleanup();
collator = NoCollator;
}
void ensureInitialized()
{
if (dirty)
init();
}
// Implemented by each back-end, in its own way:
void init();
void cleanup();
static QCollatorSortKey sortKeyFromData(CollatorKeyType &&data);
private:
Q_DISABLE_COPY_MOVE(QCollatorPrivate)
};
class QCollatorSortKeyPrivate : public QSharedData
{
public:
explicit QCollatorSortKeyPrivate(CollatorKeyType &&list)
: QSharedData(), m_key(std::move(list))
{
}
CollatorKeyType m_key;
private:
Q_DISABLE_COPY_MOVE(QCollatorSortKeyPrivate)
};
inline QCollatorSortKey QCollatorPrivate::sortKeyFromData(CollatorKeyType &&data)
{
return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(data)));
}
QT_END_NAMESPACE
#endif // QCOLLATOR_P_H