Skip to content

Commit 49da236

Browse files
dutowandreasbuhr
authored andcommitted
Support both <coroutine> and <experimental/coroutine>
Starting with GCC 10.1, GCC/libstdc++ supports coroutines, but includes them in their final location in <coroutine>. This commit generalizes the location of the header, and the name of the types (either in the std or std::experimental namespace), so that both the current Clang/MSVC and the GCC approach can be supported. This should also guarantee that both current and later Clang/MSVC releases will be supported by the library.
1 parent 7f4ca14 commit 49da236

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+213
-181
lines changed

include/cppcoro/async_auto_reset_event.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef CPPCORO_ASYNC_AUTO_RESET_EVENT_HPP_INCLUDED
66
#define CPPCORO_ASYNC_AUTO_RESET_EVENT_HPP_INCLUDED
77

8-
#include <experimental/coroutine>
8+
#include <cppcoro/coroutine.hpp>
99
#include <atomic>
1010
#include <cstdint>
1111

@@ -80,7 +80,7 @@ namespace cppcoro
8080
async_auto_reset_event_operation(const async_auto_reset_event_operation& other) noexcept;
8181

8282
bool await_ready() const noexcept { return m_event == nullptr; }
83-
bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
83+
bool await_suspend(cppcoro::coroutine_handle<> awaiter) noexcept;
8484
void await_resume() const noexcept {}
8585

8686
private:
@@ -89,7 +89,7 @@ namespace cppcoro
8989

9090
const async_auto_reset_event* m_event;
9191
async_auto_reset_event_operation* m_next;
92-
std::experimental::coroutine_handle<> m_awaiter;
92+
cppcoro::coroutine_handle<> m_awaiter;
9393
std::atomic<std::uint32_t> m_refCount;
9494

9595
};

include/cppcoro/async_generator.hpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <atomic>
1313
#include <iterator>
1414
#include <type_traits>
15-
#include <experimental/coroutine>
15+
#include <cppcoro/coroutine.hpp>
1616
#include <functional>
1717
#include <cassert>
1818

@@ -45,7 +45,7 @@ namespace cppcoro
4545
async_generator_promise_base(const async_generator_promise_base& other) = delete;
4646
async_generator_promise_base& operator=(const async_generator_promise_base& other) = delete;
4747

48-
std::experimental::suspend_always initial_suspend() const noexcept
48+
cppcoro::suspend_always initial_suspend() const noexcept
4949
{
5050
return {};
5151
}
@@ -89,7 +89,7 @@ namespace cppcoro
8989

9090
std::exception_ptr m_exception;
9191

92-
std::experimental::coroutine_handle<> m_consumerCoroutine;
92+
cppcoro::coroutine_handle<> m_consumerCoroutine;
9393

9494
protected:
9595

@@ -100,7 +100,7 @@ namespace cppcoro
100100
{
101101
public:
102102

103-
async_generator_yield_operation(std::experimental::coroutine_handle<> consumer) noexcept
103+
async_generator_yield_operation(cppcoro::coroutine_handle<> consumer) noexcept
104104
: m_consumer(consumer)
105105
{}
106106

@@ -109,8 +109,8 @@ namespace cppcoro
109109
return false;
110110
}
111111

112-
std::experimental::coroutine_handle<>
113-
await_suspend([[maybe_unused]] std::experimental::coroutine_handle<> producer) noexcept
112+
cppcoro::coroutine_handle<>
113+
await_suspend([[maybe_unused]] cppcoro::coroutine_handle<> producer) noexcept
114114
{
115115
return m_consumer;
116116
}
@@ -119,7 +119,7 @@ namespace cppcoro
119119

120120
private:
121121

122-
std::experimental::coroutine_handle<> m_consumer;
122+
cppcoro::coroutine_handle<> m_consumer;
123123

124124
};
125125

@@ -145,7 +145,7 @@ namespace cppcoro
145145

