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 FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H 17 #define FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H 18 19 #include <cstdint> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <securec.h> 25 26 namespace OHOS { 27 namespace Media { 28 template<typename ContentType> 29 class ImageHolderManager { 30 public: ImageHolderManager()31 ImageHolderManager() {} ~ImageHolderManager()32 ~ImageHolderManager() 33 { 34 std::lock_guard<std::mutex> guard(holderMutex_); 35 holder_.clear(); 36 } save(std::shared_ptr<ContentType> content)37 std::string save(std::shared_ptr<ContentType> content) 38 { 39 std::string id; 40 do { 41 id = genId(); 42 } while (exist(id)); 43 std::lock_guard<std::mutex> guard(holderMutex_); 44 holder_.insert(std::pair<std::string, std::shared_ptr<ContentType>>(id, content)); 45 return id; 46 } save(std::shared_ptr<ContentType> content,int64_t uniqueId)47 std::string save(std::shared_ptr<ContentType> content, int64_t uniqueId) 48 { 49 std::string id = std::to_string(uniqueId); 50 if (exist(id)) { 51 return id; 52 } 53 std::lock_guard<std::mutex> guard(holderMutex_); 54 holder_.insert(std::pair<std::string, std::shared_ptr<ContentType>>(id, content)); 55 return id; 56 } get(std::string id)57 std::shared_ptr<ContentType> get(std::string id) 58 { 59 std::lock_guard<std::mutex> guard(holderMutex_); 60 std::string localId = processEof(id); 61 auto iter = holder_.find(localId); 62 if (iter != holder_.end()) { 63 return iter->second.lock(); 64 } 65 return nullptr; 66 } pop(std::string id)67 std::shared_ptr<ContentType> pop(std::string id) 68 { 69 std::lock_guard<std::mutex> guard(holderMutex_); 70 std::string localId = processEof(id); 71 auto iter = holder_.find(localId); 72 if (iter != holder_.end()) { 73 auto res = iter->second; 74 while (holder_.count(localId)) { 75 holder_.erase(localId); 76 } 77 return res; 78 } 79 return nullptr; 80 } release(std::string id)81 void release(std::string id) 82 { 83 std::lock_guard<std::mutex> guard(holderMutex_); 84 std::string localId = processEof(id); 85 while (holder_.count(localId)) { 86 holder_.erase(localId); 87 } 88 } exist(std::string id)89 bool exist(std::string id) 90 { 91 std::lock_guard<std::mutex> guard(holderMutex_); 92 std::string localId = processEof(id); 93 return holder_.count(localId); 94 } 95 private: 96 std::map<std::string, std::weak_ptr<ContentType>> holder_; 97 std::mutex idMutex_; 98 std::mutex holderMutex_; 99 uint32_t gId_ = 0; genId()100 std::string genId() 101 { 102 std::lock_guard<std::mutex> guard(idMutex_); 103 std::string res = std::to_string(gId_); 104 gId_++; 105 return res; 106 } processEof(std::string id)107 std::string processEof(std::string id) 108 { 109 if (!id.empty() && (id.back() == '\0')) { 110 std::string tmp = std::string(id); 111 tmp.pop_back(); 112 return tmp; 113 } 114 return id; 115 } 116 }; 117 } // namespace Media 118 } // namespace OHOS 119 120 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H 121