1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#ifndef SCOPE_GUARD_H
17049e185fSopenharmony_ci#define SCOPE_GUARD_H
18049e185fSopenharmony_ci
19049e185fSopenharmony_ci#include <functional>
20049e185fSopenharmony_ci
21049e185fSopenharmony_cinamespace OHOS {
22049e185fSopenharmony_ci/*
23049e185fSopenharmony_ci * The RAII feature is used to implement scope exit protection so that resources
24049e185fSopenharmony_ci * can be released and the closure action can be performed in a unified manner when
25049e185fSopenharmony_ci * the scope exits.
26049e185fSopenharmony_ci */
27049e185fSopenharmony_cinamespace Detail {
28049e185fSopenharmony_citemplate<typename ExitAction>
29049e185fSopenharmony_ciclass ScopeGuard {
30049e185fSopenharmony_cipublic:
31049e185fSopenharmony_ci    explicit ScopeGuard(ExitAction &&action) : action_(action), enable_(true) {}
32049e185fSopenharmony_ci    ~ScopeGuard()
33049e185fSopenharmony_ci    {
34049e185fSopenharmony_ci        if (enable_) {
35049e185fSopenharmony_ci            action_();
36049e185fSopenharmony_ci        }
37049e185fSopenharmony_ci    }
38049e185fSopenharmony_ci
39049e185fSopenharmony_ci    void Disable()
40049e185fSopenharmony_ci    {
41049e185fSopenharmony_ci        enable_ = false;
42049e185fSopenharmony_ci    }
43049e185fSopenharmony_ci
44049e185fSopenharmony_ciprivate:
45049e185fSopenharmony_ci    ExitAction action_;
46049e185fSopenharmony_ci    bool enable_;
47049e185fSopenharmony_ci};
48049e185fSopenharmony_ci
49049e185fSopenharmony_cistruct ScopeExitGuardHelper {};
50049e185fSopenharmony_citemplate<typename ExitAction>
51049e185fSopenharmony_cistatic inline ScopeGuard<ExitAction> operator+(ScopeExitGuardHelper, ExitAction &&action)
52049e185fSopenharmony_ci{
53049e185fSopenharmony_ci    return ScopeGuard<ExitAction>(std::forward<ExitAction>(action));
54049e185fSopenharmony_ci}
55049e185fSopenharmony_ci}
56049e185fSopenharmony_ci
57049e185fSopenharmony_ci#define ON_SCOPE_EXIT(id) \
58049e185fSopenharmony_ci    auto onScopeExitGuard##id = Detail::ScopeExitGuardHelper{} + [ & ]
59049e185fSopenharmony_ci
60049e185fSopenharmony_ci#define CANCEL_SCOPE_EXIT_GUARD(id) \
61049e185fSopenharmony_ci    onScopeExitGuard##id.Disable()
62049e185fSopenharmony_ci}
63049e185fSopenharmony_ci#endif
64