mfbt/TypedEnumBits.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS allows using a typed enum as bit flags. */
michael@0 8
michael@0 9 #ifndef mozilla_TypedEnumBits_h
michael@0 10 #define mozilla_TypedEnumBits_h
michael@0 11
michael@0 12 #include "mozilla/IntegerTypeTraits.h"
michael@0 13 #include "mozilla/TypedEnumInternal.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 #define MOZ_CASTABLETYPEDENUMRESULT_BINOP(Op, OtherType, ReturnType) \
michael@0 18 template<typename E> \
michael@0 19 MOZ_CONSTEXPR ReturnType \
michael@0 20 operator Op(const OtherType& e, const CastableTypedEnumResult<E>& r) \
michael@0 21 { \
michael@0 22 return ReturnType(e Op OtherType(r)); \
michael@0 23 } \
michael@0 24 template<typename E> \
michael@0 25 MOZ_CONSTEXPR ReturnType \
michael@0 26 operator Op(const CastableTypedEnumResult<E>& r, const OtherType& e) \
michael@0 27 { \
michael@0 28 return ReturnType(OtherType(r) Op e); \
michael@0 29 } \
michael@0 30 template<typename E> \
michael@0 31 MOZ_CONSTEXPR ReturnType \
michael@0 32 operator Op(const CastableTypedEnumResult<E>& r1, \
michael@0 33 const CastableTypedEnumResult<E>& r2) \
michael@0 34 { \
michael@0 35 return ReturnType(OtherType(r1) Op OtherType(r2)); \
michael@0 36 }
michael@0 37
michael@0 38 MOZ_CASTABLETYPEDENUMRESULT_BINOP(|, E, CastableTypedEnumResult<E>)
michael@0 39 MOZ_CASTABLETYPEDENUMRESULT_BINOP(&, E, CastableTypedEnumResult<E>)
michael@0 40 MOZ_CASTABLETYPEDENUMRESULT_BINOP(^, E, CastableTypedEnumResult<E>)
michael@0 41 MOZ_CASTABLETYPEDENUMRESULT_BINOP(==, E, bool)
michael@0 42 MOZ_CASTABLETYPEDENUMRESULT_BINOP(!=, E, bool)
michael@0 43 MOZ_CASTABLETYPEDENUMRESULT_BINOP(||, bool, bool)
michael@0 44 MOZ_CASTABLETYPEDENUMRESULT_BINOP(&&, bool, bool)
michael@0 45
michael@0 46 template <typename E>
michael@0 47 MOZ_CONSTEXPR CastableTypedEnumResult<E>
michael@0 48 operator ~(const CastableTypedEnumResult<E>& r)
michael@0 49 {
michael@0 50 return CastableTypedEnumResult<E>(~(E(r)));
michael@0 51 }
michael@0 52
michael@0 53 #define MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(Op) \
michael@0 54 template<typename E> \
michael@0 55 E& \
michael@0 56 operator Op(E& r1, \
michael@0 57 const CastableTypedEnumResult<E>& r2) \
michael@0 58 { \
michael@0 59 return r1 Op E(r2); \
michael@0 60 }
michael@0 61
michael@0 62 MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(&=)
michael@0 63 MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(|=)
michael@0 64 MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(^=)
michael@0 65
michael@0 66 #undef MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP
michael@0 67
michael@0 68 #undef MOZ_CASTABLETYPEDENUMRESULT_BINOP
michael@0 69
michael@0 70 #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
michael@0 71
michael@0 72 #define MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(Op, ReturnType) \
michael@0 73 template<typename E> \
michael@0 74 MOZ_CONSTEXPR ReturnType \
michael@0 75 operator Op(typename E::Enum e, const CastableTypedEnumResult<E>& r) \
michael@0 76 { \
michael@0 77 return ReturnType(e Op E(r)); \
michael@0 78 } \
michael@0 79 template<typename E> \
michael@0 80 MOZ_CONSTEXPR ReturnType \
michael@0 81 operator Op(const CastableTypedEnumResult<E>& r, typename E::Enum e) \
michael@0 82 { \
michael@0 83 return ReturnType(E(r) Op e); \
michael@0 84 }
michael@0 85
michael@0 86 MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(|, CastableTypedEnumResult<E>)
michael@0 87 MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(&, CastableTypedEnumResult<E>)
michael@0 88 MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(^, CastableTypedEnumResult<E>)
michael@0 89 MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(==, bool)
michael@0 90 MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(!=, bool)
michael@0 91
michael@0 92 #undef MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11
michael@0 93
michael@0 94 #endif // not MOZ_HAVE_CXX11_STRONG_ENUMS
michael@0 95
michael@0 96 namespace detail {
michael@0 97 template<typename E>
michael@0 98 struct UnsignedIntegerTypeForEnum
michael@0 99 : UnsignedStdintTypeForSize<sizeof(E)>
michael@0 100 {};
michael@0 101 }
michael@0 102
michael@0 103 } // namespace mozilla
michael@0 104
michael@0 105 #define MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, Op) \
michael@0 106 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 107 operator Op(Name a, Name b) \
michael@0 108 { \
michael@0 109 typedef mozilla::CastableTypedEnumResult<Name> Result; \
michael@0 110 typedef mozilla::detail::UnsignedIntegerTypeForEnum<Name>::Type U; \
michael@0 111 return Result(Name(U(a) Op U(b))); \
michael@0 112 } \
michael@0 113 \
michael@0 114 inline Name& \
michael@0 115 operator Op##=(Name& a, Name b) \
michael@0 116 { \
michael@0 117 return a = a Op b; \
michael@0 118 }
michael@0 119
michael@0 120 #define MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name) \
michael@0 121 MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, |) \
michael@0 122 MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, &) \
michael@0 123 MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, ^) \
michael@0 124 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 125 operator~(Name a) \
michael@0 126 { \
michael@0 127 typedef mozilla::CastableTypedEnumResult<Name> Result; \
michael@0 128 typedef mozilla::detail::UnsignedIntegerTypeForEnum<Name>::Type U; \
michael@0 129 return Result(Name(~(U(a)))); \
michael@0 130 }
michael@0 131
michael@0 132 #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
michael@0 133 # define MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, Op) \
michael@0 134 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 135 operator Op(Name a, Name::Enum b) \
michael@0 136 { \
michael@0 137 return a Op Name(b); \
michael@0 138 } \
michael@0 139 \
michael@0 140 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 141 operator Op(Name::Enum a, Name b) \
michael@0 142 { \
michael@0 143 return Name(a) Op b; \
michael@0 144 } \
michael@0 145 \
michael@0 146 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 147 operator Op(Name::Enum a, Name::Enum b) \
michael@0 148 { \
michael@0 149 return Name(a) Op Name(b); \
michael@0 150 } \
michael@0 151 \
michael@0 152 inline Name& \
michael@0 153 operator Op##=(Name& a, Name::Enum b) \
michael@0 154 { \
michael@0 155 return a = a Op Name(b); \
michael@0 156 }
michael@0 157
michael@0 158 # define MOZ_MAKE_ENUM_CLASS_OPS_EXTRA_NON_CXX11(Name) \
michael@0 159 MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, |) \
michael@0 160 MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, &) \
michael@0 161 MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, ^) \
michael@0 162 inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
michael@0 163 operator~(Name::Enum a) \
michael@0 164 { \
michael@0 165 return ~(Name(a)); \
michael@0 166 }
michael@0 167 #endif
michael@0 168
michael@0 169 /**
michael@0 170 * MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS generates standard bitwise operators
michael@0 171 * for the given enum type. Use this to enable using an enum type as bit-field.
michael@0 172 */
michael@0 173 #ifdef MOZ_HAVE_CXX11_STRONG_ENUMS
michael@0 174 # define MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Name) \
michael@0 175 MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name)
michael@0 176 #else
michael@0 177 # define MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Name) \
michael@0 178 MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name) \
michael@0 179 MOZ_MAKE_ENUM_CLASS_OPS_EXTRA_NON_CXX11(Name)
michael@0 180 #endif
michael@0 181
michael@0 182 #endif // mozilla_TypedEnumBits_h

mercurial