1 /*
2  * Copyright (c) 2022 NVIDIA Corporation
3  *
4  * Licensed under the Apache License Version 2.0 with LLVM Exceptions
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  *   https://llvm.org/LICENSE.txt
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <sdbusplus/async/stdexec/__detail/__meta.hpp>
19 
20 namespace exec
21 {
22 template <stdexec::__nothrow_callable _Fn>
23 struct scope_guard
24 {
25     [[no_unique_address]] _Fn __fn_;
26     [[no_unique_address]] stdexec::__immovable __hidden_{};
27     bool __dismissed_{false};
28 
29     ~scope_guard()
30     {
31         if (!__dismissed_)
32             ((_Fn &&) __fn_)();
33     }
34 
35     void dismiss() noexcept
36     {
37         __dismissed_ = true;
38     }
39 };
40 
41 template <stdexec::__nothrow_callable _Fn>
42 scope_guard(_Fn) -> scope_guard<_Fn>;
43 } // namespace exec
44