Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Includes cleanup #518

Merged
merged 9 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Checks: >
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-type-static-cast-downcast,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-avoid-do-while
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-macro-usage,
Expand Down
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ add_library(
windowshelpers.h
macoshelpers.h

coroutines/hostinfo.h
coroutines/coroutinefwd.h
coroutines/coroutines.cpp
coroutines/coroutines.h
coroutines/hostinfo.h
coroutines/qobjectsignal.h
coroutines/scope.h
coroutines/scope.cpp
Expand Down
3 changes: 2 additions & 1 deletion src/bencodeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
#include <QFile>
#include <QString>

#include <fmt/format.h>

#include "fileutils.h"
#include "log/formatters.h"

namespace tremotesf::bencode {
namespace {
Expand Down
1 change: 0 additions & 1 deletion src/bencodeparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>

#include <QString>
Expand Down
22 changes: 22 additions & 0 deletions src/coroutines/coroutinefwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2015-2024 Alexey Rochev
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef TREMOTESF_COROUTINEFWD_H
#define TREMOTESF_COROUTINEFWD_H

#include <concepts>

namespace tremotesf {
namespace impl {
template<typename T>
concept CoroutineReturnValue = std::same_as<T, void> || std::movable<T>;

class StandaloneCoroutine;
}

template<impl::CoroutineReturnValue T = void>
class Coroutine;
}

#endif // TREMOTESF_COROUTINEFWD_H
7 changes: 7 additions & 0 deletions src/coroutines/coroutines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "coroutines.h"

#include "log/log.h"

namespace tremotesf::impl {
void CoroutinePromiseBase::setParentCoroutineHandle(std::coroutine_handle<> parentCoroutineHandle) {
mParentCoroutineHandle = parentCoroutineHandle;
Expand Down Expand Up @@ -47,6 +49,11 @@ namespace tremotesf::impl {
return mParentCoroutineHandle;
}

void CoroutinePromiseBase::abortNoParent(std::coroutine_handle<> handle) {
warning().log("No parent coroutine when completing coroutine {}", handle.address());
std::abort();
}

std::coroutine_handle<> CoroutinePromise<void>::onPerformedFinalSuspend() {
if (const auto handle = onPerformedFinalSuspendBase(); handle) {
return handle;
Expand Down
59 changes: 21 additions & 38 deletions src/coroutines/coroutines.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
#include <functional>
#include <optional>
#include <variant>
#include <utility>

#include "log/log.h"
#if __has_include(<QtClassHelperMacros>)
# include <QtClassHelperMacros>
#else
# include <QtGlobal>
#endif

#include "coroutinefwd.h"

namespace tremotesf {
namespace impl {
template<typename T>
concept CoroutineReturnValue = std::same_as<T, void> || std::movable<T>;

template<CoroutineReturnValue T>
class CoroutinePromise;

template<CoroutineReturnValue T>
class CoroutineAwaiter;

class StandaloneCoroutine;
}

template<impl::CoroutineReturnValue T = void>
template<impl::CoroutineReturnValue T>
class [[nodiscard]] Coroutine {
public:
using promise_type = impl::CoroutinePromise<T>;
Expand All @@ -51,11 +53,12 @@ namespace tremotesf {

inline impl::CoroutineAwaiter<T> operator co_await();

inline void* address() const { return mHandle.address(); }

private:
std::coroutine_handle<impl::CoroutinePromise<T>> mHandle;

friend class impl::StandaloneCoroutine;
friend struct fmt::formatter<Coroutine<T>>;
};

namespace impl {
Expand Down Expand Up @@ -92,9 +95,11 @@ namespace tremotesf {
}

protected:
inline CoroutinePromiseBase(std::coroutine_handle<> handle) : mCoroutineHandle(handle) {}
inline CoroutinePromiseBase() = default;

[[noreturn]]
static void abortNoParent(std::coroutine_handle<> handle);

std::coroutine_handle<> mCoroutineHandle;
std::coroutine_handle<> mParentCoroutineHandle{};
std::variant<std::monostate, JustCompleteCancellation, std::function<void()>>
mChildAwaiterInterruptionCallback{};
Expand All @@ -106,12 +111,10 @@ namespace tremotesf {
class CoroutinePromise final : public CoroutinePromiseBase {
public:
inline CoroutinePromise()
: CoroutinePromiseBase(std::coroutine_handle<CoroutinePromise<T>>::from_promise(*this)) {}
: mCoroutineHandle(std::coroutine_handle<CoroutinePromise<T>>::from_promise(*this)) {}

// promise object contract begin
inline Coroutine<T> get_return_object() {
return Coroutine<T>(std::coroutine_handle<CoroutinePromise<T>>::from_promise(*this));
}
inline Coroutine<T> get_return_object() { return Coroutine<T>(mCoroutineHandle); }
inline void return_value(const T& valueToReturn) { mValue = valueToReturn; }
inline void return_value(T&& valueToReturn) { mValue = std::move(valueToReturn); }
// promise object contract end
Expand All @@ -120,8 +123,7 @@ namespace tremotesf {
if (const auto handle = onPerformedFinalSuspendBase(); handle) {
return handle;
}
warning().log("No parent coroutine when completing coroutine {}", mCoroutineHandle.address());
std::abort();
abortNoParent(mCoroutineHandle);
}

inline T takeValueOrRethrowException() {
Expand All @@ -133,15 +135,13 @@ namespace tremotesf {
}

private:
std::coroutine_handle<CoroutinePromise<T>> mCoroutineHandle;
std::optional<T> mValue{};
};

template<>
class CoroutinePromise<void> final : public CoroutinePromiseBase {
public:
inline CoroutinePromise()
: CoroutinePromiseBase(std::coroutine_handle<CoroutinePromise<void>>::from_promise(*this)) {}

// promise object contract begin
inline Coroutine<void> get_return_object() {
return Coroutine<void>(std::coroutine_handle<CoroutinePromise<void>>::from_promise(*this));
Expand Down Expand Up @@ -231,6 +231,8 @@ namespace tremotesf {
inline ~StandaloneCoroutine() = default;
Q_DISABLE_COPY_MOVE(StandaloneCoroutine)

inline void* address() const { return mCoroutine.address(); }

inline void start() { mCoroutine.mHandle.resume(); }

inline StandaloneCoroutine* rootCoroutine() const { return mRootCoroutine; }
Expand All @@ -252,8 +254,6 @@ namespace tremotesf {
std::function<void(std::exception_ptr)> mCompletionCallback{};
enum class CancellationState : char { NotCancelled, Cancelling, Cancelled };
CancellationState mCancellationState{CancellationState::NotCancelled};

friend struct fmt::formatter<StandaloneCoroutine>;
};

class [[nodiscard]] CancellationAwaiter final {
Expand All @@ -277,23 +277,6 @@ namespace tremotesf {
}
}

namespace fmt {
template<typename T>
struct formatter<tremotesf::Coroutine<T>> : tremotesf::SimpleFormatter {
fmt::format_context::iterator format(const tremotesf::Coroutine<T>& coroutine, fmt::format_context& ctx) const {
return fmt::format_to(ctx.out(), "Coroutine({})", coroutine.mHandle.address());
}
};

template<>
struct formatter<tremotesf::impl::StandaloneCoroutine> : tremotesf::SimpleFormatter {
fmt::format_context::iterator
format(const tremotesf::impl::StandaloneCoroutine& coroutine, fmt::format_context& ctx) const {
return fmt::formatter<tremotesf::Coroutine<>>{}.format(coroutine.mCoroutine, ctx);
}
};
}

#define cancelCoroutine() \
do { \
co_await tremotesf::impl::CancellationAwaiter{}; \
Expand Down
2 changes: 2 additions & 0 deletions src/coroutines/coroutines_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ namespace tremotesf {
};
}

// NOLINTEND(cppcoreguidelines-avoid-reference-coroutine-parameters)

QTEST_GUILESS_MAIN(tremotesf::CoroutinesTest)

#include "coroutines_test.moc"
3 changes: 2 additions & 1 deletion src/coroutines/scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "coroutines/scope.h"
#include "coroutines/coroutines.h"
#include "log/log.h"

namespace tremotesf {
Expand Down Expand Up @@ -54,7 +55,7 @@ namespace tremotesf {
handleException(unhandledException);
const auto found = std::ranges::find(mCoroutines, coroutine);
if (found == mCoroutines.end()) {
warning().log("Did not find completed coroutine {} in CoroutineScope", *coroutine);
warning().log("Did not find completed coroutine {} in CoroutineScope", coroutine->address());
std::abort();
}
mCoroutines.erase(found);
Expand Down
9 changes: 8 additions & 1 deletion src/coroutines/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
#ifndef TREMOTESF_COROUTINES_SCOPE_H
#define TREMOTESF_COROUTINES_SCOPE_H

#if __has_include(<QtClassHelperMacros>)
# include <QtClassHelperMacros>
#else
# include <QtGlobal>
#endif

#include <exception>
#include <vector>

#include "coroutines.h"
#include "coroutinefwd.h"

namespace tremotesf {
class CoroutineScope {
Expand Down
2 changes: 1 addition & 1 deletion src/coroutines/waitall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace tremotesf::impl {
}
const auto found = std::ranges::find_if(mCoroutines, [coroutine](auto& c) { return &c == coroutine; });
if (found == mCoroutines.end()) {
warning().log("Did not find completed coroutine {} in MultipleCoroutinesAwaiter", *coroutine);
warning().log("Did not find completed coroutine {} in MultipleCoroutinesAwaiter", coroutine->address());
std::abort();
}
mCoroutines.erase(found);
Expand Down
1 change: 0 additions & 1 deletion src/desktoputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <QDesktopServices>
#include <QDir>
#include <QMessageBox>
#include <QProxyStyle>
#include <QRegularExpression>
#include <QStringBuilder>
#include <QTextCursor>
Expand Down
3 changes: 1 addition & 2 deletions src/filemanagerlauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#define TREMOTESF_FILEMANAGERLAUNCHER_H

#include <optional>
#include <utility>
#include <vector>

#include <QObject>
#include <QPointer>
#include <QString>

class QWidget;
Expand Down
1 change: 0 additions & 1 deletion src/filemanagerlauncher_freedesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <QDBusConnection>
#include <QDBusPendingReply>
#include <QUrl>
#include <QWidget>

#include "coroutines/dbus.h"
Expand Down
2 changes: 1 addition & 1 deletion src/filemanagerlauncher_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "filemanagerlauncher.h"

#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QPointer>
#include <QWidget>

#include <guiddef.h>
Expand Down
Loading
Loading