]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ADT/ScopeExit.h
MFV r322231:
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / ADT / ScopeExit.h
1 //===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the make_scope_exit function, which executes user-defined
11 // cleanup logic at scope exit.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_SCOPE_EXIT_H
16 #define LLVM_ADT_SCOPE_EXIT_H
17
18 #include "llvm/Support/Compiler.h"
19
20 #include <type_traits>
21 #include <utility>
22
23 namespace llvm {
24 namespace detail {
25
26 template <typename Callable> class scope_exit {
27   Callable ExitFunction;
28
29 public:
30   template <typename Fp>
31   explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
32
33   scope_exit(scope_exit &&Rhs) : ExitFunction(std::move(Rhs.ExitFunction)) {}
34
35   ~scope_exit() { ExitFunction(); }
36 };
37
38 } // end namespace detail
39
40 // Keeps the callable object that is passed in, and execute it at the
41 // destruction of the returned object (usually at the scope exit where the
42 // returned object is kept).
43 //
44 // Interface is specified by p0052r2.
45 template <typename Callable>
46 LLVM_NODISCARD detail::scope_exit<typename std::decay<Callable>::type>
47 make_scope_exit(Callable &&F) {
48   return detail::scope_exit<typename std::decay<Callable>::type>(
49       std::forward<Callable>(F));
50 }
51
52 } // end namespace llvm
53
54 #endif