HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ScopeExit.h File Reference
#include "UT_API.h"
#include <utility>
+ Include dependency graph for UT_ScopeExit.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  UT_ScopeExit< F >
 UT_ScopeExit which runs the given lambda on destruction. More...
 

Namespaces

 UT_ScopeGuard
 

Macros

#define UT_SCOPE_EXIT_CONCAT_IMPL(x, y)   x ## y
 
#define UT_SCOPE_EXIT_CONCAT(x, y)   UT_SCOPE_EXIT_CONCAT_IMPL(x, y)
 
#define UT_SCOPE_EXIT_VAR(name)   UT_SCOPE_EXIT_CONCAT(name, __COUNTER__)
 
#define UT_SCOPE_EXIT_RUN_IMPL(func_name, scope_name,...)
 
#define UT_AT_SCOPE_EXIT(...)
 
#define UT_SCOPE_EXIT
 

Enumerations

enum  UT_ScopeGuard::OnExit
 

Functions

template<typename F >
UT_ScopeExit< F > UT_ScopeGuard::operator+ (OnExit, F &&fn)
 

Detailed Description

UT_SCOPE_EXIT/UT_AT_SCOPE_EXIT provides convenient mechanisms to do RAII without writing a special struct does cleans up upon destruction. The code within the scope exit block is executed in reverse order of declaration as the innermost enclosing scope ends. If execution never passes through a scope exit block in the first place, then it won't be executed.

Definition in file UT_ScopeExit.h.

Macro Definition Documentation

#define UT_AT_SCOPE_EXIT (   ...)
Value:
UT_SCOPE_EXIT_VAR(scope_exit_scope), \
__VA_ARGS__) \
#define UT_SCOPE_EXIT_RUN_IMPL(func_name, scope_name,...)
Definition: UT_ScopeExit.h:79
#define UT_SCOPE_EXIT_VAR(name)
Definition: UT_ScopeExit.h:74

Runs given statement within the parentheses when the scope exits.

Note
Put the semi-colon outside the parentheses. Example
FILE *fp = ::SYSfopen("foo", "r");
if (fp)
{
UT_AT_SCOPE_EXIT(::fclose(fp)); // <= NOTE: SEMI-COLON OUTSIDE
...
}

Definition at line 95 of file UT_ScopeExit.h.

#define UT_SCOPE_EXIT
Value:
auto UT_SCOPE_EXIT_VAR(scope_exit_scope) \
= UT_ScopeGuard::OnExit() + [&]() noexcept \
#define UT_SCOPE_EXIT_VAR(name)
Definition: UT_ScopeExit.h:74

Runs given statement block after it when the current scope exits.

Note
Put a semi-colon after the statement block! Example
FILE *fp = ::SYSfopen("foo", "r");
{
if (fp)
::fclose(fp);
}; // <= NOTE: SEMI-COLON ENDS THE BLOCK

Definition at line 112 of file UT_ScopeExit.h.

#define UT_SCOPE_EXIT_CONCAT (   x,
  y 
)    UT_SCOPE_EXIT_CONCAT_IMPL(x, y)

Definition at line 73 of file UT_ScopeExit.h.

#define UT_SCOPE_EXIT_CONCAT_IMPL (   x,
  y 
)    x ## y

Definition at line 72 of file UT_ScopeExit.h.

#define UT_SCOPE_EXIT_RUN_IMPL (   func_name,
  scope_name,
  ... 
)
Value:
auto func_name = [&]() { __VA_ARGS__; }; \
UT_ScopeExit<decltype(func_name)> scope_name(func_name) \

Definition at line 79 of file UT_ScopeExit.h.

#define UT_SCOPE_EXIT_VAR (   name)    UT_SCOPE_EXIT_CONCAT(name, __COUNTER__)

Definition at line 74 of file UT_ScopeExit.h.