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 #ifndef OBJECT_MGR_H
17 #define OBJECT_MGR_H
18 
19 #include <mutex>
20 #include <vector>
21 #include <unordered_map>
22 #include <memory>
23 
24 #include "text/typeface.h"
25 
26 #ifndef USE_GRAPHIC_TEXT_GINE
27 namespace rosen {
28 class FontCollection;
29 }
30 #else
31 namespace OHOS {
32 namespace Rosen {
33 class FontCollection;
34 }
35 }
36 #endif
37 
38 namespace OHOS {
39 namespace Rosen {
40 namespace Drawing {
41 class ObjectMgr {
42 public:
43     static std::shared_ptr<ObjectMgr> GetInstance() noexcept(true);
44     void AddObject(void* obj);
45     bool HasObject(void* obj);
46     bool RemoveObject(void* obj);
47     size_t ObjectCount();
48 
49 private:
50     static inline std::shared_ptr<ObjectMgr> objectMgr = nullptr;
51     std::vector<void*> vector_;
52     std::mutex mutex_;
53 };
54 
55 class FontCollectionMgr {
56 public:
57     FontCollectionMgr(const FontCollectionMgr&) = delete;
58     FontCollectionMgr& operator=(const FontCollectionMgr&) = delete;
59 
60     static FontCollectionMgr& GetInstance();
61 
62 #ifndef USE_GRAPHIC_TEXT_GINE
63     using FontCollectionType = rosen::FontCollection;
64 #else
65     using FontCollectionType = ::OHOS::Rosen::FontCollection;
66 #endif
67 
68     void Insert(void* key, std::shared_ptr<FontCollectionType> fontCollection);
69     std::shared_ptr<FontCollectionType> Find(void* key);
70     bool Remove(void* key);
71 
72 private:
FontCollectionMgr()73     FontCollectionMgr() {}
74 
75     std::unordered_map<void*, std::shared_ptr<FontCollectionType>> collections_;
76     std::mutex mutex_;
77 };
78 
79 class TypefaceMgr {
80 public:
81     TypefaceMgr(const TypefaceMgr&) = delete;
82     TypefaceMgr& operator=(const TypefaceMgr&) = delete;
83 
84     static TypefaceMgr& GetInstance();
85 
86     void Insert(void* key, std::shared_ptr<Typeface> typeface);
87     std::shared_ptr<Typeface> Find(void* key);
88     bool Remove(void* key);
89 
90 private:
TypefaceMgr()91     TypefaceMgr() {}
92 
93     std::unordered_map<void*, std::shared_ptr<Typeface>> typeface_;
94     std::mutex mutex_;
95 };
96 } // namespace Drawing
97 } // namespace Rosen
98 } // namespace OHOS
99 #endif
100