Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: base/containers/scoped_ptr_hash_map.h

Issue 1016773002: MJPEG acceleration for video capture using VAAPI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: support multiple jpeg decoder Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
kcwu 2015/04/23 13:02:32 change of scoped_ptr_hash_map* is under review her
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_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ 5 #ifndef BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_
6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ 6 #define BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h" 12 #include "base/containers/hash_tables.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 16
17 namespace base { 17 namespace base {
18 18
19 // This type acts like a hash_map<K, scoped_ptr<V> >, based on top of 19 // This type acts like a hash_map<K, scoped_ptr<V, D> >, based on top of
20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data 20 // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data
21 // structure. 21 // structure.
22 template <typename Key, typename Value> 22 template <typename Key,
23 typename Value,
24 typename Deleter = base::DefaultDeleter<Value>>
23 class ScopedPtrHashMap { 25 class ScopedPtrHashMap {
24 typedef base::hash_map<Key, Value*> Container; 26 typedef base::hash_map<Key, Value*> Container;
25 27
26 public: 28 public:
27 typedef typename Container::key_type key_type; 29 typedef typename Container::key_type key_type;
28 typedef typename Container::mapped_type mapped_type; 30 typedef typename Container::mapped_type mapped_type;
29 typedef typename Container::value_type value_type; 31 typedef typename Container::value_type value_type;
30 typedef typename Container::iterator iterator; 32 typedef typename Container::iterator iterator;
31 typedef typename Container::const_iterator const_iterator; 33 typedef typename Container::const_iterator const_iterator;
32 34
33 ScopedPtrHashMap() {} 35 ScopedPtrHashMap() {}
34 36
35 ~ScopedPtrHashMap() { clear(); } 37 ~ScopedPtrHashMap() { clear(); }
36 38
37 void swap(ScopedPtrHashMap<Key, Value>& other) { 39 void swap(ScopedPtrHashMap<Key, Value, Deleter>& other) {
38 data_.swap(other.data_); 40 data_.swap(other.data_);
39 } 41 }
40 42
41 // Replaces value but not key if key is already present. 43 // Replaces value but not key if key is already present.
42 iterator set(const Key& key, scoped_ptr<Value> data) { 44 iterator set(const Key& key, scoped_ptr<Value, Deleter> data) {
43 iterator it = find(key); 45 iterator it = find(key);
44 if (it != end()) { 46 if (it != end()) {
45 delete it->second; 47 Deleter()(it->second);
46 it->second = data.release(); 48 it->second = data.release();
47 return it; 49 return it;
48 } 50 }
49 51
50 return data_.insert(std::make_pair(key, data.release())).first; 52 return data_.insert(std::make_pair(key, data.release())).first;
51 } 53 }
52 54
53 // Does nothing if key is already present 55 // Does nothing if key is already present
54 std::pair<iterator, bool> add(const Key& key, scoped_ptr<Value> data) { 56 std::pair<iterator, bool> add(const Key& key,
57 scoped_ptr<Value, Deleter> data) {
55 std::pair<iterator, bool> result = 58 std::pair<iterator, bool> result =
56 data_.insert(std::make_pair(key, data.get())); 59 data_.insert(std::make_pair(key, data.get()));
57 if (result.second) 60 if (result.second)
58 ignore_result(data.release()); 61 ignore_result(data.release());
59 return result; 62 return result;
60 } 63 }
61 64
62 void erase(iterator it) { 65 void erase(iterator it) {
63 delete it->second; 66 Deleter()(it->second);
64 data_.erase(it); 67 data_.erase(it);
65 } 68 }
66 69
67 size_t erase(const Key& k) { 70 size_t erase(const Key& k) {
68 iterator it = data_.find(k); 71 iterator it = data_.find(k);
69 if (it == data_.end()) 72 if (it == data_.end())
70 return 0; 73 return 0;
71 erase(it); 74 erase(it);
72 return 1; 75 return 1;
73 } 76 }
74 77
75 scoped_ptr<Value> take(iterator it) { 78 scoped_ptr<Value, Deleter> take(iterator it) {
76 DCHECK(it != data_.end()); 79 DCHECK(it != data_.end());
77 if (it == data_.end()) 80 if (it == data_.end())
78 return scoped_ptr<Value>(); 81 return scoped_ptr<Value, Deleter>();
79 82
80 scoped_ptr<Value> ret(it->second); 83 scoped_ptr<Value, Deleter> ret(it->second);
81 it->second = NULL; 84 it->second = NULL;
82 return ret.Pass(); 85 return ret.Pass();
83 } 86 }
84 87
85 scoped_ptr<Value> take(const Key& k) { 88 scoped_ptr<Value, Deleter> take(const Key& k) {
86 iterator it = find(k); 89 iterator it = find(k);
87 if (it == data_.end()) 90 if (it == data_.end())
88 return scoped_ptr<Value>(); 91 return scoped_ptr<Value, Deleter>();
89 92
90 return take(it); 93 return take(it);
91 } 94 }
92 95
93 scoped_ptr<Value> take_and_erase(iterator it) { 96 scoped_ptr<Value, Deleter> take_and_erase(iterator it) {
94 DCHECK(it != data_.end()); 97 DCHECK(it != data_.end());
95 if (it == data_.end()) 98 if (it == data_.end())
96 return scoped_ptr<Value>(); 99 return scoped_ptr<Value, Deleter>();
97 100
98 scoped_ptr<Value> ret(it->second); 101 scoped_ptr<Value, Deleter> ret(it->second);
99 data_.erase(it); 102 data_.erase(it);
100 return ret.Pass(); 103 return ret.Pass();
101 } 104 }
102 105
103 scoped_ptr<Value> take_and_erase(const Key& k) { 106 scoped_ptr<Value, Deleter> take_and_erase(const Key& k) {
104 iterator it = find(k); 107 iterator it = find(k);
105 if (it == data_.end()) 108 if (it == data_.end())
106 return scoped_ptr<Value>(); 109 return scoped_ptr<Value, Deleter>();
107 110
108 return take_and_erase(it); 111 return take_and_erase(it);
109 } 112 }
110 113
111 // Returns the element in the hash_map that matches the given key. 114 // Returns the element in the hash_map that matches the given key.
112 // If no such element exists it returns NULL. 115 // If no such element exists it returns NULL.
113 Value* get(const Key& k) const { 116 Value* get(const Key& k) const {
114 const_iterator it = find(k); 117 const_iterator it = find(k);
115 if (it == end()) 118 if (it == end())
116 return NULL; 119 return NULL;
117 return it->second; 120 return it->second;
118 } 121 }
119 122
120 inline bool contains(const Key& k) const { return data_.count(k) > 0; } 123 inline bool contains(const Key& k) const { return data_.count(k) > 0; }
121 124
122 inline void clear() { STLDeleteValues(&data_); } 125 inline void clear() {
126 auto it = data_.begin();
127 while (it != data_.end()) {
128 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
129 // Deleting the value does not always invalidate the iterator, but it may
130 // do so if the key is a pointer into the value object.
131 auto temp = it;
132 ++it;
133 Deleter()(temp->second);
134 }
135 data_.clear();
136 }
123 137
124 inline const_iterator find(const Key& k) const { return data_.find(k); } 138 inline const_iterator find(const Key& k) const { return data_.find(k); }
125 inline iterator find(const Key& k) { return data_.find(k); } 139 inline iterator find(const Key& k) { return data_.find(k); }
126 140
127 inline size_t count(const Key& k) const { return data_.count(k); } 141 inline size_t count(const Key& k) const { return data_.count(k); }
128 inline std::pair<const_iterator, const_iterator> equal_range( 142 inline std::pair<const_iterator, const_iterator> equal_range(
129 const Key& k) const { 143 const Key& k) const {
130 return data_.equal_range(k); 144 return data_.equal_range(k);
131 } 145 }
132 inline std::pair<iterator, iterator> equal_range(const Key& k) { 146 inline std::pair<iterator, iterator> equal_range(const Key& k) {
(...skipping 15 matching lines...) Expand all
148 162
149 private: 163 private:
150 Container data_; 164 Container data_;
151 165
152 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap); 166 DISALLOW_COPY_AND_ASSIGN(ScopedPtrHashMap);
153 }; 167 };
154 168
155 } // namespace base 169 } // namespace base
156 170
157 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_ 171 #endif // BASE_CONTAINERS_SCOPED_PTR_HASH_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698