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 #include <set> 19 #include <array> 20 #include <atomic> 21 #include <cinttypes> 22 23 #include <mutex> 24 #include <shared_mutex> 25 26 27 #include "base/utils/macros.h" 28 #include "base/utils/noncopyable.h" 29 30 namespace OHOS::Ace { 31 32 enum class InstanceIdGenReason : uint32_t { 33 SCOPE = 0, 34 ACTIVE, 35 DEFAULT, 36 SINGLETON, 37 FOREGROUND, 38 UNDEFINED, 39 }; 40 41 class ACE_EXPORT ContainerScope final { 42 public: 43 template<typename T> 44 explicit ContainerScope(T) = delete; 45 ContainerScope(int32_t id)46 explicit ContainerScope(int32_t id) 47 { 48 UpdateCurrent(id); 49 } 50 ContainerScope(int32_t id,bool enable)51 ContainerScope(int32_t id, bool enable) 52 { 53 if (enable) { 54 UpdateCurrent(id); 55 } 56 } 57 ~ContainerScope()58 ~ContainerScope() 59 { 60 UpdateCurrent(restoreId_); 61 } 62 63 static int32_t CurrentId(); 64 static int32_t CurrentLocalId(); 65 static int32_t DefaultId(); 66 static int32_t SingletonId(); 67 static int32_t RecentActiveId(); 68 static int32_t RecentForegroundId(); 69 static std::pair<int32_t, InstanceIdGenReason> CurrentIdWithReason(); 70 71 static void Add(int32_t id); 72 static void Remove(int32_t id); 73 static void RemoveAndCheck(int32_t id); 74 75 static uint32_t ContainerCount(); 76 77 static void UpdateCurrent(int32_t id); 78 static void UpdateLocalCurrent(int32_t id); 79 static void UpdateSingleton(int32_t id); 80 static void UpdateRecentActive(int32_t id); 81 static void UpdateRecentForeground(int32_t id); 82 private: 83 int32_t restoreId_ = CurrentId(); 84 85 ACE_DISALLOW_COPY_AND_MOVE(ContainerScope); 86 }; 87 88 } // namespace OHOS::Ace 89 90 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_SCOPE_H 91