The Battle for Wesnoth
1.19.5+dev
utils
scope_exit.hpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2003 - 2024
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
30
struct
scope_exit_syntax_support
{
31
template
<
typename
F>
32
scope_exit<F>
operator<<
(F&&
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__]()
utils::scope_exit
Definition:
scope_exit.hpp:23
utils::scope_exit::~scope_exit
~scope_exit()
Definition:
scope_exit.hpp:27
utils::scope_exit::scope_exit
scope_exit(F &&f)
Definition:
scope_exit.hpp:26
utils::scope_exit::f_
F f_
Definition:
scope_exit.hpp:24
utils
Definition:
config_attribute_value.cpp:411
utils::scope_exit_syntax_support
Definition:
scope_exit.hpp:30
utils::scope_exit_syntax_support::operator<<
scope_exit< F > operator<<(F &&f)
Definition:
scope_exit.hpp:32
f
#define f
Generated by
1.9.1