diff --git a/src/base/algorithm.h b/src/base/algorithm.h index daac26d5f..0721b8ea3 100644 --- a/src/base/algorithm.h +++ b/src/base/algorithm.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2023 Mike Tzou (Chocobo1) * Copyright (C) 2018 Vladimir Golovnev * * This program is free software; you can redistribute it and/or @@ -28,25 +29,16 @@ #pragma once -#include - namespace Algorithm { - template - struct HasMappedType - : std::false_type - { - }; - template - struct HasMappedType> - : std::true_type + concept HasMappedType = requires { + typename T::mapped_type; }; // To be used with associative array types, such as QMap, QHash and its variants - template ::value, int> = 0> + template void removeIf(T &dict, BinaryPredicate &&p) { auto it = dict.begin(); diff --git a/src/base/orderedset.h b/src/base/orderedset.h index 4e62f58fd..f553f833f 100644 --- a/src/base/orderedset.h +++ b/src/base/orderedset.h @@ -29,9 +29,9 @@ #pragma once +#include #include #include -#include #include "algorithm.h" @@ -70,8 +70,8 @@ public: return BaseType::empty(); } - template , int> = 0> QString join(const QString &separator) const + requires std::same_as { auto iter = BaseType::cbegin(); if (iter == BaseType::cend()) diff --git a/src/base/settingsstorage.h b/src/base/settingsstorage.h index b00a92a4e..130339540 100644 --- a/src/base/settingsstorage.h +++ b/src/base/settingsstorage.h @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2023 Mike Tzou (Chocobo1) * Copyright (C) 2016 Vladimir Golovnev * Copyright (C) 2014 sledgehammer999 * @@ -41,10 +42,7 @@ #include "utils/string.h" template -struct IsQFlags : std::false_type {}; - -template -struct IsQFlags> : std::true_type {}; +concept IsQFlags = std::same_as>; // There are 2 ways for class `T` provide serialization support into `SettingsStorage`: // 1. If the `T` state is intended for users to edit (via a text editor), then @@ -76,7 +74,7 @@ public: const auto value = loadValue(key); return Utils::String::toEnum(value, defaultValue); } - else if constexpr (IsQFlags::value) + else if constexpr (IsQFlags) { const typename T::Int value = loadValue(key, static_cast(defaultValue)); return T {value}; @@ -101,7 +99,7 @@ public: storeValueImpl(key, value.toString()); else if constexpr (std::is_enum_v) storeValueImpl(key, Utils::String::fromEnum(value)); - else if constexpr (IsQFlags::value) + else if constexpr (IsQFlags) storeValueImpl(key, static_cast(value)); else storeValueImpl(key, QVariant::fromValue(value)); diff --git a/src/base/utils/string.h b/src/base/utils/string.h index a6ea77292..00a30cbc1 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -67,8 +67,9 @@ namespace Utils::String QString fromDouble(double n, int precision); - template , int> = 0> + template QString fromEnum(const T &value) + requires std::is_enum_v { static_assert(std::is_same_v>, "Enumeration underlying type has to be int."); @@ -77,8 +78,9 @@ namespace Utils::String return QString::fromLatin1(metaEnum.valueToKey(static_cast(value))); } - template , int> = 0> + template T toEnum(const QString &serializedValue, const T &defaultValue) + requires std::is_enum_v { static_assert(std::is_same_v>, "Enumeration underlying type has to be int."); diff --git a/src/base/utils/version.h b/src/base/utils/version.h index ba2618a7b..2d981bd83 100644 --- a/src/base/utils/version.h +++ b/src/base/utils/version.h @@ -55,9 +55,9 @@ namespace Utils constexpr Version() = default; - template ...>, int> = 0> + template constexpr Version(Ts ... params) + requires std::conjunction_v...> : m_components {{params ...}} { static_assert((sizeof...(Ts) <= N), "Too many parameters provided"); diff --git a/test/testalgorithm.cpp b/test/testalgorithm.cpp index 3949518d0..1a17f861d 100644 --- a/test/testalgorithm.cpp +++ b/test/testalgorithm.cpp @@ -47,14 +47,14 @@ public: private slots: void testHasMappedType() const { - QVERIFY(static_cast(Algorithm::HasMappedType>::value)); - QVERIFY(static_cast(Algorithm::HasMappedType>::value)); - QVERIFY(static_cast(Algorithm::HasMappedType>::value)); - QVERIFY(static_cast(Algorithm::HasMappedType>::value)); + static_assert(Algorithm::HasMappedType>); + static_assert(Algorithm::HasMappedType>); + static_assert(Algorithm::HasMappedType>); + static_assert(Algorithm::HasMappedType>); - QVERIFY(!static_cast(Algorithm::HasMappedType>::value)); - QVERIFY(!static_cast(Algorithm::HasMappedType>::value)); - QVERIFY(!static_cast(Algorithm::HasMappedType>::value)); + static_assert(!Algorithm::HasMappedType>); + static_assert(!Algorithm::HasMappedType>); + static_assert(!Algorithm::HasMappedType>); } void testMappedTypeRemoveIf() const