Bump project requirement to C++17

This commit is contained in:
Chocobo1
2020-12-19 14:52:01 +08:00
parent a579b4a519
commit d70b893852
19 changed files with 101 additions and 139 deletions

View File

@@ -32,9 +32,6 @@
namespace Algorithm
{
template <typename ...>
using void_t = void; // replace this with std::void_t in C++17
template <typename T, typename = void>
struct HasMappedType
: std::false_type
@@ -42,7 +39,7 @@ namespace Algorithm
};
template <typename T>
struct HasMappedType<T, void_t<typename T::mapped_type>>
struct HasMappedType<T, std::void_t<typename T::mapped_type>>
: std::true_type
{
};

View File

@@ -30,5 +30,4 @@
#include <QString>
// TODO: Make it inline in C++17
extern const QString QB_EXT;
inline const QString QB_EXT {QStringLiteral(".!qB")};

View File

@@ -30,9 +30,6 @@
#include <type_traits>
template <typename ...>
using void_t = void; // replace this with std::void_t in C++17
template <typename T, typename = void>
struct HasUnderlyingType
: std::false_type
@@ -40,7 +37,7 @@ struct HasUnderlyingType
};
template <typename T>
struct HasUnderlyingType<T, void_t<typename T::underlying_type>>
struct HasUnderlyingType<T, std::void_t<typename T::underlying_type>>
: std::true_type
{
};

View File

@@ -291,9 +291,8 @@ namespace
LowerLimited<T> lowerLimited(T limit, T ret) { return LowerLimited<T>(limit, ret); }
template <typename T>
std::function<T (const T&)> clampValue(const T lower, const T upper)
auto clampValue(const T lower, const T upper)
{
// TODO: change return type to `auto` when using C++17
return [lower, upper](const T value) -> T
{
if (value < lower)

View File

@@ -69,8 +69,6 @@
#include "session.h"
#include "trackerentry.h"
const QString QB_EXT {QStringLiteral(".!qB")};
using namespace BitTorrent;
namespace

View File

@@ -37,8 +37,7 @@ class IndexInterval
public:
using IndexType = Index;
// TODO: add constexpr when using C++17
IndexInterval(const IndexType first, const IndexType last)
constexpr IndexInterval(const IndexType first, const IndexType last)
: m_first {first}
, m_last {last}
{

View File

@@ -48,7 +48,13 @@ public:
T get(const T &defaultValue = {}) const
{
return load(defaultValue);
if constexpr (std::is_enum_v<T>) {
const auto value = SettingsStorage::instance()->loadValue(m_keyName, {}).toString();
return Utils::String::toEnum(value, defaultValue);
}
else {
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue).template value<T>();
}
}
operator T() const
@@ -58,39 +64,14 @@ public:
SettingValue<T> &operator=(const T &value)
{
store(value);
if constexpr (std::is_enum_v<T>)
SettingsStorage::instance()->storeValue(m_keyName, Utils::String::fromEnum(value));
else
SettingsStorage::instance()->storeValue(m_keyName, value);
return *this;
}
private:
// regular load/store pair
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
U load(const U &defaultValue) const
{
return SettingsStorage::instance()->loadValue(m_keyName, defaultValue)
.template value<U>();
}
template <typename U, typename std::enable_if_t<!std::is_enum<U>::value, int> = 0>
void store(const U &value)
{
SettingsStorage::instance()->storeValue(m_keyName, value);
}
// load/store pair for enum type, saves literal value of the enum constant
// TODO: merge the load/store functions with `if constexpr` in C++17
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
U load(const U defaultValue) const
{
return Utils::String::toEnum(load(QString {}), defaultValue);
}
template <typename U, typename std::enable_if_t<std::is_enum<U>::value, int> = 0>
void store(const U value)
{
store(Utils::String::fromEnum(value));
}
const QString m_keyName;
};

View File

@@ -42,7 +42,7 @@ public:
*this = boolean ? True : False;
}
TriStateBool &operator=(const TriStateBool &other) = default; // TODO: add constexpr when using C++17
constexpr TriStateBool &operator=(const TriStateBool &other) = default;
explicit constexpr operator signed char() const
{

View File

@@ -60,18 +60,3 @@ Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operat
}
return *this;
}
Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operator*()
{
return *this;
}
Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operator++()
{
return *this;
}
Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operator++(int)
{
return *this;
}

View File

@@ -49,10 +49,21 @@ namespace Utils
// mimic std::ostream_iterator behavior
FileDeviceOutputIterator &operator=(char c);
// TODO: make these `constexpr` in C++17
FileDeviceOutputIterator &operator*();
FileDeviceOutputIterator &operator++();
FileDeviceOutputIterator &operator++(int);
constexpr FileDeviceOutputIterator &operator*()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++()
{
return *this;
}
constexpr FileDeviceOutputIterator &operator++(int)
{
return *this;
}
private:
QFileDevice *m_device;

View File

@@ -73,20 +73,20 @@ namespace Utils
QString join(const QVector<QStringRef> &strings, const QString &separator);
template <typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
QString fromEnum(const T &value)
{
static_assert(std::is_same<int, typename std::underlying_type_t<T>>::value,
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>();
return QString::fromLatin1(metaEnum.valueToKey(static_cast<int>(value)));
}
template <typename T, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
template <typename T, typename std::enable_if_t<std::is_enum_v<T>, int> = 0>
T toEnum(const QString &serializedValue, const T &defaultValue)
{
static_assert(std::is_same<int, typename std::underlying_type_t<T>>::value,
static_assert(std::is_same_v<int, typename std::underlying_type_t<T>>,
"Enumeration underlying type has to be int.");
const auto metaEnum = QMetaEnum::fromType<T>();

View File

@@ -715,12 +715,12 @@ void AdvancedSettings::addRow(const int row, const QString &text, T *widget)
setCellWidget(row, PROPERTY, label);
setCellWidget(row, VALUE, widget);
if (std::is_same<T, QCheckBox>::value)
if (std::is_same_v<T, QCheckBox>)
connect(widget, SIGNAL(stateChanged(int)), this, SIGNAL(settingsChanged()));
else if (std::is_same<T, QSpinBox>::value)
else if (std::is_same_v<T, QSpinBox>)
connect(widget, SIGNAL(valueChanged(int)), this, SIGNAL(settingsChanged()));
else if (std::is_same<T, QComboBox>::value)
else if (std::is_same_v<T, QComboBox>)
connect(widget, SIGNAL(currentIndexChanged(int)), this, SIGNAL(settingsChanged()));
else if (std::is_same<T, QLineEdit>::value)
else if (std::is_same_v<T, QLineEdit>)
connect(widget, SIGNAL(textChanged(QString)), this, SIGNAL(settingsChanged()));
}