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

Side by Side Diff: chrome/browser/sync/util/enum_set.h

Issue 8895008: [Sync] Relax EnumSet semantics for some functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_ 5 #ifndef CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_
6 #define CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_ 6 #define CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_
7 #pragma once 7 #pragma once
8 8
9 #include <bitset> 9 #include <bitset>
10 #include <cstddef> 10 #include <cstddef>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // Iterator is a forward-only read-only iterator for EnumSet. Its 58 // Iterator is a forward-only read-only iterator for EnumSet. Its
59 // interface is deliberately distinct from an STL iterator as its 59 // interface is deliberately distinct from an STL iterator as its
60 // semantics are substantially different. 60 // semantics are substantially different.
61 // 61 //
62 // Example usage: 62 // Example usage:
63 // 63 //
64 // for (EnumSet<...>::Iterator it = enums.First(); it.Good(); it.Inc()) { 64 // for (EnumSet<...>::Iterator it = enums.First(); it.Good(); it.Inc()) {
65 // Process(it.Get()); 65 // Process(it.Get());
66 // } 66 // }
67 // 67 //
68 // There are no guarantees as to what will happen if you modify an 68 // The iterator must not be outlived by the set. In particular, the
69 // EnumSet while traversing it with an iterator. 69 // following is an error:
70 //
71 // EnumSet<...> SomeFn() { ... }
72 //
73 // /* ERROR */
74 // for (EnumSet<...>::Iterator it = SomeFun().First(); ...
75 //
76 // Also, there are no guarantees as to what will happen if you
77 // modify an EnumSet while traversing it with an iterator.
70 class Iterator { 78 class Iterator {
71 public: 79 public:
72 // A default-constructed iterator can't do anything except check 80 // A default-constructed iterator can't do anything except check
73 // Good(). You need to call First() on an EnumSet to get a usable 81 // Good(). You need to call First() on an EnumSet to get a usable
74 // iterator. 82 // iterator.
75 Iterator() : enums_(NULL), i_(kValueCount) {} 83 Iterator() : enums_(NULL), i_(kValueCount) {}
76 ~Iterator() {} 84 ~Iterator() {}
77 85
78 // Copy constructor and assignment welcome. 86 // Copy constructor and assignment welcome.
79 87
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 149 }
142 150
143 ~EnumSet() {} 151 ~EnumSet() {}
144 152
145 // Copy constructor and assignment welcome. 153 // Copy constructor and assignment welcome.
146 154
147 // Set operations. Put, Retain, and Remove are basically 155 // Set operations. Put, Retain, and Remove are basically
148 // self-mutating versions of Union, Intersection, and Difference 156 // self-mutating versions of Union, Intersection, and Difference
149 // (defined below). 157 // (defined below).
150 158
151 // Adds the given value to our set. 159 // Adds the given value (which must be in range) to our set.
152 void Put(E value) { 160 void Put(E value) {
153 enums_.set(ToIndex(value)); 161 enums_.set(ToIndex(value));
154 } 162 }
155 163
156 // Adds all values in the given set to our set. 164 // Adds all values in the given set to our set.
157 void PutAll(EnumSet other) { 165 void PutAll(EnumSet other) {
158 enums_ |= other.enums_; 166 enums_ |= other.enums_;
159 } 167 }
160 168
161 // There's no real need for a Retain(E) member function. 169 // There's no real need for a Retain(E) member function.
162 170
163 // Removes all values not in the given set from our set. 171 // Removes all values not in the given set from our set.
164 void RetainAll(EnumSet other) { 172 void RetainAll(EnumSet other) {
165 enums_ &= other.enums_; 173 enums_ &= other.enums_;
166 } 174 }
167 175
168 // Removes the given value from our set. 176 // If the given value is in range, removes it from our set.
169 void Remove(E value) { 177 void Remove(E value) {
170 enums_.reset(ToIndex(value)); 178 if (InRange(value)) {
179 enums_.reset(ToIndex(value));
180 }
171 } 181 }
172 182
173 // Removes all values in the given set from our set. 183 // Removes all values in the given set from our set.
174 void RemoveAll(EnumSet other) { 184 void RemoveAll(EnumSet other) {
175 enums_ &= ~other.enums_; 185 enums_ &= ~other.enums_;
176 } 186 }
177 187
178 // Removes all values from our set. 188 // Removes all values from our set.
179 void Clear() { 189 void Clear() {
180 enums_.reset(); 190 enums_.reset();
181 } 191 }
182 192
183 // Returns true iff the given value is a member of our set. 193 // Returns true iff the given value is in range and a member of our
194 // set.
184 bool Has(E value) const { 195 bool Has(E value) const {
185 return enums_.test(ToIndex(value)); 196 return InRange(value) && enums_.test(ToIndex(value));
186 } 197 }
187 198
188 // Returns true iff the given set is a subset of our set. 199 // Returns true iff the given set is a subset of our set.
189 bool HasAll(EnumSet other) const { 200 bool HasAll(EnumSet other) const {
190 return (enums_ & other.enums_) == other.enums_; 201 return (enums_ & other.enums_) == other.enums_;
191 } 202 }
192 203
193 // Returns true iff our set and the given set contain exactly the 204 // Returns true iff our set and the given set contain exactly the
194 // same values. 205 // same values.
195 bool Equals(const EnumSet& other) const { 206 bool Equals(const EnumSet& other) const {
(...skipping 18 matching lines...) Expand all
214 private: 225 private:
215 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>( 226 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(
216 EnumSet set1, EnumSet set2); 227 EnumSet set1, EnumSet set2);
217 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>( 228 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(
218 EnumSet set1, EnumSet set2); 229 EnumSet set1, EnumSet set2);
219 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>( 230 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(
220 EnumSet set1, EnumSet set2); 231 EnumSet set1, EnumSet set2);
221 232
222 explicit EnumSet(EnumBitSet enums) : enums_(enums) {} 233 explicit EnumSet(EnumBitSet enums) : enums_(enums) {}
223 234
235 static bool InRange(E value) {
236 return (value >= MinEnumValue) && (value <= MaxEnumValue);
237 }
238
224 // Converts a value to/from an index into |enums_|. 239 // Converts a value to/from an index into |enums_|.
225 240
226 static size_t ToIndex(E value) { 241 static size_t ToIndex(E value) {
227 DCHECK_GE(value, MinEnumValue); 242 DCHECK_GE(value, MinEnumValue);
228 DCHECK_LE(value, MaxEnumValue); 243 DCHECK_LE(value, MaxEnumValue);
229 return value - MinEnumValue; 244 return value - MinEnumValue;
230 } 245 }
231 246
232 static E FromIndex(size_t i) { 247 static E FromIndex(size_t i) {
233 DCHECK_LT(i, kValueCount); 248 DCHECK_LT(i, kValueCount);
(...skipping 28 matching lines...) Expand all
262 277
263 template <typename E, E Min, E Max> 278 template <typename E, E Min, E Max>
264 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, 279 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1,
265 EnumSet<E, Min, Max> set2) { 280 EnumSet<E, Min, Max> set2) {
266 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); 281 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_);
267 } 282 }
268 283
269 } // namespace browser_sync 284 } // namespace browser_sync
270 285
271 #endif // CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_ 286 #endif // CHROME_BROWSER_SYNC_UTIL_ENUM_SET_H_
OLDNEW
« no previous file with comments | « chrome/browser/sync/engine/nigori_util.cc ('k') | chrome/browser/sync/util/enum_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698