File: ScopeExit.h

package info (click to toggle)
asymptote 3.02%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 33,400 kB
  • sloc: cpp: 172,516; ansic: 69,728; python: 14,967; sh: 5,599; javascript: 4,866; lisp: 1,507; perl: 1,417; makefile: 1,028; yacc: 610; lex: 449; xml: 182; asm: 8
file content (69 lines) | stat: -rw-r--r-- 1,953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//===- llvm/ADT/ScopeExit.h - Execute code at scope exit --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the make_scope_exit function, which executes user-defined
// cleanup logic at scope exit.
//
//===----------------------------------------------------------------------===//
#pragma once
#include <type_traits>
#include <utility>

namespace lsp
{
namespace detail
{

    template<typename Callable>
    class scope_exit
    {
        Callable ExitFunction;
        bool Engaged = true; // False once moved-from or release()d.

    public:
        template<typename Fp>
        explicit scope_exit(Fp&& F) : ExitFunction(std::forward<Fp>(F))
        {
        }

        scope_exit(scope_exit&& Rhs) : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged)
        {
            Rhs.release();
        }
        scope_exit(scope_exit const&) = delete;
        scope_exit& operator=(scope_exit&&) = delete;
        scope_exit& operator=(scope_exit const&) = delete;

        void release()
        {
            Engaged = false;
        }

        ~scope_exit()
        {
            if (Engaged)
            {
                ExitFunction();
            }
        }
    };

} // end namespace detail

// Keeps the callable object that is passed in, and execute it at the
// destruction of the returned object (usually at the scope exit where the
// returned object is kept).
//
// Interface is specified by p0052r2.
template<typename Callable>
detail::scope_exit<typename std::decay<Callable>::type> make_scope_exit(Callable&& F)
{
    return detail::scope_exit<typename std::decay<Callable>::type>(std::forward<Callable>(F));
}

} // end namespace lsp