1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_SCOPE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_SCOPE_H 18 19 #include <functional> 20 #include <shared_mutex> 21 #include <stdint.h> 22 23 #include "base/utils/macros.h" 24 #include "base/utils/noncopyable.h" 25 26 namespace OHOS::Ace { 27 28 class ACE_EXPORT ContainerScope { 29 public: ContainerScope(int32_t id)30 explicit ContainerScope(int32_t id) 31 { 32 restoreId_ = ContainerScope::CurrentId(); 33 ContainerScope::UpdateCurrent(id); 34 } 35 ~ContainerScope()36 ~ContainerScope() 37 { 38 ContainerScope::UpdateCurrent(restoreId_); 39 } 40 41 static int32_t CurrentId(); 42 43 static void UpdateCurrent(int32_t id); 44 45 static void SetScopeNotify(std::function<void(int32_t)>&& notify); 46 47 private: 48 static thread_local int32_t currentId_; 49 int32_t restoreId_ = -1; 50 static std::function<void(int32_t)> updateScopeNotify_; 51 static std::shared_mutex scopeLock_; 52 53 ACE_DISALLOW_COPY_AND_MOVE(ContainerScope); 54 }; 55 56 } // namespace OHOS::Ace 57 58 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_SCOPE_H 59