retdec
Classes | Namespaces | Macros | Functions
scope_exit.h File Reference

Macro for performing actions when the current block exits. More...

Go to the source code of this file.

Classes

class  retdec::utils::ScopeExitGuard< Function >
 
struct  retdec::utils::ScopeExitGuardHelper
 

Namespaces

 retdec
 
 retdec::utils
 

Macros

#define SCOPE_EXIT_CONCATENATE_IMPL(s1, s2)   s1##s2
 
#define SCOPE_EXIT_CONCATENATE(s1, s2)   SCOPE_EXIT_CONCATENATE_IMPL(s1, s2)
 
#define SCOPE_EXIT_ANONYMOUS_VARIABLE    SCOPE_EXIT_CONCATENATE(SCOPE_EXIT_ANONYMOUS_VARIABLE_, __LINE__)
 
#define SCOPE_EXIT
 Macro for performing actions when the current block exits. More...
 

Functions

template<typename Function >
auto retdec::utils::operator+ (ScopeExitGuardHelper, Function &&f)
 

Detailed Description

Macro for performing actions when the current block exits.

The code is based on the following presentation: CppCon 2015: Andrei Alexandrescu: "Declarative Control Flow" (https://www.youtube.com/watch?v=WjTrfoiB0MQ).

Macro Definition Documentation

◆ SCOPE_EXIT

#define SCOPE_EXIT
Value:
retdec::utils::ScopeExitGuardHelper() + [&]()
#define SCOPE_EXIT_ANONYMOUS_VARIABLE
Definition: scope_exit.h:18

Macro for performing actions when the current block exits.

Usage:

stmt1;
stmt2;
...
};
#define SCOPE_EXIT
Macro for performing actions when the current block exits.
Definition: scope_exit.h:41

Important: Do not forget the trailing semicolon!

The above statements are executed when the current block exits, either normally or via an exception. All variables from outer blocks are automatically captured by reference.

This macro is useful for performing automatic cleanup actions, mainly when there is no RAII support.

◆ SCOPE_EXIT_ANONYMOUS_VARIABLE

#define SCOPE_EXIT_ANONYMOUS_VARIABLE    SCOPE_EXIT_CONCATENATE(SCOPE_EXIT_ANONYMOUS_VARIABLE_, __LINE__)

◆ SCOPE_EXIT_CONCATENATE

#define SCOPE_EXIT_CONCATENATE (   s1,
  s2 
)    SCOPE_EXIT_CONCATENATE_IMPL(s1, s2)

◆ SCOPE_EXIT_CONCATENATE_IMPL

#define SCOPE_EXIT_CONCATENATE_IMPL (   s1,
  s2 
)    s1##s2