From ed62854af7722ba13a60997fa18541a939b563e0 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 23 Sep 2019 11:38:40 +0800 Subject: [PATCH 1/2] Add TriStateBool constructor taking a boolean And move the constructor taking an int to private. --- src/base/tristatebool.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/base/tristatebool.h b/src/base/tristatebool.h index 7a74eae9f..39ea92ac1 100644 --- a/src/base/tristatebool.h +++ b/src/base/tristatebool.h @@ -38,9 +38,9 @@ public: constexpr TriStateBool() = default; constexpr TriStateBool(const TriStateBool &other) = default; - explicit constexpr TriStateBool(int value) - : m_value(value < 0 ? -1 : (value > 0 ? 1 : 0)) + explicit constexpr TriStateBool(const bool boolean) { + *this = boolean ? True : False; } explicit constexpr operator int() const @@ -56,6 +56,11 @@ public: } private: + explicit constexpr TriStateBool(const int value) + : m_value((value < 0) ? -1 : ((value > 0) ? 1 : 0)) + { + } + signed char m_value = -1; // Undefined by default }; From 72faf89e26dba634324cb90aaf5582fbc748fe05 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 23 Sep 2019 12:58:51 +0800 Subject: [PATCH 2/2] Use the same internal data type for conversion function --- src/base/rss/rss_autodownloadrule.cpp | 4 ++-- src/base/tristatebool.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/base/rss/rss_autodownloadrule.cpp b/src/base/rss/rss_autodownloadrule.cpp index 8ce584e53..689f45a0d 100644 --- a/src/base/rss/rss_autodownloadrule.cpp +++ b/src/base/rss/rss_autodownloadrule.cpp @@ -64,7 +64,7 @@ namespace QJsonValue triStateBoolToJsonValue(const TriStateBool &triStateBool) { - switch (static_cast(triStateBool)) { + switch (static_cast(triStateBool)) { case 0: return false; case 1: return true; default: return {}; @@ -82,7 +82,7 @@ namespace int triStateBoolToAddPausedLegacy(const TriStateBool &triStateBool) { - switch (static_cast(triStateBool)) { + switch (static_cast(triStateBool)) { case 0: return 2; // never case 1: return 1; // always default: return 0; // default diff --git a/src/base/tristatebool.h b/src/base/tristatebool.h index 39ea92ac1..246ef1fcb 100644 --- a/src/base/tristatebool.h +++ b/src/base/tristatebool.h @@ -43,13 +43,13 @@ public: *this = boolean ? True : False; } - explicit constexpr operator int() const + TriStateBool &operator=(const TriStateBool &other) = default; // TODO: add constexpr when using C++17 + + explicit constexpr operator signed char() const { return m_value; } - TriStateBool &operator=(const TriStateBool &other) = default; // add constexpr when using C++17 - constexpr friend bool operator==(const TriStateBool &left, const TriStateBool &right) { return (left.m_value == right.m_value);