146146
async_generator_advance_operation(
147147
async_generator_promise_base& promise,
148-
std::experimental::coroutine_handle<> producerCoroutine) noexcept
148+
cppcoro::coroutine_handle<> producerCoroutine) noexcept
149149
: m_promise(std::addressof(promise))
150150
, m_producerCoroutine(producerCoroutine)
151151
{
@@ -155,8 +155,8 @@ namespace cppcoro
155155

156156
bool await_ready() const noexcept { return false; }
157157

158-
std::experimental::coroutine_handle<>
159-
await_suspend(std::experimental::coroutine_handle<> consumerCoroutine) noexcept
158+
cppcoro::coroutine_handle<>
159+
await_suspend(cppcoro::coroutine_handle<> consumerCoroutine) noexcept
160160
{
161161
m_promise->m_consumerCoroutine = consumerCoroutine;
162162
return m_producerCoroutine;
@@ -165,7 +165,7 @@ namespace cppcoro
165165
protected:
166166

167167
async_generator_promise_base* m_promise;
168-
std::experimental::coroutine_handle<> m_producerCoroutine;
168+
cppcoro::coroutine_handle<> m_producerCoroutine;
169169

170170
};
171171

@@ -242,7 +242,7 @@ namespace cppcoro
242242
class async_generator_iterator final
243243
{
244244
using promise_type = async_generator_promise<T>;
245-
using handle_type = std::experimental::coroutine_handle<promise_type>;
245+
using handle_type = cppcoro::coroutine_handle<promise_type>;
246246

247247
public:
248248

@@ -307,7 +307,7 @@ namespace cppcoro
307307
class async_generator_begin_operation final : public async_generator_advance_operation
308308
{
309309
using promise_type = async_generator_promise<T>;
310-
using handle_type = std::experimental::coroutine_handle<promise_type>;
310+
using handle_type = cppcoro::coroutine_handle<promise_type>;
311311

312312
public:
313313

@@ -358,7 +358,7 @@ namespace cppcoro
358358
{}
359359

360360
explicit async_generator(promise_type& promise) noexcept
361-
: m_coroutine(std::experimental::coroutine_handle<promise_type>::from_promise(promise))
361+
: m_coroutine(cppcoro::coroutine_handle<promise_type>::from_promise(promise))
362362
{}
363363

364364
async_generator(async_generator&& other) noexcept
@@ -408,7 +408,7 @@ namespace cppcoro
408408

409409
private:
410410

411-
std::experimental::coroutine_handle<promise_type> m_coroutine;
411+
cppcoro::coroutine_handle<promise_type> m_coroutine;
412412

413413
};
414414

@@ -451,7 +451,7 @@ namespace cppcoro
451451
async_generator_promise_base(const async_generator_promise_base& other) = delete;
452452
async_generator_promise_base& operator=(const async_generator_promise_base& other) = delete;
453453

454-
std::experimental::suspend_always initial_suspend() const noexcept
454+
cppcoro::suspend_always initial_suspend() const noexcept
455455
{
456456
return {};
457457
}
@@ -556,7 +556,7 @@ namespace cppcoro
556556

557557
std::exception_ptr m_exception;
558558

559-
std::experimental::coroutine_handle<> m_consumerCoroutine;
559+
cppcoro::coroutine_handle<> m_consumerCoroutine;
560560

561561
protected:
562562

@@ -579,7 +579,7 @@ namespace cppcoro
579579
return m_initialState == state::value_not_ready_consumer_suspended;
580580
}
581581

582-
bool await_suspend(std::experimental::coroutine_handle<> producer) noexcept;
582+
bool await_suspend(cppcoro::coroutine_handle<> producer) noexcept;
583583

584584
void await_resume() noexcept {}
585585

@@ -625,7 +625,7 @@ namespace cppcoro
625625
}
626626

