summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Yuri Kunde Schlesner2014-11-02 17:34:14 -0200
committerGravatar Yuri Kunde Schlesner2014-12-13 01:59:51 -0200
commit04b1f2936c606313ee28b505ec7cdcb81875cd8d (patch)
tree2db415f3a63a9ff297a09e29258d1982dc34350d /src
parentdoxygen: Enable EXTRACT_ALL so that Doxygen identifies namespaces (diff)
downloadyuzu-04b1f2936c606313ee28b505ec7cdcb81875cd8d.tar.gz
yuzu-04b1f2936c606313ee28b505ec7cdcb81875cd8d.tar.xz
yuzu-04b1f2936c606313ee28b505ec7cdcb81875cd8d.zip
Add SCOPE_EXIT macro to conveniently execute cleanup actions
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/scope_exit.h37
2 files changed, 38 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 9d5a90762..1dbc5db21 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -51,6 +51,7 @@ set(HEADERS
51 msg_handler.h 51 msg_handler.h
52 platform.h 52 platform.h
53 scm_rev.h 53 scm_rev.h
54 scope_exit.h
54 string_util.h 55 string_util.h
55 swap.h 56 swap.h
56 symbols.h 57 symbols.h
diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h
new file mode 100644
index 000000000..1d3e59319
--- /dev/null
+++ b/src/common/scope_exit.h
@@ -0,0 +1,37 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#pragma once
6
7namespace detail {
8 template <typename Func>
9 struct ScopeExitHelper {
10 explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {}
11 ~ScopeExitHelper() { func(); }
12
13 Func func;
14 };
15
16 template <typename Func>
17 ScopeExitHelper<Func> ScopeExit(Func&& func) { return ScopeExitHelper<Func>(std::move(func)); }
18}
19
20/**
21 * This macro allows you to conveniently specify a block of code that will run on scope exit. Handy
22 * for doing ad-hoc clean-up tasks in a function with multiple returns.
23 *
24 * Example usage:
25 * \code
26 * const int saved_val = g_foo;
27 * g_foo = 55;
28 * SCOPE_EXIT({ g_foo = saved_val; });
29 *
30 * if (Bar()) {
31 * return 0;
32 * } else {
33 * return 20;
34 * }
35 * \endcode
36 */
37#define SCOPE_EXIT(body) auto scope_exit_helper_##__LINE__ = detail::ScopeExit([&]() body)