1fa7767c5Sopenharmony_ci/* 2fa7767c5Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License. 5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at 6fa7767c5Sopenharmony_ci * 7fa7767c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fa7767c5Sopenharmony_ci * 9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and 13fa7767c5Sopenharmony_ci * limitations under the License. 14fa7767c5Sopenharmony_ci */ 15fa7767c5Sopenharmony_ci 16fa7767c5Sopenharmony_ci#ifndef SCOPE_GUARD_H 17fa7767c5Sopenharmony_ci#define SCOPE_GUARD_H 18fa7767c5Sopenharmony_ci 19fa7767c5Sopenharmony_ci#include <functional> 20fa7767c5Sopenharmony_ci 21fa7767c5Sopenharmony_cinamespace OHOS { 22fa7767c5Sopenharmony_ci/* 23fa7767c5Sopenharmony_ci * The RAII feature is used to implement scope exit protection so that resources 24fa7767c5Sopenharmony_ci * can be released and the closure action can be performed in a unified manner when 25fa7767c5Sopenharmony_ci * the scope exits. 26fa7767c5Sopenharmony_ci */ 27fa7767c5Sopenharmony_cinamespace Detail { 28fa7767c5Sopenharmony_citemplate <typename ExitAction> 29fa7767c5Sopenharmony_ciclass ScopeGuard { 30fa7767c5Sopenharmony_cipublic: 31fa7767c5Sopenharmony_ci explicit ScopeGuard(ExitAction &&action) : action_(action), enable_(true) {} 32fa7767c5Sopenharmony_ci ~ScopeGuard() 33fa7767c5Sopenharmony_ci { 34fa7767c5Sopenharmony_ci if (enable_) { 35fa7767c5Sopenharmony_ci action_(); 36fa7767c5Sopenharmony_ci } 37fa7767c5Sopenharmony_ci } 38fa7767c5Sopenharmony_ci 39fa7767c5Sopenharmony_ci void Disable() 40fa7767c5Sopenharmony_ci { 41fa7767c5Sopenharmony_ci enable_ = false; 42fa7767c5Sopenharmony_ci } 43fa7767c5Sopenharmony_ci 44fa7767c5Sopenharmony_ciprivate: 45fa7767c5Sopenharmony_ci ExitAction action_; 46fa7767c5Sopenharmony_ci bool enable_; 47fa7767c5Sopenharmony_ci}; 48fa7767c5Sopenharmony_ci 49fa7767c5Sopenharmony_cistruct ScopeExitGuardHelper {}; 50fa7767c5Sopenharmony_citemplate <typename ExitAction> 51fa7767c5Sopenharmony_cistatic inline ScopeGuard<ExitAction> operator+(ScopeExitGuardHelper, ExitAction &&action) 52fa7767c5Sopenharmony_ci{ 53fa7767c5Sopenharmony_ci return ScopeGuard<ExitAction>(std::forward<ExitAction>(action)); 54fa7767c5Sopenharmony_ci} 55fa7767c5Sopenharmony_ci} // namespace Detail 56fa7767c5Sopenharmony_ci 57fa7767c5Sopenharmony_ci#define ON_SCOPE_EXIT(id) auto onScopeExitGuard##id = Detail::ScopeExitGuardHelper{} + [&] 58fa7767c5Sopenharmony_ci 59fa7767c5Sopenharmony_ci#define CANCEL_SCOPE_EXIT_GUARD(id) onScopeExitGuard##id.Disable() 60fa7767c5Sopenharmony_ci} // namespace OHOS 61fa7767c5Sopenharmony_ci#endif 62