diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2732ba6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,53 @@ +--- +Language: Cpp +BasedOnStyle: WebKit + +Standard: Cpp11 + +ColumnLimit: '120' +UseTab: Always +TabWidth: '4' +IndentWidth: '4' +ContinuationIndentWidth: '4' + + +AllowAllArgumentsOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AlignAfterOpenBracket: DontAlign +AlignConsecutiveAssignments: true +AlignTrailingComments: true + +AlwaysBreakTemplateDeclarations: true + +BinPackArguments: false +BinPackParameters: true + +BreakBeforeBinaryOperators: All +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: true + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + BeforeLambdaBody: true + IndentBraces: false + +Cpp11BracedListStyle: false + +IndentCaseLabels: 'true' + +NamespaceIndentation: All + +SpaceBeforeCpp11BracedList: 'false' + +... diff --git a/libs/qtcoro/awaitable.hpp b/libs/qtcoro/awaitable.hpp index 130c9c0..96135e4 100644 --- a/libs/qtcoro/awaitable.hpp +++ b/libs/qtcoro/awaitable.hpp @@ -1,89 +1,87 @@ #pragma once -#include -#include #include +#include +#include namespace qtcoro { -template -void delete_later(std::coroutine_handle h) noexcept -{ - QTimer::singleShot(std::chrono::milliseconds(0), [h=std::move(h)]() mutable - { - printf("::holly shit, %p destroyed!\n", h.address()); - h.destroy(); - }); -} + template + void delete_later(std::coroutine_handle h) noexcept + { + QTimer::singleShot(std::chrono::milliseconds(0), + [h = std::move(h)]() mutable + { + printf("::holly shit, %p destroyed!\n", h.address()); + h.destroy(); + }); + } - struct coroutine_context; - struct coroutine_context_promise; - struct corotine_context_cleanup_awaiter { - bool await_ready() noexcept { return false; } - void await_resume() noexcept {} - std::coroutine_handle<> await_suspend(std::coroutine_handle h) noexcept - { - printf("::holly shit, %p destroyed!\n", h.address()); - h.destroy(); - return std::noop_coroutine(); - } - }; + struct coroutine_context; + struct coroutine_context_promise; + struct corotine_context_cleanup_awaiter + { + bool await_ready() noexcept { return false; } + void await_resume() noexcept { } + std::coroutine_handle<> await_suspend(std::coroutine_handle h) noexcept + { + printf("::holly shit, %p destroyed!\n", h.address()); + h.destroy(); + return std::noop_coroutine(); + } + }; - struct coroutine_context_promise - { - coroutine_context get_return_object(); + struct coroutine_context_promise + { + coroutine_context get_return_object(); - auto initial_suspend() { return std::suspend_always{}; } + auto initial_suspend() { return std::suspend_always{}; } - auto final_suspend() noexcept - { - return corotine_context_cleanup_awaiter{}; - } + auto final_suspend() noexcept { return corotine_context_cleanup_awaiter{}; } - void unhandled_exception(){} + void unhandled_exception() { } - void return_void() { } - }; + void return_void() { } + }; // 协程包装... struct coroutine_context - { + { using promise_type = coroutine_context_promise; coroutine_context(std::coroutine_handle h) : current_coro_handle_(h) - {} - - ~coroutine_context() { } - coroutine_context(coroutine_context&& t) noexcept : current_coro_handle_(t.current_coro_handle_) { + ~coroutine_context() { } + + coroutine_context(coroutine_context&& t) noexcept + : current_coro_handle_(t.current_coro_handle_) + { t.current_coro_handle_ = nullptr; } - coroutine_context& operator=(coroutine_context&& t) noexcept { - if (&t != this) { - if (current_coro_handle_) current_coro_handle_.destroy(); - current_coro_handle_ = t.current_coro_handle_; + coroutine_context& operator=(coroutine_context&& t) noexcept + { + if (&t != this) + { + if (current_coro_handle_) + current_coro_handle_.destroy(); + current_coro_handle_ = t.current_coro_handle_; t.current_coro_handle_ = nullptr; } return *this; } - coroutine_context(const coroutine_context&) = delete; + coroutine_context(const coroutine_context&) = delete; coroutine_context& operator=(const coroutine_context&) = delete; - void shedule() - { - current_coro_handle_.resume(); - } + void shedule() { current_coro_handle_.resume(); } - bool await_ready() const noexcept { - return false; - } + bool await_ready() const noexcept { return false; } void await_resume() { } @@ -92,138 +90,137 @@ void delete_later(std::coroutine_handle h) noexcept std::coroutine_handle current_coro_handle_; }; + inline coroutine_context coroutine_context_promise::get_return_object() + { + return coroutine_context{ std::coroutine_handle::from_promise(*this) }; + } - inline coroutine_context coroutine_context_promise::get_return_object() - { - return coroutine_context{std::coroutine_handle::from_promise(*this)}; - } - - // inline std::coroutine_handle<> corotine_context_cleanup_awaiter::await_suspend(std::coroutine_handle h) noexcept - // { + // inline std::coroutine_handle<> + // corotine_context_cleanup_awaiter::await_suspend(std::coroutine_handle h) noexcept + // { - // } + // } - template - struct awaitable; + template + struct awaitable; - template - struct awaitable_promise; + template + struct awaitable_promise; - template - struct FinalAwaiter { - bool await_ready() noexcept { return false; } - void await_resume() noexcept {} - std::coroutine_handle<> await_suspend(std::coroutine_handle> h) noexcept - { - if (h.promise().continuation) - { - return h.promise().continuation; - } - return std::noop_coroutine(); - } - }; + template + struct FinalAwaiter + { + bool await_ready() noexcept { return false; } + void await_resume() noexcept { } + std::coroutine_handle<> await_suspend(std::coroutine_handle> h) noexcept + { + if (h.promise().continuation) + { + return h.promise().continuation; + } + return std::noop_coroutine(); + } + }; - // Promise 类型实现... - template - struct awaitable_promise { - std::coroutine_handle<> continuation; - T value; // 用于存储协程返回的值 + // Promise 类型实现... + template + struct awaitable_promise + { + std::coroutine_handle<> continuation; + T value; // 用于存储协程返回的值 - awaitable get_return_object(); + awaitable get_return_object(); - void reset_handle(std::coroutine_handle<> h) { - continuation = h; - } + void reset_handle(std::coroutine_handle<> h) { continuation = h; } - auto initial_suspend() { return std::suspend_always{}; } + auto initial_suspend() { return std::suspend_always{}; } - auto final_suspend() noexcept { - return FinalAwaiter{}; - } - void unhandled_exception(){} + auto final_suspend() noexcept { return FinalAwaiter{}; } + void unhandled_exception() { } template - void return_value(V&& v) noexcept { + void return_value(V&& v) noexcept + { value = std::forward(v); } + }; - }; - - template <> - struct awaitable_promise { - std::coroutine_handle<> continuation; - awaitable get_return_object(); + template <> + struct awaitable_promise + { + std::coroutine_handle<> continuation; + awaitable get_return_object(); - void reset_handle(std::coroutine_handle<> h) { - continuation = h; - } + void reset_handle(std::coroutine_handle<> h) { continuation = h; } - auto initial_suspend() { return std::suspend_always{}; } + auto initial_suspend() { return std::suspend_always{}; } - auto final_suspend() noexcept { - return FinalAwaiter{}; - } + auto final_suspend() noexcept { return FinalAwaiter{}; } - void unhandled_exception(){} + void unhandled_exception() { } - void return_void() { } - }; + void return_void() { } + }; // awaitable 协程包装... template struct awaitable - { + { using promise_type = awaitable_promise; awaitable(std::coroutine_handle h) : current_coro_handle_(h) - {} + { + } ~awaitable() { if (current_coro_handle_) - { - Q_ASSERT(current_coro_handle_.done()); - current_coro_handle_.destroy(); - } + { + Q_ASSERT(current_coro_handle_.done()); + current_coro_handle_.destroy(); + } } - awaitable(awaitable&& t) noexcept : current_coro_handle_(t.current_coro_handle_) - { + awaitable(awaitable&& t) noexcept + : current_coro_handle_(t.current_coro_handle_) + { t.current_coro_handle_ = nullptr; } - awaitable& operator=(awaitable&& t) noexcept { - if (&t != this) { - if (current_coro_handle_) current_coro_handle_.destroy(); - current_coro_handle_ = t.current_coro_handle_; + awaitable& operator=(awaitable&& t) noexcept + { + if (&t != this) + { + if (current_coro_handle_) + current_coro_handle_.destroy(); + current_coro_handle_ = t.current_coro_handle_; t.current_coro_handle_ = nullptr; } return *this; } - awaitable(const awaitable&) = delete; + awaitable(const awaitable&) = delete; awaitable& operator=(const awaitable&) = delete; - T operator()() { - return get(); - } + T operator()() { return get(); } - T get() { + T get() + { if constexpr (!std::is_same_v) return std::move(current_coro_handle_.promise().value); } - bool await_ready() const noexcept { - return false; - } + bool await_ready() const noexcept { return false; } - T await_resume() { // 修改返回类型为 T + T await_resume() + { // 修改返回类型为 T if constexpr (!std::is_same_v) return std::move(current_coro_handle_.promise().value); } - auto await_suspend(std::coroutine_handle<> continuation) { + auto await_suspend(std::coroutine_handle<> continuation) + { current_coro_handle_.promise().reset_handle(continuation); return current_coro_handle_; } @@ -231,96 +228,95 @@ void delete_later(std::coroutine_handle h) noexcept std::coroutine_handle current_coro_handle_; }; - template - inline awaitable awaitable_promise::get_return_object() { - return awaitable{std::coroutine_handle>::from_promise(*this)}; - } + template + inline awaitable awaitable_promise::get_return_object() + { + return awaitable{ std::coroutine_handle>::from_promise(*this) }; + } - inline awaitable awaitable_promise::get_return_object() { - return awaitable{std::coroutine_handle>::from_promise(*this)}; - } + inline awaitable awaitable_promise::get_return_object() + { + return awaitable{ std::coroutine_handle>::from_promise(*this) }; + } } // namespace qtcoro -template +template struct CallbackAwaiter { public: CallbackAwaiter(CallbackFunction&& callback_function) - : callback_function_(std::move(callback_function)) {} + : callback_function_(std::move(callback_function)) + { + } bool await_ready() noexcept { return false; } - void await_suspend(std::coroutine_handle<> handle) { - callback_function_([handle = std::move(handle), this](T t) mutable { - result_ = std::move(t); - handle.resume(); + void await_suspend(std::coroutine_handle<> handle) + { + callback_function_( + [handle = std::move(handle), this](T t) mutable + { + result_ = std::move(t); + handle.resume(); }); } - T await_resume() noexcept { - return std::move(result_); - } + T await_resume() noexcept { return std::move(result_); } private: CallbackFunction callback_function_; T result_; }; -template +template struct CallbackAwaiter { public: CallbackAwaiter(CallbackFunction&& callback_function) : callback_function_(std::move(callback_function)) - {} + { + } bool await_ready() noexcept { return false; } - void await_suspend(std::coroutine_handle<> handle) { - callback_function_([handle = std::move(handle)]() mutable { - handle.resume(); - }); + void await_suspend(std::coroutine_handle<> handle) + { + callback_function_([handle = std::move(handle)]() mutable { handle.resume(); }); } - void await_resume() noexcept {} + void await_resume() noexcept { } private: CallbackFunction callback_function_; }; -template -CallbackAwaiter -callback_awaitable(callback&& cb) +template +CallbackAwaiter callback_awaitable(callback&& cb) { - return CallbackAwaiter{std::forward(cb)}; + return CallbackAwaiter{ std::forward(cb) }; } -template +template qtcoro::awaitable coro_delay_ms(INT ms) { - co_await callback_awaitable([ms](auto handle) - { - QTimer::singleShot(std::chrono::milliseconds(ms), handle); - }); - co_return ms; + co_await callback_awaitable([ms](auto handle) { QTimer::singleShot(std::chrono::milliseconds(ms), handle); }); + co_return ms; } inline qtcoro::coroutine_context coro_wapper_func(qtcoro::awaitable awaitable_coro) { - co_await callback_awaitable([](auto handle) - { - QTimer::singleShot(std::chrono::milliseconds(0), handle); - }); - co_await awaitable_coro; - co_return; + co_await callback_awaitable([](auto handle) { QTimer::singleShot(std::chrono::milliseconds(0), handle); }); + co_await awaitable_coro; + co_return; } inline void start_coro(qtcoro::awaitable&& awaitable_coro) { - QTimer::singleShot(std::chrono::milliseconds(0), [awaitable_coro=std::move(awaitable_coro)]() mutable - { - qtcoro::coroutine_context coro_wapper = coro_wapper_func(std::move(awaitable_coro)); - coro_wapper.shedule(); - }); + QTimer::singleShot(std::chrono::milliseconds(0), + [awaitable_coro = std::move(awaitable_coro)]() mutable + { + qtcoro::coroutine_context coro_wapper = coro_wapper_func(std::move(awaitable_coro)); + coro_wapper.shedule(); + }); }