1 // Copyright 2020 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_ENUMMASKITERATOR_H_ 16 #define DAWNNATIVE_ENUMMASKITERATOR_H_ 17 18 #include "common/BitSetIterator.h" 19 #include "dawn_native/EnumClassBitmasks.h" 20 21 namespace dawn_native { 22 23 template <typename T> 24 class EnumMaskIterator final { 25 static constexpr size_t N = EnumBitmaskSize<T>::value; 26 static_assert(N > 0, ""); 27 28 using U = std::underlying_type_t<T>; 29 30 public: EnumMaskIterator(const T & mask)31 EnumMaskIterator(const T& mask) : mBitSetIterator(std::bitset<N>(static_cast<U>(mask))) { 32 // If you hit this ASSERT it means that you forgot to update EnumBitmaskSize<T>::value; 33 ASSERT(U(mask) == 0 || Log2(uint64_t(U(mask))) < N); 34 } 35 36 class Iterator final { 37 public: Iterator(const typename BitSetIterator<N,U>::Iterator & iter)38 Iterator(const typename BitSetIterator<N, U>::Iterator& iter) : mIter(iter) { 39 } 40 41 Iterator& operator++() { 42 ++mIter; 43 return *this; 44 } 45 46 bool operator==(const Iterator& other) const { 47 return mIter == other.mIter; 48 } 49 50 bool operator!=(const Iterator& other) const { 51 return mIter != other.mIter; 52 } 53 54 T operator*() const { 55 U value = *mIter; 56 return static_cast<T>(U(1) << value); 57 } 58 59 private: 60 typename BitSetIterator<N, U>::Iterator mIter; 61 }; 62 begin()63 Iterator begin() const { 64 return Iterator(mBitSetIterator.begin()); 65 } 66 end()67 Iterator end() const { 68 return Iterator(mBitSetIterator.end()); 69 } 70 71 private: 72 BitSetIterator<N, U> mBitSetIterator; 73 }; 74 75 template <typename T> IterateEnumMask(const T & mask)76 EnumMaskIterator<T> IterateEnumMask(const T& mask) { 77 return EnumMaskIterator<T>(mask); 78 } 79 80 } // namespace dawn_native 81 82 #endif // DAWNNATIVE_ENUMMASKITERATOR_H_ 83