Browse Source

Add literal operator to efficiently construct `QString` in Qt5

PR #16448.
adaptive-webui-19844
Chocobo1 3 years ago committed by GitHub
parent
commit
852a14992c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      src/base/global.h
  2. 12
      src/base/settingsstorage.cpp

8
src/base/global.h

@ -48,3 +48,11 @@ constexpr typename std::add_const_t<T> asConst(T &&t) noexcept { return std::mov
// Prevent const rvalue arguments // Prevent const rvalue arguments
template <typename T> template <typename T>
void asConst(const T &&) = delete; void asConst(const T &&) = delete;
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
// https://doc.qt.io/qt-6/qstring.html#operator-22-22_qs
inline QString operator"" _qs(const char16_t *str, const std::size_t size)
{
return QString::fromRawData(reinterpret_cast<const QChar *>(str), size);
}
#endif

12
src/base/settingsstorage.cpp

@ -70,7 +70,7 @@ namespace
SettingsStorage *SettingsStorage::m_instance = nullptr; SettingsStorage *SettingsStorage::m_instance = nullptr;
SettingsStorage::SettingsStorage() SettingsStorage::SettingsStorage()
: m_data {TransactionalSettings(QLatin1String("qBittorrent")).read()} : m_data {TransactionalSettings(u"qBittorrent"_qs).read()}
{ {
m_timer.setSingleShot(true); m_timer.setSingleShot(true);
m_timer.setInterval(5 * 1000); m_timer.setInterval(5 * 1000);
@ -104,7 +104,7 @@ bool SettingsStorage::save()
const QWriteLocker locker(&m_lock); // guard for `m_dirty` too const QWriteLocker locker(&m_lock); // guard for `m_dirty` too
if (!m_dirty) return true; if (!m_dirty) return true;
const TransactionalSettings settings(QLatin1String("qBittorrent")); const TransactionalSettings settings(u"qBittorrent"_qs);
if (!settings.write(m_data)) if (!settings.write(m_data))
{ {
m_timer.start(); m_timer.start();
@ -157,7 +157,7 @@ QVariantHash TransactionalSettings::read() const
{ {
QVariantHash res; QVariantHash res;
const Path newPath = deserialize(m_name + QLatin1String("_new"), res); const Path newPath = deserialize(m_name + u"_new", res);
if (!newPath.isEmpty()) if (!newPath.isEmpty())
{ // "_new" file is NOT empty { // "_new" file is NOT empty
// This means that the PC closed either due to power outage // This means that the PC closed either due to power outage
@ -169,7 +169,7 @@ QVariantHash TransactionalSettings::read() const
, Log::WARNING); , Log::WARNING);
QString finalPathStr = newPath.data(); QString finalPathStr = newPath.data();
const int index = finalPathStr.lastIndexOf("_new", -1, Qt::CaseInsensitive); const int index = finalPathStr.lastIndexOf(u"_new", -1, Qt::CaseInsensitive);
finalPathStr.remove(index, 4); finalPathStr.remove(index, 4);
const Path finalPath {finalPathStr}; const Path finalPath {finalPathStr};
@ -191,7 +191,7 @@ bool TransactionalSettings::write(const QVariantHash &data) const
// between deleting the file and recreating it. This is a safety measure. // between deleting the file and recreating it. This is a safety measure.
// Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds // Write everything to qBittorrent_new.ini/qBittorrent_new.conf and if it succeeds
// replace qBittorrent.ini/qBittorrent.conf with it. // replace qBittorrent.ini/qBittorrent.conf with it.
const Path newPath = serialize(m_name + QLatin1String("_new"), data); const Path newPath = serialize(m_name + u"_new", data);
if (newPath.isEmpty()) if (newPath.isEmpty())
{ {
Utils::Fs::removeFile(newPath); Utils::Fs::removeFile(newPath);
@ -199,7 +199,7 @@ bool TransactionalSettings::write(const QVariantHash &data) const
} }
QString finalPathStr = newPath.data(); QString finalPathStr = newPath.data();
const int index = finalPathStr.lastIndexOf("_new", -1, Qt::CaseInsensitive); const int index = finalPathStr.lastIndexOf(u"_new", -1, Qt::CaseInsensitive);
finalPathStr.remove(index, 4); finalPathStr.remove(index, 4);
const Path finalPath {finalPathStr}; const Path finalPath {finalPathStr};

Loading…
Cancel
Save