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