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