Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_SAFE_MAP_H_ |
| 18 | #define ART_RUNTIME_SAFE_MAP_H_ |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <map> |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 21 | #include <memory> |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 22 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 23 | #include "base/logging.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular, |
| 28 | // the implicit insertion of a default-constructed value on failed lookups). |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 29 | template <typename K, typename V, typename Comparator = std::less<K>, |
| 30 | typename Allocator = std::allocator<std::pair<const K, V> > > |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 31 | class SafeMap { |
| 32 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 33 | typedef SafeMap<K, V, Comparator, Allocator> Self; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 34 | |
| 35 | public: |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 36 | typedef typename ::std::map<K, V, Comparator, Allocator>::key_compare key_compare; |
| 37 | typedef typename ::std::map<K, V, Comparator, Allocator>::allocator_type allocator_type; |
| 38 | typedef typename ::std::map<K, V, Comparator, Allocator>::iterator iterator; |
| 39 | typedef typename ::std::map<K, V, Comparator, Allocator>::const_iterator const_iterator; |
| 40 | typedef typename ::std::map<K, V, Comparator, Allocator>::size_type size_type; |
| 41 | typedef typename ::std::map<K, V, Comparator, Allocator>::value_type value_type; |
| 42 | |
| 43 | SafeMap() = default; |
| 44 | explicit SafeMap(const key_compare& cmp, const allocator_type& allocator = allocator_type()) |
| 45 | : map_(cmp, allocator) { |
| 46 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 48 | Self& operator=(const Self& rhs) { |
| 49 | map_ = rhs.map_; |
| 50 | return *this; |
| 51 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 52 | |
| 53 | iterator begin() { return map_.begin(); } |
| 54 | const_iterator begin() const { return map_.begin(); } |
| 55 | iterator end() { return map_.end(); } |
| 56 | const_iterator end() const { return map_.end(); } |
| 57 | |
| 58 | bool empty() const { return map_.empty(); } |
| 59 | size_type size() const { return map_.size(); } |
| 60 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 61 | void clear() { map_.clear(); } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 62 | void erase(iterator it) { map_.erase(it); } |
| 63 | size_type erase(const K& k) { return map_.erase(k); } |
| 64 | |
| 65 | iterator find(const K& k) { return map_.find(k); } |
| 66 | const_iterator find(const K& k) const { return map_.find(k); } |
| 67 | |
| 68 | size_type count(const K& k) const { return map_.count(k); } |
| 69 | |
| 70 | // Note that unlike std::map's operator[], this doesn't return a reference to the value. |
TDYa127 | b2eb5c1 | 2012-05-24 15:52:10 -0700 | [diff] [blame] | 71 | V Get(const K& k) const { |
| 72 | const_iterator it = map_.find(k); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 73 | DCHECK(it != map_.end()); |
| 74 | return it->second; |
| 75 | } |
| 76 | |
| 77 | // Used to insert a new mapping. |
| 78 | void Put(const K& k, const V& v) { |
| 79 | std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v)); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 80 | DCHECK(result.second); // Check we didn't accidentally overwrite an existing value. |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type |
| 84 | // of this container is a pointer, any overwritten pointer will be lost and if this container |
| 85 | // was the owner, you have a leak. |
| 86 | void Overwrite(const K& k, const V& v) { |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 87 | std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v)); |
| 88 | if (!result.second) { |
| 89 | // Already there - update the value for the existing key |
| 90 | result.first->second = v; |
| 91 | } |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool Equals(const Self& rhs) const { |
| 95 | return map_ == rhs.map_; |
| 96 | } |
| 97 | |
| 98 | private: |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 99 | ::std::map<K, V, Comparator, Allocator> map_; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | template <typename K, typename V, typename Comparator> |
| 103 | bool operator==(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) { |
| 104 | return lhs.Equals(rhs); |
| 105 | } |
| 106 | |
| 107 | template <typename K, typename V, typename Comparator> |
| 108 | bool operator!=(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) { |
| 109 | return !(lhs == rhs); |
| 110 | } |
| 111 | |
| 112 | } // namespace art |
| 113 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 114 | #endif // ART_RUNTIME_SAFE_MAP_H_ |