wwa-scope-action 1.0.0
Scope guard utilities for managing exit actions in C++
scope_action.h File Reference

Scope guard utilities for managing exit actions. More...

#include <concepts>
#include <exception>
#include <limits>
#include <type_traits>
#include <utility>
+ Include dependency graph for scope_action.h:

Go to the source code of this file.

Classes

class  wwa::utils::exit_action< ExitFunc >
 A scope guard that calls its exit function on destruction, when a scope is exited. More...
 
class  wwa::utils::fail_action< ExitFunc >
 A scope guard that calls its exit function when a scope is exited via an exception. More...
 
class  wwa::utils::success_action< ExitFunc >
 A scope guard that calls its exit function when a scope is exited normally. More...
 

Namespaces

namespace  wwa
 
namespace  wwa::utils
 Library namespace.
 

Functions

template<typename ExitFunc >
 wwa::utils::exit_action (ExitFunc) -> exit_action< ExitFunc >
 Deduction guide for exit_action.
 
template<typename ExitFunc >
 wwa::utils::fail_action (ExitFunc) -> fail_action< ExitFunc >
 Deduction guide for fail_action.
 
template<typename ExitFunc >
 wwa::utils::success_action (ExitFunc) -> success_action< ExitFunc >
 Deduction guide for success_action.
 

Detailed Description

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:

try {
auto guard = wwa::utils::exit_action{[&exit_status]() { exit_status = true; }};
maybe_throw();
}
catch (...) {
did_throw = true;
}
print_exit_status("exit_action", exit_status, did_throw);
try {
auto guard = wwa::utils::fail_action{[&exit_status]() { exit_status = true; }};
maybe_throw();
}
catch (...) {
did_throw = true;
}
print_exit_status("fail_action", exit_status, did_throw);
try {
auto guard = wwa::utils::success_action{[&exit_status] { exit_status = true; }};
maybe_throw();
}
catch (...) {
did_throw = true;
}
print_exit_status("success_action", exit_status, did_throw);
Note
Constructing these scope guards with dynamic storage duration might lead to unexpected behavior.

Definition in file scope_action.h.