Skip to content

Commit af79a8c

Browse files
authored
replace try/catch with improved portability macros (#1555)
1 parent f5ba8b5 commit af79a8c

49 files changed

Lines changed: 377 additions & 288 deletions

Some content is hidden

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

.clang-format

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ EmptyLineAfterAccessModifier: Never
7171
EmptyLineBeforeAccessModifier: Leave
7272
ExperimentalAutoDetectBinPacking: true
7373
FixNamespaceComments: true
74+
IfMacros: [
75+
STDEXEC_CATCH'
76+
]
7477
IncludeBlocks: Preserve
7578
IndentAccessModifiers: false
7679
IndentCaseBlocks: false
@@ -91,8 +94,6 @@ Macros: [
9194
'STDEXEC_MISSING_MEMBER(X,Y)=true',
9295
'STDEXEC_DEFINE_MEMBER(X)=void foo() {}',
9396
'STDEXEC_AUTO_RETURN(...)=->decltype(auto){ return __VA_ARGS__; }',
94-
'STDEXEC_TRY(...)=try { __VA_ARGS__ }',
95-
'STDEXEC_CATCH(...)=catch __VA_ARGS__',
9697
]
9798
MaxEmptyLinesToKeep: 2
9899
NamespaceIndentation: All

include/asioexec/completion_token.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ namespace asioexec {
194194
template <typename F>
195195
void run_(F&& f) noexcept {
196196
const frame_ frame(*this);
197-
try {
197+
STDEXEC_TRY {
198198
static_cast<F&&>(f)();
199-
} catch (...) {
199+
}
200+
STDEXEC_CATCH_ALL {
200201
STDEXEC_ASSERT(frame);
201202
// Do not overwrite the first exception encountered
202203
if (!ex_) {
@@ -255,10 +256,11 @@ namespace asioexec {
255256
if (self_->ex_) {
256257
::stdexec::set_error(static_cast<Receiver&&>(self_->r_), std::move(self_->ex_));
257258
} else {
258-
try {
259+
STDEXEC_TRY {
259260
completion_token::set_value<Signatures>(
260261
static_cast<Receiver&&>(self_->r_), static_cast<Args&&>(args)...);
261-
} catch (...) {
262+
}
263+
STDEXEC_CATCH_ALL {
262264
::stdexec::set_error(static_cast<Receiver&&>(self_->r_), std::current_exception());
263265
}
264266
}
@@ -299,7 +301,7 @@ namespace asioexec {
299301

300302
void start() & noexcept {
301303
const typename base_::frame_ frame(*this);
302-
try {
304+
STDEXEC_TRY {
303305
std::apply(
304306
[&](auto&&... args) {
305307
std::invoke(
@@ -308,7 +310,8 @@ namespace asioexec {
308310
static_cast<decltype(args)&&>(args)...);
309311
},
310312
std::move(args_));
311-
} catch (...) {
313+
}
314+
STDEXEC_CATCH_ALL {
312315
if (!base_::ex_) {
313316
base_::ex_ = std::current_exception();
314317
}

include/asioexec/use_sender.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ namespace asioexec {
3939
std::exception_ptr to_exception_ptr(T t) noexcept {
4040
using exception_type =
4141
std::conditional_t<std::is_same_v<T, std::error_code>, std::system_error, system_error>;
42-
try {
42+
STDEXEC_TRY {
4343
return std::make_exception_ptr(exception_type(static_cast<T&&>(t)));
44-
} catch (...) {
44+
}
45+
STDEXEC_CATCH_ALL {
4546
return std::current_exception();
4647
}
4748
}

include/exec/__detail/__system_context_default_impl.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,13 @@ namespace exec::__system_context_default_impl {
238238

239239
public:
240240
void schedule(std::span<std::byte> __storage, receiver& __r) noexcept override {
241-
try {
241+
STDEXEC_TRY {
242242
auto __sndr = stdexec::schedule(__pool_scheduler_);
243243
auto __os =
244244
__schedule_operation_t::__construct_maybe_alloc(__storage, &__r, std::move(__sndr));
245245
__os->start();
246-
} catch (std::exception& __e) {
246+
}
247+
STDEXEC_CATCH(std::exception & __e) {
247248
__r.set_error(std::current_exception());
248249
}
249250
}
@@ -252,7 +253,7 @@ namespace exec::__system_context_default_impl {
252253
uint32_t __size,
253254
std::span<std::byte> __storage,
254255
bulk_item_receiver& __r) noexcept override {
255-
try {
256+
STDEXEC_TRY {
256257
// Determine the chunking size based on the ratio between the given size and the number of workers in our pool.
257258
// Aim at having 2 chunks per worker.
258259
uint32_t __chunk_size = (__available_parallelism_ > 0
@@ -271,7 +272,8 @@ namespace exec::__system_context_default_impl {
271272
auto __os = __schedule_bulk_chunked_operation_t::__construct_maybe_alloc(
272273
__storage, &__r, std::move(__sndr));
273274
__os->start();
274-
} catch (std::exception& __e) {
275+
}
276+
STDEXEC_CATCH(std::exception & __e) {
275277
__r.set_error(std::current_exception());
276278
}
277279
}
@@ -280,7 +282,7 @@ namespace exec::__system_context_default_impl {
280282
uint32_t __size,
281283
std::span<std::byte> __storage,
282284
bulk_item_receiver& __r) noexcept override {
283-
try {
285+
STDEXEC_TRY {
284286
auto __sndr = stdexec::bulk(
285287
stdexec::schedule(__pool_scheduler_),
286288
stdexec::par,
@@ -289,7 +291,8 @@ namespace exec::__system_context_default_impl {
289291
auto __os = __schedule_bulk_unchunked_operation_t::__construct_maybe_alloc(
290292
__storage, &__r, std::move(__sndr));
291293
__os->start();
292-
} catch (std::exception& __e) {
294+
}
295+
STDEXEC_CATCH(std::exception & __e) {
293296
__r.set_error(std::current_exception());
294297
}
295298
}

include/exec/any_sender_of.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,11 @@ namespace exec {
341341
using _Alloc = typename std::allocator_traits<_Allocator>::template rebind_alloc<_Tp>;
342342
_Alloc __alloc{__allocator_};
343343
_Tp* __pointer = std::allocator_traits<_Alloc>::allocate(__alloc, 1);
344-
try {
344+
STDEXEC_TRY {
345345
std::allocator_traits<_Alloc>::construct(
346346
__alloc, __pointer, static_cast<_As&&>(__args)...);
347-
} catch (...) {
347+
}
348+
STDEXEC_CATCH_ALL {
348349
std::allocator_traits<_Alloc>::deallocate(__alloc, __pointer, 1);
349350
throw;
350351
}
@@ -505,10 +506,11 @@ namespace exec {
505506
using _Alloc = typename std::allocator_traits<_Allocator>::template rebind_alloc<_Tp>;
506507
_Alloc __alloc{__allocator_};
507508
_Tp* __pointer = std::allocator_traits<_Alloc>::allocate(__alloc, 1);
508-
try {
509+
STDEXEC_TRY {
509510
std::allocator_traits<_Alloc>::construct(
510511
__alloc, __pointer, static_cast<_As&&>(__args)...);
511-
} catch (...) {
512+
}
513+
STDEXEC_CATCH_ALL {
512514
std::allocator_traits<_Alloc>::deallocate(__alloc, __pointer, 1);
513515
throw;
514516
}

include/exec/async_scope.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ namespace exec {
313313
typename stop_token_of_t<env_of_t<_Receiver>>::template callback_type<__forward_stopped>;
314314

315315
void __complete_() noexcept {
316-
try {
316+
STDEXEC_TRY {
317317
__forward_consumer_.reset();
318318
auto __state = std::move(__state_);
319319
STDEXEC_ASSERT(__state != nullptr);
@@ -346,7 +346,8 @@ namespace exec {
346346
},
347347
__state->__data_);
348348
}
349-
} catch (...) {
349+
}
350+
STDEXEC_CATCH_ALL {
350351

351352
stdexec::set_error(static_cast<_Receiver&&>(__rcvr_), std::current_exception());
352353
}
@@ -389,7 +390,7 @@ namespace exec {
389390
}
390391

391392
void start() & noexcept {
392-
try {
393+
STDEXEC_TRY {
393394
if (!!__state_) {
394395
std::unique_lock __guard{__state_->__mutex_};
395396
if (__state_->__data_.index() != 0) {
@@ -399,7 +400,8 @@ namespace exec {
399400
__state_->__subscribers_.push_back(this);
400401
}
401402
}
402-
} catch (...) {
403+
}
404+
STDEXEC_CATCH_ALL {
403405
stdexec::set_error(static_cast<_Receiver&&>(__rcvr_), std::current_exception());
404406
}
405407
}
@@ -545,10 +547,11 @@ namespace exec {
545547
template <class _Tag, class... _As>
546548
void __save_completion(_Tag, _As&&... __as) noexcept {
547549
auto& __state = *__state_;
548-
try {
550+
STDEXEC_TRY {
549551
using _Tuple = __decayed_std_tuple<_Tag, _As...>;
550552
__state.__data_.template emplace<_Tuple>(_Tag(), static_cast<_As&&>(__as)...);
551-
} catch (...) {
553+
}
554+
STDEXEC_CATCH_ALL {
552555
using _Tuple = std::tuple<set_error_t, std::exception_ptr>;
553556
__state.__data_.template emplace<_Tuple>(set_error_t(), std::current_exception());
554557
}

include/exec/env.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ namespace exec {
8282
_Receiver __rcvr_;
8383

8484
void start() & noexcept {
85-
try {
85+
STDEXEC_TRY {
8686
if constexpr (__callable<_Tag, env_of_t<_Receiver>>) {
8787
const auto& __env = get_env(__rcvr_);
8888
stdexec::set_value(std::move(__rcvr_), _Tag{}(__env));
8989
} else {
9090
stdexec::set_value(std::move(__rcvr_), std::move(__default_));
9191
}
92-
} catch (...) {
92+
}
93+
STDEXEC_CATCH_ALL {
9394

9495
stdexec::set_error(std::move(__rcvr_), std::current_exception());
9596
}

include/exec/finally.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ namespace exec {
9696
__visitor<_Receiver>{static_cast<_Receiver&&>(__op_->__receiver_)},
9797
static_cast<_ResultType&&>(__result));
9898
} else {
99-
try {
99+
STDEXEC_TRY {
100100
_ResultType __result = static_cast<_ResultType&&>(__op_->__result_.__get());
101101
__op_->__result_.__destroy();
102102
std::visit(
103103
__visitor<_Receiver>{static_cast<_Receiver&&>(__op_->__receiver_)},
104104
static_cast<_ResultType&&>(__result));
105-
} catch (...) {
105+
}
106+
STDEXEC_CATCH_ALL {
106107
stdexec::set_error(
107108
static_cast<_Receiver&&>(__op_->__receiver_), std::current_exception());
108109
}
@@ -158,30 +159,33 @@ namespace exec {
158159

159160
template <class... _As>
160161
void set_value(_As&&... __as) noexcept {
161-
try {
162+
STDEXEC_TRY {
162163
__op_
163164
->__store_result_and_start_next_op(stdexec::set_value, static_cast<_As&&>(__as)...);
164-
} catch (...) {
165+
}
166+
STDEXEC_CATCH_ALL {
165167
stdexec::set_error(
166168
static_cast<_Receiver&&>(__op_->__receiver_), std::current_exception());
167169
}
168170
}
169171

170172
template <class _Error>
171173
void set_error(_Error&& __err) noexcept {
172-
try {
174+
STDEXEC_TRY {
173175
__op_
174176
->__store_result_and_start_next_op(stdexec::set_error, static_cast<_Error&&>(__err));
175-
} catch (...) {
177+
}
178+
STDEXEC_CATCH_ALL {
176179
stdexec::set_error(
177180
static_cast<_Receiver&&>(__op_->__receiver_), std::current_exception());
178181
}
179182
}
180183

181184
void set_stopped() noexcept {
182-
try {
185+
STDEXEC_TRY {
183186
__op_->__store_result_and_start_next_op(stdexec::set_stopped);
184-
} catch (...) {
187+
}
188+
STDEXEC_CATCH_ALL {
185189
stdexec::set_error(
186190
static_cast<_Receiver&&>(__op_->__receiver_), std::current_exception());
187191
}

include/exec/fork_join.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ namespace exec {
135135
using _env_t = stdexec::__call_result_t<stdexec::__env::__fwd_fn, stdexec::env_of_t<Rcvr>>;
136136
using _child_completions_t = stdexec::__completion_signatures_of_t<Sndr, _env_t>;
137137
using _domain_t = stdexec::__early_domain_of_t<Sndr, stdexec::__none_such>;
138-
using _when_all_sndr_t = fork_join_t::_when_all_sndr_t<_child_completions_t, Closures, _domain_t>;
138+
using _when_all_sndr_t =
139+
fork_join_t::_when_all_sndr_t<_child_completions_t, Closures, _domain_t>;
139140
using _child_opstate_t =
140141
stdexec::connect_result_t<Sndr, stdexec::__rcvr_ref_t<_opstate_t, _env_t>>;
141142
using _fork_opstate_t =
@@ -173,10 +174,11 @@ namespace exec {
173174
template <class Tag, class... Args>
174175
STDEXEC_ATTRIBUTE(host, device)
175176
void _complete(Tag, Args&&... args) noexcept {
176-
try {
177+
STDEXEC_TRY {
177178
using _tuple_t = stdexec::__decayed_tuple<Tag, Args...>;
178179
_cache_.template emplace<_tuple_t>(Tag{}, static_cast<Args&&>(args)...);
179-
} catch (...) {
180+
}
181+
STDEXEC_CATCH_ALL {
180182
if constexpr (!stdexec::__nothrow_decay_copyable<Args...>) {
181183
using _tuple_t = stdexec::__tuple_for<stdexec::set_error_t, ::std::exception_ptr>;
182184
_cache_._results_

include/exec/into_tuple.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ namespace exec {
8181
_Args &&...__args) noexcept -> void {
8282
if constexpr (same_as<_Tag, set_value_t>) {
8383
using __tuple_t = __t<_State>;
84-
try {
84+
STDEXEC_TRY {
8585
set_value(
8686
static_cast<_Receiver &&>(__rcvr), __tuple_t{static_cast<_Args &&>(__args)...});
87-
} catch (...) {
87+
}
88+
STDEXEC_CATCH_ALL {
8889
stdexec::set_error(static_cast<_Receiver &&>(__rcvr), std::current_exception());
8990
}
9091
} else {

0 commit comments

Comments
 (0)