Chromium Code Reviews| Index: base/id_map.h |
| diff --git a/base/id_map.h b/base/id_map.h |
| index 9a41d74f35414a34b5a683fa52267a98811601b6..722be23eba1b807e0124f864499813ac23f9eabe 100644 |
| --- a/base/id_map.h |
| +++ b/base/id_map.h |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -11,6 +11,7 @@ |
| #include "base/basictypes.h" |
| #include "base/hash_tables.h" |
| #include "base/logging.h" |
| +#include "base/threading/non_thread_safe.h" |
| // Ownership semantics - own pointer means the pointer is deleted in Remove() |
| // & during destruction |
| @@ -31,13 +32,17 @@ enum IDMapOwnershipSemantics { |
| // This class does not have a virtual destructor, do not inherit from it when |
| // ownership semantics are set to own because pointers will leak. |
| template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> |
| -class IDMap { |
| +class IDMap : public base::NonThreadSafe { |
| private: |
| typedef int32 KeyType; |
| typedef base::hash_map<KeyType, T*> HashTable; |
| public: |
| IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { |
| + // A number of consumers of IDMap create it on one thread but always access |
| + // it from a different, but consitent, thread post-construction. |
| + // TODO(cbentzel): Fix users of IDMap and remove this. |
|
brettw
2011/03/02 18:42:47
Unless you're actually going to change callers (wh
cbentzel
2011/03/02 19:02:45
Agreed. Removed.
|
| + DetachFromThread(); |
| } |
| ~IDMap() { |
| @@ -49,6 +54,7 @@ class IDMap { |
| // Adds a view with an automatically generated unique ID. See AddWithID. |
| KeyType Add(T* data) { |
| + DCHECK(CalledOnValidThread()); |
| CHECK(!check_on_null_data_ || data); |
| KeyType this_id = next_id_; |
| DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| @@ -62,12 +68,14 @@ class IDMap { |
| // this function, or allow this object to generate IDs and call Add. These |
| // 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(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| data_[id] = data; |
| } |
| void Remove(KeyType id) { |
| + DCHECK(CalledOnValidThread()); |
| typename HashTable::iterator i = data_.find(id); |
| if (i == data_.end()) { |
| NOTREACHED() << "Attempting to remove an item not in the list"; |
| @@ -83,10 +91,12 @@ class IDMap { |
| } |
| bool IsEmpty() const { |
| + DCHECK(CalledOnValidThread()); |
| return size() == 0u; |
| } |
| T* Lookup(KeyType id) const { |
| + DCHECK(CalledOnValidThread()); |
| typename HashTable::const_iterator i = data_.find(id); |
| if (i == data_.end()) |
| return NULL; |
| @@ -94,6 +104,7 @@ class IDMap { |
| } |
| size_t size() const { |
| + DCHECK(CalledOnValidThread()); |
| return data_.size() - removed_ids_.size(); |
| } |
| @@ -105,28 +116,34 @@ class IDMap { |
| Iterator(IDMap<T, OS>* map) |
| : map_(map), |
| iter_(map_->data_.begin()) { |
| + DCHECK(map->CalledOnValidThread()); |
| ++map_->iteration_depth_; |
| SkipRemovedEntries(); |
| } |
| ~Iterator() { |
| + DCHECK(map_->CalledOnValidThread()); |
| if (--map_->iteration_depth_ == 0) |
| map_->Compact(); |
| } |
| bool IsAtEnd() const { |
| + DCHECK(map_->CalledOnValidThread()); |
| return iter_ == map_->data_.end(); |
| } |
| KeyType GetCurrentKey() const { |
| + DCHECK(map_->CalledOnValidThread()); |
| return iter_->first; |
| } |
| ReturnType* GetCurrentValue() const { |
| + DCHECK(map_->CalledOnValidThread()); |
| return iter_->second; |
| } |
| void Advance() { |
| + DCHECK(map_->CalledOnValidThread()); |
| ++iter_; |
| SkipRemovedEntries(); |
| } |