summaryrefslogtreecommitdiffstats
path: root/runtime/safe_map.h
diff options
context:
space:
mode:
authorBrian Carlstrom <bdc@google.com>2013-07-12 13:46:57 -0700
committerBrian Carlstrom <bdc@google.com>2013-07-12 17:49:01 -0700
commit7940e44f4517de5e2634a7e07d58d0fb26160513 (patch)
treeac90242d96229a6942f6e24ab137bc1f8f2e0025 /runtime/safe_map.h
parent5cd9e3b122f276f610980cbaf0d2ad6ed4cd9088 (diff)
downloadart-7940e44f4517de5e2634a7e07d58d0fb26160513.zip
art-7940e44f4517de5e2634a7e07d58d0fb26160513.tar.gz
art-7940e44f4517de5e2634a7e07d58d0fb26160513.tar.bz2
Create separate Android.mk for main build targets
The runtime, compiler, dex2oat, and oatdump now are in seperate trees to prevent dependency creep. They can now be individually built without rebuilding the rest of the art projects. dalvikvm and jdwpspy were already this way. Builds in the art directory should behave as before, building everything including tests. Change-Id: Ic6b1151e5ed0f823c3dd301afd2b13eb2d8feb81
Diffstat (limited to 'runtime/safe_map.h')
-rw-r--r--runtime/safe_map.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/runtime/safe_map.h b/runtime/safe_map.h
new file mode 100644
index 0000000..b9a6ecf
--- /dev/null
+++ b/runtime/safe_map.h
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_SRC_SAFE_MAP_H_
+#define ART_SRC_SAFE_MAP_H_
+
+#include <map>
+
+#include "base/logging.h"
+
+namespace art {
+
+// Equivalent to std::map, but without operator[] and its bug-prone semantics (in particular,
+// the implicit insertion of a default-constructed value on failed lookups).
+template <typename K, typename V, typename Comparator = std::less<K> >
+class SafeMap {
+ private:
+ typedef SafeMap<K, V, Comparator> Self;
+
+ public:
+ typedef typename ::std::map<K, V, Comparator>::iterator iterator;
+ typedef typename ::std::map<K, V, Comparator>::const_iterator const_iterator;
+ typedef typename ::std::map<K, V, Comparator>::size_type size_type;
+ typedef typename ::std::map<K, V, Comparator>::value_type value_type;
+
+ Self& operator=(const Self& rhs) {
+ map_ = rhs.map_;
+ return *this;
+ }
+
+ iterator begin() { return map_.begin(); }
+ const_iterator begin() const { return map_.begin(); }
+ iterator end() { return map_.end(); }
+ const_iterator end() const { return map_.end(); }
+
+ bool empty() const { return map_.empty(); }
+ size_type size() const { return map_.size(); }
+
+ void clear() { map_.clear(); }
+ void erase(iterator it) { map_.erase(it); }
+ size_type erase(const K& k) { return map_.erase(k); }
+
+ iterator find(const K& k) { return map_.find(k); }
+ const_iterator find(const K& k) const { return map_.find(k); }
+
+ size_type count(const K& k) const { return map_.count(k); }
+
+ // Note that unlike std::map's operator[], this doesn't return a reference to the value.
+ V Get(const K& k) const {
+ const_iterator it = map_.find(k);
+ DCHECK(it != map_.end());
+ return it->second;
+ }
+
+ // Used to insert a new mapping.
+ void Put(const K& k, const V& v) {
+ std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
+ DCHECK(result.second); // Check we didn't accidentally overwrite an existing value.
+ }
+
+ // Used to insert a new mapping or overwrite an existing mapping. Note that if the value type
+ // of this container is a pointer, any overwritten pointer will be lost and if this container
+ // was the owner, you have a leak.
+ void Overwrite(const K& k, const V& v) {
+ std::pair<iterator, bool> result = map_.insert(std::make_pair(k, v));
+ if (!result.second) {
+ // Already there - update the value for the existing key
+ result.first->second = v;
+ }
+ }
+
+ bool Equals(const Self& rhs) const {
+ return map_ == rhs.map_;
+ }
+
+ private:
+ ::std::map<K, V, Comparator> map_;
+};
+
+template <typename K, typename V, typename Comparator>
+bool operator==(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
+ return lhs.Equals(rhs);
+}
+
+template <typename K, typename V, typename Comparator>
+bool operator!=(const SafeMap<K, V, Comparator>& lhs, const SafeMap<K, V, Comparator>& rhs) {
+ return !(lhs == rhs);
+}
+
+} // namespace art
+
+#endif // ART_SRC_SAFE_MAP_H_