Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkScopeExit.h
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkScopeExit_DEFINED
9#define SkScopeExit_DEFINED
10
12
13#include <functional>
14#include <utility>
15
16/** SkScopeExit calls a std:::function<void()> in its destructor. */
18public:
19 SkScopeExit() = default;
20 SkScopeExit(std::function<void()> f) : fFn(std::move(f)) {}
21 SkScopeExit(SkScopeExit&& that) : fFn(std::move(that.fFn)) {}
22
24 if (fFn) {
25 fFn();
26 }
27 }
28
29 void clear() { fFn = {}; }
30
32 fFn = std::move(that.fFn);
33 return *this;
34 }
35
36private:
37 std::function<void()> fFn;
38
39 SkScopeExit( const SkScopeExit& ) = delete;
40 SkScopeExit& operator=(const SkScopeExit& ) = delete;
41};
42
43/**
44 * SK_AT_SCOPE_EXIT(stmt) evaluates stmt when the current scope ends.
45 *
46 * E.g.
47 * {
48 * int x = 5;
49 * {
50 * SK_AT_SCOPE_EXIT(x--);
51 * SkASSERT(x == 5);
52 * }
53 * SkASSERT(x == 4);
54 * }
55 */
56#define SK_AT_SCOPE_EXIT(stmt) \
57 SkScopeExit SK_MACRO_APPEND_LINE(at_scope_exit_)([&]() { stmt; })
58
59#endif // SkScopeExit_DEFINED
void clear()
Definition SkScopeExit.h:29
SkScopeExit()=default
SkScopeExit(SkScopeExit &&that)
Definition SkScopeExit.h:21
SkScopeExit(std::function< void()> f)
Definition SkScopeExit.h:20
SkScopeExit & operator=(SkScopeExit &&that)
Definition SkScopeExit.h:31
Definition ref_ptr.h:256