ipc/chromium/src/base/id_map.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_ID_MAP_H__
michael@0 6 #define BASE_ID_MAP_H__
michael@0 7
michael@0 8 #include "base/basictypes.h"
michael@0 9 #include "base/hash_tables.h"
michael@0 10 #include "base/logging.h"
michael@0 11
michael@0 12 // This object maintains a list of IDs that can be quickly converted to
michael@0 13 // pointers to objects. It is implemented as a hash table, optimized for
michael@0 14 // relatively small data sets (in the common case, there will be exactly one
michael@0 15 // item in the list).
michael@0 16 //
michael@0 17 // Items can be inserted into the container with arbitrary ID, but the caller
michael@0 18 // must ensure they are unique. Inserting IDs and relying on automatically
michael@0 19 // generated ones is not allowed because they can collide.
michael@0 20 template<class T>
michael@0 21 class IDMap {
michael@0 22 private:
michael@0 23 typedef base::hash_map<int32_t, T*> HashTable;
michael@0 24 typedef typename HashTable::iterator iterator;
michael@0 25
michael@0 26 public:
michael@0 27 // support const iterators over the items
michael@0 28 // Note, use iterator->first to get the ID, iterator->second to get the T*
michael@0 29 typedef typename HashTable::const_iterator const_iterator;
michael@0 30
michael@0 31 IDMap() : next_id_(1) {
michael@0 32 }
michael@0 33 IDMap(const IDMap& other) : next_id_(other.next_id_),
michael@0 34 data_(other.data_) {
michael@0 35 }
michael@0 36
michael@0 37 const_iterator begin() const {
michael@0 38 return data_.begin();
michael@0 39 }
michael@0 40 const_iterator end() const {
michael@0 41 return data_.end();
michael@0 42 }
michael@0 43
michael@0 44 // Adds a view with an automatically generated unique ID. See AddWithID.
michael@0 45 int32_t Add(T* data) {
michael@0 46 int32_t this_id = next_id_;
michael@0 47 DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item";
michael@0 48 data_[this_id] = data;
michael@0 49 next_id_++;
michael@0 50 return this_id;
michael@0 51 }
michael@0 52
michael@0 53 // Adds a new data member with the specified ID. The ID must not be in
michael@0 54 // the list. The caller either must generate all unique IDs itself and use
michael@0 55 // this function, or allow this object to generate IDs and call Add. These
michael@0 56 // two methods may not be mixed, or duplicate IDs may be generated
michael@0 57 void AddWithID(T* data, int32_t id) {
michael@0 58 DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item";
michael@0 59 data_[id] = data;
michael@0 60 }
michael@0 61
michael@0 62 void Remove(int32_t id) {
michael@0 63 iterator i = data_.find(id);
michael@0 64 if (i == data_.end()) {
michael@0 65 NOTREACHED() << "Attempting to remove an item not in the list";
michael@0 66 return;
michael@0 67 }
michael@0 68 data_.erase(i);
michael@0 69 }
michael@0 70
michael@0 71 bool IsEmpty() const {
michael@0 72 return data_.empty();
michael@0 73 }
michael@0 74
michael@0 75 void Clear() {
michael@0 76 data_.clear();
michael@0 77 }
michael@0 78
michael@0 79 bool HasData(const T* data) const {
michael@0 80 // XXX would like to use <algorithm> here ...
michael@0 81 for (const_iterator it = begin(); it != end(); ++it)
michael@0 82 if (data == it->second)
michael@0 83 return true;
michael@0 84 return false;
michael@0 85 }
michael@0 86
michael@0 87 T* Lookup(int32_t id) const {
michael@0 88 const_iterator i = data_.find(id);
michael@0 89 if (i == data_.end())
michael@0 90 return NULL;
michael@0 91 return i->second;
michael@0 92 }
michael@0 93
michael@0 94 size_t size() const {
michael@0 95 return data_.size();
michael@0 96 }
michael@0 97
michael@0 98 protected:
michael@0 99 // The next ID that we will return from Add()
michael@0 100 int32_t next_id_;
michael@0 101
michael@0 102 HashTable data_;
michael@0 103 };
michael@0 104
michael@0 105 #endif // BASE_ID_MAP_H__

mercurial