1 | #pragma once |
---|---|
2 | |
3 | #include <functional> |
4 | |
5 | namespace c10 { |
6 | |
7 | class RegistrationHandleRAII final { |
8 | public: |
9 | explicit RegistrationHandleRAII(std::function<void()> onDestruction) |
10 | : onDestruction_(std::move(onDestruction)) {} |
11 | |
12 | ~RegistrationHandleRAII() { |
13 | if (onDestruction_) { |
14 | onDestruction_(); |
15 | } |
16 | } |
17 | |
18 | RegistrationHandleRAII(const RegistrationHandleRAII&) = delete; |
19 | RegistrationHandleRAII& operator=(const RegistrationHandleRAII&) = delete; |
20 | |
21 | RegistrationHandleRAII(RegistrationHandleRAII&& rhs) noexcept |
22 | : onDestruction_(std::move(rhs.onDestruction_)) { |
23 | rhs.onDestruction_ = nullptr; |
24 | } |
25 | |
26 | RegistrationHandleRAII& operator=(RegistrationHandleRAII&& rhs) noexcept { |
27 | onDestruction_ = std::move(rhs.onDestruction_); |
28 | rhs.onDestruction_ = nullptr; |
29 | return *this; |
30 | } |
31 | |
32 | private: |
33 | std::function<void()> onDestruction_; |
34 | }; |
35 | |
36 | } |
37 |