The Battle for Wesnoth  1.15.2+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 "global.hpp"
17 
18 #include <functional>
19 
20 namespace utils {
21 
22 class scope_exit {
23  //TODO: with c++17 we could make this a template class with 'F f_' instead of 'std::function<void ()> f_';
24  std::function<void ()> f_;
25 public:
26  template<typename F>
27  explicit scope_exit(F&& f) : f_(f) {}
28  ~scope_exit() { if(f_) { f_(); }}
29 };
30 } // namespace utils
#define f
std::function< void()> f_
Definition: scope_exit.hpp:24