Chromium Code Reviews| Index: base/id_map.h |
| diff --git a/base/id_map.h b/base/id_map.h |
| index 9cbc1f8978fa8836b770f8370ff3b2a3f44e2017..8d88f4256f922bf8ed10443cb645d4c1e479216c 100644 |
| --- a/base/id_map.h |
| +++ b/base/id_map.h |
| @@ -53,13 +53,14 @@ class IDMap : public base::NonThreadSafe { |
| Releaser<OS, 0>::release_all(&data_); |
| } |
| - // Sets whether Add should CHECK if passed in NULL data. Default is false. |
| + // Sets whether Add and Replace should CHECK if passed in NULL data. |
|
falken
2014/12/09 01:19:32
Shouldn't this read "DCHECK" now?
michaeln
2014/12/09 02:26:42
right, thnx
|
| + // Default is false. |
| void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| // Adds a view with an automatically generated unique ID. See AddWithID. |
| KeyType Add(T* data) { |
| DCHECK(CalledOnValidThread()); |
| - CHECK(!check_on_null_data_ || data); |
| + DCHECK(!check_on_null_data_ || data); |
| KeyType this_id = next_id_; |
| DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| data_[this_id] = data; |
| @@ -73,7 +74,7 @@ class IDMap : public base::NonThreadSafe { |
| // two methods may not be mixed, or duplicate IDs may be generated |
| void AddWithID(T* data, KeyType id) { |
| DCHECK(CalledOnValidThread()); |
| - CHECK(!check_on_null_data_ || data); |
| + DCHECK(!check_on_null_data_ || data); |
| DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| data_[id] = data; |
| } |
| @@ -94,6 +95,25 @@ class IDMap : public base::NonThreadSafe { |
| } |
| } |
| + // Replaces the value for |id| with |new_data| and returns a pointer to the |
| + // existing value. If there is no entry for |id|, the map is not altered and |
| + // nullptr is returned. The OwnershipSemantics of the map have no effect on |
| + // how the existing value is treated, the IDMap does not delete the existing |
| + // value being replaced. |
| + T* Replace(KeyType id, T* new_data) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!check_on_null_data_ || new_data); |
| + typename HashTable::iterator i = data_.find(id); |
| + if (i == data_.end()) { |
| + NOTREACHED() << "Attempting to replace an item not in the list"; |
| + return nullptr; |
| + } |
| + |
| + T* temp = i->second; |
| + i->second = new_data; |
| + return temp; |
| + } |
| + |
| void Clear() { |
| DCHECK(CalledOnValidThread()); |
| if (iteration_depth_ == 0) { |