-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_scope
More file actions
52 lines (40 loc) · 1.17 KB
/
Copy pathlib_scope
File metadata and controls
52 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* File: /cpp/lib_scope
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__cpp__lib_scope
#define CPPLIB__cpp__lib_scope
#include <utility>
/////////////////////////////////// scope_exit /////////////////////////////////////////////
namespace cpp {
/// @see https://en.cppreference.com/w/cpp/experimental/scope_exit
/// @todo Satisfy all the requirements.
template< class EF >
class scope_exit {
public:
scope_exit( const scope_exit & ) = delete;
scope_exit & operator = ( const scope_exit & ) = delete;
scope_exit & operator = ( scope_exit && ) = delete;
constexpr explicit scope_exit( EF && fn_ ) noexcept
: fn{ ::std::move( fn_ ) }
, active{ true }
{}
constexpr scope_exit( scope_exit && other ) noexcept
: fn{ ::std::move( other.fn ) }
, active{ ::std::move( other.active ) }
{}
constexpr ~scope_exit() noexcept {
if ( active ) fn();
}
constexpr void release() noexcept {
active = false;
}
private:
EF fn;
bool active;
};
} // namespace cpp
#endif // CPPLIB__cpp__lib_scope