• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
get(std::string id)47     std::shared_ptr<ContentType> get(std::string id)
48     {
49         std::lock_guard<std::mutex> guard(holderMutex_);
50         std::string localId = processEof(id);
51         auto iter = holder_.find(localId);
52         if (iter != holder_.end()) {
53             return iter->second;
54         }
55         return nullptr;
56     }
pop(std::string id)57     std::shared_ptr<ContentType> pop(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             auto res = iter->second;
64             while (holder_.count(localId)) {
65                 holder_.erase(localId);
66             }
67             return res;
68         }
69         return nullptr;
70     }
release(std::string id)71     void release(std::string id)
72     {
73         std::lock_guard<std::mutex> guard(holderMutex_);
74         std::string localId = processEof(id);
75         while (holder_.count(localId)) {
76             holder_.erase(localId);
77         }
78     }
exist(std::string id)79     bool exist(std::string id)
80     {
81         std::lock_guard<std::mutex> guard(holderMutex_);
82         std::string localId = processEof(id);
83         return holder_.count(localId);
84     }
85 private:
86     std::map<std::string, std::shared_ptr<ContentType>> holder_;
87     std::mutex idMutex_;
88     std::mutex holderMutex_;
89     uint32_t gId_ = 0;
genId()90     std::string genId()
91     {
92         std::lock_guard<std::mutex> guard(idMutex_);
93         std::string res = std::to_string(gId_);
94         gId_++;
95         return res;
96     }
processEof(std::string id)97     std::string processEof(std::string id)
98     {
99         if (!id.empty() && (id.back() == '\0')) {
100             std::string tmp = std::string(id);
101             tmp.pop_back();
102             return tmp;
103         }
104         return id;
105     }
106 };
107 } // namespace Media
108 } // namespace OHOS
109 
110 #endif // FRAMEWORKS_INNERKITSIMPL_UTILS_INCLUDE_IMAGE_HOLDER_MANAGER_H_
111