Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame^] | 1 | #ifndef ART_SRC_SAFE_MAP_H_ |
| 2 | #define ART_SRC_SAFE_MAP_H_ |
| 3 | |
| 4 | #include <map> |
| 5 | |
| 6 | #include "logging.h" |
| 7 | |
| 8 | namespace art { |
| 9 | |
| 10 | // Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular, |
| 11 | // the implicit insertion of a default-constructed value on failed lookups). |
| 12 | template <typename K, typename V, typename Comparator = std::less<K> > |
| 13 | class SafeMap { |
| 14 | private: |
| 15 | typedef SafeMap<K, V, Comparator> Self; |
| 16 | |
| 17 | public: |
| 18 | typedef typename ::std::map<K, V, Comparator>::iterator iterator; |
| 19 | typedef typename ::std::map<K, V, Comparator>::const_iterator const_iterator; |
| 20 | typedef typename ::std::map<K, V, Comparator>::size_type size_type; |
| 21 | typedef typename ::std::map<K, V, Comparator>::value_type value_type; |
| 22 | |
| 23 | Self& operator=(const Self& rhs) { map_ = rhs.map_; return *this; } |
| 24 | |
| 25 | iterator begin() { return map_.begin(); } |
| 26 | const_iterator begin() const { return map_.begin(); } |
| 27 | iterator end() { return map_.end(); } |
| 28 | const_iterator end() const { return map_.end(); } |
| 29 | |
| 30 | bool empty() const { return map_.empty(); } |
| 31 | size_type size() const { return map_.size(); } |
| 32 | |
| 33 | void clear() { return map_.clear(); } |
| 34 | void erase(iterator it) { map_.erase(it); } |
| 35 | size_type erase(const K& k) { return map_.erase(k); } |
| 36 | |
| 37 | iterator find(const K& k) { return map_.find(k); } |
| 38 | const_iterator find(const K& k) const { return map_.find(k); } |
| 39 | |
| 40 | size_type count(const K& k) const { return map_.count(k); } |
| 41 | |
| 42 | // Note that unlike std::map's operator[], this doesn't return a reference to the value. |
| 43 | V Get(const K& k) { |
| 44 | iterator it = map_.find(k); |
| 45 | DCHECK(it != map_.end()); |
| 46 | return it->second; |
| 47 | } |
| 48 | |
| 49 | // Used to insert a new mapping. |
| 50 | void Put(const K& k, const V& v) { |
| 51 | std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v)); |
| 52 | DCHECK(result.second); // Check we didn't accidentally overwrite an existing value. |
| 53 | } |
| 54 | |
| 55 | // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type |
| 56 | // of this container is a pointer, any overwritten pointer will be lost and if this container |
| 57 | // was the owner, you have a leak. |
| 58 | void Overwrite(const K& k, const V& v) { |
| 59 | map_.insert(std::make_pair(k, v)); |
| 60 | } |
| 61 | |
| 62 | bool Equals(const Self& rhs) const { |
| 63 | return map_ == rhs.map_; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | ::std::map<K, V, Comparator> map_; |
| 68 | }; |
| 69 | |
| 70 | template <typename K, typename V, typename Comparator> |
| 71 | bool operator==(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) { |
| 72 | return lhs.Equals(rhs); |
| 73 | } |
| 74 | |
| 75 | template <typename K, typename V, typename Comparator> |
| 76 | bool operator!=(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) { |
| 77 | return !(lhs == rhs); |
| 78 | } |
| 79 | |
| 80 | } // namespace art |
| 81 | |
| 82 | #endif // ART_SRC_SAFE_MAP_H_ |