1 //===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the make_scope_exit function, which executes user-defined
11 // cleanup logic at scope exit.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_SCOPE_EXIT_H
16 #define LLVM_ADT_SCOPE_EXIT_H
18 #include "llvm/Support/Compiler.h"
20 #include <type_traits>
26 template <typename Callable> class scope_exit {
27 Callable ExitFunction;
28 bool Engaged = true; // False once moved-from or release()d.
31 template <typename Fp>
32 explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
34 scope_exit(scope_exit &&Rhs)
35 : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
38 scope_exit(const scope_exit &) = delete;
39 scope_exit &operator=(scope_exit &&) = delete;
40 scope_exit &operator=(const scope_exit &) = delete;
42 void release() { Engaged = false; }
50 } // end namespace detail
52 // Keeps the callable object that is passed in, and execute it at the
53 // destruction of the returned object (usually at the scope exit where the
54 // returned object is kept).
56 // Interface is specified by p0052r2.
57 template <typename Callable>
58 LLVM_NODISCARD detail::scope_exit<typename std::decay<Callable>::type>
59 make_scope_exit(Callable &&F) {
60 return detail::scope_exit<typename std::decay<Callable>::type>(
61 std::forward<Callable>(F));
64 } // end namespace llvm