diff --git a/examples/any_unique.cpp b/examples/any_unique.cpp index 16989a24..056afcbd 100644 --- a/examples/any_unique.cpp +++ b/examples/any_unique.cpp @@ -14,13 +14,13 @@ * limitations under the License. */ #include -#include #include #include +#include +#include #include #include -#include template using is_type_index = std::is_same; @@ -35,21 +35,18 @@ inline constexpr struct get_typeid_cpo { } template - auto operator()(const T& x) const noexcept -> - unifex::tag_invoke_result_t { - static_assert( - std::is_same_v< - unifex::type_index, - unifex::tag_invoke_result_t>); + auto operator()(const T& x) const noexcept + -> unifex::tag_invoke_result_t { + static_assert(std::is_same_v< + unifex::type_index, + unifex::tag_invoke_result_t>); return tag_invoke(get_typeid_cpo{}, x); } } get_typeid; struct destructor { explicit destructor(bool& x) : ref_(x) {} - ~destructor() { - ref_ = true; - } + ~destructor() { ref_ = true; } bool& ref_; }; @@ -57,22 +54,20 @@ struct destructor { using namespace unifex::pmr; class counting_memory_resource : public memory_resource { - public: +public: explicit counting_memory_resource(memory_resource* r) noexcept : inner_(r) {} - std::size_t total_allocated_bytes() const { - return allocated_.load(); - } + std::size_t total_allocated_bytes() const { return allocated_.load(); } - private: +private: void* do_allocate(std::size_t bytes, std::size_t alignment) override { void* p = inner_->allocate(bytes, alignment); allocated_ += bytes; return p; } - void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) - override { + void + do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override { allocated_ -= bytes; inner_->deallocate(p, bytes, alignment); } diff --git a/examples/async_mutex.cpp b/examples/async_mutex.cpp index a758ac2a..864498b0 100644 --- a/examples/async_mutex.cpp +++ b/examples/async_mutex.cpp @@ -21,12 +21,12 @@ #if !UNIFEX_NO_COROUTINES -#include -#include -#include -#include +# include +# include +# include +# include -#include +# include using namespace unifex; @@ -48,8 +48,8 @@ int main() { single_thread_context ctx1; single_thread_context ctx2; - sync_wait(when_all(makeTask(ctx1.get_scheduler()), - makeTask(ctx2.get_scheduler()))); + sync_wait( + when_all(makeTask(ctx1.get_scheduler()), makeTask(ctx2.get_scheduler()))); if (sharedState != 200'000) { std::printf("error: incorrect result %i, expected 2000000\n", sharedState); @@ -59,18 +59,18 @@ int main() { return 0; } -#else // UNIFEX_NO_COROUTINES +#else // UNIFEX_NO_COROUTINES -#include +# include int main() { - // Very simple usage of async_mutex. + // Very simple usage of async_mutex. - unifex::async_mutex m; - unifex::sync_wait(m.async_lock()); - m.unlock(); - - return 0; + unifex::async_mutex m; + unifex::sync_wait(m.async_lock()); + m.unlock(); + + return 0; } -#endif // UNIFEX_NO_COROUTINES +#endif // UNIFEX_NO_COROUTINES diff --git a/examples/async_trace.cpp b/examples/async_trace.cpp index bf221370..af741599 100644 --- a/examples/async_trace.cpp +++ b/examples/async_trace.cpp @@ -15,20 +15,20 @@ */ #include -#include #include +#include #include #include +#include +#include #include -#include #include -#include +#include #include -#include #if !UNIFEX_NO_COROUTINES -#include +# include #endif #include @@ -44,16 +44,16 @@ using namespace std::chrono_literals; auto dump_async_trace(std::string tag = {}) { return then( async_trace_sender{}, - [tag = std::move(tag)](const std::vector &entries) { + [tag = std::move(tag)](const std::vector& entries) { std::cout << "Async Trace (" << tag << "):\n"; - for (auto &entry : entries) { - std::cout << " " << entry.depth << " [-> " << entry.parentIndex + for (auto& entry : entries) { + std::cout << " " << entry.depth << " [-> " << entry.parentIndex << "]: " - #if UNIFEX_ENABLE_CONTINUATION_VISITATIONS +#if UNIFEX_ENABLE_CONTINUATION_VISITATIONS << entry.continuation.type().name() - #else +#else << "(continuation typenames disabled)" - #endif +#endif << " @ 0x"; std::cout.setf(std::ios::hex, std::ios::basefield); std::cout << entry.continuation.address(); @@ -64,14 +64,13 @@ auto dump_async_trace(std::string tag = {}) { } template -auto dump_async_trace_on_start(Sender &&sender, std::string tag = {}) { +auto dump_async_trace_on_start(Sender&& sender, std::string tag = {}) { return unifex::sequence(dump_async_trace(std::move(tag)), (Sender &&) sender); } template -auto dump_async_trace_on_completion(Sender &&sender, std::string tag = {}) { - return unifex::finally((Sender &&) sender, - dump_async_trace(std::move(tag))); +auto dump_async_trace_on_completion(Sender&& sender, std::string tag = {}) { + return unifex::finally((Sender &&) sender, dump_async_trace(std::move(tag))); } #if !UNIFEX_NO_COROUTINES @@ -87,42 +86,39 @@ int main() { auto startTime = steady_clock::now(); - sync_wait( - then( + sync_wait(then( when_all( - then( - dump_async_trace_on_start( - schedule_after(context.get_scheduler(), 100ms), "part1"), - [=]() { - auto time = steady_clock::now() - startTime; - auto timeMs = duration_cast(time).count(); - std::cout << "part1 finished - [" << timeMs << "]\n"; - return time; - }), - then( - dump_async_trace_on_completion( - schedule_after(context.get_scheduler(), 200ms), "part2"), - [=]() { - auto time = steady_clock::now() - startTime; - auto timeMs = duration_cast(time).count(); - std::cout << "part2 finished - [" << timeMs << "]\n"; - return time; - }), + then( + dump_async_trace_on_start( + schedule_after(context.get_scheduler(), 100ms), "part1"), + [=]() { + auto time = steady_clock::now() - startTime; + auto timeMs = duration_cast(time).count(); + std::cout << "part1 finished - [" << timeMs << "]\n"; + return time; + }), + then( + dump_async_trace_on_completion( + schedule_after(context.get_scheduler(), 200ms), "part2"), + [=]() { + auto time = steady_clock::now() - startTime; + auto timeMs = duration_cast(time).count(); + std::cout << "part2 finished - [" << timeMs << "]\n"; + return time; + }), #if !UNIFEX_NO_COROUTINES - dump_async_trace_in_coroutine() + dump_async_trace_in_coroutine() #else - just(42) -#endif // UNIFEX_NO_COROUTINES - ), - [](auto &&a, auto &&b, auto &&c) { + just(42) +#endif // UNIFEX_NO_COROUTINES + ), + [](auto&& a, auto&& b, auto&& c) { std::cout << "when_all finished - [" << duration_cast(std::get<0>(std::get<0>(a))).count() << ", " << duration_cast(std::get<0>(std::get<0>(b))).count() - << ", " - << std::get<0>(std::get<0>(c)) - << "]\n"; + << ", " << std::get<0>(std::get<0>(c)) << "]\n"; })); std::cout << "all done\n"; diff --git a/examples/coroutine_stream_consumer.cpp b/examples/coroutine_stream_consumer.cpp index 792baab1..6b8f9af3 100644 --- a/examples/coroutine_stream_consumer.cpp +++ b/examples/coroutine_stream_consumer.cpp @@ -17,29 +17,29 @@ #if !UNIFEX_NO_COROUTINES -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +# include +# include using namespace unifex; -template +template auto done_as_void(Sender&& sender) { - return let_done((Sender&&)sender, [] { return just(); }); + return let_done((Sender &&) sender, [] { return just(); }); } int main() { @@ -78,9 +78,9 @@ int main() { return 0; } -#else // UNIFEX_NO_COROUTINES +#else // UNIFEX_NO_COROUTINES -#include +# include int main() { std::printf( @@ -88,4 +88,4 @@ int main() { return 0; } -#endif // UNIFEX_NO_COROUTINES +#endif // UNIFEX_NO_COROUTINES diff --git a/examples/delayed_stream_cancellation.cpp b/examples/delayed_stream_cancellation.cpp index edd71f58..d3f6a314 100644 --- a/examples/delayed_stream_cancellation.cpp +++ b/examples/delayed_stream_cancellation.cpp @@ -17,12 +17,12 @@ #include #include #include +#include +#include #include +#include #include #include -#include -#include -#include #include #include @@ -37,17 +37,17 @@ int main() { auto startTime = steady_clock::now(); - sync_wait( - stop_when( - for_each( - delay(range_stream{0, 100}, context.get_scheduler(), 100ms), - [startTime](int value) { - auto ms = duration_cast(steady_clock::now() - startTime); - std::printf("[%i ms] %i\n", (int)ms.count(), value); - }), - then( - schedule_after(context.get_scheduler(), 500ms), - [] { std::printf("cancelling\n"); }))); + sync_wait(stop_when( + for_each( + delay(range_stream{0, 100}, context.get_scheduler(), 100ms), + [startTime](int value) { + auto ms = + duration_cast(steady_clock::now() - startTime); + std::printf("[%i ms] %i\n", (int)ms.count(), value); + }), + then(schedule_after(context.get_scheduler(), 500ms), [] { + std::printf("cancelling\n"); + }))); return 0; } diff --git a/examples/for_each_synchronous.cpp b/examples/for_each_synchronous.cpp index b26d1db4..1dd1900d 100644 --- a/examples/for_each_synchronous.cpp +++ b/examples/for_each_synchronous.cpp @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include #include -#include #include +#include +#include +#include #include diff --git a/examples/for_each_via_trampoline.cpp b/examples/for_each_via_trampoline.cpp index b97a6ced..491e04a3 100644 --- a/examples/for_each_via_trampoline.cpp +++ b/examples/for_each_via_trampoline.cpp @@ -16,8 +16,8 @@ #include #include #include -#include #include +#include #include #include diff --git a/examples/fp_delegation.cpp b/examples/fp_delegation.cpp index bc50efca..eb010d0d 100644 --- a/examples/fp_delegation.cpp +++ b/examples/fp_delegation.cpp @@ -36,29 +36,25 @@ namespace { class delegating_scheduler; class delegating_context { - public: +public: delegating_context(int capacity) : capacity_{capacity} {} bool reserve() { auto lck = std::scoped_lock{m_}; bool reservation_made = reservation_count_ < capacity_; - if(reservation_made) { + if (reservation_made) { ++reservation_count_; } return reservation_made; } - int get_count() { - return run_count_; - } + int get_count() { return run_count_; } - void run() { - run_count_++; - } + void run() { run_count_++; } delegating_scheduler get_scheduler() noexcept; - private: +private: friend class delegating_sender; std::mutex m_; int reservation_count_{0}; @@ -67,34 +63,30 @@ class delegating_context { timed_single_thread_context single_thread_context_; }; -template +template struct FuncWrapper { T func_; - operator std::invoke_result_t() { - return func_(); - } + operator std::invoke_result_t() { return func_(); } }; template class delegating_operation final { - public: - template - delegating_operation(InitFunc&& func, delegating_context* context) : - op_{ - std::in_place_type_t>{}, - FuncWrapper{std::forward(func)}}, - context_{context} { - } - +public: + template + delegating_operation(InitFunc&& func, delegating_context* context) + : op_{std::in_place_type_t>{}, + FuncWrapper{std::forward(func)}} + , context_{context} {} inline void start() noexcept { - if(std::holds_alternative(op_)) { + if (std::holds_alternative(op_)) { // Start a delegated operation std::get(op_).start(); } else { // Start immediately on the local context context_->run(); - std::get(op_).start();; + std::get(op_).start(); + ; } } @@ -103,10 +95,12 @@ class delegating_operation final { }; class delegating_sender { - public: +public: template < - template class Variant, - template class Tuple> + template + class Variant, + template + class Tuple> using value_types = Variant>; template