13ceed64fSopenharmony_ci/*
23ceed64fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
33ceed64fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43ceed64fSopenharmony_ci * you may not use this file except in compliance with the License.
53ceed64fSopenharmony_ci * You may obtain a copy of the License at
63ceed64fSopenharmony_ci *
73ceed64fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83ceed64fSopenharmony_ci *
93ceed64fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103ceed64fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113ceed64fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123ceed64fSopenharmony_ci * See the License for the specific language governing permissions and
133ceed64fSopenharmony_ci * limitations under the License.
143ceed64fSopenharmony_ci */
153ceed64fSopenharmony_ci#ifndef OHOS_FORM_FWK_SCOPE_GUARD_H
163ceed64fSopenharmony_ci#define OHOS_FORM_FWK_SCOPE_GUARD_H
173ceed64fSopenharmony_ci
183ceed64fSopenharmony_ci#include <string>
193ceed64fSopenharmony_ci
203ceed64fSopenharmony_cinamespace OHOS {
213ceed64fSopenharmony_cinamespace AppExecFwk {
223ceed64fSopenharmony_ciclass ScopeGuard final {
233ceed64fSopenharmony_cipublic:
243ceed64fSopenharmony_ci    using Function = std::function<void()>;
253ceed64fSopenharmony_ci    explicit ScopeGuard(Function func) : func_(func), dismissed_(false)
263ceed64fSopenharmony_ci    {}
273ceed64fSopenharmony_ci
283ceed64fSopenharmony_ci    ~ScopeGuard()
293ceed64fSopenharmony_ci    {
303ceed64fSopenharmony_ci        if (!dismissed_) {
313ceed64fSopenharmony_ci            func_();
323ceed64fSopenharmony_ci        }
333ceed64fSopenharmony_ci    }
343ceed64fSopenharmony_ci
353ceed64fSopenharmony_ci    void Dismiss()
363ceed64fSopenharmony_ci    {
373ceed64fSopenharmony_ci        dismissed_ = true;
383ceed64fSopenharmony_ci    }
393ceed64fSopenharmony_ci
403ceed64fSopenharmony_ciprivate:
413ceed64fSopenharmony_ci    Function func_;
423ceed64fSopenharmony_ci    bool dismissed_ = false;
433ceed64fSopenharmony_ci};
443ceed64fSopenharmony_ci}
453ceed64fSopenharmony_ci}
463ceed64fSopenharmony_ci#endif // OHOS_FORM_FWK_SCOPE_GUARD_H