Page Speed Optimization Libraries  1.5.27.2
net/instaweb/util/public/enum_set.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2013 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_ENUM_SET_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_ENUM_SET_H_
00021 
00022 #include <bitset>
00023 #include <cstddef>
00024 
00025 namespace net_instaweb {
00026 
00028 template<typename EnumType, size_t NumEnums> class EnumSet {
00029  public:
00030   bool IsSet(EnumType value) const {
00031     return bits_.test(static_cast<size_t>(value));
00032   }
00033 
00035   bool Insert(EnumType value) {
00036     bool result = !IsSet(value);
00037     insert(value);
00038     return result;
00039   }
00040 
00044   void insert(EnumType value) {
00045     bits_.set(static_cast<size_t>(value));
00046   }
00047 
00049   bool Erase(EnumType value) {
00050     bool result = IsSet(value);
00051     bits_.reset(static_cast<size_t>(value));
00052     return result;
00053   }
00054 
00056   bool Merge(const EnumSet& src) {
00060     EnumSet save(*this);
00061     bits_ |= src.bits_;
00062     return bits_ != save.bits_;
00063   }
00064 
00067   bool MergeInverted(const EnumSet& src) {
00068     EnumSet save(*this);
00069     bits_ |= ~src.bits_;
00070     return bits_ != save.bits_;
00071   }
00072 
00073   void EraseSet(const EnumSet& src) {
00074     bits_ &= ~src.bits_;
00075   }
00076 
00078   void SetAll() {
00079     bits_.set();
00080   }
00081 
00083   void clear() { bits_.reset(); }
00084   size_t size() const { return bits_.count(); }
00085   bool empty() const { return bits_.none(); }
00086 
00088   bool operator==(const EnumSet& that) const {
00089     return bits_ == that.bits_;
00090   }
00091 
00092  private:
00093   typedef std::bitset<NumEnums> BitSet;
00094   BitSet bits_;
00095 
00097 };
00098 
00099 }  
00100 
00101 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_ENUM_SET_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines