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