CPatch: New memory management
[TortoiseGit.git] / src / Utils / scope_exit_noexcept.h
blobda63d3b72cbacac4d85441a7f4f5d039b2fcc2d5
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.
23 #pragma once
25 template <typename D>
26 class scope_exit_t
28 public:
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(); }
34 private:
35 // Prohibit construction from lvalues.
36 scope_exit_t(D&);
38 // Prohibit copying.
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;
48 const D func;
51 struct scope_exit_helper
53 template <typename D>
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() << [&]