LLVM 20.0.0git
ScopeExit.h
Go to the documentation of this file.
1//===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
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_SCOPEEXIT_H
16#define LLVM_ADT_SCOPEEXIT_H
17
19
20#include <type_traits>
21#include <utility>
22
23namespace llvm {
24namespace detail {
25
26template <typename Callable> class scope_exit {
27 Callable ExitFunction;
28 bool Engaged = true; // False once moved-from or release()d.
29
30public:
31 template <typename Fp>
32 explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
33
35 : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
36 Rhs.release();
37 }
38 scope_exit(const scope_exit &) = delete;
40 scope_exit &operator=(const scope_exit &) = delete;
41
42 void release() { Engaged = false; }
43
45 if (Engaged)
46 ExitFunction();
47 }
48};
49
50} // end namespace detail
51
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).
55//
56// Interface is specified by p0052r2.
57template <typename Callable>
58[[nodiscard]] detail::scope_exit<std::decay_t<Callable>>
59make_scope_exit(Callable &&F) {
60 return detail::scope_exit<std::decay_t<Callable>>(std::forward<Callable>(F));
61}
62
63} // end namespace llvm
64
65#endif
#define F(x, y, z)
Definition: MD5.cpp:55
scope_exit & operator=(scope_exit &&)=delete
scope_exit(const scope_exit &)=delete
scope_exit & operator=(const scope_exit &)=delete
scope_exit(scope_exit &&Rhs)
Definition: ScopeExit.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858