627627
inline bool async_generator_yield_operation::await_suspend(
628-
std::experimental::coroutine_handle<> producer) noexcept
628+
cppcoro::coroutine_handle<> producer) noexcept
629629
{
630630
state currentState = m_initialState;
631631
if (currentState == state::value_not_ready_consumer_active)
@@ -711,7 +711,7 @@ namespace cppcoro
711711

712712
async_generator_advance_operation(
713713
async_generator_promise_base& promise,
714-
std::experimental::coroutine_handle<> producerCoroutine) noexcept
714+
cppcoro::coroutine_handle<> producerCoroutine) noexcept
715715
: m_promise(std::addressof(promise))
716716
, m_producerCoroutine(producerCoroutine)
717717
{
@@ -740,7 +740,7 @@ namespace cppcoro
740740
return m_initialState == state::value_ready_producer_suspended;
741741
}
742742

743-
bool await_suspend(std::experimental::coroutine_handle<> consumerCoroutine) noexcept
743+
bool await_suspend(cppcoro::coroutine_handle<> consumerCoroutine) noexcept
744744
{
745745
m_promise->m_consumerCoroutine = consumerCoroutine;
746746

@@ -791,7 +791,7 @@ namespace cppcoro
791791
protected:
792792

793793
async_generator_promise_base* m_promise;
794-
std::experimental::coroutine_handle<> m_producerCoroutine;
794+
cppcoro::coroutine_handle<> m_producerCoroutine;
795795

796796
private:
797797

@@ -872,7 +872,7 @@ namespace cppcoro
872872
class async_generator_iterator final
873873
{
874874
using promise_type = async_generator_promise<T>;
875-
using handle_type = std::experimental::coroutine_handle<promise_type>;
875+
using handle_type = cppcoro::coroutine_handle<promise_type>;
876876

877877
public:
878878

@@ -937,7 +937,7 @@ namespace cppcoro
937937
class async_generator_begin_operation final : public async_generator_advance_operation
938938
{
939939
using promise_type = async_generator_promise<T>;
940-
using handle_type = std::experimental::coroutine_handle<promise_type>;
940+
using handle_type = cppcoro::coroutine_handle<promise_type>;
941941

942942
public:
943943

@@ -988,7 +988,7 @@ namespace cppcoro
988988
{}
989989

990990
explicit async_generator(promise_type& promise) noexcept
991-
: m_coroutine(std::experimental::coroutine_handle<promise_type>::from_promise(promise))
991+
: m_coroutine(cppcoro::coroutine_handle<promise_type>::from_promise(promise))
992992
{}
993993

994994
async_generator(async_generator&& other) noexcept
@@ -1041,7 +1041,7 @@ namespace cppcoro
10411041

10421042
private:
10431043

1044-
std::experimental::coroutine_handle<promise_type> m_coroutine;
1044+
cppcoro::coroutine_handle<promise_type> m_coroutine;
10451045

10461046
};
10471047

include/cppcoro/async_manual_reset_event.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef CPPCORO_ASYNC_MANUAL_RESET_EVENT_HPP_INCLUDED
66
#define CPPCORO_ASYNC_MANUAL_RESET_EVENT_HPP_INCLUDED
77

8-
#include <experimental/coroutine>
8+
#include <cppcoro/coroutine.hpp>
99
#include <atomic>
1010
#include <cstdint>
1111

@@ -87,7 +87,7 @@ namespace cppcoro
8787
explicit async_manual_reset_event_operation(const async_manual_reset_event& event) noexcept;
8888

8989
bool await_ready() const noexcept;
90-
bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
90+
bool await_suspend(cppcoro::coroutine_handle<> awaiter) noexcept;
9191
void await_resume() const noexcept {}
9292

9393
private:
@@ -96,7 +96,7 @@ namespace cppcoro
9696

9797
const async_manual_reset_event& m_event;
9898
async_manual_reset_event_operation* m_next;
99-
std::experimental::coroutine_handle<> m_awaiter;
99+
cppcoro::coroutine_handle<> m_awaiter;
100100

101101
};
102102
}

