diff options
author | 2017-11-17 09:25:23 -0500 | |
---|---|---|
committer | 2017-11-17 16:22:56 +0000 | |
commit | 55ad77481290384038d7638ac4136ebf32a4ee2b (patch) | |
tree | c5c99f170efa149a933d0839c7ae96645e25c243 /src/core/SkScopeExit.h | |
parent | 3e4d1fde7fab46875cb70e23003b40aac262f0bc (diff) |
Revert "Revert "Add method to sk_gpu_test::TestContext to automatically restore the previous context.""
This reverts commit 1e09e461d2ffcf8b07242cfe93dd7d12c4d75866.
Change-Id: I95d5544a7baaa078536790493ce4119816a77e94
Reviewed-on: https://skia-review.googlesource.com/72903
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'src/core/SkScopeExit.h')
-rw-r--r-- | src/core/SkScopeExit.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/core/SkScopeExit.h b/src/core/SkScopeExit.h new file mode 100644 index 0000000000..95804e637a --- /dev/null +++ b/src/core/SkScopeExit.h @@ -0,0 +1,53 @@ +/* + * 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" + +/** SkScopeExit calls a std:::function<void()> in its destructor. */ +class SkScopeExit { +public: + SkScopeExit(std::function<void()> f) : fFn(std::move(f)) {} + SkScopeExit(SkScopeExit&& that) : fFn(std::move(that.fFn)) {} + + ~SkScopeExit() { + if (fFn) { + fFn(); + } + } + + SkScopeExit& operator=(SkScopeExit&& that) { + fFn = std::move(that.fFn); + return *this; + } + +private: + std::function<void()> fFn; + + SkScopeExit( const SkScopeExit& ) = delete; + SkScopeExit& operator=(const SkScopeExit& ) = delete; +}; + +/** + * 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); + * } + */ +#define SK_AT_SCOPE_EXIT(stmt) \ + SkScopeExit SK_MACRO_APPEND_LINE(at_scope_exit_)([&]() { stmt; }) + +#endif // SkScopeExit_DEFINED |