1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2015 - TortoiseGit
5 // based on public domain implementation:
6 // https://codeproject.cachefly.net/Articles/124130/Simple-Look-Scope-Guard-for-Visual-C
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software Foundation,
20 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 explicit scope_exit_t(D
&& f
) : func(f
) {}
30 scope_exit_t(scope_exit_t
&& s
) : func(s
.func
) {}
32 ~scope_exit_t() { func(); }
35 // Prohibit construction from lvalues.
39 scope_exit_t(const scope_exit_t
&);
40 scope_exit_t
& operator=(const scope_exit_t
&);
42 // Prohibit new/delete.
43 void* operator new(size_t) = delete;
44 void* operator new[](size_t) = delete;
45 void operator delete(void*) = delete;
46 void operator delete[](void*) = delete;
51 struct scope_exit_helper
54 scope_exit_t
<D
> operator<< (D
&& f
) {
55 return scope_exit_t
<D
>(std::forward
<D
>(f
));
59 #define SCOPE_EXIT_CAT2(x, y) x##y
60 #define SCOPE_EXIT_CAT1(x, y) SCOPE_EXIT_CAT2(x, y)
61 #define SCOPE_EXIT auto SCOPE_EXIT_CAT1(scope_exit_, __COUNTER__) \
62 = scope_exit_helper() << [&]