From 20f69beae48d2efb1fdb5754218a8cdc990ba13e Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 8 Aug 2019 19:40:32 +0200 Subject: [PATCH 01/12] Optimize QDate by shortcutting some gregorian calendar methods Add some static methods to QGregorianCalendar, some of which serve to implement its methods, that QDate can use to bypass vtables and exploit fixed truths of the Gregorian calendar in its default handling. Change-Id: Iec191cdf4d52945dbd5679e609180cb4fe59b5fd Reviewed-by: Volker Hilsheimer --- src/corelib/time/qdatetime.cpp | 107 ++++++++++++++++++++---- src/corelib/time/qgregoriancalendar.cpp | 39 ++++++++- src/corelib/time/qgregoriancalendar_p.h | 8 ++ 3 files changed, 139 insertions(+), 15 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 020eac6dec..a9fc47e053 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -74,6 +74,7 @@ #endif #include "qcalendar.h" +#include "qgregoriancalendar_p.h" QT_BEGIN_NAMESPACE @@ -105,6 +106,17 @@ static inline QDate fixedDate(QCalendar::YearMonthDay &&parts, QCalendar cal) return cal.dateFromParts(parts); } +static inline QDate fixedDate(QCalendar::YearMonthDay &&parts) +{ + if (parts.year) { + parts.day = qMin(parts.day, QGregorianCalendar::monthLength(parts.month, parts.year)); + qint64 jd; + if (QGregorianCalendar::julianFromParts(parts.year, parts.month, parts.day, &jd)) + return QDate::fromJulianDay(jd); + } + return QDate(); +} + /***************************************************************************** Date/Time formatting helper functions *****************************************************************************/ @@ -340,7 +352,8 @@ static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcep QDate::QDate(int y, int m, int d) { - *this = QCalendar().dateFromParts(y, m, d); + if (!QGregorianCalendar::julianFromParts(y, m, d, &jd)) + jd = nullJd(); } QDate::QDate(int y, int m, int d, QCalendar cal) @@ -405,7 +418,12 @@ int QDate::year(QCalendar cal) const int QDate::year() const { - return year(QCalendar()); + if (isValid()) { + const auto parts = QGregorianCalendar::partsFromJulian(jd); + if (parts.isValid()) + return parts.year; + } + return 0; } /*! @@ -452,7 +470,12 @@ int QDate::month(QCalendar cal) const int QDate::month() const { - return month(QCalendar()); + if (isValid()) { + const auto parts = QGregorianCalendar::partsFromJulian(jd); + if (parts.isValid()) + return parts.month; + } + return 0; } /*! @@ -480,7 +503,12 @@ int QDate::day(QCalendar cal) const int QDate::day() const { - return day(QCalendar()); + if (isValid()) { + const auto parts = QGregorianCalendar::partsFromJulian(jd); + if (parts.isValid()) + return parts.day; + } + return 0; } /*! @@ -507,7 +535,7 @@ int QDate::dayOfWeek(QCalendar cal) const int QDate::dayOfWeek() const { - return dayOfWeek(QCalendar()); + return isValid() ? QGregorianCalendar::weekDayOfJulian(jd) : 0; } /*! @@ -535,7 +563,12 @@ int QDate::dayOfYear(QCalendar cal) const int QDate::dayOfYear() const { - return dayOfYear(QCalendar()); + if (isValid()) { + qint64 first; + if (QGregorianCalendar::julianFromParts(year(), 1, 1, &first)) + return jd - first + 1; + } + return 0; } /*! @@ -563,7 +596,12 @@ int QDate::daysInMonth(QCalendar cal) const int QDate::daysInMonth() const { - return daysInMonth(QCalendar()); + if (isValid()) { + const auto parts = QGregorianCalendar::partsFromJulian(jd); + if (parts.isValid()) + return QGregorianCalendar::monthLength(parts.month, parts.year); + } + return 0; } /*! @@ -589,7 +627,7 @@ int QDate::daysInYear(QCalendar cal) const int QDate::daysInYear() const { - return daysInYear(QCalendar()); + return isValid() ? QGregorianCalendar::leapTest(year()) ? 366 : 365 : 0; } /*! @@ -1303,7 +1341,11 @@ QString QDate::toString(const QString &format, QCalendar cal) const */ bool QDate::setDate(int year, int month, int day) { - return setDate(year, month, day, QCalendar()); + if (QGregorianCalendar::julianFromParts(year, month, day, &jd)) + return true; + + jd = nullJd(); + return false; } /*! @@ -1339,7 +1381,7 @@ void QDate::getDate(int *year, int *month, int *day) const { QCalendar::YearMonthDay parts; // invalid by default if (isValid()) - parts = QCalendar().partsFromDate(*this); + parts = QGregorianCalendar::partsFromJulian(jd); const bool ok = parts.isValid(); if (year) @@ -1428,7 +1470,30 @@ QDate QDate::addMonths(int nmonths, QCalendar cal) const QDate QDate::addMonths(int nmonths) const { - return addMonths(nmonths, QCalendar()); + if (isNull()) + return QDate(); + + if (nmonths == 0) + return *this; + + auto parts = QGregorianCalendar::partsFromJulian(jd); + + if (!parts.isValid()) + return QDate(); + Q_ASSERT(parts.year); + + parts.month += nmonths; + while (parts.month <= 0) { + if (--parts.year) // skip over year 0 + parts.month += 12; + } + while (parts.month > 12) { + parts.month -= 12; + if (!++parts.year) // skip over year 0 + ++parts.year; + } + + return fixedDate(std::move(parts)); } /*! @@ -1470,7 +1535,21 @@ QDate QDate::addYears(int nyears, QCalendar cal) const QDate QDate::addYears(int nyears) const { - return addYears(nyears, QCalendar()); + if (isNull()) + return QDate(); + + auto parts = QGregorianCalendar::partsFromJulian(jd); + if (!parts.isValid()) + return QDate(); + + int old_y = parts.year; + parts.year += nyears; + + // If we just crossed (or hit) a missing year zero, adjust year by +/- 1: + if ((old_y > 0) != (parts.year > 0) || !parts.year) + parts.year += nyears > 0 ? +1 : -1; + + return fixedDate(std::move(parts)); } /*! @@ -1719,7 +1798,7 @@ QDate QDate::fromString(const QString &string, const QString &format) bool QDate::isValid(int year, int month, int day) { - return QCalendar().isDateValid(year, month, day); + return QGregorianCalendar::validParts(year, month, day); } /*! @@ -1733,7 +1812,7 @@ bool QDate::isValid(int year, int month, int day) bool QDate::isLeapYear(int y) { - return QCalendar().isLeapYear(y); + return QGregorianCalendar::leapTest(y); } /*! \fn static QDate QDate::fromJulianDay(qint64 jd) diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp index d3db572aa7..447185d124 100644 --- a/src/corelib/time/qgregoriancalendar.cpp +++ b/src/corelib/time/qgregoriancalendar.cpp @@ -77,6 +77,11 @@ QCalendar::System QGregorianCalendar::calendarSystem() const } bool QGregorianCalendar::isLeapYear(int year) const +{ + return leapTest(year); +} + +bool QGregorianCalendar::leapTest(int year) { if (year == QCalendar::Unspecified) return false; @@ -88,10 +93,37 @@ bool QGregorianCalendar::isLeapYear(int year) const return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); } +// Duplicating code from QRomanCalendar, but inlining isLeapYear() as leapTest(): +int QGregorianCalendar::monthLength(int month, int year) +{ + if (month < 1 || month > 12) + return 0; + + if (month == 2) + return leapTest(year) ? 29 : 28; + + return 30 | ((month & 1) ^ (month >> 3)); +} + +bool QGregorianCalendar::validParts(int year, int month, int day) +{ + return year && 0 < day && day <= monthLength(month, year); +} + +int QGregorianCalendar::weekDayOfJulian(qint64 jd) +{ + return qMod(jd, 7) + 1; +} + bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const +{ + return julianFromParts(year, month, day, jd); +} + +bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd) { Q_ASSERT(jd); - if (!isDateValid(year, month, day)) + if (!validParts(year, month, day)) return false; if (year < 0) @@ -111,6 +143,11 @@ bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *j } QCalendar::YearMonthDay QGregorianCalendar::julianDayToDate(qint64 jd) const +{ + return partsFromJulian(jd); +} + +QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd) { /* * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php diff --git a/src/corelib/time/qgregoriancalendar_p.h b/src/corelib/time/qgregoriancalendar_p.h index 4e6c42ef76..191f9c127b 100644 --- a/src/corelib/time/qgregoriancalendar_p.h +++ b/src/corelib/time/qgregoriancalendar_p.h @@ -75,6 +75,14 @@ public: QLocale::FormatType format) const override; QString standaloneMonthName(const QLocale &locale, int month, int year, QLocale::FormatType format) const override; + + // Static optimized versions for the benefit of QDate: + static int weekDayOfJulian(qint64 jd); + static bool leapTest(int year); + static int monthLength(int month, int year); + static bool validParts(int year, int month, int day); + static QCalendar::YearMonthDay partsFromJulian(qint64 jd); + static bool julianFromParts(int year, int month, int day, qint64 *jd); }; QT_END_NAMESPACE From 2faa6cafa91b4478d129ec570d1cd8e6ed2f147d Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 8 Aug 2019 15:27:11 +0200 Subject: [PATCH 02/12] Pass QDate, QTime as value classes, rather than by const reference This can, of course, only be done in private APIs - but comment on public APIs to do the same at Qt 6. Change-Id: I3c79951572be3c37b0b0c5b1b05bced051a40964 Reviewed-by: Volker Hilsheimer Reviewed-by: Paul Wicking --- src/corelib/text/qlocale_win.cpp | 8 +- src/corelib/time/qdatetime.h | 16 +-- src/widgets/widgets/qcalendarwidget.cpp | 106 +++++++++--------- src/widgets/widgets/qdatetimeedit.cpp | 6 +- src/widgets/widgets/qdatetimeedit_p.h | 12 +- .../qdatetimeedit/tst_qdatetimeedit.cpp | 6 +- 6 files changed, 76 insertions(+), 78 deletions(-) diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp index dc904ad02d..79ea67f966 100644 --- a/src/corelib/text/qlocale_win.cpp +++ b/src/corelib/text/qlocale_win.cpp @@ -116,8 +116,8 @@ struct QSystemLocalePrivate QVariant dateTimeFormat(QLocale::FormatType); QVariant dayName(int, QLocale::FormatType); QVariant monthName(int, QLocale::FormatType); - QVariant toString(const QDate &, QLocale::FormatType); - QVariant toString(const QTime &, QLocale::FormatType); + QVariant toString(QDate, QLocale::FormatType); + QVariant toString(QTime, QLocale::FormatType); QVariant toString(const QDateTime &, QLocale::FormatType); QVariant measurementSystem(); QVariant collation(); @@ -394,7 +394,7 @@ QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type) return getLocaleInfo(lctype); } -QVariant QSystemLocalePrivate::toString(const QDate &date, QLocale::FormatType type) +QVariant QSystemLocalePrivate::toString(QDate date, QLocale::FormatType type) { SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); @@ -413,7 +413,7 @@ QVariant QSystemLocalePrivate::toString(const QDate &date, QLocale::FormatType t return QString(); } -QVariant QSystemLocalePrivate::toString(const QTime &time, QLocale::FormatType type) +QVariant QSystemLocalePrivate::toString(QTime time, QLocale::FormatType type) { SYSTEMTIME st; memset(&st, 0, sizeof(SYSTEMTIME)); diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index e1909b85e3..3eae8ebf64 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -58,7 +58,7 @@ class QCalendar; class QTimeZone; class QDateTime; -class Q_CORE_EXPORT QDate +class Q_CORE_EXPORT QDate // ### Qt 6: change to be used by value, not const & { public: enum MonthNameType { // ### Qt 6: remove, along with methods using it @@ -138,7 +138,7 @@ public: Q_REQUIRED_RESULT QDate addYears(int years) const; Q_REQUIRED_RESULT QDate addMonths(int months, QCalendar cal) const; Q_REQUIRED_RESULT QDate addYears(int years, QCalendar cal) const; - qint64 daysTo(const QDate &) const; + qint64 daysTo(const QDate &) const; // ### Qt 6: QDate Q_DECL_CONSTEXPR bool operator==(const QDate &other) const { return jd == other.jd; } Q_DECL_CONSTEXPR bool operator!=(const QDate &other) const { return jd != other.jd; } @@ -177,7 +177,7 @@ private: }; Q_DECLARE_TYPEINFO(QDate, Q_MOVABLE_TYPE); -class Q_CORE_EXPORT QTime +class Q_CORE_EXPORT QTime // ### Qt 6: change to be used by value, not const & { explicit Q_DECL_CONSTEXPR QTime(int ms) : mds(ms) {} @@ -203,9 +203,9 @@ public: bool setHMS(int h, int m, int s, int ms = 0); Q_REQUIRED_RESULT QTime addSecs(int secs) const; - int secsTo(const QTime &) const; + int secsTo(const QTime &) const; // ### Qt 6: plain QTime Q_REQUIRED_RESULT QTime addMSecs(int ms) const; - int msecsTo(const QTime &) const; + int msecsTo(const QTime &) const; // ### Qt 6: plain QTime Q_DECL_CONSTEXPR bool operator==(const QTime &other) const { return mds == other.mds; } Q_DECL_CONSTEXPR bool operator!=(const QTime &other) const { return mds != other.mds; } @@ -236,7 +236,7 @@ private: friend class QDateTime; friend class QDateTimePrivate; -#ifndef QT_NO_DATASTREAM +#ifndef QT_NO_DATASTREAM // ### Qt 6: plain QTime friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QTime &); friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QTime &); #endif @@ -287,7 +287,7 @@ class Q_CORE_EXPORT QDateTime public: QDateTime() noexcept(Data::CanBeSmall); - explicit QDateTime(const QDate &); + explicit QDateTime(const QDate &); // ### Qt 6: plain QDate, QTime QDateTime(const QDate &, const QTime &, Qt::TimeSpec spec = Qt::LocalTime); // ### Qt 6: Merge with above with default offsetSeconds = 0 QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec, int offsetSeconds); @@ -319,7 +319,7 @@ public: qint64 toMSecsSinceEpoch() const; qint64 toSecsSinceEpoch() const; - void setDate(const QDate &date); + void setDate(const QDate &date); // ### Qt 6: plain QDate void setTime(const QTime &time); void setTimeSpec(Qt::TimeSpec spec); void setOffsetFromUtc(int offsetSeconds); diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index 296a15f94b..ec19b64d4a 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -91,10 +91,10 @@ public: QCalendarDateSectionValidator() {} virtual ~QCalendarDateSectionValidator() {} virtual Section handleKey(int key) = 0; - virtual QDate applyToDate(const QDate &date, QCalendar cal = QCalendar()) const = 0; - virtual void setDate(const QDate &date, QCalendar cal = QCalendar()) = 0; + virtual QDate applyToDate(QDate date, QCalendar cal = QCalendar()) const = 0; + virtual void setDate(QDate date, QCalendar cal = QCalendar()) = 0; virtual QString text() const = 0; - virtual QString text(const QDate &date, QCalendar cal, int repeat) const = 0; + virtual QString text(QDate date, QCalendar cal, int repeat) const = 0; QLocale m_locale; @@ -117,10 +117,10 @@ class QCalendarDayValidator : public QCalendarDateSectionValidator public: QCalendarDayValidator(); virtual Section handleKey(int key) override; - virtual QDate applyToDate(const QDate &date, QCalendar cal) const override; - virtual void setDate(const QDate &date, QCalendar cal) override; + virtual QDate applyToDate(QDate date, QCalendar cal) const override; + virtual void setDate(QDate date, QCalendar cal) override; virtual QString text() const override; - virtual QString text(const QDate &date, QCalendar cal, int repeat) const override; + virtual QString text(QDate date, QCalendar cal, int repeat) const override; private: int m_pos; int m_day; @@ -181,7 +181,7 @@ QCalendarDateSectionValidator::Section QCalendarDayValidator::handleKey(int key) return QCalendarDateSectionValidator::ThisSection; } -QDate QCalendarDayValidator::applyToDate(const QDate &date, QCalendar cal) const +QDate QCalendarDayValidator::applyToDate(QDate date, QCalendar cal) const { auto parts = cal.partsFromDate(date); if (!parts.isValid()) @@ -190,7 +190,7 @@ QDate QCalendarDayValidator::applyToDate(const QDate &date, QCalendar cal) const return cal.dateFromParts(parts); } -void QCalendarDayValidator::setDate(const QDate &date, QCalendar cal) +void QCalendarDayValidator::setDate(QDate date, QCalendar cal) { m_day = m_oldDay = date.day(cal); m_pos = 0; @@ -201,7 +201,7 @@ QString QCalendarDayValidator::text() const return highlightString(formatNumber(m_day, 2), m_pos); } -QString QCalendarDayValidator::text(const QDate &date, QCalendar cal, int repeat) const +QString QCalendarDayValidator::text(QDate date, QCalendar cal, int repeat) const { if (repeat <= 1) { return QString::number(date.day(cal)); @@ -222,10 +222,10 @@ class QCalendarMonthValidator : public QCalendarDateSectionValidator public: QCalendarMonthValidator(); virtual Section handleKey(int key) override; - virtual QDate applyToDate(const QDate &date, QCalendar cal) const override; - virtual void setDate(const QDate &date, QCalendar cal) override; + virtual QDate applyToDate(QDate date, QCalendar cal) const override; + virtual void setDate(QDate date, QCalendar cal) override; virtual QString text() const override; - virtual QString text(const QDate &date, QCalendar cal, int repeat) const override; + virtual QString text(QDate date, QCalendar cal, int repeat) const override; private: int m_pos; int m_month; @@ -286,7 +286,7 @@ QCalendarDateSectionValidator::Section QCalendarMonthValidator::handleKey(int ke return QCalendarDateSectionValidator::ThisSection; } -QDate QCalendarMonthValidator::applyToDate(const QDate &date, QCalendar cal) const +QDate QCalendarMonthValidator::applyToDate(QDate date, QCalendar cal) const { auto parts = cal.partsFromDate(date); if (!parts.isValid()) @@ -296,7 +296,7 @@ QDate QCalendarMonthValidator::applyToDate(const QDate &date, QCalendar cal) con return cal.dateFromParts(parts); } -void QCalendarMonthValidator::setDate(const QDate &date, QCalendar cal) +void QCalendarMonthValidator::setDate(QDate date, QCalendar cal) { m_month = m_oldMonth = date.month(cal); m_pos = 0; @@ -307,7 +307,7 @@ QString QCalendarMonthValidator::text() const return highlightString(formatNumber(m_month, 2), m_pos); } -QString QCalendarMonthValidator::text(const QDate &date, QCalendar cal, int repeat) const +QString QCalendarMonthValidator::text(QDate date, QCalendar cal, int repeat) const { if (repeat <= 1) { return QString::number(date.month(cal)); @@ -328,10 +328,10 @@ class QCalendarYearValidator : public QCalendarDateSectionValidator public: QCalendarYearValidator(); virtual Section handleKey(int key) override; - virtual QDate applyToDate(const QDate &date, QCalendar cal) const override; - virtual void setDate(const QDate &date, QCalendar cal) override; + virtual QDate applyToDate(QDate date, QCalendar cal) const override; + virtual void setDate(QDate date, QCalendar cal) override; virtual QString text() const override; - virtual QString text(const QDate &date, QCalendar cal, int repeat) const override; + virtual QString text(QDate date, QCalendar cal, int repeat) const override; private: int pow10(int n); int m_pos; @@ -392,7 +392,7 @@ QCalendarDateSectionValidator::Section QCalendarYearValidator::handleKey(int key return QCalendarDateSectionValidator::ThisSection; } -QDate QCalendarYearValidator::applyToDate(const QDate &date, QCalendar cal) const +QDate QCalendarYearValidator::applyToDate(QDate date, QCalendar cal) const { auto parts = cal.partsFromDate(date); if (!parts.isValid()) @@ -403,7 +403,7 @@ QDate QCalendarYearValidator::applyToDate(const QDate &date, QCalendar cal) cons return cal.dateFromParts(parts); } -void QCalendarYearValidator::setDate(const QDate &date, QCalendar cal) +void QCalendarYearValidator::setDate(QDate date, QCalendar cal) { m_year = m_oldYear = date.year(cal); m_pos = 0; @@ -414,7 +414,7 @@ QString QCalendarYearValidator::text() const return highlightString(formatNumber(m_year, 4), m_pos); } -QString QCalendarYearValidator::text(const QDate &date, QCalendar cal, int repeat) const +QString QCalendarYearValidator::text(QDate date, QCalendar cal, int repeat) const { if (repeat < 4) return formatNumber(date.year(cal) % 100, 2); @@ -444,7 +444,7 @@ public: QString currentText(QCalendar cal) const; QDate currentDate() const { return m_currentDate; } void setFormat(const QString &format); - void setInitialDate(const QDate &date, QCalendar cal); + void setInitialDate(QDate date, QCalendar cal); void setLocale(const QLocale &locale); @@ -501,7 +501,7 @@ int QCalendarDateValidator::countRepeat(const QString &str, int index) const return count; } -void QCalendarDateValidator::setInitialDate(const QDate &date, QCalendar cal) +void QCalendarDateValidator::setInitialDate(QDate date, QCalendar cal) { m_yearValidator.setDate(date, cal); m_monthValidator.setDate(date, cal); @@ -648,13 +648,13 @@ public: int dateEditAcceptDelay() const; void setDateEditAcceptDelay(int delay); - void setDate(const QDate &date); + void setDate(QDate date); bool eventFilter(QObject *o, QEvent *e) override; void timerEvent(QTimerEvent *e) override; signals: - void dateChanged(const QDate &date); + void dateChanged(QDate date); void editingFinished(); private: @@ -684,7 +684,7 @@ void QCalendarTextNavigator::setWidget(QWidget *widget) m_widget = widget; } -void QCalendarTextNavigator::setDate(const QDate &date) +void QCalendarTextNavigator::setDate(QDate date) { m_date = date; } @@ -891,15 +891,15 @@ public: } void showMonth(int year, int month); - void setDate(const QDate &d); + void setDate(QDate d); void setCalendar(QCalendar c); QCalendar calendar() const; - void setMinimumDate(const QDate &date); - void setMaximumDate(const QDate &date); + void setMinimumDate(QDate date); + void setMaximumDate(QDate date); - void setRange(const QDate &min, const QDate &max); + void setRange(QDate min, QDate max); void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format); @@ -913,7 +913,7 @@ public: Qt::DayOfWeek dayOfWeekForColumn(int section) const; int columnForDayOfWeek(Qt::DayOfWeek day) const; QDate dateForCell(int row, int column) const; - void cellForDate(const QDate &date, int *row, int *column) const; + void cellForDate(QDate date, int *row, int *column) const; QString dayName(Qt::DayOfWeek day) const; void setView(QCalendarView *view) @@ -921,7 +921,7 @@ public: void internalUpdate(); QDate referenceDate() const; - int columnForFirstOfMonth(const QDate &date) const; + int columnForFirstOfMonth(QDate date) const; int m_firstColumn; int m_firstRow; @@ -951,9 +951,9 @@ public: virtual void keyboardSearch(const QString & search) override { Q_UNUSED(search) } signals: - void showDate(const QDate &date); - void changeDate(const QDate &date, bool changeMonth); - void clicked(const QDate &date); + void showDate(QDate date); + void changeDate(QDate date, bool changeMonth); + void clicked(QDate date); void editingFinished(); protected: QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override; @@ -1039,7 +1039,7 @@ QDate QCalendarModel::referenceDate() const return QDate(); } -int QCalendarModel::columnForFirstOfMonth(const QDate &date) const +int QCalendarModel::columnForFirstOfMonth(QDate date) const { return (columnForDayOfWeek(static_cast(m_calendar.dayOfWeek(date))) - (date.day(m_calendar) % 7) + 8) % 7; @@ -1063,7 +1063,7 @@ QDate QCalendarModel::dateForCell(int row, int column) const return refDate.addDays(requestedDay); } -void QCalendarModel::cellForDate(const QDate &date, int *row, int *column) const +void QCalendarModel::cellForDate(QDate date, int *row, int *column) const { if (!row && !column) return; @@ -1206,7 +1206,7 @@ Qt::ItemFlags QCalendarModel::flags(const QModelIndex &index) const return QAbstractTableModel::flags(index); } -void QCalendarModel::setDate(const QDate &d) +void QCalendarModel::setDate(QDate d) { m_date = d; if (m_date < m_minimumDate) @@ -1240,7 +1240,7 @@ void QCalendarModel::showMonth(int year, int month) internalUpdate(); } -void QCalendarModel::setMinimumDate(const QDate &d) +void QCalendarModel::setMinimumDate(QDate d) { if (!d.isValid() || d == m_minimumDate) return; @@ -1253,7 +1253,7 @@ void QCalendarModel::setMinimumDate(const QDate &d) internalUpdate(); } -void QCalendarModel::setMaximumDate(const QDate &d) +void QCalendarModel::setMaximumDate(QDate d) { if (!d.isValid() || d == m_maximumDate) return; @@ -1266,7 +1266,7 @@ void QCalendarModel::setMaximumDate(const QDate &d) internalUpdate(); } -void QCalendarModel::setRange(const QDate &min, const QDate &max) +void QCalendarModel::setRange(QDate min, QDate max) { m_minimumDate = min; m_maximumDate = max; @@ -1597,7 +1597,7 @@ public: { } virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; - void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const; + void paintCell(QPainter *painter, const QRect &rect, QDate date) const; private: QCalendarWidgetPrivate *calendarWidgetPrivate; @@ -1658,11 +1658,11 @@ public: void showMonth(int year, int month); void update(); - void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const; + void paintCell(QPainter *painter, const QRect &rect, QDate date) const; - void _q_slotShowDate(const QDate &date); - void _q_slotChangeDate(const QDate &date); - void _q_slotChangeDate(const QDate &date, bool changeMonth); + void _q_slotShowDate(QDate date); + void _q_slotChangeDate(QDate date); + void _q_slotChangeDate(QDate date, bool changeMonth); void _q_editingFinished(); void _q_monthChanged(QAction*); void _q_prevMonthClicked(); @@ -1675,7 +1675,7 @@ public: void updateMonthMenu(); void updateMonthMenuNames(); void updateNavigationBar(); - void updateCurrentPage(const QDate &newDate); + void updateCurrentPage(QDate newDate); inline QDate getCurrentDate(); void setNavigatorEnabled(bool enable); @@ -1714,7 +1714,7 @@ void QCalendarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt } } -void QCalendarDelegate::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const +void QCalendarDelegate::paintCell(QPainter *painter, const QRect &rect, QDate date) const { storedOption.rect = rect; int row = -1; @@ -1881,7 +1881,7 @@ void QCalendarWidgetPrivate::updateMonthMenuNames() } } -void QCalendarWidgetPrivate::updateCurrentPage(const QDate &date) +void QCalendarWidgetPrivate::updateCurrentPage(QDate date) { Q_Q(QCalendarWidget); QCalendar cal = m_model->calendar(); @@ -1999,23 +1999,23 @@ void QCalendarWidgetPrivate::update() } } -void QCalendarWidgetPrivate::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const +void QCalendarWidgetPrivate::paintCell(QPainter *painter, const QRect &rect, QDate date) const { Q_Q(const QCalendarWidget); q->paintCell(painter, rect, date); } -void QCalendarWidgetPrivate::_q_slotShowDate(const QDate &date) +void QCalendarWidgetPrivate::_q_slotShowDate(QDate date) { updateCurrentPage(date); } -void QCalendarWidgetPrivate::_q_slotChangeDate(const QDate &date) +void QCalendarWidgetPrivate::_q_slotChangeDate(QDate date) { _q_slotChangeDate(date, true); } -void QCalendarWidgetPrivate::_q_slotChangeDate(const QDate &date, bool changeMonth) +void QCalendarWidgetPrivate::_q_slotChangeDate(QDate date, bool changeMonth) { QDate oldDate = m_model->m_date; m_model->setDate(date); diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp index 31607d77e7..e26993fb23 100644 --- a/src/widgets/widgets/qdatetimeedit.cpp +++ b/src/widgets/widgets/qdatetimeedit.cpp @@ -2636,13 +2636,13 @@ void QCalendarPopup::setCalendarWidget(QCalendarWidget *cw) } -void QCalendarPopup::setDate(const QDate &date) +void QCalendarPopup::setDate(QDate date) { oldDate = date; verifyCalendarInstance()->setSelectedDate(date); } -void QCalendarPopup::setDateRange(const QDate &min, const QDate &max) +void QCalendarPopup::setDateRange(QDate min, QDate max) { QCalendarWidget *cw = verifyCalendarInstance(); cw->setMinimumDate(min); @@ -2686,7 +2686,7 @@ void QCalendarPopup::dateSelectionChanged() dateChanged = true; emit newDateSelected(verifyCalendarInstance()->selectedDate()); } -void QCalendarPopup::dateSelected(const QDate &date) +void QCalendarPopup::dateSelected(QDate date) { dateChanged = true; emit activated(date); diff --git a/src/widgets/widgets/qdatetimeedit_p.h b/src/widgets/widgets/qdatetimeedit_p.h index 453ac36d15..dcf8863c8b 100644 --- a/src/widgets/widgets/qdatetimeedit_p.h +++ b/src/widgets/widgets/qdatetimeedit_p.h @@ -148,19 +148,19 @@ public: explicit QCalendarPopup(QWidget *parent = nullptr, QCalendarWidget *cw = nullptr, QCalendar ca = QCalendar()); QDate selectedDate() { return verifyCalendarInstance()->selectedDate(); } - void setDate(const QDate &date); - void setDateRange(const QDate &min, const QDate &max); + void setDate(QDate date); + void setDateRange(QDate min, QDate max); void setFirstDayOfWeek(Qt::DayOfWeek dow) { verifyCalendarInstance()->setFirstDayOfWeek(dow); } QCalendarWidget *calendarWidget() const { return const_cast(this)->verifyCalendarInstance(); } void setCalendarWidget(QCalendarWidget *cw); Q_SIGNALS: - void activated(const QDate &date); - void newDateSelected(const QDate &newDate); - void hidingCalendar(const QDate &oldDate); + void activated(QDate date); + void newDateSelected(QDate newDate); + void hidingCalendar(QDate oldDate); void resetButton(); private Q_SLOTS: - void dateSelected(const QDate &date); + void dateSelected(QDate date); void dateSelectionChanged(); protected: diff --git a/tests/auto/widgets/widgets/qdatetimeedit/tst_qdatetimeedit.cpp b/tests/auto/widgets/widgets/qdatetimeedit/tst_qdatetimeedit.cpp index 810f081b73..26b4b7d020 100644 --- a/tests/auto/widgets/widgets/qdatetimeedit/tst_qdatetimeedit.cpp +++ b/tests/auto/widgets/widgets/qdatetimeedit/tst_qdatetimeedit.cpp @@ -348,8 +348,7 @@ static QLatin1String sectionToName(const QDateTimeEdit::Section section) } } -static QDate stepDate(const QDate& startDate, const QDateTimeEdit::Section section, - const int steps) +static QDate stepDate(QDate startDate, const QDateTimeEdit::Section section, const int steps) { switch (section) { case QDateTimeEdit::DaySection: @@ -364,8 +363,7 @@ static QDate stepDate(const QDate& startDate, const QDateTimeEdit::Section secti } } -static QTime stepTime(const QTime& startTime, const QDateTimeEdit::Section section, - const int steps) +static QTime stepTime(QTime startTime, const QDateTimeEdit::Section section, const int steps) { switch (section) { case QDateTimeEdit::SecondSection: From 06f69700af0bb312f08f9455f127abf452896d48 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 13:41:17 +0200 Subject: [PATCH 03/12] Fix some bogus date calculations in QDateTime's benchmark Multiplying a Julian Day number by the number of milliseconds per day does not get you a time since the start of 1970; it gets you a time since the start of the Julian Day number system, which was several millennia earlier. Change-Id: Ic90a6c3de445baf9cfd30f28dd847f146e6a7adf Reviewed-by: David Faure --- .../corelib/time/qdatetime/main.cpp | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/benchmarks/corelib/time/qdatetime/main.cpp b/tests/benchmarks/corelib/time/qdatetime/main.cpp index b693400376..728c4acb91 100644 --- a/tests/benchmarks/corelib/time/qdatetime/main.cpp +++ b/tests/benchmarks/corelib/time/qdatetime/main.cpp @@ -41,6 +41,7 @@ class tst_QDateTime : public QObject MSECS_PER_DAY = 86400000, JULIAN_DAY_1950 = 2433283, JULIAN_DAY_1960 = 2436935, + JULIAN_DAY_1970 = 2440588, // Epoch JULIAN_DAY_2010 = 2455198, JULIAN_DAY_2011 = 2455563, JULIAN_DAY_2020 = 2458850, @@ -299,7 +300,7 @@ void tst_QDateTime::setOffsetFromUtc() void tst_QDateTime::setMSecsSinceEpoch() { - qint64 msecs = qint64(JULIAN_DAY_2010 + 180) * MSECS_PER_DAY; + qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -313,11 +314,12 @@ void tst_QDateTime::setMSecsSinceEpochTz() { QTimeZone cet = QTimeZone("Europe/Oslo"); QList list; + const qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); QBENCHMARK { foreach (QDateTime test, list) - test.setMSecsSinceEpoch((JULIAN_DAY_2010 + 180) * MSECS_PER_DAY); + test.setMSecsSinceEpoch(msecs); } } @@ -424,7 +426,8 @@ void tst_QDateTime::toOffsetFromUtc() void tst_QDateTime::daysTo() { - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -436,7 +439,8 @@ void tst_QDateTime::daysTo() void tst_QDateTime::msecsTo() { - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -449,7 +453,8 @@ void tst_QDateTime::msecsTo() void tst_QDateTime::equivalent() { bool result; - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -463,7 +468,8 @@ void tst_QDateTime::equivalent() void tst_QDateTime::equivalentUtc() { bool result = false; - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY, Qt::UTC); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -477,7 +483,8 @@ void tst_QDateTime::equivalentUtc() void tst_QDateTime::lessThan() { bool result = false; - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -491,7 +498,8 @@ void tst_QDateTime::lessThan() void tst_QDateTime::lessThanUtc() { bool result = false; - QDateTime other = QDateTime::fromMSecsSinceEpoch(qint64(JULIAN_DAY_2010) * MSECS_PER_DAY, Qt::UTC); + const QDateTime other = QDateTime::fromMSecsSinceEpoch( + qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); @@ -573,25 +581,31 @@ void tst_QDateTime::fromStringIso() void tst_QDateTime::fromMSecsSinceEpoch() { + const int start = JULIAN_DAY_2010 - JULIAN_DAY_1970; + const int end = JULIAN_DAY_2020 - JULIAN_DAY_1970; QBENCHMARK { - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) + for (int jd = start; jd < end; ++jd) QDateTime::fromMSecsSinceEpoch(jd * MSECS_PER_DAY, Qt::LocalTime); } } void tst_QDateTime::fromMSecsSinceEpochUtc() { + const int start = JULIAN_DAY_2010 - JULIAN_DAY_1970; + const int end = JULIAN_DAY_2020 - JULIAN_DAY_1970; QBENCHMARK { - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) + for (int jd = start; jd < end; ++jd) QDateTime::fromMSecsSinceEpoch(jd * MSECS_PER_DAY, Qt::UTC); } } void tst_QDateTime::fromMSecsSinceEpochTz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); + const int start = JULIAN_DAY_2010 - JULIAN_DAY_1970; + const int end = JULIAN_DAY_2020 - JULIAN_DAY_1970; + const QTimeZone cet("Europe/Oslo"); QBENCHMARK { - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) + for (int jd = start; jd < end; ++jd) QDateTime test = QDateTime::fromMSecsSinceEpoch(jd * MSECS_PER_DAY, cet); } } From 54f6c9bfd900fbee8800d720879ecb0c023585ac Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 14:05:48 +0200 Subject: [PATCH 04/12] Fix unused variable warnings in QDateTime benchmark Change-Id: Id123ace74cfa7b5ff406eabbfda0aad9f58c3fd4 Reviewed-by: David Faure --- .../corelib/time/qdatetime/main.cpp | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/benchmarks/corelib/time/qdatetime/main.cpp b/tests/benchmarks/corelib/time/qdatetime/main.cpp index 728c4acb91..e616181bea 100644 --- a/tests/benchmarks/corelib/time/qdatetime/main.cpp +++ b/tests/benchmarks/corelib/time/qdatetime/main.cpp @@ -221,37 +221,43 @@ void tst_QDateTime::toMSecsSinceEpoch2050() void tst_QDateTime::toMSecsSinceEpochTz() { QTimeZone cet = QTimeZone("Europe/Oslo"); + qint64 result; QList list; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); QBENCHMARK { foreach (const QDateTime &test, list) - qint64 result = test.toMSecsSinceEpoch(); + result = test.toMSecsSinceEpoch(); } + Q_UNUSED(result); } void tst_QDateTime::toMSecsSinceEpoch1950Tz() { QTimeZone cet = QTimeZone("Europe/Oslo"); + qint64 result; QList list; for (int jd = JULIAN_DAY_1950; jd < JULIAN_DAY_1960; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); QBENCHMARK { foreach (const QDateTime &test, list) - qint64 result = test.toMSecsSinceEpoch(); + result = test.toMSecsSinceEpoch(); } + Q_UNUSED(result); } void tst_QDateTime::toMSecsSinceEpoch2050Tz() { QTimeZone cet = QTimeZone("Europe/Oslo"); + qint64 result; QList list; for (int jd = JULIAN_DAY_2050; jd < JULIAN_DAY_2060; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); QBENCHMARK { foreach (const QDateTime &test, list) - qint64 result = test.toMSecsSinceEpoch(); + result = test.toMSecsSinceEpoch(); } + Q_UNUSED(result); } void tst_QDateTime::setDate() @@ -359,12 +365,14 @@ void tst_QDateTime::toStringIsoFormat() void tst_QDateTime::addDays() { QList list; + QDateTime next; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); QBENCHMARK { foreach (const QDateTime &test, list) - test.addDays(1); + next = test.addDays(1); } + Q_UNUSED(next); } void tst_QDateTime::addDaysTz() @@ -382,12 +390,14 @@ void tst_QDateTime::addDaysTz() void tst_QDateTime::addMSecs() { QList list; + QDateTime next; for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); QBENCHMARK { foreach (const QDateTime &test, list) - test.addMSecs(1); + next = test.addMSecs(1); } + Q_UNUSED(next); } void tst_QDateTime::addMSecsTz() From c78a716b723812e14563b2e1c96c050703dbc9c7 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 29 Aug 2019 21:58:32 +0200 Subject: [PATCH 05/12] QDateTimeParser: replace QVector with QVarLengthArray<12> The lists of month and day-of-week names usually currently do not exceed 13 elements (incl. possible leap months), so don't use QVector, use QVarLengthArray with Prealloc = 13. This value may be increased when ISO week-as-month is merged, but the container remains valid even with 52 "months" (though the code that calculates its runtime size will naturally need to be adjusted). Change-Id: I4ead897d933f89fc092850fcc22ca41da0a6ddc6 Reviewed-by: Edward Welbourne --- src/corelib/time/qdatetimeparser.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 3c54a259a4..d0f34358b9 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -60,6 +60,9 @@ QT_BEGIN_NAMESPACE +template +using ShortVector = QVarLengthArray; // enough for month (incl. leap) and day-of-week names + QDateTimeParser::~QDateTimeParser() { } @@ -1538,7 +1541,7 @@ QDateTimeParser::parse(QString input, int position, const QDateTime &defaultValu length of overlap in *used (if \a used is non-NULL) and the first entry that overlapped this much in *usedText (if \a usedText is non-NULL). */ -static int findTextEntry(const QString &text, const QVector &entries, QString *usedText, int *used) +static int findTextEntry(const QString &text, const ShortVector &entries, QString *usedText, int *used) { if (text.isEmpty()) return -1; @@ -1586,7 +1589,7 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI QLocale::FormatType type = sn.count == 3 ? QLocale::ShortFormat : QLocale::LongFormat; QLocale l = locale(); - QVector monthNames; + ShortVector monthNames; monthNames.reserve(13 - startMonth); for (int month = startMonth; month <= 12; ++month) monthNames.append(calendar.monthName(l, month, year, type)); @@ -1605,7 +1608,7 @@ int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex QLocale::FormatType type = sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat; QLocale l = locale(); - QVector daysOfWeek; + ShortVector daysOfWeek; daysOfWeek.reserve(8 - startDay); for (int day = startDay; day <= 7; ++day) daysOfWeek.append(l.dayName(day, type)); From 0556366ddb3a59071256a5646c3c82e7da23d619 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 15:24:52 +0200 Subject: [PATCH 06/12] Deduplicate list-building code in QDateTime benchmark Aside from the start-date and the end-date, and a variant with a time-zone, the lists various tests were building were all built the same way; so pack that up as a pair of functions (one without time-zone, one with) to save duplication. Make the list in each function const, ready for conversion of foreach loops to ranged for. In the process, replace QList with QVector, reserve space before we populate and use auto for the now-const list variables it's saved in. Change-Id: I7d8cce459a4d6111cd645e8d3966ad769ab7e201 Reviewed-by: Marc Mutz --- .../corelib/time/qdatetime/main.cpp | 165 ++++++------------ 1 file changed, 57 insertions(+), 108 deletions(-) diff --git a/tests/benchmarks/corelib/time/qdatetime/main.cpp b/tests/benchmarks/corelib/time/qdatetime/main.cpp index e616181bea..2730e7e18b 100644 --- a/tests/benchmarks/corelib/time/qdatetime/main.cpp +++ b/tests/benchmarks/corelib/time/qdatetime/main.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include class tst_QDateTime : public QObject @@ -49,6 +50,9 @@ class tst_QDateTime : public QObject JULIAN_DAY_2060 = 2473460 }; + static QVector daily(qint64 start, qint64 end); + static QVector norse(qint64 start, qint64 end); + private Q_SLOTS: void create(); void isNull(); @@ -98,6 +102,25 @@ private Q_SLOTS: void fromMSecsSinceEpochTz(); }; +QVector tst_QDateTime::daily(qint64 start, qint64 end) +{ + QVector list; + list.reserve(end - start); + for (int jd = start; jd < end; ++jd) + list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + return list; +} + +QVector tst_QDateTime::norse(qint64 start, qint64 end) +{ + const QTimeZone cet("Europe/Oslo"); + QVector list; + list.reserve(end - start); + for (int jd = start; jd < end; ++jd) + list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + return list; +} + void tst_QDateTime::create() { QBENCHMARK { @@ -110,9 +133,7 @@ void tst_QDateTime::create() void tst_QDateTime::isNull() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.isNull(); @@ -121,9 +142,7 @@ void tst_QDateTime::isNull() void tst_QDateTime::isValid() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.isValid(); @@ -132,9 +151,7 @@ void tst_QDateTime::isValid() void tst_QDateTime::date() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.date(); @@ -143,9 +160,7 @@ void tst_QDateTime::date() void tst_QDateTime::time() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.time(); @@ -154,9 +169,7 @@ void tst_QDateTime::time() void tst_QDateTime::timeSpec() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.timeSpec(); @@ -165,9 +178,7 @@ void tst_QDateTime::timeSpec() void tst_QDateTime::offsetFromUtc() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.offsetFromUtc(); @@ -176,9 +187,7 @@ void tst_QDateTime::offsetFromUtc() void tst_QDateTime::timeZoneAbbreviation() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.timeZoneAbbreviation(); @@ -187,9 +196,7 @@ void tst_QDateTime::timeZoneAbbreviation() void tst_QDateTime::toMSecsSinceEpoch() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.toMSecsSinceEpoch(); @@ -198,9 +205,7 @@ void tst_QDateTime::toMSecsSinceEpoch() void tst_QDateTime::toMSecsSinceEpoch1950() { - QList list; - for (int jd = JULIAN_DAY_1950; jd < JULIAN_DAY_1960; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_1950, JULIAN_DAY_1960); QBENCHMARK { foreach (const QDateTime &test, list) test.toMSecsSinceEpoch(); @@ -209,9 +214,7 @@ void tst_QDateTime::toMSecsSinceEpoch1950() void tst_QDateTime::toMSecsSinceEpoch2050() { - QList list; - for (int jd = JULIAN_DAY_2050; jd < JULIAN_DAY_2060; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2050, JULIAN_DAY_2060); QBENCHMARK { foreach (const QDateTime &test, list) test.toMSecsSinceEpoch(); @@ -220,11 +223,8 @@ void tst_QDateTime::toMSecsSinceEpoch2050() void tst_QDateTime::toMSecsSinceEpochTz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); qint64 result; - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) result = test.toMSecsSinceEpoch(); @@ -234,11 +234,8 @@ void tst_QDateTime::toMSecsSinceEpochTz() void tst_QDateTime::toMSecsSinceEpoch1950Tz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); qint64 result; - QList list; - for (int jd = JULIAN_DAY_1950; jd < JULIAN_DAY_1960; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_1950, JULIAN_DAY_1960); QBENCHMARK { foreach (const QDateTime &test, list) result = test.toMSecsSinceEpoch(); @@ -248,11 +245,8 @@ void tst_QDateTime::toMSecsSinceEpoch1950Tz() void tst_QDateTime::toMSecsSinceEpoch2050Tz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); qint64 result; - QList list; - for (int jd = JULIAN_DAY_2050; jd < JULIAN_DAY_2060; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_2050, JULIAN_DAY_2060); QBENCHMARK { foreach (const QDateTime &test, list) result = test.toMSecsSinceEpoch(); @@ -262,9 +256,7 @@ void tst_QDateTime::toMSecsSinceEpoch2050Tz() void tst_QDateTime::setDate() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setDate(QDate::fromJulianDay(JULIAN_DAY_2010)); @@ -273,9 +265,7 @@ void tst_QDateTime::setDate() void tst_QDateTime::setTime() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setTime(QTime(12, 0, 0)); @@ -284,9 +274,7 @@ void tst_QDateTime::setTime() void tst_QDateTime::setTimeSpec() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setTimeSpec(Qt::UTC); @@ -295,9 +283,7 @@ void tst_QDateTime::setTimeSpec() void tst_QDateTime::setOffsetFromUtc() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setOffsetFromUtc(3600); @@ -307,9 +293,7 @@ void tst_QDateTime::setOffsetFromUtc() void tst_QDateTime::setMSecsSinceEpoch() { qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setMSecsSinceEpoch(msecs); @@ -318,11 +302,8 @@ void tst_QDateTime::setMSecsSinceEpoch() void tst_QDateTime::setMSecsSinceEpochTz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); - QList list; const qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (QDateTime test, list) test.setMSecsSinceEpoch(msecs); @@ -331,9 +312,7 @@ void tst_QDateTime::setMSecsSinceEpochTz() void tst_QDateTime::toString() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2011; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { foreach (const QDateTime &test, list) test.toString(QStringLiteral("yyy-MM-dd hh:mm:ss.zzz t")); @@ -342,9 +321,7 @@ void tst_QDateTime::toString() void tst_QDateTime::toStringTextFormat() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2011; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { foreach (const QDateTime &test, list) test.toString(Qt::TextDate); @@ -353,9 +330,7 @@ void tst_QDateTime::toStringTextFormat() void tst_QDateTime::toStringIsoFormat() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2011; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { foreach (const QDateTime &test, list) test.toString(Qt::ISODate); @@ -364,10 +339,8 @@ void tst_QDateTime::toStringIsoFormat() void tst_QDateTime::addDays() { - QList list; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QDateTime next; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); QBENCHMARK { foreach (const QDateTime &test, list) next = test.addDays(1); @@ -377,10 +350,7 @@ void tst_QDateTime::addDays() void tst_QDateTime::addDaysTz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) QDateTime result = test.addDays(1); @@ -389,10 +359,8 @@ void tst_QDateTime::addDaysTz() void tst_QDateTime::addMSecs() { - QList list; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QDateTime next; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); QBENCHMARK { foreach (const QDateTime &test, list) next = test.addMSecs(1); @@ -402,10 +370,7 @@ void tst_QDateTime::addMSecs() void tst_QDateTime::addMSecsTz() { - QTimeZone cet = QTimeZone("Europe/Oslo"); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0), cet)); + const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) QDateTime result = test.addMSecs(1); @@ -414,9 +379,7 @@ void tst_QDateTime::addMSecsTz() void tst_QDateTime::toTimeSpec() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.toTimeSpec(Qt::UTC); @@ -425,9 +388,7 @@ void tst_QDateTime::toTimeSpec() void tst_QDateTime::toOffsetFromUtc() { - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.toOffsetFromUtc(3600); @@ -438,9 +399,7 @@ void tst_QDateTime::daysTo() { const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.daysTo(other); @@ -451,9 +410,7 @@ void tst_QDateTime::msecsTo() { const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) test.msecsTo(other); @@ -465,9 +422,7 @@ void tst_QDateTime::equivalent() bool result; const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) result = (test == other); @@ -480,9 +435,7 @@ void tst_QDateTime::equivalentUtc() bool result = false; const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) result = (test == other); @@ -495,9 +448,7 @@ void tst_QDateTime::lessThan() bool result = false; const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) result = (test < other); @@ -510,9 +461,7 @@ void tst_QDateTime::lessThanUtc() bool result = false; const QDateTime other = QDateTime::fromMSecsSinceEpoch( qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); - QList list; - for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) - list.append(QDateTime(QDate::fromJulianDay(jd), QTime::fromMSecsSinceStartOfDay(0))); + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { foreach (const QDateTime &test, list) result = (test < other); From 14aa4816149bc05de753e3cc00740fc3666aab1b Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 14:16:32 +0200 Subject: [PATCH 07/12] Convert foreach to ranged for in QDateTime benchmark Change-Id: I05cf7b1916afa94a9f0f9b83af9b4ebe20a04cf0 Reviewed-by: Marc Mutz --- .../corelib/time/qdatetime/main.cpp | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/tests/benchmarks/corelib/time/qdatetime/main.cpp b/tests/benchmarks/corelib/time/qdatetime/main.cpp index 2730e7e18b..740e08cc46 100644 --- a/tests/benchmarks/corelib/time/qdatetime/main.cpp +++ b/tests/benchmarks/corelib/time/qdatetime/main.cpp @@ -135,7 +135,7 @@ void tst_QDateTime::isNull() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.isNull(); } } @@ -144,7 +144,7 @@ void tst_QDateTime::isValid() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.isValid(); } } @@ -153,7 +153,7 @@ void tst_QDateTime::date() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.date(); } } @@ -162,7 +162,7 @@ void tst_QDateTime::time() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.time(); } } @@ -171,7 +171,7 @@ void tst_QDateTime::timeSpec() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.timeSpec(); } } @@ -180,7 +180,7 @@ void tst_QDateTime::offsetFromUtc() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.offsetFromUtc(); } } @@ -189,7 +189,7 @@ void tst_QDateTime::timeZoneAbbreviation() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.timeZoneAbbreviation(); } } @@ -198,7 +198,7 @@ void tst_QDateTime::toMSecsSinceEpoch() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toMSecsSinceEpoch(); } } @@ -207,7 +207,7 @@ void tst_QDateTime::toMSecsSinceEpoch1950() { const auto list = daily(JULIAN_DAY_1950, JULIAN_DAY_1960); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toMSecsSinceEpoch(); } } @@ -216,7 +216,7 @@ void tst_QDateTime::toMSecsSinceEpoch2050() { const auto list = daily(JULIAN_DAY_2050, JULIAN_DAY_2060); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toMSecsSinceEpoch(); } } @@ -226,7 +226,7 @@ void tst_QDateTime::toMSecsSinceEpochTz() qint64 result; const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = test.toMSecsSinceEpoch(); } Q_UNUSED(result); @@ -237,7 +237,7 @@ void tst_QDateTime::toMSecsSinceEpoch1950Tz() qint64 result; const auto list = norse(JULIAN_DAY_1950, JULIAN_DAY_1960); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = test.toMSecsSinceEpoch(); } Q_UNUSED(result); @@ -248,7 +248,7 @@ void tst_QDateTime::toMSecsSinceEpoch2050Tz() qint64 result; const auto list = norse(JULIAN_DAY_2050, JULIAN_DAY_2060); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = test.toMSecsSinceEpoch(); } Q_UNUSED(result); @@ -258,7 +258,7 @@ void tst_QDateTime::setDate() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setDate(QDate::fromJulianDay(JULIAN_DAY_2010)); } } @@ -267,7 +267,7 @@ void tst_QDateTime::setTime() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setTime(QTime(12, 0, 0)); } } @@ -276,7 +276,7 @@ void tst_QDateTime::setTimeSpec() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setTimeSpec(Qt::UTC); } } @@ -285,7 +285,7 @@ void tst_QDateTime::setOffsetFromUtc() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setOffsetFromUtc(3600); } } @@ -295,7 +295,7 @@ void tst_QDateTime::setMSecsSinceEpoch() qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setMSecsSinceEpoch(msecs); } } @@ -305,7 +305,7 @@ void tst_QDateTime::setMSecsSinceEpochTz() const qint64 msecs = qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970 + 180) * MSECS_PER_DAY; const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (QDateTime test, list) + for (QDateTime test : list) test.setMSecsSinceEpoch(msecs); } } @@ -314,7 +314,7 @@ void tst_QDateTime::toString() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toString(QStringLiteral("yyy-MM-dd hh:mm:ss.zzz t")); } } @@ -323,7 +323,7 @@ void tst_QDateTime::toStringTextFormat() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toString(Qt::TextDate); } } @@ -332,7 +332,7 @@ void tst_QDateTime::toStringIsoFormat() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2011); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toString(Qt::ISODate); } } @@ -342,7 +342,7 @@ void tst_QDateTime::addDays() const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QDateTime next; QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) next = test.addDays(1); } Q_UNUSED(next); @@ -352,7 +352,7 @@ void tst_QDateTime::addDaysTz() { const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) QDateTime result = test.addDays(1); } } @@ -362,7 +362,7 @@ void tst_QDateTime::addMSecs() const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QDateTime next; QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) next = test.addMSecs(1); } Q_UNUSED(next); @@ -372,7 +372,7 @@ void tst_QDateTime::addMSecsTz() { const auto list = norse(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) QDateTime result = test.addMSecs(1); } } @@ -381,7 +381,7 @@ void tst_QDateTime::toTimeSpec() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toTimeSpec(Qt::UTC); } } @@ -390,7 +390,7 @@ void tst_QDateTime::toOffsetFromUtc() { const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.toOffsetFromUtc(3600); } } @@ -401,7 +401,7 @@ void tst_QDateTime::daysTo() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.daysTo(other); } } @@ -412,7 +412,7 @@ void tst_QDateTime::msecsTo() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) test.msecsTo(other); } } @@ -424,7 +424,7 @@ void tst_QDateTime::equivalent() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = (test == other); } Q_UNUSED(result) @@ -437,7 +437,7 @@ void tst_QDateTime::equivalentUtc() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = (test == other); } Q_UNUSED(result) @@ -450,7 +450,7 @@ void tst_QDateTime::lessThan() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = (test < other); } Q_UNUSED(result) @@ -463,7 +463,7 @@ void tst_QDateTime::lessThanUtc() qint64(JULIAN_DAY_2010 - JULIAN_DAY_1970) * MSECS_PER_DAY, Qt::UTC); const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); QBENCHMARK { - foreach (const QDateTime &test, list) + for (const QDateTime &test : list) result = (test < other); } Q_UNUSED(result) From acd7259a03d0168682fedd278ee14ae70a4d87a6 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 15:27:28 +0200 Subject: [PATCH 08/12] Extend QDate's benchmark Test more methods. Document what the existing test covers. Use the right #include for QDate. Change-Id: I051542c244e5bc381aafa3ae38144e246919db7a Reviewed-by: Marc Mutz --- .../corelib/time/qdate/tst_bench_qdate.cpp | 170 +++++++++++++++++- 1 file changed, 165 insertions(+), 5 deletions(-) diff --git a/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp b/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp index 399ac44065..10c013c080 100644 --- a/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp +++ b/tests/benchmarks/corelib/time/qdate/tst_bench_qdate.cpp @@ -26,27 +26,187 @@ ** ****************************************************************************/ -#include +#include #include +#include class tst_QDate : public QObject { Q_OBJECT + enum : qint64 + { + JULIAN_DAY_2010 = 2455198, + JULIAN_DAY_2011 = 2455563, + JULIAN_DAY_2020 = 2458850, + }; + + static QVector daily(qint64 start, qint64 end); + static QVector yearly(qint32 first, qint32 last); + private Q_SLOTS: - void monthLengths(); + void create(); + void year(); + void month(); + void day(); + void dayOfWeek(); + void dayOfYear(); + void monthLengths(); // isValid() and daysInMonth() + void daysInYear(); + void isLeapYear(); + void getSetDate(); + void addDays(); + void addMonths(); + void addYears(); }; +QVector tst_QDate::daily(qint64 start, qint64 end) +{ + QVector list; + list.reserve(end - start); + for (qint64 jd = start; jd < end; ++jd) + list.append(QDate::fromJulianDay(jd)); + return list; +} + +QVector tst_QDate::yearly(qint32 first, qint32 last) +{ + QVector list; + list.reserve(last + 1 - first); + for (qint32 year = first; year <= last; ++year) + list.append(QDate(year, 3, 21)); + return list; +} + +void tst_QDate::create() +{ + QDate test; + QBENCHMARK { + for (int jd = JULIAN_DAY_2010; jd < JULIAN_DAY_2020; ++jd) + test = QDate::fromJulianDay(jd); + } + Q_UNUSED(test); +} + +void tst_QDate::year() +{ + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const QDate &test : list) + test.year(); + } +} + +void tst_QDate::month() +{ + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const QDate &test : list) + test.month(); + } +} + +void tst_QDate::day() +{ + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const QDate &test : list) + test.day(); + } +} + +void tst_QDate::dayOfWeek() +{ + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const QDate &test : list) + test.dayOfWeek(); + } +} + +void tst_QDate::dayOfYear() +{ + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const QDate &test : list) + test.dayOfYear(); + } +} + void tst_QDate::monthLengths() { + bool check = true; QBENCHMARK { for (int year = 1900; year <= 2100; year++) { - bool check = true; for (int month = 1; month <= 12; month++) - check &= QDate::isValid(year, month, QDate(year, month, 1).daysInMonth()); - Q_UNUSED(check); + check = QDate::isValid(year, month, QDate(year, month, 1).daysInMonth()); } } + Q_UNUSED(check); +} + +void tst_QDate::daysInYear() +{ + const auto list = yearly(1601, 2401); + QBENCHMARK { + for (const QDate date : list) + date.daysInYear(); + } +} + +void tst_QDate::isLeapYear() +{ + QBENCHMARK { + for (qint32 year = 1601; year <= 2401; year++) + QDate::isLeapYear(year); + } +} + +void tst_QDate::getSetDate() +{ + QDate store; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const auto test : list) { + int year, month, day; + test.getDate(&year, &month, &day); + store.setDate(year, month, day); + } + } + Q_UNUSED(store); +} + +void tst_QDate::addDays() +{ + QDate store; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const auto test : list) + store = test.addDays(17); + } + Q_UNUSED(store); +} + +void tst_QDate::addMonths() +{ + QDate store; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const auto test : list) + store = test.addMonths(17); + } + Q_UNUSED(store); +} + +void tst_QDate::addYears() +{ + QDate store; + const auto list = daily(JULIAN_DAY_2010, JULIAN_DAY_2020); + QBENCHMARK { + for (const auto test : list) + store = test.addYears(17); + } + Q_UNUSED(store); } QTEST_MAIN(tst_QDate) From f00bbd5eb77d0d4669e0a15a277c3937c49ed813 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Fri, 7 Jun 2019 13:03:20 +0200 Subject: [PATCH 09/12] Update QPA mouse event handling in offscreen and VNC plugins This is needed after a37785ec7638e7485112b87dd7e767881fecc114 deprecated the versions of QWindowSystemInterface::handleMouseEvent() that were in use here. Change-Id: Ib704ae2be905436f5a4a80ae6686b5fe3972d34c Reviewed-by: Andy Nichols --- .../platforms/offscreen/qoffscreencommon.cpp | 3 ++- src/plugins/platforms/vnc/qvncclient.cpp | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/plugins/platforms/offscreen/qoffscreencommon.cpp b/src/plugins/platforms/offscreen/qoffscreencommon.cpp index f0eb69718a..eae25012c1 100644 --- a/src/plugins/platforms/offscreen/qoffscreencommon.cpp +++ b/src/plugins/platforms/offscreen/qoffscreencommon.cpp @@ -77,7 +77,8 @@ public: if (containing != previous) QWindowSystemInterface::handleEnterLeaveEvent(containing, previous, local, pos); - QWindowSystemInterface::handleMouseEvent(containing, local, pos, QGuiApplication::mouseButtons(), QGuiApplication::keyboardModifiers()); + QWindowSystemInterface::handleMouseEvent(containing, local, pos, QGuiApplication::mouseButtons(), Qt::NoButton, + QEvent::MouseMove, QGuiApplication::keyboardModifiers(), Qt::MouseEventSynthesizedByQt); QOffscreenScreen::windowContainingCursor = containing ? containing->handle() : 0; } diff --git a/src/plugins/platforms/vnc/qvncclient.cpp b/src/plugins/platforms/vnc/qvncclient.cpp index 9dfe873927..3a373a5e4b 100644 --- a/src/plugins/platforms/vnc/qvncclient.cpp +++ b/src/plugins/platforms/vnc/qvncclient.cpp @@ -587,9 +587,18 @@ void QVncClient::frameBufferUpdateRequest() void QVncClient::pointerEvent() { QRfbPointerEvent ev; + static int buttonState = Qt::NoButton; if (ev.read(m_clientSocket)) { - const QPoint pos = m_server->screen()->geometry().topLeft() + QPoint(ev.x, ev.y); - QWindowSystemInterface::handleMouseEvent(0, pos, pos, ev.buttons, QGuiApplication::keyboardModifiers()); + const QPointF pos = m_server->screen()->geometry().topLeft() + QPoint(ev.x, ev.y); + int buttonStateChange = buttonState ^ int(ev.buttons); + QEvent::Type type = QEvent::MouseMove; + if (int(ev.buttons) > buttonState) + type = QEvent::MouseButtonPress; + else if (int(ev.buttons) < buttonState) + type = QEvent::MouseButtonRelease; + QWindowSystemInterface::handleMouseEvent(nullptr, pos, pos, ev.buttons, Qt::MouseButton(buttonStateChange), + type, QGuiApplication::keyboardModifiers()); + buttonState = int(ev.buttons); m_handleMsg = false; } } From ca3be922349d5fa282578fbb6c1dc2bd25d1f5aa Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 29 Aug 2019 16:22:53 +0200 Subject: [PATCH 10/12] Remove QFileInfo::type and related enum from 5.14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The API is problematic for several reasons: - the mixing of flags and enum in a single enum type - the name "type" as somewhat overloaded - the ease of misuse when comparing the result rather than testing for a bit being set In light of this, focus for 5.14 on the new isShortcut and isSymbolicLink functions, thus migitating the problematic isSymLink which conflates the two concepts. Change-Id: I57e02321edd5061f69a775f04a0932ef89adf866 Reviewed-by: Tor Arne Vestbø Reviewed-by: Marc Mutz --- src/corelib/io/qfileinfo.cpp | 93 +++------ src/corelib/io/qfileinfo.h | 19 +- .../corelib/io/qfileinfo/tst_qfileinfo.cpp | 189 ++++++++++-------- 3 files changed, 136 insertions(+), 165 deletions(-) diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index b720966d8f..a6f8b45ea3 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -310,19 +310,6 @@ QDateTime &QFileInfoPrivate::getFileTime(QAbstractFileEngine::FileTime request) \sa QDir, QFile */ -/*! - \enum QFileInfo::FileType - - This enum is returned by type() to describe the type of the file system - entity described by the QFileInfo object. - - \value Unknown The object refers to an unknown item. - \value Regular The object refers to a regular file. - \value Directory The object refers to a directory. - \value SymbolicLink The object refers to a symbolic link. - \value Shortcut The object refers to a shortcut. -*/ - /*! \fn QFileInfo &QFileInfo::operator=(QFileInfo &&other) @@ -1008,7 +995,11 @@ bool QFileInfo::isNativePath() const */ bool QFileInfo::isFile() const { - return (type() & FileTypeMask) == Regular; + Q_D(const QFileInfo); + return d->checkAttribute( + QFileSystemMetaData::FileType, + [d]() { return d->metaData.isFile(); }, + [d]() { return d->getFileFlags(QAbstractFileEngine::FileType); }); } /*! @@ -1019,7 +1010,11 @@ bool QFileInfo::isFile() const */ bool QFileInfo::isDir() const { - return (type() & FileTypeMask) == Directory; + Q_D(const QFileInfo); + return d->checkAttribute( + QFileSystemMetaData::DirectoryType, + [d]() { return d->metaData.isDirectory(); }, + [d]() { return d->getFileFlags(QAbstractFileEngine::DirectoryType); }); } @@ -1070,8 +1065,6 @@ bool QFileInfo::isSymLink() const } /*! - \fn bool QFileInfo::isSymbolicLink() const - Returns \c true if this object points to a symbolic link; otherwise returns \c false. @@ -1091,9 +1084,16 @@ bool QFileInfo::isSymLink() const \sa isFile(), isDir(), isShortcut(), symLinkTarget() */ -/*! - \fn bool QFileInfo::isShortcut() const +bool QFileInfo::isSymbolicLink() const +{ + Q_D(const QFileInfo); + return d->checkAttribute( + QFileSystemMetaData::LegacyLinkType, + [d]() { return d->metaData.isLink(); }, + [d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); }); +} +/*! Returns \c true if this object points to a shortcut; otherwise returns \c false. @@ -1110,6 +1110,14 @@ bool QFileInfo::isSymLink() const \sa isFile(), isDir(), isSymbolicLink(), symLinkTarget() */ +bool QFileInfo::isShortcut() const +{ + Q_D(const QFileInfo); + return d->checkAttribute( + QFileSystemMetaData::LegacyLinkType, + [d]() { return d->metaData.isLnkFile(); }, + [d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); }); +} /*! Returns \c true if the object points to a directory or to a symbolic @@ -1314,53 +1322,6 @@ qint64 QFileInfo::size() const }); } -/*! - Returns the QFileInfo::FileTypes. - - QFileInfo::FileTypes combines with an indirection flag (link type) and a - base type it refers to. - - For example, \c SymbolicLink combines with \c Regular meaning a symlink to - a regular file. - - In addition, FileTypeMask and LinkTypeMask are used to extract the base - type and link type respectively. - - \sa isFile(), isDir(), isShortcut(), isSymbolicLink() -*/ -QFileInfo::FileTypes QFileInfo::type() const -{ - Q_D(const QFileInfo); - - QFileInfo::FileTypes type = QFileInfo::Unknown; - if (d->checkAttribute( - QFileSystemMetaData::LegacyLinkType, - [d]() { return d->metaData.isLnkFile(); }, - [d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); })) { - type = QFileInfo::Shortcut; - } else if (d->checkAttribute( - QFileSystemMetaData::LegacyLinkType, - [d]() { return d->metaData.isLink(); }, - [d]() { return d->getFileFlags(QAbstractFileEngine::LinkType); })) { - type = QFileInfo::SymbolicLink; - } - - if (d->checkAttribute( - QFileSystemMetaData::DirectoryType, - [d]() { return d->metaData.isDirectory(); }, - [d]() { return d->getFileFlags(QAbstractFileEngine::DirectoryType); })) { - return type | QFileInfo::Directory; - } - - if (d->checkAttribute( - QFileSystemMetaData::FileType, - [d]() { return d->metaData.isFile(); }, - [d]() { return d->getFileFlags(QAbstractFileEngine::FileType); })) { - return type | QFileInfo::Regular; - } - return type; -} - #if QT_DEPRECATED_SINCE(5, 10) /*! \deprecated diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 1cbeafdd4a..3ac028085a 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -66,20 +66,6 @@ public: QFileInfo(const QFileInfo &fileinfo); ~QFileInfo(); - enum FileType { - Unknown, - // base type - Regular, - Directory, - // indirection flag - SymbolicLink = 0x10, - Shortcut = 0x20, - // mask - FileTypeMask = 0x0f, - LinkTypeMask = 0xf0 - }; - Q_DECLARE_FLAGS(FileTypes, FileType) - QFileInfo &operator=(const QFileInfo &fileinfo); QFileInfo &operator=(QFileInfo &&other) noexcept { swap(other); return *this; } @@ -125,8 +111,8 @@ public: bool isFile() const; bool isDir() const; bool isSymLink() const; - inline bool isSymbolicLink() const { return type() & SymbolicLink; } - inline bool isShortcut() const { return type() & Shortcut; } + bool isSymbolicLink() const; + bool isShortcut() const; bool isRoot() const; bool isBundle() const; @@ -145,7 +131,6 @@ public: QFile::Permissions permissions() const; qint64 size() const; - FileTypes type() const; // ### Qt6: inline these functions #if QT_DEPRECATED_SINCE(5, 10) diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index 6fcfe87c83..921847a087 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -236,6 +236,13 @@ private slots: void isSymLink_data(); void isSymLink(); + + void isSymbolicLink_data(); + void isSymbolicLink(); + + void isShortcut_data(); + void isShortcut(); + void link_data(); void link(); @@ -279,9 +286,6 @@ private slots: void invalidState(); void nonExistingFile(); - void type_data(); - void type(); - private: const QString m_currentDir; QString m_sourceFile; @@ -1342,7 +1346,92 @@ void tst_QFileInfo::isSymLink() #endif } -Q_DECLARE_METATYPE(QFileInfo::FileType) +void tst_QFileInfo::isShortcut_data() +{ + QFile::remove("link.lnk"); + QFile::remove("symlink.lnk"); + QFile::remove("link"); + QFile::remove("symlink"); + QFile::remove("directory.lnk"); + QFile::remove("directory"); + + QTest::addColumn("path"); + QTest::addColumn("isShortcut"); + + QFile regularFile(m_sourceFile); + QTest::newRow("regular") + << regularFile.fileName() << false; + QTest::newRow("directory") + << QDir::currentPath() << false; +#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) + // windows shortcuts + QVERIFY(regularFile.link("link.lnk")); + QTest::newRow("shortcut") + << "link.lnk" << true; + QVERIFY(regularFile.link("link")); + QTest::newRow("invalid-shortcut") + << "link" << false; + QVERIFY(QFile::link(QDir::currentPath(), "directory.lnk")); + QTest::newRow("directory-shortcut") + << "directory.lnk" << true; +#endif +} + +void tst_QFileInfo::isShortcut() +{ + QFETCH(QString, path); + QFETCH(bool, isShortcut); + + QFileInfo fi(path); + QCOMPARE(fi.isShortcut(), isShortcut); +} + +void tst_QFileInfo::isSymbolicLink_data() +{ + QTest::addColumn("path"); + QTest::addColumn("isSymbolicLink"); + + QFile regularFile(m_sourceFile); + QTest::newRow("regular") + << regularFile.fileName() << false; + QTest::newRow("directory") + << QDir::currentPath() << false; + +#ifndef Q_NO_SYMLINKS +#if defined(Q_OS_WIN) +#if !defined(Q_OS_WINRT) + QString errorMessage; + const DWORD creationResult = createSymbolicLink("symlink", m_sourceFile, &errorMessage); + if (creationResult == ERROR_PRIVILEGE_NOT_HELD) { + QWARN(msgInsufficientPrivileges(errorMessage)); + } else { + QVERIFY2(creationResult == ERROR_SUCCESS, qPrintable(errorMessage)); + QTest::newRow("NTFS-symlink") + << "symlink" << true; + } +#endif // !Q_OS_WINRT +#else // Unix: + QVERIFY(regularFile.link("symlink.lnk")); + QTest::newRow("symlink.lnk") + << "symlink.lnk" << true; + QVERIFY(regularFile.link("symlink")); + QTest::newRow("symlink") + << "symlink" << true; + QVERIFY(QFile::link(QDir::currentPath(), "directory")); + QTest::newRow("directory-symlink") + << "directory" << true; +#endif +#endif // !Q_NO_SYMLINKS +} + +void tst_QFileInfo::isSymbolicLink() +{ + QFETCH(QString, path); + QFETCH(bool, isSymbolicLink); + + QFileInfo fi(path); + QCOMPARE(fi.isSymbolicLink(), isSymbolicLink); +} void tst_QFileInfo::link_data() { @@ -1354,23 +1443,24 @@ void tst_QFileInfo::link_data() QFile::remove("relative/link"); QTest::addColumn("path"); - QTest::addColumn("linkType"); + QTest::addColumn("isShortcut"); + QTest::addColumn("isSymbolicLink"); QTest::addColumn("linkTarget"); QFile file1(m_sourceFile); QFile file2("dummyfile"); file2.open(QIODevice::WriteOnly); - QTest::newRow("existent file") << m_sourceFile << QFileInfo::Unknown << ""; + QTest::newRow("existent file") << m_sourceFile << false << false << ""; #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) // windows shortcuts QVERIFY(file1.link("link.lnk")); QTest::newRow("link.lnk") - << "link.lnk" << QFileInfo::Shortcut << QFileInfo(m_sourceFile).absoluteFilePath(); + << "link.lnk" << true << false << QFileInfo(m_sourceFile).absoluteFilePath(); QVERIFY(file2.link("brokenlink.lnk")); QTest::newRow("broken link.lnk") - << "brokenlink.lnk" << QFileInfo::Shortcut << QFileInfo("dummyfile").absoluteFilePath(); + << "brokenlink.lnk" << true << false << QFileInfo("dummyfile").absoluteFilePath(); #endif #ifndef Q_NO_SYMLINKS @@ -1383,7 +1473,7 @@ void tst_QFileInfo::link_data() } else { QVERIFY2(creationResult == ERROR_SUCCESS, qPrintable(errorMessage)); QTest::newRow("link") - << "link" << QFileInfo::SymbolicLink << QFileInfo(m_sourceFile).absoluteFilePath(); + << "link" << false << true << QFileInfo(m_sourceFile).absoluteFilePath(); } creationResult = createSymbolicLink("brokenlink", "dummyfile", &errorMessage); @@ -1392,22 +1482,22 @@ void tst_QFileInfo::link_data() } else { QVERIFY2(creationResult == ERROR_SUCCESS, qPrintable(errorMessage)); QTest::newRow("broken link") - << "brokenlink" << QFileInfo::SymbolicLink << QFileInfo("dummyfile").absoluteFilePath(); + << "brokenlink" << false << true << QFileInfo("dummyfile").absoluteFilePath(); } #endif // !Q_OS_WINRT #else // Unix: QVERIFY(file1.link("link")); QTest::newRow("link") - << "link" << QFileInfo::SymbolicLink << QFileInfo(m_sourceFile).absoluteFilePath(); + << "link" << false << true << QFileInfo(m_sourceFile).absoluteFilePath(); QVERIFY(file2.link("brokenlink")); QTest::newRow("broken link") - << "brokenlink" << QFileInfo::SymbolicLink << QFileInfo("dummyfile").absoluteFilePath(); + << "brokenlink" << false << true << QFileInfo("dummyfile").absoluteFilePath(); QDir::current().mkdir("relative"); QFile::link("../dummyfile", "relative/link"); QTest::newRow("relative link") - << "relative/link" << QFileInfo::SymbolicLink << QFileInfo("dummyfile").absoluteFilePath(); + << "relative/link" << false << true << QFileInfo("dummyfile").absoluteFilePath(); #endif #endif // !Q_NO_SYMLINKS file2.remove(); @@ -1416,11 +1506,13 @@ void tst_QFileInfo::link_data() void tst_QFileInfo::link() { QFETCH(QString, path); - QFETCH(QFileInfo::FileType, linkType); + QFETCH(bool, isShortcut); + QFETCH(bool, isSymbolicLink); QFETCH(QString, linkTarget); QFileInfo fi(path); - QCOMPARE(fi.type() & QFileInfo::LinkTypeMask, linkType); + QCOMPARE(fi.isShortcut(), isShortcut); + QCOMPARE(fi.isSymbolicLink(), isSymbolicLink); QCOMPARE(fi.symLinkTarget(), linkTarget); } @@ -2179,73 +2271,6 @@ void tst_QFileInfo::nonExistingFile() stateCheck(info, dirname, filename); } -Q_DECLARE_METATYPE(QFileInfo::FileTypes) - -void tst_QFileInfo::type_data() -{ - QFile::remove("link.lnk"); - QFile::remove("symlink.lnk"); - QFile::remove("link"); - QFile::remove("symlink"); - QFile::remove("directory.lnk"); - QFile::remove("directory"); - - QTest::addColumn("path"); - QTest::addColumn("type"); - - QFile regularFile(m_sourceFile); - QTest::newRow("regular") - << regularFile.fileName() << QFileInfo::FileTypes(QFileInfo::Regular); - QTest::newRow("directory") - << QDir::currentPath() << QFileInfo::FileTypes(QFileInfo::Directory); -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - // windows shortcuts - QVERIFY(regularFile.link("link.lnk")); - QTest::newRow("shortcut") - << "link.lnk" << QFileInfo::FileTypes(QFileInfo::Shortcut | QFileInfo::Regular); - QVERIFY(regularFile.link("link")); - QTest::newRow("invalid-shortcut") - << "link" << QFileInfo::FileTypes(QFileInfo::Regular); - QVERIFY(QFile::link(QDir::currentPath(), "directory.lnk")); - QTest::newRow("directory-shortcut") - << "directory.lnk" << QFileInfo::FileTypes(QFileInfo::Shortcut | QFileInfo::Directory); -#endif - -#ifndef Q_NO_SYMLINKS -#if defined(Q_OS_WIN) -#if !defined(Q_OS_WINRT) - QString errorMessage; - const DWORD creationResult = createSymbolicLink("symlink", m_sourceFile, &errorMessage); - if (creationResult == ERROR_PRIVILEGE_NOT_HELD) { - QWARN(msgInsufficientPrivileges(errorMessage)); - } else { - QVERIFY2(creationResult == ERROR_SUCCESS, qPrintable(errorMessage)); - QTest::newRow("NTFS-symlink") - << "symlink" << QFileInfo::FileTypes(QFileInfo::SymbolicLink | QFileInfo::Regular); - } -#endif // !Q_OS_WINRT -#else // Unix: - QVERIFY(regularFile.link("symlink.lnk")); - QTest::newRow("symlink.lnk") - << "symlink.lnk" << QFileInfo::FileTypes(QFileInfo::SymbolicLink | QFileInfo::Regular); - QVERIFY(regularFile.link("symlink")); - QTest::newRow("symlink") - << "symlink" << QFileInfo::FileTypes(QFileInfo::SymbolicLink | QFileInfo::Regular); - QVERIFY(QFile::link(QDir::currentPath(), "directory")); - QTest::newRow("directory-symlink") - << "directory" << QFileInfo::FileTypes(QFileInfo::SymbolicLink | QFileInfo::Directory); -#endif -#endif // !Q_NO_SYMLINKS -} - -void tst_QFileInfo::type() -{ - QFETCH(QString, path); - QFETCH(QFileInfo::FileTypes, type); - - QFileInfo info(path); - QCOMPARE(info.type(), type); -} QTEST_MAIN(tst_QFileInfo) #include "tst_qfileinfo.moc" From fc49d73c44463d35c9cd0753e678e9ebcf668fe4 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 28 Aug 2019 16:06:49 +0200 Subject: [PATCH 11/12] Fix spelling of primarily in QCalendar::isSolar()'s doc It was missing a y. Change-Id: I12dac02e451addff966f554811ca1999acadbb1b Reviewed-by: Jesus Fernandez --- src/corelib/time/qcalendar.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/time/qcalendar.cpp b/src/corelib/time/qcalendar.cpp index f6bb242788..b569a6834c 100644 --- a/src/corelib/time/qcalendar.cpp +++ b/src/corelib/time/qcalendar.cpp @@ -810,7 +810,7 @@ bool QCalendar::isLuniSolar() const /*! Returns \c true if this calendar is solar. - A solar calendar is based primaril on the Sun's varying position in the sky, + A solar calendar is based primarily on the Sun's varying position in the sky, relative to the fixed stars. */ bool QCalendar::isSolar() const From 65dfc485adc1c5de4840f217c47c6ad79d26ae82 Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Wed, 28 Aug 2019 08:31:00 +0300 Subject: [PATCH 12/12] Android: clean configure params for android MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -android-toolchain-version is not needed anymore as we are using exclusively the llvm toolchain. Change-Id: Ia033297a6a2c968352c364758eb1436380a5f96e Reviewed-by: Jörg Bornemann --- config_help.txt | 1 - configure.json | 1 - configure.pri | 12 ++---------- .../features/android/android_deployment_settings.prf | 1 - 4 files changed, 2 insertions(+), 13 deletions(-) diff --git a/config_help.txt b/config_help.txt index 5880c1e00d..e47fc9adb7 100644 --- a/config_help.txt +++ b/config_help.txt @@ -205,7 +205,6 @@ Build environment: [$ANDROID_NDK_HOST] -android-abis ....... Comma separated Android abis, default is: armeabi-v7a,arm64-v8a,x86,x86_64 - -android-toolchain-version ... Set Android toolchain version -android-style-assets Automatically extract style assets from the device at run time. This option makes the Android style behave correctly, but also makes the Android platform plugin diff --git a/configure.json b/configure.json index c6ea80b076..5fc35a1658 100644 --- a/configure.json +++ b/configure.json @@ -56,7 +56,6 @@ "android-ndk-host": "string", "android-ndk-platform": "string", "android-sdk": "string", - "android-toolchain-version": "string", "android-style-assets": "boolean", "appstore-compliant": "boolean", diff --git a/configure.pri b/configure.pri index d803dcf086..ecd46b8e38 100644 --- a/configure.pri +++ b/configure.pri @@ -573,14 +573,7 @@ defineTest(qtConfOutput_prepareOptions) { qtConfFatalError("Cannot find Android NDK." \ "Please use -android-ndk option to specify one.") - ndk_tc_ver = $$eval(config.input.android-toolchain-version) - isEmpty(ndk_tc_ver): \ - ndk_tc_ver = 4.9 - !exists($$ndk_root/toolchains/arm-linux-androideabi-$$ndk_tc_ver/prebuilt/*): \ - qtConfFatalError("Cannot detect Android NDK toolchain." \ - "Please use -android-toolchain-version to specify it.") - - ndk_tc_pfx = $$ndk_root/toolchains/arm-linux-androideabi-$$ndk_tc_ver/prebuilt + ndk_tc_pfx = $$ndk_root/toolchains/llvm/prebuilt ndk_host = $$eval(config.input.android-ndk-host) isEmpty(ndk_host): \ ndk_host = $$getenv(ANDROID_NDK_HOST) @@ -632,8 +625,7 @@ defineTest(qtConfOutput_prepareOptions) { "DEFAULT_ANDROID_NDK_ROOT = $$val_escape(ndk_root)" \ "DEFAULT_ANDROID_PLATFORM = $$platform" \ "DEFAULT_ANDROID_NDK_HOST = $$ndk_host" \ - "DEFAULT_ANDROID_ABIS = $$split(android_abis, ',')" \ - "DEFAULT_ANDROID_NDK_TOOLCHAIN_VERSION = $$ndk_tc_ver" + "DEFAULT_ANDROID_ABIS = $$split(android_abis, ',')" } export($${currentConfig}.output.devicePro) diff --git a/mkspecs/features/android/android_deployment_settings.prf b/mkspecs/features/android/android_deployment_settings.prf index 998a985bb5..4d6101e297 100644 --- a/mkspecs/features/android/android_deployment_settings.prf +++ b/mkspecs/features/android/android_deployment_settings.prf @@ -71,4 +71,3 @@ contains(TEMPLATE, ".*app"):!build_pass:!android-embedded { write_file($$ANDROID_DEPLOYMENT_SETTINGS_FILE, FILE_CONTENT)|error() } -