Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:07

0001 //===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file defines the make_scope_exit function, which executes user-defined
0011 /// cleanup logic at scope exit.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_ADT_SCOPEEXIT_H
0016 #define LLVM_ADT_SCOPEEXIT_H
0017 
0018 #include "llvm/Support/Compiler.h"
0019 
0020 #include <type_traits>
0021 #include <utility>
0022 
0023 namespace llvm {
0024 namespace detail {
0025 
0026 template <typename Callable> class scope_exit {
0027   Callable ExitFunction;
0028   bool Engaged = true; // False once moved-from or release()d.
0029 
0030 public:
0031   template <typename Fp>
0032   explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
0033 
0034   scope_exit(scope_exit &&Rhs)
0035       : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
0036     Rhs.release();
0037   }
0038   scope_exit(const scope_exit &) = delete;
0039   scope_exit &operator=(scope_exit &&) = delete;
0040   scope_exit &operator=(const scope_exit &) = delete;
0041 
0042   void release() { Engaged = false; }
0043 
0044   ~scope_exit() {
0045     if (Engaged)
0046       ExitFunction();
0047   }
0048 };
0049 
0050 } // end namespace detail
0051 
0052 // Keeps the callable object that is passed in, and execute it at the
0053 // destruction of the returned object (usually at the scope exit where the
0054 // returned object is kept).
0055 //
0056 // Interface is specified by p0052r2.
0057 template <typename Callable>
0058 [[nodiscard]] detail::scope_exit<std::decay_t<Callable>>
0059 make_scope_exit(Callable &&F) {
0060   return detail::scope_exit<std::decay_t<Callable>>(std::forward<Callable>(F));
0061 }
0062 
0063 } // end namespace llvm
0064 
0065 #endif