From 42a745b72d919c817a5ecd390eb4e70820942401 Mon Sep 17 00:00:00 2001 From: bizehao <18635939450@163.com> Date: Sat, 19 Jul 2025 00:49:07 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E4=BC=98=E5=8C=96=20rcvr=5Fref,=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E4=BC=A0=5FEnv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ustdex/detail/conditional.hpp | 2 +- include/ustdex/detail/continues_on.hpp | 2 +- include/ustdex/detail/let_value.hpp | 2 +- include/ustdex/detail/rcvr_ref.hpp | 4 ++-- include/ustdex/detail/sequence.hpp | 2 +- include/ustdex/detail/then.hpp | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/ustdex/detail/conditional.hpp b/include/ustdex/detail/conditional.hpp index c7ece94..a530afc 100644 --- a/include/ustdex/detail/conditional.hpp +++ b/include/ustdex/detail/conditional.hpp @@ -165,7 +165,7 @@ struct _cond_t Rcvr _rcvr_; _data_t _data_; - connect_result_t> _op_; + connect_result_t> _op_; _next_ops_variant_t _ops_; }; diff --git a/include/ustdex/detail/continues_on.hpp b/include/ustdex/detail/continues_on.hpp index 87a5bcb..65c4871 100644 --- a/include/ustdex/detail/continues_on.hpp +++ b/include/ustdex/detail/continues_on.hpp @@ -155,7 +155,7 @@ struct USTDEX_TYPE_VISIBILITY_DEFAULT continue_on_t } _rcvr_t _rcvr_; - connect_result_t> _opstate1_; + connect_result_t> _opstate1_; connect_result_t, _rcvr_ref<_rcvr_t>> _opstate2_; }; diff --git a/include/ustdex/detail/let_value.hpp b/include/ustdex/detail/let_value.hpp index 4dbfb8b..647295f 100644 --- a/include/ustdex/detail/let_value.hpp +++ b/include/ustdex/detail/let_value.hpp @@ -167,7 +167,7 @@ struct _let Rcvr _rcvr_; Fn _fn_; _results _result_; - connect_result_t> _opstate1_; + connect_result_t> _opstate1_; _opstate_variant_t _opstate2_; }; diff --git a/include/ustdex/detail/rcvr_ref.hpp b/include/ustdex/detail/rcvr_ref.hpp index 6ec59e5..bbbc7bd 100644 --- a/include/ustdex/detail/rcvr_ref.hpp +++ b/include/ustdex/detail/rcvr_ref.hpp @@ -25,7 +25,7 @@ namespace ustdex { -template > +template struct _rcvr_ref { using receiver_concept = receiver_t; @@ -48,7 +48,7 @@ struct _rcvr_ref static_cast<_Rcvr&&>(_rcvr_).set_stopped(); } - USTDEX_API auto get_env() const noexcept -> _Env + USTDEX_API auto get_env() const noexcept { return ustdex::get_env(_rcvr_); } diff --git a/include/ustdex/detail/sequence.hpp b/include/ustdex/detail/sequence.hpp index 11c036b..767700e 100644 --- a/include/ustdex/detail/sequence.hpp +++ b/include/ustdex/detail/sequence.hpp @@ -82,7 +82,7 @@ struct _seq } _rcvr_t _rcvr_; - connect_result_t<_sndr1_t, _rcvr_ref<_opstate, _env_t>> _opstate1_; + connect_result_t<_sndr1_t, _rcvr_ref<_opstate>> _opstate1_; connect_result_t<_sndr2_t, _rcvr_ref<_rcvr_t>> _opstate2_; }; diff --git a/include/ustdex/detail/then.hpp b/include/ustdex/detail/then.hpp index 16da99e..4b4b576 100644 --- a/include/ustdex/detail/then.hpp +++ b/include/ustdex/detail/then.hpp @@ -176,7 +176,7 @@ struct _upon_t Rcvr _rcvr_; Fn _fn_; - connect_result_t> _opstate_; + connect_result_t> _opstate_; }; template From b5e66b13a086804241ead5546c2084f23bd4af21 Mon Sep 17 00:00:00 2001 From: bizehao <18635939450@163.com> Date: Tue, 22 Jul 2025 20:18:02 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=AE=97=E6=B3=95=20when=5Fany,=20staop=5Fwhen,=20stop=5Fwith?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakePresets.json | 83 ++-- examples/scratch.cpp | 217 ++++++--- include/ustdex/detail/binder_closure.hpp | 43 ++ .../ustdex/detail/completion_signatures.hpp | 6 +- include/ustdex/detail/exclusive_scan.hpp | 2 +- include/ustdex/detail/lazy.hpp | 8 +- include/ustdex/detail/meta.hpp | 10 +- include/ustdex/detail/starts_on.hpp | 2 +- include/ustdex/detail/stop_when.hpp | 155 +++++++ include/ustdex/detail/stop_with.hpp | 198 ++++++++ include/ustdex/detail/tuple.hpp | 16 +- include/ustdex/detail/when_any.hpp | 427 ++++++++++++++++++ include/ustdex/ustdex.hpp | 3 + 13 files changed, 1048 insertions(+), 122 deletions(-) create mode 100644 include/ustdex/detail/binder_closure.hpp create mode 100644 include/ustdex/detail/stop_when.hpp create mode 100644 include/ustdex/detail/stop_with.hpp create mode 100644 include/ustdex/detail/when_any.hpp diff --git a/CMakePresets.json b/CMakePresets.json index 9704f3b..fc779f1 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -1,36 +1,57 @@ { - "version": 3, - "cmakeMinimumRequired": { - "major": 3, - "minor": 21, - "patch": 3 + "version": 3, + "cmakeMinimumRequired": { + "major": 3, + "minor": 21, + "patch": 3 + }, + "configurePresets": [ + { + "name": "Debug", + "binaryDir": "${sourceDir}/build/Debug", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_CXX_STANDARD": "17" + } }, - "configurePresets": [ - { - "name": "Debug", - "binaryDir": "${sourceDir}/build/Debug", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug", - "CMAKE_CXX_STANDARD": "17" - } + { + "name": "Release", + "binaryDir": "${sourceDir}/build/Release", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release", + "CMAKE_CXX_STANDARD": "17" + } + }, + { + "name": "clang-x64-debug", + "displayName": "Clang x64 Debug", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "installDir": "${sourceDir}/out/install/${presetName}", + "architecture": { + "value": "x64", + "strategy": "external" }, - { - "name": "Release", - "binaryDir": "${sourceDir}/build/Release", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release", - "CMAKE_CXX_STANDARD": "17" + "cacheVariables": { + "CMAKE_C_COMPILER": "clang.exe", + "CMAKE_CXX_COMPILER": "clang++.exe", + "CMAKE_BUILD_TYPE": "Debug" + }, + "vendor": { + "microsoft.com/VisualStudioSettings/CMake/1.0": { + "intelliSenseMode": "windows-clang-x64" } } - ], - "buildPresets": [ - { - "name": "Debug", - "configurePreset": "Debug" - }, - { - "name": "Release", - "configurePreset": "Release" - } - ] - } + } + ], + "buildPresets": [ + { + "name": "Debug", + "configurePreset": "Debug" + }, + { + "name": "Release", + "configurePreset": "Release" + }, + + ] +} \ No newline at end of file diff --git a/examples/scratch.cpp b/examples/scratch.cpp index 46d5da4..f25c52f 100644 --- a/examples/scratch.cpp +++ b/examples/scratch.cpp @@ -19,29 +19,34 @@ #include #include "ustdex/ustdex.hpp" +#include +#include using namespace ustdex; +template +void _whatis(); + struct sink { - using receiver_concept = receiver_t; + using receiver_concept = receiver_t; - void set_value() noexcept {} + void set_value() noexcept {} - void set_value(int a) noexcept - { - std::printf("%d\n", a); - } + void set_value(int a) noexcept + { + std::printf("%d\n", a); + } - template - void set_value(As&&...) noexcept - { - std::puts("In sink::set_value(auto&&...)"); - } + template + void set_value(As&&...) noexcept + { + std::puts("In sink::set_value(auto&&...)"); + } - void set_error(std::exception_ptr) noexcept {} + void set_error(std::exception_ptr) noexcept {} - void set_stopped() noexcept {} + void set_stopped() noexcept {} }; template @@ -50,56 +55,142 @@ template static_assert(dependent_sender); + int main() { - thread_context ctx; - auto sch = ctx.get_scheduler(); - - auto work = just(1, 2, 3) // - | then([](int a, int b, int c) { - std::printf("%d %d %d\n", a, b, c); - return a + b + c; - }); - auto s = starts_on(sch, std::move(work)); - static_assert(!dependent_sender); - std::puts("Hello, world!"); - sync_wait(s); - - auto s3 = just(42) | let_value([](int a) { - std::puts("here"); - return just(a + 1); - }); - sync_wait(s3); - - auto [sch2] = sync_wait(read_env(get_scheduler)).value(); - - auto [i1, i2] = sync_wait(when_all(just(42), just(43))).value(); - std::cout << i1 << ' ' << i2 << '\n'; - - auto s4 = just(42) | then([](int) {}) | upon_error([](auto) { /*return 42;*/ }); - auto s5 = when_all(std::move(s4), just(42, 43), just(+"hello")); - auto [i, j, k] = sync_wait(std::move(s5)).value(); - std::cout << i << ' ' << j << ' ' << k << '\n'; - - auto s6 = sequence(just(42) | then([](int) { - std::cout << "sequence sender 1\n"; - }), - just(42) | then([](int) { - std::cout << "sequence sender 2\n"; - })); - sync_wait(std::move(s6)); - - auto s7 = - just(42) - | conditional( - [](int i) { - return i % 2 == 0; - }, - then([](int) { - std::cout << "even\n"; - }), - then([](int) { - std::cout << "odd\n"; - })); - sync_wait(std::move(s7)); + { + using CS = completion_signatures; + CS cs{}; + auto c = cs.select(set_value); + + _whatis(); + } + + thread_context ctx; + auto sch = ctx.get_scheduler(); + + auto work = just(1, 2, 3) // + | then([](int a, int b, int c) + { + std::printf("%d %d %d\n", a, b, c); + return a + b + c; + }); + auto s = starts_on(sch, std::move(work)); + static_assert(!dependent_sender); + std::puts("Hello, world!"); + sync_wait(s); + + auto s3 = just(42) | let_value([](int a) + { + std::puts("here"); + return just(a + 1); + }); + sync_wait(s3); + + auto [sch2] = sync_wait(read_env(get_scheduler)).value(); + + auto [i1, i2] = sync_wait(when_all(just(42), just(43))).value(); + std::cout << i1 << ' ' << i2 << '\n'; + + auto s4 = just(42) | then([](int) {}) | upon_error([](auto) { /*return 42;*/ }); + auto s5 = when_all(std::move(s4), just(42, 43), just(+"hello")); + auto [i, j, k] = sync_wait(std::move(s5)).value(); + std::cout << i << ' ' << j << ' ' << k << '\n'; + + auto s6 = sequence(just(42) | then([](int) + { + std::cout << "sequence sender 1\n"; + }), + just(42) | then([](int) + { + std::cout << "sequence sender 2\n"; + })); + sync_wait(std::move(s6)); + + auto s7 = + just(42) + | conditional( + [](int i) + { + return i % 2 == 0; + }, + then([](int) + { + std::cout << "even\n"; + }), + then([](int) + { + std::cout << "odd\n"; + })); + sync_wait(std::move(s7)); + + + { + inplace_stop_source stop_source; + + auto task = just(100) + | then([&stop_source](int i) + { + stop_source.request_stop(); + std::cout << "111_Value: " << i << '\n'; + return i; + }) + //| stop_when(stop_source.get_token()) + | stop_with([](const int& i) + { + if (i == 100) + { + return false; + } + else + { + return false; + } + }) + | then([](int i) + { + std::cout << "222_Value: " << i << '\n'; + return i; + }) + | upon_stopped([]() + { + std::cout << "Stopped!\n"; + }) + | upon_error([](std::exception_ptr e) + { + try + { + std::rethrow_exception(e); + } + catch (const std::exception& ex) + { + std::cout << "Error: " << ex.what() << '\n'; + } + }); + + auto op = connect(std::move(task), sink{}); + start(op); + //sync_wait(std::move(task)); + + } + + { + /*auto task = when_any(just(5), just('a'), just(3.14)) | + then([](std::any v) + { + int aa = 0; + }); + + auto op = connect(std::move(task), sink{}); + start(op);*/ + + //using TA = completion_signatures_of_t; + //_m_self_or<_nil>::call + //using TB = _value_types; + + //_whatis(); + } + + + } diff --git a/include/ustdex/detail/binder_closure.hpp b/include/ustdex/detail/binder_closure.hpp new file mode 100644 index 0000000..48c93b9 --- /dev/null +++ b/include/ustdex/detail/binder_closure.hpp @@ -0,0 +1,43 @@ +#ifndef USTDEX_ASYNC_DETAIL_BINDER_CLOSURE +#define USTDEX_ASYNC_DETAIL_BINDER_CLOSURE + +#include "config.hpp" +#include "tuple.hpp" + +#include "prologue.hpp" + +namespace ustdex +{ + + template + struct binder_closure + { + _tuple _args_; + + binder_closure(Args... args) noexcept :_args_({ static_cast(args)... }) + {} + + template + static auto make(Sndr&& sndr, _tuple _args, std::index_sequence) noexcept ->_call_result_t + { + return bind_t()(static_cast(sndr), static_cast(const_cast(_cget(_args)))...); + } + + template + USTDEX_TRIVIAL_API auto operator()(Sndr _sndr) noexcept ->_call_result_t + { + return this->make(static_cast(_sndr), static_cast<_tuple&&>(_args_), std::make_index_sequence()); + } + + template + USTDEX_TRIVIAL_API friend auto operator|(Sndr sndr, binder_closure&& self) noexcept ->_call_result_t + { + return self.make(static_cast(sndr), static_cast<_tuple&&>(self._args_), std::make_index_sequence()); + } + }; + +} + +#include "epilogue.hpp" + +#endif // USTDEX_ASYNC_DETAIL_BINDER_CLOSURE \ No newline at end of file diff --git a/include/ustdex/detail/completion_signatures.hpp b/include/ustdex/detail/completion_signatures.hpp index eeaf109..abb1543 100644 --- a/include/ustdex/detail/completion_signatures.hpp +++ b/include/ustdex/detail/completion_signatures.hpp @@ -169,15 +169,15 @@ USTDEX_API constexpr auto completion_signatures::select(Tag) const noex { if constexpr (Tag() == set_value) { - return _partitioned::template _value_types<_m_quote_f::call, completion_signatures>(); + return typename _partitioned::template _value_types<_m_quote_f::call, completion_signatures>(); } else if constexpr (Tag() == set_error) { - return _partitioned::template _error_types::call>(); + return typename _partitioned::template _error_types::call>(); } else { - return _partitioned::template _stopped_types(); + return typename _partitioned::template _stopped_types(); } } diff --git a/include/ustdex/detail/exclusive_scan.hpp b/include/ustdex/detail/exclusive_scan.hpp index fbdb26b..48632d9 100644 --- a/include/ustdex/detail/exclusive_scan.hpp +++ b/include/ustdex/detail/exclusive_scan.hpp @@ -24,7 +24,7 @@ namespace ustdex { -#if __cplusplus >= 202002L +#if __cplusplus >= 202302L // exclusive_scan is constexpr in C++20 using std::exclusive_scan; #else diff --git a/include/ustdex/detail/lazy.hpp b/include/ustdex/detail/lazy.hpp index 7c11c80..eb254b0 100644 --- a/include/ustdex/detail/lazy.hpp +++ b/include/ustdex/detail/lazy.hpp @@ -38,16 +38,16 @@ struct _lazy template USTDEX_API Ty& construct(Ts&&... _ts) noexcept(_nothrow_constructible) { - Ty* _value_ = ::new (static_cast(std::addressof(_value_))) Ty{static_cast(_ts)...}; - return *std::launder(_value_); + Ty* rst = ::new (static_cast(std::addressof(_value_))) Ty{static_cast(_ts)...}; + return *std::launder(rst); } template USTDEX_API Ty& construct_from(Fn&& _fn, Ts&&... _ts) noexcept(_nothrow_callable) { - Ty* _value_ = ::new (static_cast(std::addressof(_value_))) + Ty* rst = ::new (static_cast(std::addressof(_value_))) Ty{static_cast(_fn)(static_cast(_ts)...)}; - return *std::launder(_value_); + return *std::launder(rst); } USTDEX_API void destroy() noexcept diff --git a/include/ustdex/detail/meta.hpp b/include/ustdex/detail/meta.hpp index 456be3f..86798ab 100644 --- a/include/ustdex/detail/meta.hpp +++ b/include/ustdex/detail/meta.hpp @@ -448,16 +448,16 @@ template