/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef PRINT_MAP_SAFE_H #define PRINT_MAP_SAFE_H #include #include #include namespace OHOS { namespace Print { template class PrintMapSafe { public: bool Insert(const std::string &key, const T &value) { if (key.empty()) { return false; } std::lock_guard lock(mapMutex); printMap.insert(std::make_pair(key, std::make_shared(value))); return true; } bool Insert(const std::string &key, std::shared_ptr value) { if (key.empty()) { return false; } std::lock_guard lock(mapMutex); printMap.insert(std::make_pair(key, value)); return true; } bool Remove(const std::string &key) { std::lock_guard lock(mapMutex); auto it = printMap.find(key); if (it == printMap.end()) { return false; } printMap.erase(it); return true; } void Clear() { std::lock_guard lock(mapMutex); printMap.clear(); } std::shared_ptr Find(const std::string &key) { std::lock_guard lock(mapMutex); auto it = printMap.find(key); if (it == printMap.end()) { return nullptr; } return it->second; } std::string FindKey(std::function comp) { if (comp == nullptr) { return ""; } std::lock_guard lock(mapMutex); for (const auto &pair : printMap) { if (pair.second == nullptr) { continue; } if (comp(*(pair.second))) { return pair.first; } } return ""; } size_t Count() { std::lock_guard lock(mapMutex); return printMap.size(); } std::vector GetKeyList() { std::vector list; std::lock_guard lock(mapMutex); for (const auto &pair : printMap) { if (pair.second == nullptr) { continue; } list.push_back(pair.first); } return list; } private: std::mutex mapMutex; std::map> printMap; }; } // namespace Print } // namespace OHOS #endif // PRINT_MAP_SAFE_H