1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/id_map.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_ID_MAP_H__ 1.9 +#define BASE_ID_MAP_H__ 1.10 + 1.11 +#include "base/basictypes.h" 1.12 +#include "base/hash_tables.h" 1.13 +#include "base/logging.h" 1.14 + 1.15 +// This object maintains a list of IDs that can be quickly converted to 1.16 +// pointers to objects. It is implemented as a hash table, optimized for 1.17 +// relatively small data sets (in the common case, there will be exactly one 1.18 +// item in the list). 1.19 +// 1.20 +// Items can be inserted into the container with arbitrary ID, but the caller 1.21 +// must ensure they are unique. Inserting IDs and relying on automatically 1.22 +// generated ones is not allowed because they can collide. 1.23 +template<class T> 1.24 +class IDMap { 1.25 + private: 1.26 + typedef base::hash_map<int32_t, T*> HashTable; 1.27 + typedef typename HashTable::iterator iterator; 1.28 + 1.29 + public: 1.30 + // support const iterators over the items 1.31 + // Note, use iterator->first to get the ID, iterator->second to get the T* 1.32 + typedef typename HashTable::const_iterator const_iterator; 1.33 + 1.34 + IDMap() : next_id_(1) { 1.35 + } 1.36 + IDMap(const IDMap& other) : next_id_(other.next_id_), 1.37 + data_(other.data_) { 1.38 + } 1.39 + 1.40 + const_iterator begin() const { 1.41 + return data_.begin(); 1.42 + } 1.43 + const_iterator end() const { 1.44 + return data_.end(); 1.45 + } 1.46 + 1.47 + // Adds a view with an automatically generated unique ID. See AddWithID. 1.48 + int32_t Add(T* data) { 1.49 + int32_t this_id = next_id_; 1.50 + DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; 1.51 + data_[this_id] = data; 1.52 + next_id_++; 1.53 + return this_id; 1.54 + } 1.55 + 1.56 + // Adds a new data member with the specified ID. The ID must not be in 1.57 + // the list. The caller either must generate all unique IDs itself and use 1.58 + // this function, or allow this object to generate IDs and call Add. These 1.59 + // two methods may not be mixed, or duplicate IDs may be generated 1.60 + void AddWithID(T* data, int32_t id) { 1.61 + DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; 1.62 + data_[id] = data; 1.63 + } 1.64 + 1.65 + void Remove(int32_t id) { 1.66 + iterator i = data_.find(id); 1.67 + if (i == data_.end()) { 1.68 + NOTREACHED() << "Attempting to remove an item not in the list"; 1.69 + return; 1.70 + } 1.71 + data_.erase(i); 1.72 + } 1.73 + 1.74 + bool IsEmpty() const { 1.75 + return data_.empty(); 1.76 + } 1.77 + 1.78 + void Clear() { 1.79 + data_.clear(); 1.80 + } 1.81 + 1.82 + bool HasData(const T* data) const { 1.83 + // XXX would like to use <algorithm> here ... 1.84 + for (const_iterator it = begin(); it != end(); ++it) 1.85 + if (data == it->second) 1.86 + return true; 1.87 + return false; 1.88 + } 1.89 + 1.90 + T* Lookup(int32_t id) const { 1.91 + const_iterator i = data_.find(id); 1.92 + if (i == data_.end()) 1.93 + return NULL; 1.94 + return i->second; 1.95 + } 1.96 + 1.97 + size_t size() const { 1.98 + return data_.size(); 1.99 + } 1.100 + 1.101 + protected: 1.102 + // The next ID that we will return from Add() 1.103 + int32_t next_id_; 1.104 + 1.105 + HashTable data_; 1.106 +}; 1.107 + 1.108 +#endif // BASE_ID_MAP_H__