OnScopeExit.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20 * @date 2016
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <utility>
27
29
30/**
31 * \page OnScopeExitGuardsDoc Execution of code when a scope is left
32 *
33 * You can execute code when the enclosing scope is left by using the macro \ref ARMARX_ON_SCOPE_EXIT.
34 * Later macro instantiations are executed first (order of c++ stack unwinding).
35 *
36 * \code{.cpp}
37 * {
38 * ARMARX_ON_SCOPE_EXIT
39 * {
40 * std::cout<<"bar";
41 * }
42 * ARMARX_ON_SCOPE_EXIT
43 * {
44 * std::cout<<"foo";
45 * }
46 * std::cout << "baz"; //prints "baz"
47 * }//prints "foo" then "bar"
48 * \endcode
49 * This code prints "bazfoobar" on std::cout. ("foobar" is printed when the scope is left).
50 *
51 * \defgroup OnScopeExitGuards
52 * \ingroup core-utility
53 */
54
55namespace armarx::detail
56{
57 /**
58 * @brief Tag used by the macro ARMARX_ON_SCOPE_EXIT;
59 *
60 * \ingroup OnScopeExitGuards
61 */
63 {
64 };
65
66 /**
67 * @brief Executes a given function on scope exit.
68 * Use with the macro ARMARX_ON_SCOPE_EXIT;
69 *
70 * \ingroup OnScopeExitGuards
71 */
72 template <typename FunctionType>
74 {
75 public:
76 explicit ScopeGuard(const FunctionType& fn) : function(fn)
77 {
78 }
79
80 explicit ScopeGuard(FunctionType&& fn) : function(std::move(fn))
81 {
82 }
83
85 {
86 function();
87 }
88
89 private:
90 FunctionType function;
91 };
92
93 template <typename Fun>
94 ScopeGuard<Fun>
96 {
97 return ScopeGuard<Fun>(std::forward<Fun>(fn));
98 }
99} // namespace armarx::detail
100
101/**
102 * @brief Executes given code when the enclosing scope is left. (later macro instantiations are executed first (order of stack unwinding))
103 *
104 * \code{.cpp}
105 * {
106 * ARMARX_ON_SCOPE_EXIT
107 * {
108 * std::cout<<"bar";
109 * }
110 * ARMARX_ON_SCOPE_EXIT
111 * {
112 * std::cout<<"foo";
113 * }
114 * }
115 * \endcode
116 * prints foobar on std::cout.
117 *
118 * \ingroup OnScopeExitGuards
119 */
120#define ARMARX_ON_SCOPE_EXIT \
121 auto ARMARX_ANONYMOUS_VARIABLE_WITH_PREFIX(SCOPE_EXIT_GUARD) = \
122 ::armarx::detail::ScopeGuardOnExit() + [&]()
Executes a given function on scope exit.
Definition OnScopeExit.h:74
ScopeGuard(FunctionType &&fn)
Definition OnScopeExit.h:80
ScopeGuard(const FunctionType &fn)
Definition OnScopeExit.h:76
ScopeGuard< Fun > operator+(ScopeGuardOnExit, Fun &&fn)
Definition OnScopeExit.h:95
Tag used by the macro ARMARX_ON_SCOPE_EXIT;.
Definition OnScopeExit.h:63