Browse Source

Merge pull request #8953 from Chocobo1/constexpr

Add constexpr to various classes
adaptive-webui-19844
Mike Tzou 7 years ago committed by GitHub
parent
commit
1d25d95740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/base/indexrange.h
  2. 25
      src/base/tristatebool.cpp
  3. 28
      src/base/tristatebool.h
  4. 46
      src/base/utils/version.h

18
src/base/indexrange.h

@ -38,30 +38,30 @@ class IndexInterval
public: public:
using IndexType = Index; using IndexType = Index;
IndexInterval(IndexType first, IndexType last) IndexInterval(IndexType first, IndexType last) // add constexpr when using C++14
: m_first {first} : m_first {first}
, m_last {last} , m_last {last}
{ {
Q_ASSERT(first <= last); Q_ASSERT(first <= last);
} }
IndexType first() const constexpr IndexType first() const
{ {
return m_first; return m_first;
} }
IndexType last() const constexpr IndexType last() const
{ {
return m_last; return m_last;
} }
private: private:
IndexType m_first; const IndexType m_first;
IndexType m_last; const IndexType m_last;
}; };
template <typename T> template <typename T>
inline IndexInterval<T> makeInterval(T first, T last) constexpr IndexInterval<T> makeInterval(T first, T last)
{ {
return {first, last}; return {first, last};
} }
@ -99,7 +99,7 @@ public:
constexpr IndexType end() const constexpr IndexType end() const
{ {
return m_first + m_size; return (m_first + m_size);
} }
constexpr IndexDiffType size() const constexpr IndexDiffType size() const
@ -114,12 +114,12 @@ public:
constexpr IndexType last() const constexpr IndexType last() const
{ {
return m_first + m_size - 1; return (m_first + m_size - 1);
} }
constexpr bool isEmpty() const constexpr bool isEmpty() const
{ {
return m_size == 0; return (m_size == 0);
} }
private: private:

25
src/base/tristatebool.cpp

@ -31,28 +31,3 @@
const TriStateBool TriStateBool::Undefined(-1); const TriStateBool TriStateBool::Undefined(-1);
const TriStateBool TriStateBool::False(0); const TriStateBool TriStateBool::False(0);
const TriStateBool TriStateBool::True(1); 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);
}

28
src/base/tristatebool.h

@ -36,15 +36,29 @@ public:
static const TriStateBool False; static const TriStateBool False;
static const TriStateBool True; static const TriStateBool True;
TriStateBool() = default; constexpr TriStateBool() = default;
TriStateBool(const TriStateBool &other) = default; constexpr TriStateBool(const TriStateBool &other) = default;
explicit constexpr TriStateBool(int value)
: m_value(value < 0 ? -1 : (value > 0 ? 1 : 0))
{
}
explicit constexpr operator int() const
{
return m_value;
}
explicit TriStateBool(int value); TriStateBool &operator=(const TriStateBool &other) = default; // add constexpr when using C++14
explicit operator int() const;
TriStateBool &operator=(const TriStateBool &other) = default; constexpr bool operator==(const TriStateBool &other) const
bool operator==(const TriStateBool &other) const; {
bool operator!=(const TriStateBool &other) const; return (m_value == other.m_value);
}
constexpr bool operator!=(const TriStateBool &other) const
{
return !operator==(other);
}
private: private:
signed char m_value = -1; // Undefined by default signed char m_value = -1; // Undefined by default

46
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"); 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"); 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"); 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"); 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); return m_components.at(i);
} }
@ -129,19 +129,19 @@ namespace Utils
return res; 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 <typename StringClassWithSplitMethod> template <typename StringClassWithSplitMethod>
@ -150,7 +150,7 @@ namespace Utils
try { try {
return Version(s); return Version(s);
} }
catch (std::runtime_error &er) { catch (const std::runtime_error &er) {
qDebug() << "Error parsing version:" << er.what(); qDebug() << "Error parsing version:" << er.what();
return defaultVersion; return defaultVersion;
} }
@ -186,10 +186,22 @@ namespace Utils
}; };
template <typename T, std::size_t N, std::size_t Mandatory> template <typename T, std::size_t N, std::size_t Mandatory>
inline bool operator!=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right) constexpr bool operator!=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
{ {
return !(left == right); return !(left == right);
} }
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator<=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
{
return !(left > right);
}
template <typename T, std::size_t N, std::size_t Mandatory>
constexpr bool operator>=(const Version<T, N, Mandatory> &left, const Version<T, N, Mandatory> &right)
{
return !(left < right);
}
} }
#endif // QBITTORRENT_UTILS_VERSION_H #endif // QBITTORRENT_UTILS_VERSION_H

Loading…
Cancel
Save