1 /*
2  * Copyright (c) 2024 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 PRINT_MAP_SAFE_H
17 #define PRINT_MAP_SAFE_H
18 
19 #include <map>
20 #include <mutex>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace Print {
25 template <typename T>
26 class PrintMapSafe {
27 public:
Insert(const std::string & key,const T & value)28     bool Insert(const std::string &key, const T &value)
29     {
30         if (key.empty()) {
31             return false;
32         }
33         std::lock_guard<std::mutex> lock(mapMutex);
34         printMap.insert(std::make_pair(key, std::make_shared<T>(value)));
35         return true;
36     }
Insert(const std::string & key,std::shared_ptr<T> value)37     bool Insert(const std::string &key, std::shared_ptr<T> value)
38     {
39         if (key.empty()) {
40             return false;
41         }
42         std::lock_guard<std::mutex> lock(mapMutex);
43         printMap.insert(std::make_pair(key, value));
44         return true;
45     }
Remove(const std::string & key)46     bool Remove(const std::string &key)
47     {
48         std::lock_guard<std::mutex> lock(mapMutex);
49         auto it = printMap.find(key);
50         if (it == printMap.end()) {
51             return false;
52         }
53         printMap.erase(it);
54         return true;
55     }
Clear()56     void Clear()
57     {
58         std::lock_guard<std::mutex> lock(mapMutex);
59         printMap.clear();
60     }
Find(const std::string & key)61     std::shared_ptr<T> Find(const std::string &key)
62     {
63         std::lock_guard<std::mutex> lock(mapMutex);
64         auto it = printMap.find(key);
65         if (it == printMap.end()) {
66             return nullptr;
67         }
68         return it->second;
69     }
FindKey(std::function<bool (const T &)> comp)70     std::string FindKey(std::function<bool(const T &)> comp)
71     {
72         if (comp == nullptr) {
73             return "";
74         }
75         std::lock_guard<std::mutex> lock(mapMutex);
76         for (const auto &pair : printMap) {
77             if (pair.second == nullptr) {
78                 continue;
79             }
80             if (comp(*(pair.second))) {
81                 return pair.first;
82             }
83         }
84         return "";
85     }
Count()86     size_t Count()
87     {
88         std::lock_guard<std::mutex> lock(mapMutex);
89         return printMap.size();
90     }
GetKeyList()91     std::vector<std::string> GetKeyList()
92     {
93         std::vector<std::string> list;
94         std::lock_guard<std::mutex> lock(mapMutex);
95         for (const auto &pair : printMap) {
96             if (pair.second == nullptr) {
97                 continue;
98             }
99             list.push_back(pair.first);
100         }
101         return list;
102     }
103 
104 private:
105     std::mutex mapMutex;
106     std::map<std::string, std::shared_ptr<T>> printMap;
107 };
108 }  // namespace Print
109 }  // namespace OHOS
110 #endif  // PRINT_MAP_SAFE_H
111