| 1 | /* |
| 2 | * Copyright (C) 2016-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include <initializer_list> |
| 29 | #include <iterator> |
| 30 | #include <type_traits> |
| 31 | #include <wtf/Assertions.h> |
| 32 | #include <wtf/MathExtras.h> |
| 33 | |
| 34 | namespace WTF { |
| 35 | |
| 36 | // OptionSet is a class that represents a set of enumerators in a space-efficient manner. The enumerators |
| 37 | // must be powers of two greater than 0. This class is useful as a replacement for passing a bitmask of |
| 38 | // enumerators around. |
| 39 | template<typename T> class OptionSet { |
| 40 | static_assert(std::is_enum<T>::value, "T is not an enum type" ); |
| 41 | typedef typename std::make_unsigned<typename std::underlying_type<T>::type>::type StorageType; |
| 42 | |
| 43 | public: |
| 44 | template<typename StorageType> class Iterator { |
| 45 | public: |
| 46 | // Isolate the rightmost set bit. |
| 47 | T operator*() const { return static_cast<T>(m_value & -m_value); } |
| 48 | |
| 49 | // Iterates from smallest to largest enum value by turning off the rightmost set bit. |
| 50 | Iterator& operator++() |
| 51 | { |
| 52 | m_value &= m_value - 1; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | Iterator& operator++(int) = delete; |
| 57 | |
| 58 | bool operator==(const Iterator& other) const { return m_value == other.m_value; } |
| 59 | bool operator!=(const Iterator& other) const { return m_value != other.m_value; } |
| 60 | |
| 61 | private: |
| 62 | Iterator(StorageType value) : m_value(value) { } |
| 63 | friend OptionSet; |
| 64 | |
| 65 | StorageType m_value; |
| 66 | }; |
| 67 | using iterator = Iterator<StorageType>; |
| 68 | |
| 69 | static constexpr OptionSet fromRaw(StorageType storageType) |
| 70 | { |
| 71 | return OptionSet(static_cast<T>(storageType), FromRawValue); |
| 72 | } |
| 73 | |
| 74 | constexpr OptionSet() = default; |
| 75 | |
| 76 | constexpr OptionSet(T t) |
| 77 | : m_storage(static_cast<StorageType>(t)) |
| 78 | { |
| 79 | ASSERT_WITH_MESSAGE(!m_storage || hasOneBitSet(m_storage), "Enumerator is not a zero or a positive power of two." ); |
| 80 | } |
| 81 | |
| 82 | constexpr OptionSet(std::initializer_list<T> initializerList) |
| 83 | { |
| 84 | for (auto& option : initializerList) { |
| 85 | ASSERT_WITH_MESSAGE(hasOneBitSet(static_cast<StorageType>(option)), "Enumerator is not a positive power of two." ); |
| 86 | m_storage |= static_cast<StorageType>(option); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | constexpr StorageType toRaw() const { return m_storage; } |
| 91 | |
| 92 | constexpr bool isEmpty() const { return !m_storage; } |
| 93 | |
| 94 | constexpr iterator begin() const { return m_storage; } |
| 95 | constexpr iterator end() const { return 0; } |
| 96 | |
| 97 | constexpr explicit operator bool() { return !isEmpty(); } |
| 98 | |
| 99 | constexpr bool contains(T option) const |
| 100 | { |
| 101 | return containsAny(option); |
| 102 | } |
| 103 | |
| 104 | constexpr bool containsAny(OptionSet optionSet) const |
| 105 | { |
| 106 | return !!(*this & optionSet); |
| 107 | } |
| 108 | |
| 109 | constexpr bool containsAll(OptionSet optionSet) const |
| 110 | { |
| 111 | return (*this & optionSet) == optionSet; |
| 112 | } |
| 113 | |
| 114 | constexpr void add(OptionSet optionSet) |
| 115 | { |
| 116 | m_storage |= optionSet.m_storage; |
| 117 | } |
| 118 | |
| 119 | constexpr void remove(OptionSet optionSet) |
| 120 | { |
| 121 | m_storage &= ~optionSet.m_storage; |
| 122 | } |
| 123 | |
| 124 | constexpr friend bool operator==(OptionSet lhs, OptionSet rhs) |
| 125 | { |
| 126 | return lhs.m_storage == rhs.m_storage; |
| 127 | } |
| 128 | |
| 129 | constexpr friend bool operator!=(OptionSet lhs, OptionSet rhs) |
| 130 | { |
| 131 | return lhs.m_storage != rhs.m_storage; |
| 132 | } |
| 133 | |
| 134 | constexpr friend OptionSet operator|(OptionSet lhs, OptionSet rhs) |
| 135 | { |
| 136 | return fromRaw(lhs.m_storage | rhs.m_storage); |
| 137 | } |
| 138 | |
| 139 | constexpr friend OptionSet operator&(OptionSet lhs, OptionSet rhs) |
| 140 | { |
| 141 | return fromRaw(lhs.m_storage & rhs.m_storage); |
| 142 | } |
| 143 | |
| 144 | constexpr friend OptionSet operator-(OptionSet lhs, OptionSet rhs) |
| 145 | { |
| 146 | return fromRaw(lhs.m_storage & ~rhs.m_storage); |
| 147 | } |
| 148 | |
| 149 | constexpr friend OptionSet operator^(OptionSet lhs, OptionSet rhs) |
| 150 | { |
| 151 | return fromRaw(lhs.m_storage ^ rhs.m_storage); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | enum InitializationTag { FromRawValue }; |
| 156 | constexpr OptionSet(T t, InitializationTag) |
| 157 | : m_storage(static_cast<StorageType>(t)) |
| 158 | { |
| 159 | } |
| 160 | StorageType m_storage { 0 }; |
| 161 | }; |
| 162 | |
| 163 | } |
| 164 | |
| 165 | using WTF::OptionSet; |
| 166 | |