blob: 5b7bcdc07686cb334417659cb0d312dcf36a9c53 (
plain)
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
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkScopeExit_DEFINED
#define SkScopeExit_DEFINED
#include "SkTypes.h"
/**
* SK_AT_SCOPE_EXIT(stmt) evaluates stmt when the current scope ends.
*
* E.g.
* {
* int x = 5;
* {
* SK_AT_SCOPE_EXIT(x--);
* SkASSERT(x == 5);
* }
* SkASSERT(x == 4);
* }
*/
template <typename Fn>
class SkScopeExit {
public:
SkScopeExit(Fn f) : fFn(std::move(f)) {}
~SkScopeExit() { fFn(); }
private:
Fn fFn;
SkScopeExit( const SkScopeExit& ) = delete;
SkScopeExit& operator=(const SkScopeExit& ) = delete;
SkScopeExit( SkScopeExit&&) = delete;
SkScopeExit& operator=( SkScopeExit&&) = delete;
};
template <typename Fn>
inline SkScopeExit<Fn> SkMakeScopeExit(Fn&& fn) {
return {std::move(fn)};
}
#define SK_AT_SCOPE_EXIT(stmt) \
SK_UNUSED auto&& SK_MACRO_APPEND_LINE(at_scope_exit_) = \
SkMakeScopeExit([&]() { stmt; });
#endif // SkScopeExit_DEFINED
|