1 /* 2 * Copyright (c) 2021 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 #ifndef STORAGE_RL_MAP_H 16 #define STORAGE_RL_MAP_H 17 18 #include <map> 19 #include <nocopyable.h> 20 #include <unordered_map> 21 #include "rwlock.h" 22 23 namespace OHOS { 24 namespace StorageManager { 25 template <typename K, typename V> 26 class StorageRlMap : public NoCopyable { 27 public: StorageRlMap()28 StorageRlMap() {} ~StorageRlMap()29 ~StorageRlMap() {} 30 V& operator[](const K& key) 31 { 32 return map_[key]; 33 } Erase(const K & key)34 void Erase(const K& key) 35 { 36 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 37 map_.erase(key); 38 } Insert(const K & key,const V & value)39 bool Insert(const K& key, const V& value) 40 { 41 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 42 auto result = map_.insert(std::pair<K, V>(key, value)); 43 return result.second; 44 } Clear()45 void Clear() 46 { 47 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 48 map_.clear(); 49 } Empty()50 void Empty() 51 { 52 Utils::UniqueReadGuard<Utils::RWLock> volumeGuard(this->rl_); 53 map_.empty(); 54 } Contains(const K & key)55 bool Contains(const K& key) 56 { 57 Utils::UniqueReadGuard<Utils::RWLock> mapGuard(this->rl_); 58 auto it = map_.find(key); 59 if (it != map_.end()) { 60 return true; 61 } 62 return false; 63 } Find(const K & key)64 typename std::map<K, V>::iterator Find(const K& key) 65 { 66 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 67 return map_.find(key); 68 } Find(const K & key)69 typename std::map<K, V>::const_iterator Find(const K& key) const 70 { 71 Utils::UniqueReadGuard<Utils::RWLock> mapGuard(this->rl_); 72 return map_.find(key); 73 } Size()74 size_t Size() 75 { 76 Utils::UniqueReadGuard<Utils::RWLock> volumeGuard(this->rl_); 77 return map_.size(); 78 } Begin()79 typename std::map<K, V>::iterator Begin() 80 { 81 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 82 return map_.begin(); 83 } Begin()84 typename std::map<K, V>::const_iterator Begin() const 85 { 86 Utils::UniqueReadGuard<Utils::RWLock> mapGuard(this->rl_); 87 return map_.begin(); 88 } End()89 typename std::map<K, V>::iterator End() 90 { 91 Utils::UniqueWriteGuard<Utils::RWLock> mapGuard(this->rl_); 92 return map_.end(); 93 } End()94 typename std::map<K, V>::const_iterator End() const 95 { 96 Utils::UniqueReadGuard<Utils::RWLock> mapGuard(this->rl_); 97 return map_.end(); 98 } 99 100 private: 101 OHOS::Utils::RWLock rl_; 102 std::map<K, V> map_; 103 }; 104 } 105 } 106 #endif // STORAGE_RL_MAP_H