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