From b38925413dd4452c621fc309391368990441cc9d Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 15 May 2018 23:49:54 +0800 Subject: [PATCH 1/3] Improve Utils::Version class Add operator>=() and operator<=(). More methods are suitable to be constexpr. Remove redundant boundary checking. --- src/base/utils/version.h | 46 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/base/utils/version.h b/src/base/utils/version.h index e8a169007..19213cb35 100644 --- a/src/base/utils/version.h +++ b/src/base/utils/version.h @@ -84,31 +84,31 @@ namespace Utils { } - ComponentType majorNumber() const + constexpr ComponentType majorNumber() const { static_assert(N >= 1, "The number of version components is too small"); - return (*this)[0]; + return m_components[0]; } - ComponentType minorNumber() const + constexpr ComponentType minorNumber() const { static_assert(N >= 2, "The number of version components is too small"); - return (*this)[1]; + return m_components[1]; } - ComponentType revisionNumber() const + constexpr ComponentType revisionNumber() const { static_assert(N >= 3, "The number of version components is too small"); - return (*this)[2]; + return m_components[2]; } - ComponentType patchNumber() const + constexpr ComponentType patchNumber() const { static_assert(N >= 4, "The number of version components is too small"); - return (*this)[3]; + return m_components[3]; } - ComponentType operator[](std::size_t i) const + constexpr ComponentType operator[](const std::size_t i) const { return m_components.at(i); } @@ -129,19 +129,19 @@ namespace Utils return res; } - bool operator==(const ThisType &other) const + constexpr bool operator==(const ThisType &other) const { - return m_components == other.m_components; + return (m_components == other.m_components); } - bool operator<(const ThisType &other) const + constexpr bool operator<(const ThisType &other) const { - return m_components < other.m_components; + return (m_components < other.m_components); } - bool operator>(const ThisType &other) const + constexpr bool operator>(const ThisType &other) const { - return m_components > other.m_components; + return (m_components > other.m_components); } template @@ -150,7 +150,7 @@ namespace Utils try { return Version(s); } - catch (std::runtime_error &er) { + catch (const std::runtime_error &er) { qDebug() << "Error parsing version:" << er.what(); return defaultVersion; } @@ -186,10 +186,22 @@ namespace Utils }; template - inline bool operator!=(const Version &left, const Version &right) + constexpr bool operator!=(const Version &left, const Version &right) { return !(left == right); } + + template + constexpr bool operator<=(const Version &left, const Version &right) + { + return !(left > right); + } + + template + constexpr bool operator>=(const Version &left, const Version &right) + { + return !(left < right); + } } #endif // QBITTORRENT_UTILS_VERSION_H From 9631a9c2ad56c2825918ae0b45cbac17cd86d173 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 16 May 2018 01:16:58 +0800 Subject: [PATCH 2/3] Add constexpr to TriStateBool class --- src/base/tristatebool.cpp | 25 ------------------------- src/base/tristatebool.h | 28 +++++++++++++++++++++------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/base/tristatebool.cpp b/src/base/tristatebool.cpp index 1de7efc7f..bdabb0948 100644 --- a/src/base/tristatebool.cpp +++ b/src/base/tristatebool.cpp @@ -31,28 +31,3 @@ const TriStateBool TriStateBool::Undefined(-1); const TriStateBool TriStateBool::False(0); const TriStateBool TriStateBool::True(1); - -TriStateBool::operator int() const -{ - return m_value; -} - -TriStateBool::TriStateBool(int value) -{ - if (value < 0) - m_value = -1; - else if (value > 0) - m_value = 1; - else - m_value = 0; -} - -bool TriStateBool::operator==(const TriStateBool &other) const -{ - return (m_value == other.m_value); -} - -bool TriStateBool::operator!=(const TriStateBool &other) const -{ - return !operator==(other); -} diff --git a/src/base/tristatebool.h b/src/base/tristatebool.h index 62821e30d..269a68c1a 100644 --- a/src/base/tristatebool.h +++ b/src/base/tristatebool.h @@ -36,15 +36,29 @@ public: static const TriStateBool False; static const TriStateBool True; - TriStateBool() = default; - TriStateBool(const TriStateBool &other) = default; + constexpr TriStateBool() = default; + constexpr TriStateBool(const TriStateBool &other) = default; + explicit constexpr TriStateBool(int value) + : m_value(value < 0 ? -1 : (value > 0 ? 1 : 0)) + { + } - explicit TriStateBool(int value); - explicit operator int() const; + explicit constexpr operator int() const + { + return m_value; + } - TriStateBool &operator=(const TriStateBool &other) = default; - bool operator==(const TriStateBool &other) const; - bool operator!=(const TriStateBool &other) const; + TriStateBool &operator=(const TriStateBool &other) = default; // add constexpr when using C++14 + + constexpr bool operator==(const TriStateBool &other) const + { + return (m_value == other.m_value); + } + + constexpr bool operator!=(const TriStateBool &other) const + { + return !operator==(other); + } private: signed char m_value = -1; // Undefined by default From e099f6ad3305275594366e52b3465a4be101e5f4 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 16 May 2018 01:38:27 +0800 Subject: [PATCH 3/3] Add constexpr to IndexInterval class Add const to IndexRange private members. Remove redundant inline specifier. Add missing parentheses. --- src/base/indexrange.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/base/indexrange.h b/src/base/indexrange.h index 77384d89f..0bcff1ae6 100644 --- a/src/base/indexrange.h +++ b/src/base/indexrange.h @@ -38,30 +38,30 @@ class IndexInterval public: using IndexType = Index; - IndexInterval(IndexType first, IndexType last) + IndexInterval(IndexType first, IndexType last) // add constexpr when using C++14 : m_first {first} , m_last {last} { Q_ASSERT(first <= last); } - IndexType first() const + constexpr IndexType first() const { return m_first; } - IndexType last() const + constexpr IndexType last() const { return m_last; } private: - IndexType m_first; - IndexType m_last; + const IndexType m_first; + const IndexType m_last; }; template -inline IndexInterval makeInterval(T first, T last) +constexpr IndexInterval makeInterval(T first, T last) { return {first, last}; } @@ -99,7 +99,7 @@ public: constexpr IndexType end() const { - return m_first + m_size; + return (m_first + m_size); } constexpr IndexDiffType size() const @@ -114,12 +114,12 @@ public: constexpr IndexType last() const { - return m_first + m_size - 1; + return (m_first + m_size - 1); } constexpr bool isEmpty() const { - return m_size == 0; + return (m_size == 0); } private: