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

Format examples #584

Merged
merged 1 commit into from
Nov 3, 2023
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
31 changes: 13 additions & 18 deletions examples/any_unique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/
#include <unifex/any_unique.hpp>
#include <unifex/type_traits.hpp>
#include <unifex/memory_resource.hpp>
#include <unifex/type_index.hpp>
#include <unifex/type_traits.hpp>

#include <atomic>
#include <string>
#include <typeindex>
#include <atomic>

template <typename T>
using is_type_index = std::is_same<std::type_index, T>;
Expand All @@ -35,44 +35,39 @@ inline constexpr struct get_typeid_cpo {
}

template <typename T>
auto operator()(const T& x) const noexcept ->
unifex::tag_invoke_result_t<get_typeid_cpo, const T&> {
static_assert(
std::is_same_v<
unifex::type_index,
unifex::tag_invoke_result_t<get_typeid_cpo, const T&>>);
auto operator()(const T& x) const noexcept
-> unifex::tag_invoke_result_t<get_typeid_cpo, const T&> {
static_assert(std::is_same_v<
unifex::type_index,
unifex::tag_invoke_result_t<get_typeid_cpo, const 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_;
};

#if !UNIFEX_NO_MEMORY_RESOURCE
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);
}
Expand Down
32 changes: 16 additions & 16 deletions examples/async_mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

#if !UNIFEX_NO_COROUTINES

#include <unifex/scheduler_concepts.hpp>
#include <unifex/task.hpp>
#include <unifex/when_all.hpp>
#include <unifex/single_thread_context.hpp>
# include <unifex/scheduler_concepts.hpp>
# include <unifex/single_thread_context.hpp>
# include <unifex/task.hpp>
# include <unifex/when_all.hpp>

#include <cstdio>
# include <cstdio>
ispeters marked this conversation as resolved.
Show resolved Hide resolved

using namespace unifex;

Expand All @@ -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);
Expand All @@ -59,18 +59,18 @@ int main() {
return 0;
}

#else // UNIFEX_NO_COROUTINES
#else // UNIFEX_NO_COROUTINES

#include <cstdio>
# include <cstdio>

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
82 changes: 39 additions & 43 deletions examples/async_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
#include <unifex/async_trace.hpp>

#include <unifex/sequence.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/sequence.hpp>
#include <unifex/sync_wait.hpp>

#include <unifex/config.hpp>
#include <unifex/defer.hpp>
#include <unifex/finally.hpp>
#include <unifex/just.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/then.hpp>
#include <unifex/finally.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/when_all.hpp>
#include <unifex/defer.hpp>

#if !UNIFEX_NO_COROUTINES
#include <unifex/task.hpp>
# include <unifex/task.hpp>
#endif

#include <chrono>
Expand All @@ -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<async_trace_entry> &entries) {
[tag = std::move(tag)](const std::vector<async_trace_entry>& 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();
Expand All @@ -64,14 +64,13 @@ auto dump_async_trace(std::string tag = {}) {
}

template <typename Sender>
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 <typename Sender>
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
Expand All @@ -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<milliseconds>(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<milliseconds>(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<milliseconds>(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<milliseconds>(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<milliseconds>(std::get<0>(std::get<0>(a))).count()
<< ", "
<< duration_cast<milliseconds>(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";
Expand Down
44 changes: 22 additions & 22 deletions examples/coroutine_stream_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@

#if !UNIFEX_NO_COROUTINES

#include <unifex/delay.hpp>
#include <unifex/range_stream.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/single.hpp>
#include <unifex/stop_immediately.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/take_until.hpp>
#include <unifex/task.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/via_stream.hpp>
#include <unifex/let_done.hpp>
#include <unifex/then.hpp>
#include <unifex/just.hpp>
#include <unifex/done_as_optional.hpp>

#include <chrono>
#include <cstdio>
# include <unifex/delay.hpp>
# include <unifex/done_as_optional.hpp>
# include <unifex/just.hpp>
# include <unifex/let_done.hpp>
# include <unifex/range_stream.hpp>
# include <unifex/scheduler_concepts.hpp>
# include <unifex/single.hpp>
# include <unifex/stop_immediately.hpp>
# include <unifex/sync_wait.hpp>
# include <unifex/take_until.hpp>
# include <unifex/task.hpp>
# include <unifex/then.hpp>
# include <unifex/timed_single_thread_context.hpp>
# include <unifex/via_stream.hpp>

# include <chrono>
# include <cstdio>

using namespace unifex;

template<typename Sender>
template <typename Sender>
auto done_as_void(Sender&& sender) {
return let_done((Sender&&)sender, [] { return just(); });
return let_done((Sender &&) sender, [] { return just(); });
}

int main() {
Expand Down Expand Up @@ -78,14 +78,14 @@ int main() {
return 0;
}

#else // UNIFEX_NO_COROUTINES
#else // UNIFEX_NO_COROUTINES

#include <cstdio>
# include <cstdio>

int main() {
std::printf(
"This test only supported for compilers that support coroutines\n");
return 0;
}

#endif // UNIFEX_NO_COROUTINES
#endif // UNIFEX_NO_COROUTINES
28 changes: 14 additions & 14 deletions examples/delayed_stream_cancellation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
#include <unifex/for_each.hpp>
#include <unifex/inplace_stop_token.hpp>
#include <unifex/range_stream.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/stop_when.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/then.hpp>
#include <unifex/timed_single_thread_context.hpp>
#include <unifex/via_stream.hpp>
#include <unifex/scheduler_concepts.hpp>
#include <unifex/then.hpp>
#include <unifex/stop_when.hpp>

#include <chrono>
#include <cstdio>
Expand All @@ -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<milliseconds>(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<milliseconds>(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;
}
6 changes: 3 additions & 3 deletions examples/for_each_synchronous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unifex/sync_wait.hpp>
#include <unifex/transform_stream.hpp>
#include <unifex/for_each.hpp>
#include <unifex/then.hpp>
#include <unifex/range_stream.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/then.hpp>
#include <unifex/transform_stream.hpp>

#include <cstdio>

Expand Down
Loading
Loading