-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_action.h
567 lines (529 loc) · 25.5 KB
/
scope_action.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#ifndef A2100C2E_3B3D_4875_ACA4_BFEF2E5B6120
#define A2100C2E_3B3D_4875_ACA4_BFEF2E5B6120
/**
* @file
* @brief Scope guard utilities for managing exit actions.
*
* This file provides the implementation of scope guards that execute specified actions
* when a scope is exited. The scope guards include:
* - `exit_action`: Executes an action when the scope is exited.
* - `fail_action`: Executes an action when the scope is exited due to an exception.
* - `success_action`: Executes an action when the scope is exited normally.
*
* These utilities are useful for ensuring that resources are properly released or
* actions are taken when a scope is exited, regardless of how the exit occurs.
*
* Examples:
* @snippet{trimleft} scope_action.cpp Using exit_action: runs on scope exit (success or exception)
* @snippet{trimleft} scope_action.cpp Using fail_action: runs only if an exception occurs
* @snippet{trimleft} scope_action.cpp Using success_action: runs only if no exception occurs
*
* @note Constructing these scope guards with dynamic storage duration might lead to
* unexpected behavior.
*/
#include <concepts>
#include <exception>
#include <limits>
#include <type_traits>
#include <utility>
/** @brief Library namespace. */
namespace wwa::utils {
/// @cond INTERNAL
namespace detail {
template<typename Self, typename What, typename From>
concept can_construct_from = !std::is_same_v<std::remove_cvref_t<From>, Self> && std::constructible_from<What, From>;
template<typename Self, typename What, typename From>
concept can_move_construct_from_noexcept = can_construct_from<Self, What, From> && !std::is_lvalue_reference_v<From> &&
std::is_nothrow_constructible_v<What, From>;
template<typename T>
T&& conditional_forward(T&& t, std::true_type)
{
return std::forward<T>(t);
}
template<typename T>
const T& conditional_forward(T&& t, std::false_type) // NOLINT(cppcoreguidelines-missing-std-forward)
{
return t;
}
} // namespace detail
/// @endcond
/**
* @brief A scope guard that calls its exit function on destruction, when a scope is exited.
*
* An `exit_action` may be either active (i.e., it will calls its exit function on destruction),
* or inactive (it does nothing on destruction). An `exit_action` is active after construction from an exit function.
*
* An `exit_action` becomes inactive by calling `release()` or a move constructor. An inactive `exit_action`
* may also be obtained by initializing with another inactive `exit_action`. Once an `exit_action` is inactive,
* it cannot become active again.
*
* Usage example:
* @snippet{trimleft} scope_action.cpp Using exit_action: runs on scope exit (success or exception)
*
* @tparam ExitFunc Exit function type. Func is either a
* [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible)
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) type, or an lvalue reference to a
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) or function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit
* @see https://github.com/microsoft/GSL/blob/main/docs/headers.md#user-content-H-util-final_action
* @note Constructing an `exit_action` of dynamic storage duration might lead to unexpected behavior.
* @note If the exit function stored in an `exit_action` object refers to a local variable of the function where it is
* defined (e.g., as a lambda capturing the variable by reference), and that variable is used as a return operand in
* that function, that variable might have already been returned when the `exit_action`'s destructor executes, calling
* the exit function. This can lead to surprising behavior.
*/
template<typename ExitFunc>
class [[nodiscard("The object must be used to ensure the exit function is called on scope exit.")]] exit_action {
public:
/**
* @brief Constructs a new @a exit_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object `fn`. The constructed `exit_action` is active.
* If `Func` is not an lvalue reference type, and `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`, the
* stored exit function is initialized with `std::forward<Func>(fn)`; otherwise it is initialized with `fn`. If
* initialization of the stored exit function throws an exception, calls `fn()`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, exit_action>` is `false`, and
* - `std::is_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit
*/
template<typename Func>
requires(detail::can_construct_from<exit_action, ExitFunc, Func>)
explicit exit_action(
Func&& fn
) noexcept(std::is_nothrow_constructible_v<ExitFunc, Func> || std::is_nothrow_constructible_v<ExitFunc, Func&>)
try
: m_exit_function(
detail::conditional_forward(
std::forward<Func>(fn),
std::bool_constant<
std::is_nothrow_constructible_v<ExitFunc, Func> && !std::is_lvalue_reference_v<Func>>()
)
)
{}
catch (...) {
fn();
}
/**
* @brief Constructs a new @a exit_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object `fn`. The constructed `exit_action` is active.
* The stored exit function is initialized with `std::forward<Func>(fn)`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, exit_action>` is `false`, and
* - `std::is_lvalue_reference_v<Func>` is `false`, and
* - `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit
*/
template<typename Func>
requires(detail::can_move_construct_from_noexcept<exit_action, ExitFunc, Func>)
explicit exit_action(Func&& fn) noexcept : m_exit_function(std::forward<Func>(fn))
{}
/**
* @brief Move constructor.
*
* Initializes the stored exit function with the one in `other`. The constructed `exit_action` is active
* if and only if `other` is active before the construction.
*
* If `std::is_nothrow_move_constructible_v<ExitFunc>` is true, initializes stored exit function (denoted by
* `exitfun`) with `std::forward<ExitFunc>(other.exitfun)`, otherwise initializes it with `other.exitfun`.
*
* After successful move construction, `other` becomes inactive.
*
* This overload participates in overload resolution only if:
* - `std::is_nothrow_move_constructible_v<ExitFunc>` is `true`, or
* - `std::is_copy_constructible_v<ExitFunc>` is `true`.
*
* @param other `exit_action` to move from.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/scope_exit
*/
exit_action(
exit_action&& other
) noexcept(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_nothrow_copy_constructible_v<ExitFunc>)
requires(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_copy_constructible_v<ExitFunc>)
: m_exit_function(
detail::conditional_forward(
std::forward<ExitFunc>(other.m_exit_function),
std::bool_constant<std::is_nothrow_move_constructible_v<ExitFunc>>()
)
),
m_is_armed(other.m_is_armed)
{
other.release();
}
/** @cond */
/** @brief @a exit_action is not @a CopyConstructible */
exit_action(const exit_action&) = delete;
/** @brief @a exit_action is not @a CopyAssignable */
exit_action& operator=(const exit_action&) = delete;
/** @brief @a exit_action is not @a MoveAssignable */
exit_action& operator=(exit_action&&) = delete;
/** @endcond */
/**
* @brief Calls the exit function if @a m_is_armed is active, then destroys the object.
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/%7Escope_exit
*/
~exit_action() noexcept
{
if (this->m_is_armed) {
this->m_exit_function();
}
}
/**
* @brief Makes the @a exit_action object inactive.
*
* Once an @a exit_action is inactive, it cannot become active again, and it will not call its exit function upon
* destruction.
*
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/release
*/
void release() noexcept { this->m_is_armed = false; }
private:
ExitFunc m_exit_function; ///< The stored exit function.
bool m_is_armed = true; ///< Whether this `exit_action` is active.
};
/**
* @brief Deduction guide for @a exit_action.
*
* @tparam ExitFunc Exit function type.
*/
template<typename ExitFunc>
exit_action(ExitFunc) -> exit_action<ExitFunc>;
/**
* @brief A scope guard that calls its exit function when a scope is exited via an exception.
*
* Like `exit_action`, a `fail_action` may be active or inactive. A `fail_action` is active after construction from an
* exit function.
*
* An `fail_action` becomes inactive by calling `release()` or a move constructor. An inactive `fail_action`
* may also be obtained by initializing with another inactive `fail_action`. Once an `fail_action` is inactive,
* it cannot become active again.
*
* Usage example:
* @snippet{trimleft} scope_action.cpp Using fail_action: runs only if an exception occurs
*
* @tparam ExitFunc Exit function type. Func is either a
* [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible)
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) type, or an lvalue reference to a
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) or function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail
* @note Constructing a `fail_action` of dynamic storage duration might lead to unexpected behavior.
* @note Constructing a `fail_action` from another `fail_action` created in a different thread might also lead to
* unexpected behavior since the count of uncaught exceptions obtained in different threads may be compared during
* the destruction.
*/
template<typename ExitFunc>
class [[nodiscard("The object must be used to ensure the exit function is called due to an exception.")]] fail_action {
public:
/**
* @brief Constructs a new @a fail_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object, and initializes
* the counter of uncaught exceptions as if with `std::uncaught_exceptions()`.
* The constructed `fail_action` is active.
*
* If `Func` is not an lvalue reference type, and `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`, the
* stored exit function is initialized with `std::forward<Func>(fn)`; otherwise it is initialized with `fn`. If
* initialization of the stored exit function throws an exception, calls `fn()`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, fail_action>` is `false`, and
* - `std::is_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail/scope_fail
*/
template<typename Func>
requires(detail::can_construct_from<fail_action, ExitFunc, Func>)
explicit fail_action(
Func&& fn
) noexcept(std::is_nothrow_constructible_v<ExitFunc, Func> || std::is_nothrow_constructible_v<ExitFunc, Func&>)
try
: m_exit_function(
detail::conditional_forward(
std::forward<Func>(fn),
std::bool_constant<
std::is_nothrow_constructible_v<ExitFunc, Func> && !std::is_lvalue_reference_v<Func>>()
)
)
{}
catch (...) {
fn();
}
/**
* @brief Constructs a new @a fail_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object `fn`. The constructed `fail_action` is active.
* The stored exit function is initialized with `std::forward<Func>(fn)`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, fail_action>` is `false`, and
* - `std::is_lvalue_reference_v<Func>` is `false`, and
* - `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail/scope_fail
*/
template<typename Func>
requires(detail::can_move_construct_from_noexcept<fail_action, ExitFunc, Func>)
explicit fail_action(Func&& fn) noexcept : m_exit_function(std::forward<Func>(fn))
{}
/**
* @brief Move constructor.
*
* Initializes the stored exit function with the one in `other`, and initializes the counter of
* uncaught exceptions with the one in `other`. The constructed `fail_action` is active
* if and only if `other` is active before the construction.
*
* If `std::is_nothrow_move_constructible_v<ExitFunc>` is true, initializes stored exit function (denoted by
* `exitfun`) with `std::forward<ExitFunc>(other.exitfun)`, otherwise initializes it with `other.exitfun`.
*
* After successful move construction, `other.release()` is called and `other` becomes inactive.
*
* This overload participates in overload resolution only if:
* - `std::is_nothrow_move_constructible_v<ExitFunc>` is `true`, or
* - `std::is_copy_constructible_v<ExitFunc>` is `true`.
*
* @param other `fail_action` to move from.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail/scope_fail
*/
fail_action(
fail_action&& other
) noexcept(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_nothrow_copy_constructible_v<ExitFunc>)
requires(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_copy_constructible_v<ExitFunc>)
: m_exit_function(
detail::conditional_forward(
std::forward<ExitFunc>(other.m_exit_function),
std::bool_constant<std::is_nothrow_move_constructible_v<ExitFunc>>()
)
),
m_uncaught_exceptions_count(other.m_uncaught_exceptions_count)
{
other.release();
}
/** @cond */
/** @brief @a fail_action is not @a CopyConstructible */
fail_action(const fail_action&) = delete;
/** @brief @a fail_action is not @a CopyAssignable */
fail_action& operator=(const fail_action&) = delete;
/** @brief @a fail_action is not @a MoveAssignable */
fail_action& operator=(fail_action&&) = delete;
/** @endcond */
/**
* @brief Calls the exit function if the scope is exited via an exception and destroys the object.
*
* Calls the exit function if the result of `std::uncaught_exceptions()` is greater than the counter of uncaught
* exceptions (typically on stack unwinding) and the `fail_action` is active; then destroys the object.
*
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail/%7Escope_fail
*/
~fail_action() noexcept
{
if (std::uncaught_exceptions() > this->m_uncaught_exceptions_count) {
this->m_exit_function();
}
}
/**
* @brief Makes the @a fail_action object inactive.
*
* Once an @a fail_action is inactive, it cannot become active again, and it will not call its exit function upon
* destruction.
*
* @note @a release() may be either manually called or automatically called by `fail_action`'s move constructor.
* @see https://en.cppreference.com/w/cpp/experimental/scope_fail/release
*/
void release() noexcept { this->m_uncaught_exceptions_count = std::numeric_limits<int>::max(); }
private:
ExitFunc m_exit_function; ///< The stored exit function.
int m_uncaught_exceptions_count = std::uncaught_exceptions(); ///< The counter of uncaught exceptions.
};
/**
* @brief Deduction guide for @a fail_action.
*
* @tparam ExitFunc Exit function type.
*/
template<typename ExitFunc>
fail_action(ExitFunc) -> fail_action<ExitFunc>;
/**
* @brief A scope guard that calls its exit function when a scope is exited normally.
*
* Like `exit_action`, a `success_action` may be active or inactive. A `success_action` is active after construction
* from an exit function.
*
* An `success_action` becomes inactive by calling `release()` or a move constructor. An inactive `success_action`
* may also be obtained by initializing with another inactive `success_action`. Once an `success_action` is inactive,
* it cannot become active again.
*
* Usage example:
* @snippet{trimleft} scope_action.cpp Using success_action: runs only if no exception occurs
*
* @tparam ExitFunc Exit function type. Func is either a
* [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible)
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) type, or an lvalue reference to a
* [FunctionObject](https://en.cppreference.com/w/cpp/named_req/FunctionObject) or function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_success
* @note Constructing a `success_action` of dynamic storage duration might lead to unexpected behavior.
* @note Constructing a `success_action` from another `success_action` created in a different thread might also lead to
* unexpected behavior since the count of uncaught exceptions obtained in different threads may be compared during
* the destruction.
* @note If the exit function stored in an `success_action` object refers to a local variable of the function where it
* is defined (e.g., as a lambda capturing the variable by reference), and that variable is used as a return operand in
* that function, that variable might have already been returned when the `success_action`'s destructor executes,
* calling the exit function. This can lead to surprising behavior.
*/
template<typename ExitFunc>
class [[nodiscard(
"The object must be used to ensure the exit function is called on a clean scope exit."
)]] success_action {
public:
/**
* @brief Constructs a new @a success_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object, and initializes
* the counter of uncaught exceptions as if with `std::uncaught_exceptions()`.
* The constructed `success_action` is active.
*
* If `Func` is not an lvalue reference type, and `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`, the
* stored exit function is initialized with `std::forward<Func>(fn)`; otherwise it is initialized with `fn`. If
* initialization of the stored exit function throws an exception, calls `fn()`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, success_action>` is `false`, and
* - `std::is_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_success/scope_success
*/
template<typename Func>
requires(detail::can_construct_from<success_action, ExitFunc, Func>)
explicit success_action(
Func&& fn
) noexcept(std::is_nothrow_constructible_v<ExitFunc, Func> || std::is_nothrow_constructible_v<ExitFunc, Func&>)
: m_exit_function(
detail::conditional_forward(
std::forward<Func>(fn),
std::bool_constant<
std::is_nothrow_constructible_v<ExitFunc, Func> && !std::is_lvalue_reference_v<Func>>()
)
)
{}
/**
* @brief Constructs a new @a success_action from an exit function of type @a Func.
*
* Initializes the exit function with a function or function object `fn`. The constructed `success_action` is
* active. The stored exit function is initialized with `std::forward<Func>(fn)`.
*
* This overload participates in overload resolution only if:
* - `std::is_same_v<std::remove_cvref_t<Func>, success_action>` is `false`, and
* - `std::is_lvalue_reference_v<Func>` is `false`, and
* - `std::is_nothrow_constructible_v<ExitFunc, Func>` is `true`.
*
* @tparam Func Exit function type. Must be constructible from @a ExitFunc.
* @param fn Exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_success/scope_success
*/
template<typename Func>
requires detail::can_move_construct_from_noexcept<success_action, ExitFunc, Func>
explicit success_action(Func&& fn) noexcept : m_exit_function(std::forward<Func>(fn))
{}
/**
* @brief Move constructor.
*
* Initializes the stored exit function with the one in `other`, and initializes the counter of
* uncaught exceptions with the one in `other`. The constructed `success_action` is active
* if and only if `other` is active before the construction.
*
* If `std::is_nothrow_move_constructible_v<ExitFunc>` is true, initializes stored exit function (denoted by
* `exitfun`) with `std::forward<ExitFunc>(other.exitfun)`, otherwise initializes it with `other.exitfun`.
*
* After successful move construction, `other.release()` is called and `other` becomes inactive.
*
* This overload participates in overload resolution only if:
* - `std::is_nothrow_move_constructible_v<ExitFunc>` is `true`, or
* - `std::is_copy_constructible_v<ExitFunc>` is `true`.
*
* @param other `success_action` to move from.
* @throw anything Any exception thrown during the initialization of the stored exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_success/scope_success
*/
success_action(
success_action&& other
) noexcept(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_nothrow_copy_constructible_v<ExitFunc>)
requires(std::is_nothrow_move_constructible_v<ExitFunc> || std::is_copy_constructible_v<ExitFunc>)
: m_exit_function(
detail::conditional_forward(
std::forward<ExitFunc>(other.m_exit_function),
std::bool_constant<std::is_nothrow_move_constructible_v<ExitFunc>>()
)
),
m_uncaught_exceptions_count(other.m_uncaught_exceptions_count)
{
other.release();
}
/** @cond */
/** @brief @a success_action is not @a CopyConstructible */
success_action(const success_action&) = delete;
/** @brief @a success_action is not @a CopyAssignable */
success_action& operator=(const success_action&) = delete;
/** @brief @a success_action is not @a MoveAssignable */
success_action& operator=(success_action&&) = delete;
/** @endcond */
/**
* @brief Calls the exit function when the scope is exited normally if the `success_action` is active, then destroys
* the object.
*
* Calls the exit function if the result of `std::uncaught_exceptions()` is less than or equal
* to the counter of uncaught exceptions (typically on normal exit) and the `success_action` is active,
* then destroys the object.
*
* @throws anything Throws any exception thrown by calling the exit function.
* @see https://en.cppreference.com/w/cpp/experimental/scope_success/%7Escope_success
*/
~success_action() noexcept(noexcept(this->m_exit_function()))
{
if (std::uncaught_exceptions() <= this->m_uncaught_exceptions_count) {
this->m_exit_function();
}
}
/**
* @brief Makes the @a success_action object inactive.
*
* Once an @a success_action is inactive, it cannot become active again, and it will not call its exit function upon
* destruction.
*
* @see https://en.cppreference.com/w/cpp/experimental/scope_exit/release
*/
void release() noexcept { this->m_uncaught_exceptions_count = std::numeric_limits<int>::min(); }
private:
ExitFunc m_exit_function; ///< The stored exit function.
int m_uncaught_exceptions_count = std::uncaught_exceptions(); ///< The counter of uncaught exceptions.
};
/**
* @brief Deduction guide for @a success_action.
*
* @tparam ExitFunc Exit function type.
*/
template<typename ExitFunc>
success_action(ExitFunc) -> success_action<ExitFunc>;
/**
* @example scope_action.cpp
* Example of using `exit_action`, `fail_action`, and `success_action`.
*/
} // namespace wwa::utils
#endif /* A2100C2E_3B3D_4875_ACA4_BFEF2E5B6120 */