Skip to content

Commit 007e855

Browse files
committed
allow disable exception handling for use with MCU
1 parent 2bd8707 commit 007e855

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

include/ucoro/awaitable.hpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
//
1+
//
22
// Distributed under the Boost Software License, Version 1.0. (See accompanying
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
//
55

66
#pragma once
77

88
#include <concepts>
9+
#include <any>
10+
#include <cassert>
11+
#include <cstdlib>
12+
#include <memory>
13+
#include <type_traits>
14+
#include <atomic>
15+
#ifdef DISABLE_EXCEPTION
16+
#include <optional>
17+
#else
918
#include <variant>
19+
#endif
1020

1121
#if defined(__has_include)
1222

@@ -30,14 +40,7 @@ namespace std
3040

3141
#endif
3242

33-
#include <any>
34-
#include <cassert>
35-
#include <cstdlib>
36-
#include <functional>
37-
#include <memory>
38-
#include <type_traits>
39-
#include <atomic>
40-
#include <cassert>
43+
4144

4245
#if defined(DEBUG) || defined(_DEBUG)
4346
#if defined(ENABLE_DEBUG_CORO_LEAK)
@@ -225,44 +228,58 @@ namespace ucoro
225228

226229
void unhandled_exception() noexcept
227230
{
231+
#ifndef DISABLE_EXCEPTION
228232
value_.template emplace<std::exception_ptr>(std::current_exception());
233+
#endif
229234
}
230235

231236
T get_value() const
232237
{
238+
#ifndef DISABLE_EXCEPTION
233239
if (std::holds_alternative<std::exception_ptr>(value_))
234240
{
235241
std::rethrow_exception(std::get<std::exception_ptr>(value_));
236242
}
237243

238244
return std::get<T>(value_);
245+
#else
246+
return value_.value();
247+
#endif
239248
}
240-
249+
#ifndef DISABLE_EXCEPTION
241250
std::variant<std::exception_ptr, T> value_{nullptr};
251+
#else
252+
std::optional<T> value_;
253+
#endif
242254
};
243255

244256
//////////////////////////////////////////////////////////////////////////
245257
// 存储协程 promise 的返回值 void 的特化实现
246258
template<>
247259
struct awaitable_promise_value<void>
248260
{
261+
#ifndef DISABLE_EXCEPTION
249262
std::exception_ptr exception_{nullptr};
250-
263+
#endif
251264
constexpr void return_void() noexcept
252265
{
253266
}
254267

255268
void unhandled_exception() noexcept
256269
{
270+
#ifndef DISABLE_EXCEPTION
257271
exception_ = std::current_exception();
272+
#endif
258273
}
259274

260275
void get_value() const
261276
{
277+
#ifndef DISABLE_EXCEPTION
262278
if (exception_)
263279
{
264280
std::rethrow_exception(exception_);
265281
}
282+
#endif
266283
}
267284
};
268285

0 commit comments

Comments
 (0)