Skip to content

Commit

Permalink
Specify interface requirements as an C++ concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobo1 authored Aug 12, 2023
1 parent 850da9d commit 69d60b5
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_library(qbt_base STATIC
bittorrent/torrentinfo.h
bittorrent/tracker.h
bittorrent/trackerentry.h
concepts/stringable.h
digest32.h
exceptions.h
global.h
Expand All @@ -54,7 +55,6 @@ add_library(qbt_base STATIC
iconprovider.h
indexrange.h
interfaces/iapplication.h
interfaces/istringable.h
logger.h
net/dnsupdater.h
net/downloadhandlerimpl.h
Expand Down Expand Up @@ -187,6 +187,7 @@ add_library(qbt_base STATIC
utils/random.cpp
utils/string.cpp
utils/thread.cpp
utils/version.cpp
)

target_link_libraries(qbt_base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

#pragma once

#include <concepts>

class QString;

class IStringable
template <typename T>
concept Stringable = requires (T t)
{
public:
virtual ~IStringable() = default;

// requirement: T(const QString &) constructor for derived class `T` // TODO: try enforce it in C++20 concept
virtual QString toString() const = 0;
requires std::constructible_from<T, QString>;
{ t.toString() } -> std::same_as<QString>;
};
4 changes: 4 additions & 0 deletions src/base/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <QRegularExpression>
#include <QStringView>

#include "base/concepts/stringable.h"
#include "base/global.h"

#if defined(Q_OS_WIN)
Expand Down Expand Up @@ -69,6 +70,9 @@ namespace
#endif
}

// `Path` should satisfy `Stringable` concept in order to be stored in settings as string
static_assert(Stringable<Path>);

Path::Path(const QString &pathStr)
: m_pathStr {cleanPath(pathStr)}
{
Expand Down
6 changes: 2 additions & 4 deletions src/base/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@

#include "pathfwd.h"

#include "base/interfaces/istringable.h"

class QStringView;

class Path final : public IStringable
class Path final
{
public:
Path() = default;
Expand Down Expand Up @@ -71,7 +69,7 @@ class Path final : public IStringable
Path relativePathOf(const Path &childPath) const;

QString data() const;
QString toString() const override;
QString toString() const;
std::filesystem::path toStdFsPath() const;

Path &operator/=(const Path &other);
Expand Down
20 changes: 11 additions & 9 deletions src/base/settingsstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
#include <QVariant>
#include <QVariantHash>

#include "base/interfaces/istringable.h"
#include "base/concepts/stringable.h"
#include "utils/string.h"

template <typename T>
concept IsQFlags = std::same_as<T, QFlags<typename T::enum_type>>;

// 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
// implement `IStringable` interface
// `T` should satisfy `Stringable` concept
// 2. Otherwise, use `Q_DECLARE_METATYPE(T)` and let `QMetaType` handle the serialization
class SettingsStorage final : public QObject
{
Expand All @@ -64,7 +64,12 @@ class SettingsStorage final : public QObject
template <typename T>
T loadValue(const QString &key, const T &defaultValue = {}) const
{
if constexpr (std::is_base_of_v<IStringable, T>)
if constexpr (std::same_as<T, QVariant>)
{
// fast path for loading QVariant
return loadValueImpl(key, defaultValue);
}
else if constexpr (Stringable<T>)
{
const QString value = loadValue(key, defaultValue.toString());
return T {value};
Expand All @@ -79,11 +84,6 @@ class SettingsStorage final : public QObject
const typename T::Int value = loadValue(key, static_cast<typename T::Int>(defaultValue));
return T {value};
}
else if constexpr (std::is_same_v<T, QVariant>)
{
// fast path for loading QVariant
return loadValueImpl(key, defaultValue);
}
else
{
const QVariant value = loadValueImpl(key);
Expand All @@ -95,7 +95,9 @@ class SettingsStorage final : public QObject
template <typename T>
void storeValue(const QString &key, const T &value)
{
if constexpr (std::is_base_of_v<IStringable, T>)
if constexpr (std::same_as<T, QVariant>)
storeValueImpl(key, value);
else if constexpr (Stringable<T>)
storeValueImpl(key, value.toString());
else if constexpr (std::is_enum_v<T>)
storeValueImpl(key, Utils::String::fromEnum(value));
Expand Down
34 changes: 34 additions & 0 deletions src/base/utils/version.cpp
Original file line number Diff line number Diff line change
@@ -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>>);
11 changes: 7 additions & 4 deletions src/base/utils/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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>...>
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include_directories("../src")
set(testFiles
testalgorithm.cpp
testbittorrenttrackerentry.cpp
testconceptsstringable.cpp
testglobal.cpp
testorderedset.cpp
testpath.cpp
Expand Down
73 changes: 73 additions & 0 deletions test/testconceptsstringable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 <QObject>
#include <QString>
#include <QTest>

#include "base/concepts/stringable.h"

class TestConceptsStringable final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TestConceptsStringable)

public:
TestConceptsStringable() = default;

private slots:
void testStringable() const
{
struct A
{
};
static_assert(!Stringable<A>);

struct B
{
B(QString) {}
};
static_assert(!Stringable<B>);

struct C
{
QString toString() const { return {}; }
};
static_assert(!Stringable<C>);

struct D
{
D(QString) {}
QString toString() const { return {}; }
};
static_assert(Stringable<D>);
}
};

QTEST_APPLESS_MAIN(TestConceptsStringable)
#include "testconceptsstringable.moc"
8 changes: 4 additions & 4 deletions test/testutilsversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ private slots:
// Utils::Version<2, 0>();

using TwoDigits = Utils::Version<2, 1>;
TwoDigits(0);
TwoDigits(50);
TwoDigits(0, 1);
QCOMPARE(TwoDigits(0), TwoDigits(u"0"_s));
QCOMPARE(TwoDigits(50), TwoDigits(u"50"_s));
QCOMPARE(TwoDigits(0, 1), TwoDigits(u"0.1"_s));

using ThreeDigits = Utils::Version<3, 3>;
// should not compile:
// ThreeDigits(1);
// ThreeDigits(1, 2);
// ThreeDigits(1.0, 2, 3);
// ThreeDigits(1, 2, 3, 4);
ThreeDigits(1, 2, 3);
QCOMPARE(ThreeDigits(1, 2, 3), ThreeDigits(u"1.2.3"_s));
}

void testIsValid() const
Expand Down

0 comments on commit 69d60b5

Please sign in to comment.