1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03, c++11 11 12 // <experimental/coroutine> 13 14 // template <class Promise> 15 // struct coroutine_handle<Promise>; 16 17 // Promise& promise() const 18 19 #include <experimental/coroutine> 20 #include <type_traits> 21 #include <memory> 22 #include <utility> 23 #include <cstdint> 24 #include <cassert> 25 26 #include "test_macros.h" 27 28 namespace coro = std::experimental; 29 30 struct MyCoro { 31 struct promise_type { unhandled_exceptionMyCoro::promise_type32 void unhandled_exception() {} return_voidMyCoro::promise_type33 void return_void() {} initial_suspendMyCoro::promise_type34 coro::suspend_never initial_suspend() { return {}; } final_suspendMyCoro::promise_type35 coro::suspend_never final_suspend() { return {}; } get_return_objectMyCoro::promise_type36 MyCoro get_return_object() { 37 do_runtime_test(); 38 return {}; 39 } do_runtime_testMyCoro::promise_type40 void do_runtime_test() { 41 // Test that a coroutine_handle<const T> can be created from a const 42 // promise_type and that it represents the same coroutine as 43 // coroutine_handle<T> 44 using CH = coro::coroutine_handle<promise_type>; 45 using CCH = coro::coroutine_handle<const promise_type>; 46 const auto &cthis = *this; 47 CH h = CH::from_promise(*this); 48 CCH h2 = CCH::from_promise(*this); 49 CCH h3 = CCH::from_promise(cthis); 50 assert(&h.promise() == this); 51 assert(&h2.promise() == this); 52 assert(&h3.promise() == this); 53 assert(h.address() == h2.address()); 54 assert(h2.address() == h3.address()); 55 } 56 }; 57 }; 58 do_runtime_test()59MyCoro do_runtime_test() { 60 co_await coro::suspend_never{}; 61 } 62 63 template <class Promise> do_test(coro::coroutine_handle<Promise> && H)64void do_test(coro::coroutine_handle<Promise>&& H) { 65 66 // FIXME Add a runtime test 67 { 68 ASSERT_SAME_TYPE(decltype(H.promise()), Promise&); 69 LIBCPP_ASSERT_NOT_NOEXCEPT(H.promise()); 70 } 71 { 72 auto const& CH = H; 73 ASSERT_SAME_TYPE(decltype(CH.promise()), Promise&); 74 LIBCPP_ASSERT_NOT_NOEXCEPT(CH.promise()); 75 } 76 } 77 main(int,char **)78int main(int, char**) 79 { 80 do_test(coro::coroutine_handle<int>{}); 81 do_test(coro::coroutine_handle<const int>{}); 82 do_runtime_test(); 83 84 return 0; 85 } 86