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

Side by Side Diff: components/sync/base/enum_set.h

Issue 2701003002: [Sync] Clean up ModelType code. (Closed)
Patch Set: Now with EnumSet tests. Created 3 years, 10 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 COMPONENTS_SYNC_BASE_ENUM_SET_H_ 5 #ifndef COMPONENTS_SYNC_BASE_ENUM_SET_H_
6 #define COMPONENTS_SYNC_BASE_ENUM_SET_H_ 6 #define COMPONENTS_SYNC_BASE_ENUM_SET_H_
7 7
8 #include <bitset> 8 #include <bitset>
9 #include <cstddef> 9 #include <cstddef>
10 #include <string> 10 #include <string>
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 while ((i < kValueCount) && !enums_->test(i)) { 110 while ((i < kValueCount) && !enums_->test(i)) {
111 ++i; 111 ++i;
112 } 112 }
113 return i; 113 return i;
114 } 114 }
115 115
116 const EnumBitSet* enums_; 116 const EnumBitSet* enums_;
117 size_t i_; 117 size_t i_;
118 }; 118 };
119 119
120 // You can construct an EnumSet with 0, 1, 2, or 3 initial values.
121
122 EnumSet() {} 120 EnumSet() {}
123 121
124 explicit EnumSet(E value) { Put(value); } 122 // Recursively chain constructors. Base case is the empty pack.
125 123 template <class... T>
126 EnumSet(E value1, E value2) { 124 EnumSet(E head, T... tail) : EnumSet(tail...) {
127 Put(value1); 125 Put(head);
128 Put(value2);
129 }
130
131 EnumSet(E value1, E value2, E value3) {
132 Put(value1);
133 Put(value2);
134 Put(value3);
135 } 126 }
136 127
137 // Returns an EnumSet with all possible values. 128 // Returns an EnumSet with all possible values.
138 static EnumSet All() { 129 static EnumSet All() {
139 EnumBitSet enums; 130 EnumBitSet enums;
140 enums.set(); 131 enums.set();
141 return EnumSet(enums); 132 return EnumSet(enums);
142 } 133 }
143 134
135 // Returns an EnumSet with all the values from start to end, inclusive.
136 static EnumSet FromRange(E start, E end) {
137 EnumSet set;
138 set.PutRange(start, end);
139 return set;
140 }
141
144 ~EnumSet() {} 142 ~EnumSet() {}
145 143
146 // Copy constructor and assignment welcome. 144 // Copy constructor and assignment welcome.
147 145
148 // Set operations. Put, Retain, and Remove are basically 146 // Set operations. Put, Retain, and Remove are basically
149 // self-mutating versions of Union, Intersection, and Difference 147 // self-mutating versions of Union, Intersection, and Difference
150 // (defined below). 148 // (defined below).
151 149
152 // Adds the given value (which must be in range) to our set. 150 // Adds the given value (which must be in range) to our set.
153 void Put(E value) { enums_.set(ToIndex(value)); } 151 void Put(E value) { enums_.set(ToIndex(value)); }
154 152
155 // Adds all values in the given set to our set. 153 // Adds all values in the given set to our set.
156 void PutAll(EnumSet other) { enums_ |= other.enums_; } 154 void PutAll(EnumSet other) { enums_ |= other.enums_; }
157 155
156 // Adds all values in the given range to our set, inclusive.
157 void PutRange(E start, E end) {
158 int endIndexInclusive = ToIndex(end);
pavely 2017/02/23 23:23:03 Could you DCHECK that ToIndex(start) <= ToIndex(en
skym 2017/02/24 18:59:07 Done.
159 for (int current = ToIndex(start); current <= endIndexInclusive;
160 current++) {
161 enums_.set(current);
162 }
163 }
164
158 // There's no real need for a Retain(E) member function. 165 // There's no real need for a Retain(E) member function.
159 166
160 // Removes all values not in the given set from our set. 167 // Removes all values not in the given set from our set.
161 void RetainAll(EnumSet other) { enums_ &= other.enums_; } 168 void RetainAll(EnumSet other) { enums_ &= other.enums_; }
162 169
163 // If the given value is in range, removes it from our set. 170 // If the given value is in range, removes it from our set.
164 void Remove(E value) { 171 void Remove(E value) {
165 if (InRange(value)) { 172 if (InRange(value)) {
166 enums_.reset(ToIndex(value)); 173 enums_.reset(ToIndex(value));
167 } 174 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 261
255 template <typename E, E Min, E Max> 262 template <typename E, E Min, E Max>
256 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, 263 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1,
257 EnumSet<E, Min, Max> set2) { 264 EnumSet<E, Min, Max> set2) {
258 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); 265 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_);
259 } 266 }
260 267
261 } // namespace syncer 268 } // namespace syncer
262 269
263 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ 270 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698