rclcpp  beta1
C++ ROS Client Library API
scope_exit.hpp
Go to the documentation of this file.
1 // Copyright 2015 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 // Based on: http://the-witness.net/news/2012/11/scopeexit-in-c11/
16 // But I changed the lambda to include by reference rather than value, see:
17 // http://the-witness.net/news/2012/11/scopeexit-in-c11/comment-page-1/#comment-86873
18 
19 #ifndef RCLCPP__SCOPE_EXIT_HPP_
20 #define RCLCPP__SCOPE_EXIT_HPP_
21 
22 #include <functional>
23 
24 #include "rclcpp/macros.hpp"
25 
26 namespace rclcpp
27 {
28 
29 template<typename Callable>
30 struct ScopeExit
31 {
32  explicit ScopeExit(Callable callable)
33  : callable_(callable) {}
34  ~ScopeExit() {callable_(); }
35 
36 private:
37  Callable callable_;
38 };
39 
40 template<typename Callable>
42 make_scope_exit(Callable callable)
43 {
44  return ScopeExit<Callable>(callable);
45 }
46 
47 } // namespace rclcpp
48 
49 #define RCLCPP_SCOPE_EXIT(code) \
50  auto RCLCPP_STRING_JOIN(scope_exit_, __LINE__) = rclcpp::make_scope_exit([&]() {code; })
51 
52 #endif // RCLCPP__SCOPE_EXIT_HPP_
~ScopeExit()
Definition: scope_exit.hpp:34
Definition: allocator_common.hpp:24
Definition: scope_exit.hpp:30
ScopeExit(Callable callable)
Definition: scope_exit.hpp:32
ScopeExit< Callable > make_scope_exit(Callable callable)
Definition: scope_exit.hpp:42