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