// Copyright 2017 The Dawn Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef INCLUDE_DAWN_ENUMCLASSBITMASKS_H_ #define INCLUDE_DAWN_ENUMCLASSBITMASKS_H_ #include // The operators in dawn:: namespace need be introduced into other namespaces with // using-declarations for C++ Argument Dependent Lookup to work. #define DAWN_IMPORT_BITMASK_OPERATORS \ using dawn::operator|; \ using dawn::operator&; \ using dawn::operator^; \ using dawn::operator~; \ using dawn::operator&=; \ using dawn::operator|=; \ using dawn::operator^=; \ using dawn::HasZeroOrOneBits; namespace dawn { template struct IsDawnBitmask { static constexpr bool enable = false; }; template struct LowerBitmask { static constexpr bool enable = false; }; template struct LowerBitmask::enable>::type> { static constexpr bool enable = true; using type = T; constexpr static T Lower(T t) { return t; } }; template struct BoolConvertible { using Integral = typename std::underlying_type::type; // NOLINTNEXTLINE(runtime/explicit) constexpr BoolConvertible(Integral value) : value(value) { } constexpr operator bool() const { return value != 0; } constexpr operator T() const { return static_cast(value); } Integral value; }; template struct LowerBitmask> { static constexpr bool enable = true; using type = T; static constexpr type Lower(BoolConvertible t) { return t; } }; template ::enable && LowerBitmask::enable>::type> constexpr BoolConvertible::type> operator|(T1 left, T2 right) { using T = typename LowerBitmask::type; using Integral = typename std::underlying_type::type; return static_cast(LowerBitmask::Lower(left)) | static_cast(LowerBitmask::Lower(right)); } template ::enable && LowerBitmask::enable>::type> constexpr BoolConvertible::type> operator&(T1 left, T2 right) { using T = typename LowerBitmask::type; using Integral = typename std::underlying_type::type; return static_cast(LowerBitmask::Lower(left)) & static_cast(LowerBitmask::Lower(right)); } template ::enable && LowerBitmask::enable>::type> constexpr BoolConvertible::type> operator^(T1 left, T2 right) { using T = typename LowerBitmask::type; using Integral = typename std::underlying_type::type; return static_cast(LowerBitmask::Lower(left)) ^ static_cast(LowerBitmask::Lower(right)); } template constexpr BoolConvertible::type> operator~(T1 t) { using T = typename LowerBitmask::type; using Integral = typename std::underlying_type::type; return ~static_cast(LowerBitmask::Lower(t)); } template ::enable && LowerBitmask::enable>::type> constexpr T& operator&=(T& l, T2 right) { T r = LowerBitmask::Lower(right); l = l & r; return l; } template ::enable && LowerBitmask::enable>::type> constexpr T& operator|=(T& l, T2 right) { T r = LowerBitmask::Lower(right); l = l | r; return l; } template ::enable && LowerBitmask::enable>::type> constexpr T& operator^=(T& l, T2 right) { T r = LowerBitmask::Lower(right); l = l ^ r; return l; } template constexpr bool HasZeroOrOneBits(T value) { using Integral = typename std::underlying_type::type; return (static_cast(value) & (static_cast(value) - 1)) == 0; } } // namespace dawn #endif // INCLUDE_DAWN_ENUMCLASSBITMASKS_H_