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_CARD_SCOPE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CARD_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 constexpr uint64_t INVALID_CARD_ID = 0;
29 
30 class ACE_EXPORT CardScope {
31 public:
CardScope(uint64_t id)32     explicit CardScope(uint64_t id)
33     {
34         restoreId_ = CardScope::CurrentId();
35         CardScope::UpdateCurrent(id);
36     }
37 
~CardScope()38     ~CardScope()
39     {
40         CardScope::UpdateCurrent(restoreId_);
41     }
42 
43     static uint64_t CurrentId();
44 
45     static void UpdateCurrent(uint64_t id);
46 
47 private:
48     static thread_local uint64_t currentId_;
49     uint64_t restoreId_ = INVALID_CARD_ID;
50 
51     ACE_DISALLOW_COPY_AND_MOVE(CardScope);
52 };
53 
54 } // namespace OHOS::Ace
55 
56 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_CARD_SCOPE_H
57