OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_SCOPED_PTR_HASH_MAP_H_ | 5 #ifndef CC_SCOPED_PTR_HASH_MAP_H_ |
6 #define CC_SCOPED_PTR_HASH_MAP_H_ | 6 #define CC_SCOPED_PTR_HASH_MAP_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 | 12 |
14 namespace cc { | 13 namespace cc { |
15 | 14 |
16 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of | 15 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of |
17 // std::hash_map. The ScopedPtrHashMap has ownership of all values in the data | 16 // std::hash_map. The ScopedPtrHashMap has ownership of all values in the data |
18 // structure. | 17 // structure. |
19 template <typename Key, typename Value> | 18 template <typename Key, typename Value> |
20 class ScopedPtrHashMap { | 19 class ScopedPtrHashMap { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 59 |
61 size_t erase(const Key& k) { | 60 size_t erase(const Key& k) { |
62 iterator it = data_.find(k); | 61 iterator it = data_.find(k); |
63 if (it == data_.end()) | 62 if (it == data_.end()) |
64 return 0; | 63 return 0; |
65 erase(it); | 64 erase(it); |
66 return 1; | 65 return 1; |
67 } | 66 } |
68 | 67 |
69 scoped_ptr<Value> take(iterator it) { | 68 scoped_ptr<Value> take(iterator it) { |
70 DCHECK(it != data_.end()); | 69 ASSERT(it != data_.end()); |
71 if (it == data_.end()) | 70 if (it == data_.end()) |
72 return scoped_ptr<Value>(NULL); | 71 return scoped_ptr<Value>(NULL); |
73 | 72 |
74 Key key = it->first; | 73 Key key = it->first; |
75 scoped_ptr<Value> ret(it->second); | 74 scoped_ptr<Value> ret(it->second); |
76 data_.erase(it); | 75 data_.erase(it); |
77 data_.insert(std::pair<Key, Value*>(key, static_cast<Value*>(NULL))); | 76 data_.insert(std::pair<Key, Value*>(key, static_cast<Value*>(NULL))); |
78 return ret.Pass(); | 77 return ret.Pass(); |
79 } | 78 } |
80 | 79 |
81 scoped_ptr<Value> take(const Key& k) { | 80 scoped_ptr<Value> take(const Key& k) { |
82 iterator it = find(k); | 81 iterator it = find(k); |
83 if (it == data_.end()) | 82 if (it == data_.end()) |
84 return scoped_ptr<Value>(NULL); | 83 return scoped_ptr<Value>(NULL); |
85 | 84 |
86 return take(it); | 85 return take(it); |
87 } | 86 } |
88 | 87 |
89 scoped_ptr<Value> take_and_erase(iterator it) { | 88 scoped_ptr<Value> take_and_erase(iterator it) { |
90 DCHECK(it != data_.end()); | 89 ASSERT(it != data_.end()); |
91 if (it == data_.end()) | 90 if (it == data_.end()) |
92 return scoped_ptr<Value>(NULL); | 91 return scoped_ptr<Value>(NULL); |
93 | 92 |
94 scoped_ptr<Value> ret(it->second); | 93 scoped_ptr<Value> ret(it->second); |
95 data_.erase(it); | 94 data_.erase(it); |
96 return ret.Pass(); | 95 return ret.Pass(); |
97 } | 96 } |
98 | 97 |
99 scoped_ptr<Value> take_and_erase(const Key& k) { | 98 scoped_ptr<Value> take_and_erase(const Key& k) { |
100 iterator it = find(k); | 99 iterator it = find(k); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 143 |
145 private: | 144 private: |
146 Container data_; | 145 Container data_; |
147 | 146 |
148 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); | 147 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); |
149 }; | 148 }; |
150 | 149 |
151 } // namespace cc | 150 } // namespace cc |
152 | 151 |
153 #endif // CC_SCOPED_PTR_HASH_MAP_H_ | 152 #endif // CC_SCOPED_PTR_HASH_MAP_H_ |
OLD | NEW |