The Battle for Wesnoth  1.15.12+dev
scope_exit.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include <functional>
17 #include <boost/preprocessor/cat.hpp>
18 
19 namespace utils {
20 
21 template<typename F>
22 class scope_exit {
23  F f_;
24 public:
25  explicit scope_exit(F&& f) : f_(std::forward<F>(f)) {}
26  ~scope_exit() { f_(); }
27 };
28 
30  template<typename F>
32  return scope_exit<F>(std::forward<F>(f));
33  }
34 };
35 } // namespace utils
36 
37 /**
38  * Run some arbitrary code (a lambda) when the current scope exits
39  * The lambda body follows this header, terminated by a semicolon.
40  * @arg ... Capture clause for the lambda.
41  */
42 #define ON_SCOPE_EXIT(...) [[maybe_unused]] const auto& BOOST_PP_CAT(scope_exit, __LINE__) = utils::scope_exit_syntax_support() << [__VA_ARGS__]()
STL namespace.
std::ostream & operator<<(std::ostream &s, const ai::attack_result &r)
Definition: actions.cpp:1130
#define f