Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
enum_set.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_ENUM_SET_H_
20 #define PAGESPEED_KERNEL_BASE_ENUM_SET_H_
21 
22 #include <bitset>
23 #include <cstddef>
24 
25 namespace net_instaweb {
26 
28 template<typename EnumType, size_t NumEnums> class EnumSet {
29  public:
30  bool IsSet(EnumType value) const {
31  return bits_.test(static_cast<size_t>(value));
32  }
33 
35  bool Insert(EnumType value) {
36  bool result = !IsSet(value);
37  insert(value);
38  return result;
39  }
40 
44  void insert(EnumType value) {
45  bits_.set(static_cast<size_t>(value));
46  }
47 
49  bool Erase(EnumType value) {
50  bool result = IsSet(value);
51  bits_.reset(static_cast<size_t>(value));
52  return result;
53  }
54 
56  bool Merge(const EnumSet& src) {
60  EnumSet save(*this);
61  bits_ |= src.bits_;
62  return bits_ != save.bits_;
63  }
64 
67  bool MergeInverted(const EnumSet& src) {
68  EnumSet save(*this);
69  bits_ |= ~src.bits_;
70  return bits_ != save.bits_;
71  }
72 
73  void EraseSet(const EnumSet& src) {
74  bits_ &= ~src.bits_;
75  }
76 
78  void SetAll() {
79  bits_.set();
80  }
81 
83  void clear() { bits_.reset(); }
84  size_t size() const { return bits_.count(); }
85  bool empty() const { return bits_.none(); }
86 
88  bool operator==(const EnumSet& that) const {
89  return bits_ == that.bits_;
90  }
91 
92  private:
93  typedef std::bitset<NumEnums> BitSet;
94  BitSet bits_;
95 
97 };
98 
99 }
100 
101 #endif
void insert(EnumType value)
Definition: enum_set.h:44
bool Insert(EnumType value)
Inserts a new value, returning true if a change was made.
Definition: enum_set.h:35
Represents a set of values – implemented via a bitset.
Definition: enum_set.h:28
bool operator==(const EnumSet &that) const
This overload is required for use in EXPECT_EQ in tests.
Definition: enum_set.h:88
bool Erase(EnumType value)
Returns true if a change was made.
Definition: enum_set.h:49
void SetAll()
Sets all the entries to true.
Definition: enum_set.h:78
void clear()
Standard STL-like methods.
Definition: enum_set.h:83
bool MergeInverted(const EnumSet &src)
Definition: enum_set.h:67
bool Merge(const EnumSet &src)
Merges src into this, returning whether this resulted in a change.
Definition: enum_set.h:56