Specify interface requirements as an C++ concept

PR #19440.
This commit is contained in:
Chocobo1
2023-08-12 20:53:03 +08:00
committed by GitHub
parent 850da9dd83
commit 69d60b5f1c
10 changed files with 144 additions and 28 deletions

View File

@@ -0,0 +1,34 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Mike Tzou (Chocobo1)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "version.h"
#include "base/concepts/stringable.h"
// `Version` should satisfy `Stringable` concept in order to be stored in settings as string
static_assert(Stringable<Utils::Version<1>>);

View File

@@ -36,14 +36,12 @@
#include <QString>
#include <QStringView>
#include "base/interfaces/istringable.h"
namespace Utils
{
// This class provides a default implementation of `isValid()` that should work for most cases
// It is ultimately up to the user to decide whether the version numbers are useful/meaningful
template <int N, int Mandatory = N>
class Version final : public IStringable
class Version final
{
static_assert((N > 0), "The number of version components may not be smaller than 1");
static_assert((Mandatory > 0), "The number of mandatory components may not be smaller than 1");
@@ -55,6 +53,11 @@ namespace Utils
constexpr Version() = default;
Version(const QStringView string)
{
*this = fromString(string);
}
template <typename ... Ts>
constexpr Version(Ts ... params)
requires std::conjunction_v<std::is_convertible<Ts, int>...>
@@ -108,7 +111,7 @@ namespace Utils
return m_components.at(i);
}
QString toString() const override
QString toString() const
{
// find the last one non-zero component
int lastSignificantIndex = N - 1;