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 {
42 public:
ContainerScope(int32_t id)43     explicit ContainerScope(int32_t id)
44     {
45         UpdateCurrent(id);
46     }
47 
~ContainerScope()48     ~ContainerScope()
49     {
50         UpdateCurrent(restoreId_);
51     }
52 
53     static int32_t CurrentId();
54     static int32_t DefaultId();
55     static int32_t SingletonId();
56     static int32_t RecentActiveId();
57     static int32_t RecentForegroundId();
58     static std::pair<int32_t, InstanceIdGenReason> CurrentIdWithReason();
59 
60     static void Add(int32_t id);
61     static void Remove(int32_t id);
62     static void RemoveAndCheck(int32_t id);
63 
64     static uint32_t ContainerCount();
65 
66     static void UpdateCurrent(int32_t id);
67     static void UpdateSingleton(int32_t id);
68     static void UpdateRecentActive(int32_t id);
69     static void UpdateRecentForeground(int32_t id);
70 private:
71     int32_t restoreId_ = CurrentId();
72 
73     ACE_DISALLOW_COPY_AND_MOVE(ContainerScope);
74 };
75 
76 } // namespace OHOS::Ace
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CONTAINER_SCOPE_H
79