retdec
scope_exit.h
Go to the documentation of this file.
1 
11 #ifndef RETDEC_UTILS_SCOPE_EXIT_H
12 #define RETDEC_UTILS_SCOPE_EXIT_H
13 
14 // The _IMPL macro is needed to force the expansion of s1 and s2.
15 #define SCOPE_EXIT_CONCATENATE_IMPL(s1, s2) s1##s2
16 #define SCOPE_EXIT_CONCATENATE(s1, s2) SCOPE_EXIT_CONCATENATE_IMPL(s1, s2)
17 
18 #define SCOPE_EXIT_ANONYMOUS_VARIABLE \
19  SCOPE_EXIT_CONCATENATE(SCOPE_EXIT_ANONYMOUS_VARIABLE_, __LINE__)
20 
41 #define SCOPE_EXIT \
42  const auto SCOPE_EXIT_ANONYMOUS_VARIABLE = \
43  retdec::utils::ScopeExitGuardHelper() + [&]()
44 
45 namespace retdec {
46 namespace utils {
47 
48 // Calls the given function in its destructor.
49 template<typename Function>
51 public:
52  ScopeExitGuard(Function &&f):
53  f(std::forward<Function>(f)) {}
54  ~ScopeExitGuard() { f(); }
55 
56 private:
57  Function f;
58 };
59 
60 // A helper type that allows the main macro to have a nice syntax.
62 
63 template<typename Function>
64 auto operator+(ScopeExitGuardHelper /* unused */, Function &&f) {
65  return ScopeExitGuard<Function>(std::forward<Function>(f));
66 }
67 
68 } // namespace utils
69 } // namespace retdec
70 
71 #endif
Definition: scope_exit.h:50
ScopeExitGuard(Function &&f)
Definition: scope_exit.h:52
Function f
Definition: scope_exit.h:57
~ScopeExitGuard()
Definition: scope_exit.h:54
auto operator+(ScopeExitGuardHelper, Function &&f)
Definition: scope_exit.h:64
Definition: archive_wrapper.h:19
Definition: scope_exit.h:61