blob: 4001aaf31c7d7a43ae3dd4ba378431fb697cc121 [file] [log] [blame]
Elliott Hughes74847412012-06-20 18:10:21 -07001/*
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
Elliott Hughesa0e18062012-04-13 15:59:59 -070017#ifndef ART_SRC_SAFE_MAP_H_
18#define ART_SRC_SAFE_MAP_H_
19
20#include <map>
21
22#include "logging.h"
23
24namespace art {
25
26// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
27// the implicit insertion of a default-constructed value on failed lookups).
28template <typename K, typename V, typename Comparator = std::less<K> >
29class SafeMap {
30 private:
31 typedef SafeMap<K, V, Comparator> Self;
32
33 public:
34 typedef typename ::std::map<K, V, Comparator>::iterator iterator;
35 typedef typename ::std::map<K, V, Comparator>::const_iterator const_iterator;
36 typedef typename ::std::map<K, V, Comparator>::size_type size_type;
37 typedef typename ::std::map<K, V, Comparator>::value_type value_type;
38
Elliott Hughes74847412012-06-20 18:10:21 -070039 Self& operator=(const Self& rhs) {
40 map_ = rhs.map_;
41 return *this;
42 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070043
44 iterator begin() { return map_.begin(); }
45 const_iterator begin() const { return map_.begin(); }
46 iterator end() { return map_.end(); }
47 const_iterator end() const { return map_.end(); }
48
49 bool empty() const { return map_.empty(); }
50 size_type size() const { return map_.size(); }
51
Ian Rogers08f753d2012-08-24 14:35:25 -070052 void clear() { map_.clear(); }
Elliott Hughesa0e18062012-04-13 15:59:59 -070053 void erase(iterator it) { map_.erase(it); }
54 size_type erase(const K& k) { return map_.erase(k); }
55
56 iterator find(const K& k) { return map_.find(k); }
57 const_iterator find(const K& k) const { return map_.find(k); }
58
59 size_type count(const K& k) const { return map_.count(k); }
60
61 // Note that unlike std::map's operator[], this doesn't return a reference to the value.
TDYa127b2eb5c12012-05-24 15:52:10 -070062 V Get(const K& k) const {
63 const_iterator it = map_.find(k);
Elliott Hughesa0e18062012-04-13 15:59:59 -070064 DCHECK(it != map_.end());
65 return it->second;
66 }
67
68 // Used to insert a new mapping.
69 void Put(const K& k, const V& v) {
70 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
71 DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
72 }
73
74 // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
75 // of this container is a pointer, any overwritten pointer will be lost and if this container
76 // was the owner, you have a leak.
77 void Overwrite(const K& k, const V& v) {
buzbeed1643e42012-09-05 14:06:51 -070078 std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
79 if (!result.second) {
80 // Already there - update the value for the existing key
81 result.first->second = v;
82 }
Elliott Hughesa0e18062012-04-13 15:59:59 -070083 }
84
85 bool Equals(const Self& rhs) const {
86 return map_ == rhs.map_;
87 }
88
89 private:
90 ::std::map<K, V, Comparator> map_;
91};
92
93template <typename K, typename V, typename Comparator>
94bool operator==(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
95 return lhs.Equals(rhs);
96}
97
98template <typename K, typename V, typename Comparator>
99bool operator!=(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
100 return !(lhs == rhs);
101}
102
103} // namespace art
104
105#endif // ART_SRC_SAFE_MAP_H_