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 <
ArmarXCore/util/CPPUtility/Preprocessor/anonymous_variable.h
>
27
28
#include <utility>
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
55
namespace
armarx::detail
56
{
57
/**
58
* @brief Tag used by the macro ARMARX_ON_SCOPE_EXIT;
59
*
60
* \ingroup OnScopeExitGuards
61
*/
62
struct
ScopeGuardOnExit
{};
63
64
/**
65
* @brief Executes a given function on scope exit.
66
* Use with the macro ARMARX_ON_SCOPE_EXIT;
67
*
68
* \ingroup OnScopeExitGuards
69
*/
70
template
<
typename
FunctionType>
71
class
ScopeGuard
72
{
73
public
:
74
explicit
ScopeGuard
(
const
FunctionType& fn) : function(fn) {}
75
76
explicit
ScopeGuard
(FunctionType&& fn) : function(
std
::move(fn)) {}
77
78
~ScopeGuard
()
79
{
80
function
();
81
}
82
private
:
83
FunctionType
function
;
84
};
85
86
template
<
typename
Fun>
87
ScopeGuard<Fun>
operator+
(
ScopeGuardOnExit
, Fun&& fn)
88
{
89
return
ScopeGuard<Fun>
(std::forward<Fun>(fn));
90
}
91
}
92
93
/**
94
* @brief Executes given code when the enclosing scope is left. (later macro instantiations are executed first (order of stack unwinding))
95
*
96
* \code{.cpp}
97
* {
98
* ARMARX_ON_SCOPE_EXIT
99
* {
100
* std::cout<<"bar";
101
* }
102
* ARMARX_ON_SCOPE_EXIT
103
* {
104
* std::cout<<"foo";
105
* }
106
* }
107
* \endcode
108
* prints foobar on std::cout.
109
*
110
* \ingroup OnScopeExitGuards
111
*/
112
#define ARMARX_ON_SCOPE_EXIT auto ARMARX_ANONYMOUS_VARIABLE_WITH_PREFIX(SCOPE_EXIT_GUARD) = ::armarx::detail::ScopeGuardOnExit() + [&]()
113
armarx::detail::ScopeGuard
Executes a given function on scope exit.
Definition:
OnScopeExit.h:71
armarx::detail::operator+
ScopeGuard< Fun > operator+(ScopeGuardOnExit, Fun &&fn)
Definition:
OnScopeExit.h:87
armarx::detail::ScopeGuard::ScopeGuard
ScopeGuard(FunctionType &&fn)
Definition:
OnScopeExit.h:76
armarx::detail::ScopeGuard::ScopeGuard
ScopeGuard(const FunctionType &fn)
Definition:
OnScopeExit.h:74
armarx::detail::ScopeGuardOnExit
Tag used by the macro ARMARX_ON_SCOPE_EXIT;.
Definition:
OnScopeExit.h:62
anonymous_variable.h
armarx::detail
Definition:
ApplicationNetworkStats.cpp:34
std
Definition:
Application.h:66
armarx::detail::ScopeGuard::~ScopeGuard
~ScopeGuard()
Definition:
OnScopeExit.h:78
ArmarXCore
core
util
OnScopeExit.h
Generated on Sat Oct 12 2024 09:14:02 for armarx_documentation by
1.8.17