Skip to content

Commit 4fec693

Browse files
authored
Allow beman::execution to work with exceptions and RTTI disabled (#342)
* Support -fno-exception * Support -fno-rtti * Disable some test cases under -fno-exceptions * Make the result of `type_id` in clang-cl more as expected
1 parent dd7ecf6 commit 4fec693

49 files changed

Lines changed: 338 additions & 258 deletions

Some content is hidden

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

‎examples/inspect.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace ex = beman::execution;
2323

2424
// ----------------------------------------------------------------------------
2525

26+
#ifdef __cpp_rtti
2627
namespace meta {
2728
// The code in this namespace is a fairly basic way to print types. It can
2829
// almost certainly be done better, in particular using reflection. However,
@@ -186,10 +187,12 @@ struct logger_t {
186187

187188
inline constexpr logger_t logger{};
188189
} // namespace
190+
#endif
189191

190192
// ----------------------------------------------------------------------------
191193

192194
int main() {
195+
#ifdef __cpp_rtti
193196
auto log = [](std::string_view name) {
194197
return [name](std::string_view msg) { std::cout << name << " message='" << msg << "'\n"; };
195198
};
@@ -201,4 +204,5 @@ int main() {
201204
ex::sync_wait(logger(ex::just(0, 1, 2), log("just(0, 1, 2)")));
202205
ex::sync_wait(logger(ex::just_error(0), log("just_error(0)")) | ex::upon_error([](auto) {}));
203206
ex::sync_wait(logger(ex::just_stopped(), log("just_stopped()")) | ex::upon_stopped([]() {}));
207+
#endif
204208
}

‎examples/meta.hpp‎

Lines changed: 0 additions & 124 deletions
This file was deleted.

‎examples/sender_demo.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ static_assert(ex::sender<just_sender<std::pmr::string>>);
6464
static_assert(ex::sender_in<just_sender<std::pmr::string>>);
6565

6666
int main() {
67+
#ifdef __cpp_exceptions
6768
try {
69+
#endif
6870
auto j = just_sender<std::pmr::string>{std::pmr::string("value")};
6971
auto t = std::move(j) | ex::then([](const std::pmr::string& v) { return v + " then"; });
7072
auto w = ex::when_all(std::move(t));
@@ -79,7 +81,9 @@ int main() {
7981
} else
8082
std::cout << "operation was cancelled\n";
8183
std::cout << "after start\n";
84+
#ifdef __cpp_exceptions
8285
} catch (const std::exception& ex) {
8386
std::cout << "ERROR: " << ex.what() << "\n";
8487
}
88+
#endif
8589
}

‎examples/stackoverflow.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ int main(int ac, char*[]) {
9797
for (int i{}; i < n; ++i) {
9898
std::cout << "await just=" << (co_await ex::just(i)) << "\n";
9999
}
100+
#ifdef __cpp_exceptions
100101
if constexpr (false)
101102
for (int i{}; i < n; ++i) {
102103
try {
@@ -105,6 +106,7 @@ int main(int ac, char*[]) {
105106
std::cout << "await error=" << x << "\n";
106107
}
107108
}
109+
#endif
108110
co_await ex::just_stopped();
109111
}(ac < 2 ? 3 : 30000));
110112
#endif

‎examples/stop_token.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ auto inactive(Token token) -> void {
106106
} // namespace
107107

108108
auto main() -> int {
109+
#ifdef __cpp_exceptions
109110
try {
111+
#endif
110112

111113
exec::stop_source source;
112114
::std::thread act([token = source.get_token()] { active(token); });
@@ -119,7 +121,9 @@ auto main() -> int {
119121
act.join();
120122
inact.join();
121123
print("done\n");
124+
#ifdef __cpp_exceptions
122125
} catch (const std::exception& ex) {
123126
std::cout << "ERROR: " << ex.what() << "\n";
124127
}
128+
#endif
125129
}

‎include/beman/execution/detail/allocator_aware_move.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ template <typename T, typename Context>
3232
* \internal
3333
*/
3434
auto allocator_aware_move(T&& obj, Context&& context) noexcept -> decltype(auto) {
35-
try {
35+
BEMAN_EXECUTION_TRY {
3636
if constexpr (requires { ::beman::execution::get_allocator(::beman::execution::get_env(context)); }) {
3737
if constexpr (decltype(::beman::execution::detail::is_product_type(obj))()) {
3838
return obj.make_from(::beman::execution::get_allocator(::beman::execution::get_env(context)),
@@ -44,7 +44,8 @@ auto allocator_aware_move(T&& obj, Context&& context) noexcept -> decltype(auto)
4444
} else {
4545
return ::std::forward<T>(obj);
4646
}
47-
} catch (...) {
47+
}
48+
BEMAN_EXECUTION_CATCH(...) {
4849
::std::terminate(); //-dk:TODO investigate if that can be avoided
4950
}
5051
}

‎include/beman/execution/detail/as_except_ptr.hpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ namespace beman::execution::detail {
2727
template <typename Error>
2828
//-dk:TODO add a proper precondition
2929
decltype(auto) as_except_ptr(Error&& error) {
30+
#ifdef __cpp_exceptions
3031
if constexpr (::std::same_as<::std::exception_ptr, ::std::decay_t<Error>>) {
3132
return ::std::forward<Error>(error);
3233
} else if constexpr (::std::same_as<::std::error_code, ::std::decay_t<Error>>) {
3334
return ::std::make_exception_ptr(::std::system_error(error));
3435
} else {
3536
return ::std::make_exception_ptr(::std::forward<Error>(error));
3637
}
38+
#else
39+
if constexpr (::std::same_as<::std::exception_ptr, ::std::decay_t<Error>>) {
40+
return ::std::forward<Error>(error);
41+
} else {
42+
::std::terminate();
43+
return ::std::exception_ptr();
44+
}
45+
#endif
3746
}
3847
} // namespace beman::execution::detail
3948

‎include/beman/execution/detail/bulk.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ struct bulk_algo_t {
179179
auto& [policy, shape, f] = state;
180180
constexpr bool nothrow =
181181
::beman::execution::detail::bulk_traits<is_chunked, Fun, Shape, Args...>::is_nothrow_invocable;
182-
try {
182+
BEMAN_EXECUTION_TRY {
183183
[&]() noexcept(nothrow) {
184184
::beman::execution::detail::bulk_traits<is_chunked, Fun, Shape, Args...>::invoke(
185185
f, shape, args...);
186186
Tag()(::std::move(rcvr), ::std::forward<Args>(args)...);
187187
}();
188-
} catch (...) {
188+
}
189+
BEMAN_EXECUTION_CATCH(...) {
189190
if constexpr (!nothrow) {
190191
::beman::execution::set_error(::std::move(rcvr), ::std::current_exception());
191192
}

‎include/beman/execution/detail/common.hpp‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,22 @@
2121
#define BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND 1
2222
#endif
2323

24+
#ifdef __cpp_exceptions
25+
#define BEMAN_EXECUTION_TRY try
26+
#define BEMAN_EXECUTION_CATCH(e) catch (e)
27+
#define BEMAN_EXECUTION_THROW(...) throw __VA_ARGS__
28+
#define BEMAN_EXECUTION_RETHROW(ep) ::std::rethrow_exception(ep)
29+
#else
30+
#define BEMAN_EXECUTION_TRY if constexpr (true)
31+
#define BEMAN_EXECUTION_CATCH(e) else
32+
#define BEMAN_EXECUTION_THROW(...) ::std::terminate()
33+
#define BEMAN_EXECUTION_RETHROW(ep) ::std::terminate()
34+
#endif
35+
2436
#define BEMAN_EXECUTION_TRY_EVAL(rcvr, expr) \
2537
do { \
26-
try { \
27-
(expr); \
28-
} catch (...) { \
38+
BEMAN_EXECUTION_TRY { (expr); } \
39+
BEMAN_EXECUTION_CATCH(...) { \
2940
if constexpr (!noexcept((expr))) { \
3041
::beman::execution::set_error(::std::move((rcvr)), ::std::current_exception()); \
3142
} \

‎include/beman/execution/detail/completion_storage.hpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ struct variant_for_signatures<::beman::execution::completion_signatures<Sigs...>
6363
template <typename Tag, typename... Args>
6464
static auto store_into_variant(type& storage, Tag t, Args&&... args) noexcept {
6565
using tuple_type = decltype(select_tuple(t, ::std::forward<Args>(args)...));
66-
try {
67-
storage.template emplace<tuple_type>(t, ::std::forward<Args>(args)...);
68-
} catch (...) {
66+
BEMAN_EXECUTION_TRY { storage.template emplace<tuple_type>(t, ::std::forward<Args>(args)...); }
67+
BEMAN_EXECUTION_CATCH(...) {
6968
if constexpr (!std::is_nothrow_constructible_v<tuple_type, Tag, Args&&...>) {
7069
store_into_variant(storage, ::beman::execution::set_error_t{}, ::std::current_exception());
7170
} else {
@@ -82,7 +81,7 @@ struct variant_for_signatures<::beman::execution::completion_signatures<Sigs...>
8281
std::visit(
8382
[&](auto&& tuple_data) {
8483
using tuple_type = ::std::remove_cvref_t<decltype(tuple_data)>;
85-
try {
84+
BEMAN_EXECUTION_TRY {
8685
if constexpr (std::is_same_v<tuple_type, std::monostate>) {
8786
::beman::execution::detail::unreachable();
8887
} else {
@@ -93,7 +92,8 @@ struct variant_for_signatures<::beman::execution::completion_signatures<Sigs...>
9392
// Make a copy, in case the tuple is destroyed by the completion call.
9493
tuple_type{::std::move(tuple_data)});
9594
}
96-
} catch (...) {
95+
}
96+
BEMAN_EXECUTION_CATCH(...) {
9797
if constexpr (!std::is_nothrow_constructible_v<tuple_type, tuple_type&&>) {
9898
::beman::execution::set_error(::std::forward<Receiver>(receiver), ::std::current_exception());
9999
} else {

0 commit comments

Comments
 (0)