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

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

Issue 2859033002: [sync] Add constexpr to EnumSet (Closed)
Patch Set: Move reading list switches and buildflag to /features Created 3 years, 6 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
« no previous file with comments | « components/sync/base/DEPS ('k') | components/sync/base/enum_set_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 EnumSet() {} 120 EnumSet() {}
121 121
122 // Recursively chain constructors. Base case is the empty pack. 122 ~EnumSet() = default;
123 template <class... T> 123
124 EnumSet(E head, T... tail) : EnumSet(tail...) { 124 static constexpr uint64_t single_val_bitstring(E val) {
125 Put(head); 125 return 1ULL << (ToIndex(val));
126 } 126 }
127 127
128 // Base case for recursive packing of a list of enum values. The uint64_t
129 // corresponding to an empty list is 0.
130 static constexpr uint64_t bitstring() { return 0ULL; }
131
132 // As of writing, constexpr expressions can't contain anything other than a
133 // return statement (and static asserts). To pack a variable number of enum
134 // value arguments into a bitstring, we use template varargs with a recursive
135 // constructor. Each recursive call packs one more enum into the bitstring,
136 // and the individual results are combined with bitwise or.
137 template <class... T>
138 static constexpr uint64_t bitstring(E head, T... tail) {
139 return (single_val_bitstring(head)) | bitstring(tail...);
140 }
141
142 template <class... T>
143 constexpr EnumSet(E head, T... tail)
144 : EnumSet(EnumBitSet(bitstring(head, tail...))) {}
145
128 // Returns an EnumSet with all possible values. 146 // Returns an EnumSet with all possible values.
129 static EnumSet All() { 147 static constexpr EnumSet All() {
130 EnumBitSet enums; 148 return EnumSet(EnumBitSet((1ULL << kValueCount) - 1));
131 enums.set();
132 return EnumSet(enums);
133 } 149 }
134 150
135 // Returns an EnumSet with all the values from start to end, inclusive. 151 // Returns an EnumSet with all the values from start to end, inclusive.
136 static EnumSet FromRange(E start, E end) { 152 static constexpr EnumSet FromRange(E start, E end) {
137 EnumSet set; 153 return EnumSet(EnumBitSet(
138 set.PutRange(start, end); 154 ((single_val_bitstring(end)) - (single_val_bitstring(start))) |
139 return set; 155 (single_val_bitstring(end))));
140 } 156 }
141 157
142 ~EnumSet() {}
143
144 // Copy constructor and assignment welcome. 158 // Copy constructor and assignment welcome.
145 159
146 // Set operations. Put, Retain, and Remove are basically 160 // Set operations. Put, Retain, and Remove are basically
147 // self-mutating versions of Union, Intersection, and Difference 161 // self-mutating versions of Union, Intersection, and Difference
148 // (defined below). 162 // (defined below).
149 163
150 // Adds the given value (which must be in range) to our set. 164 // Adds the given value (which must be in range) to our set.
151 void Put(E value) { enums_.set(ToIndex(value)); } 165 void Put(E value) { enums_.set(ToIndex(value)); }
152 166
153 // Adds all values in the given set to our set. 167 // Adds all values in the given set to our set.
(...skipping 21 matching lines...) Expand all
175 } 189 }
176 } 190 }
177 191
178 // Removes all values in the given set from our set. 192 // Removes all values in the given set from our set.
179 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; } 193 void RemoveAll(EnumSet other) { enums_ &= ~other.enums_; }
180 194
181 // Removes all values from our set. 195 // Removes all values from our set.
182 void Clear() { enums_.reset(); } 196 void Clear() { enums_.reset(); }
183 197
184 // Returns true iff the given value is in range and a member of our set. 198 // Returns true iff the given value is in range and a member of our set.
185 bool Has(E value) const { 199 constexpr bool Has(E value) const {
186 return InRange(value) && enums_.test(ToIndex(value)); 200 return InRange(value) && enums_[ToIndex(value)];
187 } 201 }
188 202
189 // Returns true iff the given set is a subset of our set. 203 // Returns true iff the given set is a subset of our set.
190 bool HasAll(EnumSet other) const { 204 bool HasAll(EnumSet other) const {
191 return (enums_ & other.enums_) == other.enums_; 205 return (enums_ & other.enums_) == other.enums_;
192 } 206 }
193 207
194 // Returns true iff our set is empty. 208 // Returns true iff our set is empty.
195 bool Empty() const { return !enums_.any(); } 209 bool Empty() const { return !enums_.any(); }
196 210
(...skipping 11 matching lines...) Expand all
208 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; } 222 bool operator!=(const EnumSet& other) const { return enums_ != other.enums_; }
209 223
210 private: 224 private:
211 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 225 friend EnumSet Union<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
212 EnumSet set2); 226 EnumSet set2);
213 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 227 friend EnumSet Intersection<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
214 EnumSet set2); 228 EnumSet set2);
215 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1, 229 friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1,
216 EnumSet set2); 230 EnumSet set2);
217 231
218 explicit EnumSet(EnumBitSet enums) : enums_(enums) {} 232 // A bitset can't be constexpr constructed if it has size > 64, since the
233 // constexpr constructor uses a uint64_t. If your EnumSet has > 64 values, you
234 // can safely remove the constepxr qualifiers from this file, at the cost of
235 // some minor optimizations.
236 explicit constexpr EnumSet(EnumBitSet enums) : enums_(enums) {
237 static_assert(kValueCount < 64,
238 "Max number of enum values is 64 for constexpr ");
239 }
219 240
220 static bool InRange(E value) { 241 static constexpr bool InRange(E value) {
221 return (value >= MinEnumValue) && (value <= MaxEnumValue); 242 return (value >= MinEnumValue) && (value <= MaxEnumValue);
222 } 243 }
223 244
224 // Converts a value to/from an index into |enums_|. 245 // Converts a value to/from an index into |enums_|.
225 246
226 static size_t ToIndex(E value) { 247 static constexpr size_t ToIndex(E value) { return value - MinEnumValue; }
227 DCHECK_GE(value, MinEnumValue);
228 DCHECK_LE(value, MaxEnumValue);
229 return value - MinEnumValue;
230 }
231 248
232 static E FromIndex(size_t i) { 249 static E FromIndex(size_t i) {
233 DCHECK_LT(i, kValueCount); 250 DCHECK_LT(i, kValueCount);
234 return static_cast<E>(MinEnumValue + i); 251 return static_cast<E>(MinEnumValue + i);
235 } 252 }
236 253
237 EnumBitSet enums_; 254 EnumBitSet enums_;
238 }; 255 };
239 256
240 template <typename E, E MinEnumValue, E MaxEnumValue> 257 template <typename E, E MinEnumValue, E MaxEnumValue>
(...skipping 21 matching lines...) Expand all
262 279
263 template <typename E, E Min, E Max> 280 template <typename E, E Min, E Max>
264 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1, 281 EnumSet<E, Min, Max> Difference(EnumSet<E, Min, Max> set1,
265 EnumSet<E, Min, Max> set2) { 282 EnumSet<E, Min, Max> set2) {
266 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_); 283 return EnumSet<E, Min, Max>(set1.enums_ & ~set2.enums_);
267 } 284 }
268 285
269 } // namespace syncer 286 } // namespace syncer
270 287
271 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_ 288 #endif // COMPONENTS_SYNC_BASE_ENUM_SET_H_
OLDNEW
« no previous file with comments | « components/sync/base/DEPS ('k') | components/sync/base/enum_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698