xref: /haiku/headers/private/shared/ScopeExit.h (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2022, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _SCOPE_EXIT_H
6 #define _SCOPE_EXIT_H
7 
8 
9 #if __cplusplus < 201103L
10 #error This file requires compiler support for the C++11 standard.
11 #endif
12 
13 
14 #include <utility>
15 
16 
17 template<typename F>
18 class ScopeExit
19 {
20 public:
21 	explicit ScopeExit(F&& fn) : fFn(fn)
22 	{
23 	}
24 
25 	~ScopeExit()
26 	{
27 		fFn();
28 	}
29 
30 	ScopeExit(ScopeExit&& other) : fFn(std::move(other.fFn))
31 	{
32 	}
33 
34 private:
35 	ScopeExit(const ScopeExit&);
36 	ScopeExit& operator=(const ScopeExit&);
37 
38 private:
39 	F fFn;
40 };
41 
42 
43 #endif // _SCOPE_EXIT_H
44