From 57839425aa4802986acbb17392ca697ee76aa633 Mon Sep 17 00:00:00 2001 From: Brian Gesiak Date: Tue, 26 Mar 2019 17:46:06 +0000 Subject: [coroutines] Add std::experimental::task type Summary: Adds the coroutine `std::experimental::task` type described in proposal P1056R0. See https://wg21.link/P1056R0. This implementation allows customization of the allocator used to allocate the coroutine frame by passing std::allocator_arg as the first argument, followed by the allocator to use. This supports co_awaiting the same task multiple times. The second and subsequent times it returns a reference to the already-computed value. This diff also adds some implementations of other utilities that have potential for standardization as helpers within the test/... area: - `sync_wait(awaitable)` - See P1171R0 - `manual_reset_event` Move the definition of the __aligned_allocation_size helper function from to so it can be more widely used without pulling in memory_resource. Outstanding work: - Use C++14 keywords directly rather than macro versions eg. use `noexcept` instead of `_NOEXCEPT`). - Add support for overaligned coroutine frames. This may need wording in the Coroutines TS to support passing the extra `std::align_val_t`. - Eliminate use of `if constexpr` if we want it to compile under C++14. Patch by @lewissbaker (Lewis Baker). llvm-svn: 357010 --- .../task/task.basic/task_of_value.pass.cpp | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 libcxx/test/std/experimental/task/task.basic/task_of_value.pass.cpp (limited to 'libcxx/test/std/experimental/task/task.basic/task_of_value.pass.cpp') diff --git a/libcxx/test/std/experimental/task/task.basic/task_of_value.pass.cpp b/libcxx/test/std/experimental/task/task.basic/task_of_value.pass.cpp new file mode 100644 index 00000000000..0d5a8c9f619 --- /dev/null +++ b/libcxx/test/std/experimental/task/task.basic/task_of_value.pass.cpp @@ -0,0 +1,70 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +#include +#include +#include +#include +#include "../sync_wait.hpp" + +void test_returning_move_only_type() +{ + auto move_only_async = + [](bool x) -> std::experimental::task> { + if (x) { + auto p = std::make_unique(123); + co_return p; // Should be implicit std::move(p) here. + } + + co_return std::make_unique(456); + }; + + assert(*sync_wait(move_only_async(true)) == 123); + assert(*sync_wait(move_only_async(false)) == 456); +} + +void test_co_return_with_curly_braces() +{ + auto t = []() -> std::experimental::task> + { + co_return { 123, "test" }; + }(); + + auto result = sync_wait(std::move(t)); + + assert(std::get<0>(result) == 123); + assert(std::get<1>(result) == "test"); +} + +void test_co_return_by_initialiser_list() +{ + auto t = []() -> std::experimental::task> + { + co_return { 2, 10, -1 }; + }(); + + auto result = sync_wait(std::move(t)); + + assert(result.size() == 3); + assert(result[0] == 2); + assert(result[1] == 10); + assert(result[2] == -1); +} + +int main() +{ + test_returning_move_only_type(); + test_co_return_with_curly_braces(); + test_co_return_by_initialiser_list(); + + return 0; +} -- cgit v1.2.3