xref: /aosp_15_r20/external/pytorch/aten/src/ATen/core/dispatch/RegistrationHandleRAII.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #pragma once
2 
3 #include <functional>
4 
5 namespace c10 {
6 
7 class RegistrationHandleRAII final {
8 public:
RegistrationHandleRAII(std::function<void ()> onDestruction)9   explicit RegistrationHandleRAII(std::function<void()> onDestruction)
10       : onDestruction_(std::move(onDestruction)) {}
11 
~RegistrationHandleRAII()12   ~RegistrationHandleRAII() {
13     if (onDestruction_) {
14       onDestruction_();
15     }
16   }
17 
18   RegistrationHandleRAII(const RegistrationHandleRAII&) = delete;
19   RegistrationHandleRAII& operator=(const RegistrationHandleRAII&) = delete;
20 
RegistrationHandleRAII(RegistrationHandleRAII && rhs)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