include/cppcoro/async_mutex.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef CPPCORO_ASYNC_MUTEX_HPP_INCLUDED
66
#define CPPCORO_ASYNC_MUTEX_HPP_INCLUDED
77

8-
#include <experimental/coroutine>
8+
#include <cppcoro/coroutine.hpp>
99
#include <atomic>
1010
#include <cstdint>
1111
#include <mutex> // for std::adopt_lock_t
@@ -166,7 +166,7 @@ namespace cppcoro
166166
{}
167167

168168
bool await_ready() const noexcept { return false; }
169-
bool await_suspend(std::experimental::coroutine_handle<> awaiter) noexcept;
169+
bool await_suspend(cppcoro::coroutine_handle<> awaiter) noexcept;
170170
void await_resume() const noexcept {}
171171

172172
protected:
@@ -178,7 +178,7 @@ namespace cppcoro
178178
private:
179179

180180
async_mutex_lock_operation* m_next;
181-
std::experimental::coroutine_handle<> m_awaiter;
181+
cppcoro::coroutine_handle<> m_awaiter;
182182

183183
};
184184

include/cppcoro/async_scope.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <cppcoro/on_scope_exit.hpp>
99

1010
#include <atomic>
11-
#include <experimental/coroutine>
11+
#include <cppcoro/coroutine.hpp>
1212
#include <type_traits>
1313
#include <cassert>
1414

@@ -52,7 +52,7 @@ namespace cppcoro
5252
return m_scope->m_count.load(std::memory_order_acquire) == 0;
5353
}
5454

55-
bool await_suspend(std::experimental::coroutine_handle<> continuation) noexcept
55+
bool await_suspend(cppcoro::coroutine_handle<> continuation) noexcept
5656
{
5757
m_scope->m_continuation = continuation;
5858
return m_scope->m_count.fetch_sub(1u, std::memory_order_acq_rel) > 1u;
@@ -85,16 +85,16 @@ namespace cppcoro
8585
{
8686
struct promise_type
8787
{
88-
std::experimental::suspend_never initial_suspend() { return {}; }
89-
std::experimental::suspend_never final_suspend() { return {}; }
88+
cppcoro::suspend_never initial_suspend() { return {}; }
89+
cppcoro::suspend_never final_suspend() { return {}; }
9090
void unhandled_exception() { std::terminate(); }
9191
oneway_task get_return_object() { return {}; }
9292
void return_void() {}
9393
};
9494
};
9595

9696
std::atomic<size_t> m_count;
97-
std::experimental::coroutine_handle<> m_continuation;
97+
cppcoro::coroutine_handle<> m_continuation;
9898

9999
};
100100
}

include/cppcoro/coroutine.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef CPPCORO_COROUTINE_HPP_INCLUDED
2+
#define CPPCORO_COROUTINE_HPP_INCLUDED
3+
4+
#if __has_include(<coroutine>)
5+
6+
#include <coroutine>
7+
8+
namespace cppcoro {
9+
template<typename Promise=void>
10+
using coroutine_handle = std::coroutine_handle<Promise>;
11+
12+
using suspend_always = std::suspend_always;
13+
using suspend_never = std::suspend_never;
14+
}
15+
16+
#elif __has_include(<experimental/coroutine>)
17+
18+
#include <experimental/coroutine>
19+
20+
namespace cppcoro {
21+
template<typename Promise=void>
22+
using coroutine_handle = std::experimental::coroutine_handle<Promise>;
23+
24+
using suspend_always = std::experimental::suspend_always;
25+
using suspend_never = std::experimental::suspend_never;
26+
}
27+
28+
#else
29+
#error Cppcoro requires a C++20 compiler with coroutine support
30+
#endif
31+
32+
#endif

0 commit comments

Comments
 (0)