safe_map.h revision fc0e3219edc9a5bf81b166e82fd5db2796eb6a0d
1748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes/*
2748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes *
4748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * you may not use this file except in compliance with the License.
6748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * You may obtain a copy of the License at
7748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes *
8748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes *
10748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * See the License for the specific language governing permissions and
14748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes * limitations under the License.
15748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes */
16748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_SAFE_MAP_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_SAFE_MAP_H_
19a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
20a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include <map>
21a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
2207ed66b5ae659c452cbe1ab20c3dbf1d6f546461Elliott Hughes#include "base/logging.h"
23a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
24a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughesnamespace art {
25a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
26a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
27a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes// the implicit insertion of a default-constructed value on failed lookups).
28a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughestemplate <typename K, typename V, typename Comparator = std::less<K> >
29a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughesclass SafeMap {
30a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes private:
31a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef SafeMap<K, V, Comparator> Self;
32a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
33a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes public:
34a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef typename ::std::map<K, V, Comparator>::iterator iterator;
35a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef typename ::std::map<K, V, Comparator>::const_iterator const_iterator;
36a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef typename ::std::map<K, V, Comparator>::size_type size_type;
37a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  typedef typename ::std::map<K, V, Comparator>::value_type value_type;
38a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
39748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  Self& operator=(const Self& rhs) {
40748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    map_ = rhs.map_;
41748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    return *this;
42748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  }
43a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
44a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  iterator begin() { return map_.begin(); }
45a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  const_iterator begin() const { return map_.begin(); }
46a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  iterator end() { return map_.end(); }
47a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  const_iterator end() const { return map_.end(); }
48a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
49a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  bool empty() const { return map_.empty(); }
50a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_type size() const { return map_.size(); }
51a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
5208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  void clear() { map_.clear(); }
53a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  void erase(iterator it) { map_.erase(it); }
54a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_type erase(const K& k) { return map_.erase(k); }
55a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
56a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  iterator find(const K& k) { return map_.find(k); }
57a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  const_iterator find(const K& k) const { return map_.find(k); }
58a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
59a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_type count(const K& k) const { return map_.count(k); }
60a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
61a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  // Note that unlike std::map's operator[], this doesn't return a reference to the value.
62b2eb5c18d628dc84bdc424b5e5a491382d867e36TDYa  V Get(const K& k) const {
63b2eb5c18d628dc84bdc424b5e5a491382d867e36TDYa    const_iterator it = map_.find(k);
64a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    DCHECK(it != map_.end());
65a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    return it->second;
66a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  }
67a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
68a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  // Used to insert a new mapping.
69a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  void Put(const K& k, const V& v) {
70a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
71a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
72a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  }
73a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
74a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
75a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  // of this container is a pointer, any overwritten pointer will be lost and if this container
76a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  // was the owner, you have a leak.
77a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  void Overwrite(const K& k, const V& v) {
78d1643e41ef242ae656f667bf3c8b0324635cefd3buzbee    std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
79d1643e41ef242ae656f667bf3c8b0324635cefd3buzbee    if (!result.second) {
80d1643e41ef242ae656f667bf3c8b0324635cefd3buzbee      // Already there - update the value for the existing key
81d1643e41ef242ae656f667bf3c8b0324635cefd3buzbee      result.first->second = v;
82d1643e41ef242ae656f667bf3c8b0324635cefd3buzbee    }
83a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  }
84a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
85a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  bool Equals(const Self& rhs) const {
86a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    return map_ == rhs.map_;
87a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  }
88a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
89a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes private:
90a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  ::std::map<K, V, Comparator> map_;
91a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes};
92a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
93a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughestemplate <typename K, typename V, typename Comparator>
94a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughesbool operator==(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
95a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  return lhs.Equals(rhs);
96a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes}
97a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
98a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughestemplate <typename K, typename V, typename Comparator>
99a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughesbool operator!=(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
100a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  return !(lhs == rhs);
101a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes}
102a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
103a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes}  // namespace art
104a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
105fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_SAFE_MAP_H_
106