rcpputils
master
C++ API providing common utilities and data structures.
Main Page
Related Pages
Namespaces
Namespace List
Namespace Members
All
a
c
e
f
g
i
j
m
o
p
r
s
t
Functions
a
c
e
f
g
i
j
m
o
p
r
s
t
Enumerations
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
c
e
f
g
h
i
o
p
s
t
u
v
w
~
Functions
a
c
e
f
g
h
i
o
p
s
u
w
~
Variables
Typedefs
Files
File List
File Members
All
Functions
Macros
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
include
rcpputils
scope_exit.hpp
Go to the documentation of this file.
1
// Copyright 2015-2020 Open Source Robotics Foundation, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef RCPPUTILS__SCOPE_EXIT_HPP_
16
#define RCPPUTILS__SCOPE_EXIT_HPP_
17
18
#include <utility>
19
20
#include "rcutils/macros.h"
21
22
namespace
rcpputils
23
{
24
25
template
<
typename
CallableT>
26
struct
scope_exit
final
27
{
28
explicit
scope_exit
(CallableT && callable)
29
: callable_(
std
::forward<CallableT>(callable))
30
{
31
}
32
33
scope_exit
(
const
scope_exit
&) =
delete
;
34
scope_exit
(
scope_exit
&&) =
default
;
35
36
scope_exit
&
operator=
(
const
scope_exit
&) =
delete
;
37
scope_exit
&
operator=
(
scope_exit
&&) =
default
;
38
39
~scope_exit
()
40
{
41
if
(!cancelled_) {
42
callable_();
43
}
44
}
45
46
void
cancel
()
47
{
48
cancelled_ =
true
;
49
}
50
51
private
:
52
CallableT callable_;
53
bool
cancelled_{
false
};
54
};
55
56
template
<
typename
CallableT>
57
scope_exit<CallableT>
58
make_scope_exit
(CallableT && callable)
59
{
60
return
scope_exit<CallableT>
(std::forward<CallableT>(callable));
61
}
62
63
}
// namespace rcpputils
64
65
#define RCPPUTILS_SCOPE_EXIT(code) \
66
auto RCUTILS_JOIN(scope_exit_, __LINE__) = rcpputils::make_scope_exit([&]() {code;})
67
68
#endif // RCPPUTILS__SCOPE_EXIT_HPP_
rcpputils::make_scope_exit
scope_exit< CallableT > make_scope_exit(CallableT &&callable)
Definition:
scope_exit.hpp:58
rcpputils
Definition:
asserts.hpp:37
rcpputils::scope_exit::cancel
void cancel()
Definition:
scope_exit.hpp:46
rcpputils::scope_exit::~scope_exit
~scope_exit()
Definition:
scope_exit.hpp:39
rcpputils::scope_exit::scope_exit
scope_exit(CallableT &&callable)
Definition:
scope_exit.hpp:28
std
rcpputils::scope_exit::operator=
scope_exit & operator=(const scope_exit &)=delete
rcpputils::scope_exit
Definition:
scope_exit.hpp:26
Generated by
1.8.17