| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 BASE_ID_MAP_H__ | 5 #ifndef BASE_ID_MAP_H__ |
| 6 #define BASE_ID_MAP_H__ | 6 #define BASE_ID_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/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 class IDMap { | 21 class IDMap { |
| 22 private: | 22 private: |
| 23 typedef base::hash_map<int32, T*> HashTable; | 23 typedef base::hash_map<int32, T*> HashTable; |
| 24 typedef typename HashTable::iterator iterator; | 24 typedef typename HashTable::iterator iterator; |
| 25 | 25 |
| 26 public: | 26 public: |
| 27 // support const iterators over the items | 27 // support const iterators over the items |
| 28 // Note, use iterator->first to get the ID, iterator->second to get the T* | 28 // Note, use iterator->first to get the ID, iterator->second to get the T* |
| 29 typedef typename HashTable::const_iterator const_iterator; | 29 typedef typename HashTable::const_iterator const_iterator; |
| 30 | 30 |
| 31 IDMap() : next_id_(1) { | 31 IDMap() : next_id_(1), check_on_null_data_(false) { |
| 32 } | 32 } |
| 33 IDMap(const IDMap& other) : next_id_(other.next_id_), | 33 IDMap(const IDMap& other) |
| 34 data_(other.data_) { | 34 : next_id_(other.next_id_), |
| 35 data_(other.data_), |
| 36 check_on_null_data_(other.check_on_null_data_) { |
| 35 } | 37 } |
| 36 | 38 |
| 39 // Sets whether Add should CHECK if passed in NULL data. Default is false. |
| 40 void set_check_on_null_data(bool value) { check_on_null_data_ = value; } |
| 41 |
| 37 const_iterator begin() const { | 42 const_iterator begin() const { |
| 38 return data_.begin(); | 43 return data_.begin(); |
| 39 } | 44 } |
| 40 const_iterator end() const { | 45 const_iterator end() const { |
| 41 return data_.end(); | 46 return data_.end(); |
| 42 } | 47 } |
| 43 | 48 |
| 44 // Adds a view with an automatically generated unique ID. See AddWithID. | 49 // Adds a view with an automatically generated unique ID. See AddWithID. |
| 45 int32 Add(T* data) { | 50 int32 Add(T* data) { |
| 51 CHECK(!check_on_null_data_ || data); |
| 46 int32 this_id = next_id_; | 52 int32 this_id = next_id_; |
| 47 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; | 53 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; |
| 48 data_[this_id] = data; | 54 data_[this_id] = data; |
| 49 next_id_++; | 55 next_id_++; |
| 50 return this_id; | 56 return this_id; |
| 51 } | 57 } |
| 52 | 58 |
| 53 // Adds a new data member with the specified ID. The ID must not be in | 59 // Adds a new data member with the specified ID. The ID must not be in |
| 54 // the list. The caller either must generate all unique IDs itself and use | 60 // the list. The caller either must generate all unique IDs itself and use |
| 55 // this function, or allow this object to generate IDs and call Add. These | 61 // this function, or allow this object to generate IDs and call Add. These |
| 56 // two methods may not be mixed, or duplicate IDs may be generated | 62 // two methods may not be mixed, or duplicate IDs may be generated |
| 57 void AddWithID(T* data, int32 id) { | 63 void AddWithID(T* data, int32 id) { |
| 64 CHECK(!check_on_null_data_ || data); |
| 58 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; | 65 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; |
| 59 data_[id] = data; | 66 data_[id] = data; |
| 60 } | 67 } |
| 61 | 68 |
| 62 void Remove(int32 id) { | 69 void Remove(int32 id) { |
| 63 iterator i = data_.find(id); | 70 iterator i = data_.find(id); |
| 64 if (i == data_.end()) { | 71 if (i == data_.end()) { |
| 65 NOTREACHED() << "Attempting to remove an item not in the list"; | 72 NOTREACHED() << "Attempting to remove an item not in the list"; |
| 66 return; | 73 return; |
| 67 } | 74 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 81 | 88 |
| 82 size_t size() const { | 89 size_t size() const { |
| 83 return data_.size(); | 90 return data_.size(); |
| 84 } | 91 } |
| 85 | 92 |
| 86 protected: | 93 protected: |
| 87 // The next ID that we will return from Add() | 94 // The next ID that we will return from Add() |
| 88 int32 next_id_; | 95 int32 next_id_; |
| 89 | 96 |
| 90 HashTable data_; | 97 HashTable data_; |
| 98 |
| 99 private: |
| 100 // See description above setter. |
| 101 bool check_on_null_data_; |
| 91 }; | 102 }; |
| 92 | 103 |
| 93 #endif // BASE_ID_MAP_H__ | 104 #endif // BASE_ID_MAP_H__ |
| OLD | NEW |