1 /*
2  * Copyright (c) 2023 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 #include "utils/object_mgr.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
GetInstance()21 std::shared_ptr<ObjectMgr> ObjectMgr::GetInstance() noexcept(true)
22 {
23     if (objectMgr == nullptr) {
24         static std::mutex mutex;
25         std::lock_guard<std::mutex> lock(mutex);
26         if (objectMgr == nullptr) {
27             objectMgr.reset(new ObjectMgr());
28         }
29     }
30     return objectMgr;
31 }
32 
AddObject(void * obj)33 void ObjectMgr::AddObject(void* obj)
34 {
35     std::lock_guard<std::mutex> lock(mutex_);
36     vector_.push_back(obj);
37 }
38 
HasObject(void * obj)39 bool ObjectMgr::HasObject(void* obj)
40 {
41     bool has = false;
42     std::lock_guard<std::mutex> lock(mutex_);
43     for (auto it = vector_.begin(); it != vector_.end(); it++) {
44         if (*it == obj) {
45             has = true;
46             break;
47         }
48     }
49     return has;
50 }
51 
RemoveObject(void * obj)52 bool ObjectMgr::RemoveObject(void* obj)
53 {
54     bool removed = false;
55     std::lock_guard<std::mutex> lock(mutex_);
56     for (auto it = vector_.begin(); it != vector_.end(); it++) {
57         if (*it == obj) {
58             vector_.erase(it);
59             removed = true;
60             break;
61         }
62     }
63     return removed;
64 }
65 
ObjectCount()66 size_t ObjectMgr::ObjectCount()
67 {
68     return vector_.size();
69 }
70 
GetInstance()71 FontCollectionMgr& FontCollectionMgr::GetInstance()
72 {
73     static FontCollectionMgr instance;
74     return instance;
75 }
76 
Insert(void * key,std::shared_ptr<FontCollectionType> fontCollection)77 void FontCollectionMgr::Insert(void* key, std::shared_ptr<FontCollectionType> fontCollection)
78 {
79     std::unique_lock lock(mutex_);
80     collections_.insert({key, std::move(fontCollection)});
81 }
82 
Find(void * key)83 std::shared_ptr<FontCollectionMgr::FontCollectionType> FontCollectionMgr::Find(void* key)
84 {
85     std::unique_lock lock(mutex_);
86     auto iter = collections_.find(key);
87     if (iter != collections_.end()) {
88         return iter->second;
89     } else {
90         return {nullptr};
91     }
92 }
93 
Remove(void * key)94 bool FontCollectionMgr::Remove(void* key)
95 {
96     std::unique_lock lock(mutex_);
97     auto iter = collections_.find(key);
98     if (iter != collections_.end()) {
99         collections_.erase(iter);
100         return true;
101     } else {
102         return false;
103     }
104 }
105 
GetInstance()106 TypefaceMgr& TypefaceMgr::GetInstance()
107 {
108     static TypefaceMgr instance;
109     return instance;
110 }
111 
Insert(void * key,std::shared_ptr<Typeface> typeface)112 void TypefaceMgr::Insert(void* key, std::shared_ptr<Typeface> typeface)
113 {
114     std::unique_lock lock(mutex_);
115     typeface_.insert({key, std::move(typeface)});
116 }
117 
Find(void * key)118 std::shared_ptr<Typeface> TypefaceMgr::Find(void* key)
119 {
120     std::unique_lock lock(mutex_);
121     auto iter = typeface_.find(key);
122     if (iter != typeface_.end()) {
123         return iter->second;
124     } else {
125         return {nullptr};
126     }
127 }
128 
Remove(void * key)129 bool TypefaceMgr::Remove(void* key)
130 {
131     std::unique_lock lock(mutex_);
132     auto iter = typeface_.find(key);
133     if (iter != typeface_.end()) {
134         typeface_.erase(iter);
135         return true;
136     } else {
137         return false;
138     }
139 }
140 } // namespace Drawing
141 } // namespace Rosen
142 } // namespace OHOS
143