mfbt/TypedEnum.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 /* Macros to emulate C++11 typed enums and enum classes. */
michael@0 8
michael@0 9 #ifndef mozilla_TypedEnum_h
michael@0 10 #define mozilla_TypedEnum_h
michael@0 11
michael@0 12 #include "mozilla/TypedEnumInternal.h"
michael@0 13 #include "mozilla/MacroArgs.h"
michael@0 14
michael@0 15 #if defined(__cplusplus)
michael@0 16
michael@0 17 /**
michael@0 18 * MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's
michael@0 19 * specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
michael@0 20 * its declaration, and before the opening curly brace, like
michael@0 21 *
michael@0 22 * enum MyEnum MOZ_ENUM_TYPE(uint16_t)
michael@0 23 * {
michael@0 24 * A,
michael@0 25 * B = 7,
michael@0 26 * C
michael@0 27 * };
michael@0 28 *
michael@0 29 * In supporting compilers, the macro will expand to ": uint16_t". The
michael@0 30 * compiler will allocate exactly two bytes for MyEnum and will require all
michael@0 31 * enumerators to have values between 0 and 65535. (Thus specifying "B =
michael@0 32 * 100000" instead of "B = 7" would fail to compile.) In old compilers the
michael@0 33 * macro expands to the empty string, and the underlying type is generally
michael@0 34 * undefined.
michael@0 35 */
michael@0 36 #ifdef MOZ_HAVE_CXX11_ENUM_TYPE
michael@0 37 # define MOZ_ENUM_TYPE(type) : type
michael@0 38 #else
michael@0 39 # define MOZ_ENUM_TYPE(type) /* no support */
michael@0 40 #endif
michael@0 41
michael@0 42 /**
michael@0 43 * MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
michael@0 44 * strongly-typed enumeration feature of C++11 ("enum class"). If supported
michael@0 45 * by the compiler, an enum defined using these macros will not be implicitly
michael@0 46 * converted to any other type, and its enumerators will be scoped using the
michael@0 47 * enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName [, type]) in place of
michael@0 48 * "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
michael@0 49 * "};". For example,
michael@0 50 *
michael@0 51 * MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
michael@0 52 * A,
michael@0 53 * B = 6
michael@0 54 * MOZ_END_ENUM_CLASS(Enum)
michael@0 55 *
michael@0 56 * This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
michael@0 57 * and "B" will not. In compilers that support C++11 strongly-typed
michael@0 58 * enumerations, implicit conversions of Enum values to numeric types will
michael@0 59 * fail. In other compilers, Enum itself will actually be defined as a class,
michael@0 60 * and some implicit conversions will fail while others will succeed.
michael@0 61 *
michael@0 62 * The optional type argument specifies the underlying type for the enum where
michael@0 63 * supported, as with MOZ_ENUM_TYPE(). As with MOZ_ENUM_TYPE(), it will do
michael@0 64 * nothing on compilers that do not support it.
michael@0 65 *
michael@0 66 * MOZ_{BEGIN,END}_ENUM_CLASS doesn't work for defining enum classes nested
michael@0 67 * inside classes. To define an enum class nested inside another class, use
michael@0 68 * MOZ_{BEGIN,END}_NESTED_ENUM_CLASS, and place a MOZ_FINISH_NESTED_ENUM_CLASS
michael@0 69 * in namespace scope to handle bits that can only be implemented with
michael@0 70 * namespace-scoped code. For example:
michael@0 71 *
michael@0 72 * class FooBar {
michael@0 73 *
michael@0 74 * MOZ_BEGIN_NESTED_ENUM_CLASS(Enum, int32_t)
michael@0 75 * A,
michael@0 76 * B = 6
michael@0 77 * MOZ_END_NESTED_ENUM_CLASS(Enum)
michael@0 78 *
michael@0 79 * };
michael@0 80 *
michael@0 81 * MOZ_FINISH_NESTED_ENUM_CLASS(FooBar::Enum)
michael@0 82 */
michael@0 83 #if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
michael@0 84 /*
michael@0 85 * All compilers that support strong enums also support an explicit
michael@0 86 * underlying type, so no extra check is needed.
michael@0 87 */
michael@0 88
michael@0 89 /* Single-argument form. */
michael@0 90 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
michael@0 91 enum class Name {
michael@0 92 /* Two-argument form. */
michael@0 93 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
michael@0 94 enum class Name : type {
michael@0 95 # define MOZ_END_NESTED_ENUM_CLASS(Name) \
michael@0 96 };
michael@0 97 # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) /* nothing */
michael@0 98
michael@0 99 /*
michael@0 100 * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
michael@0 101 * as template parameter types. For that, we need integer types.
michael@0 102 * In the present case where the compiler supports strong enums,
michael@0 103 * these are already integer types so there is nothing more to do.
michael@0 104 */
michael@0 105 # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name
michael@0 106 /*
michael@0 107 * See the comment below about MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE.
michael@0 108 */
michael@0 109 # define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) Name
michael@0 110 #else
michael@0 111 /**
michael@0 112 * We need Name to both name a type, and scope the provided enumerator
michael@0 113 * names. Namespaces and classes both provide scoping, but namespaces
michael@0 114 * aren't types, so we need to use a class that wraps the enum values. We
michael@0 115 * have an implicit conversion from the inner enum type to the class, so
michael@0 116 * statements like
michael@0 117 *
michael@0 118 * Enum x = Enum::A;
michael@0 119 *
michael@0 120 * will still work. We need to define an implicit conversion from the class
michael@0 121 * to the inner enum as well, so that (for instance) switch statements will
michael@0 122 * work. This means that the class can be implicitly converted to a numeric
michael@0 123 * value as well via the enum type, since C++ allows an implicit
michael@0 124 * user-defined conversion followed by a standard conversion to still be
michael@0 125 * implicit.
michael@0 126 *
michael@0 127 * We have an explicit constructor from int defined, so that casts like
michael@0 128 * (Enum)7 will still work. We also have a zero-argument constructor with
michael@0 129 * no arguments, so declaration without initialization (like "Enum foo;")
michael@0 130 * will work.
michael@0 131 *
michael@0 132 * Additionally, we'll delete as many operators as possible for the inner
michael@0 133 * enum type, so statements like this will still fail:
michael@0 134 *
michael@0 135 * f(5 + Enum::B); // deleted operator+
michael@0 136 *
michael@0 137 * But we can't prevent things like this, because C++ doesn't allow
michael@0 138 * overriding conversions or assignment operators for enums:
michael@0 139 *
michael@0 140 * int x = Enum::A;
michael@0 141 * int f()
michael@0 142 * {
michael@0 143 * return Enum::A;
michael@0 144 * }
michael@0 145 */
michael@0 146
michael@0 147 /* Single-argument form. */
michael@0 148 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
michael@0 149 class Name \
michael@0 150 { \
michael@0 151 public: \
michael@0 152 enum Enum \
michael@0 153 {
michael@0 154 /* Two-argument form. */
michael@0 155 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
michael@0 156 class Name \
michael@0 157 { \
michael@0 158 public: \
michael@0 159 enum Enum MOZ_ENUM_TYPE(type) \
michael@0 160 {
michael@0 161 # define MOZ_END_NESTED_ENUM_CLASS(Name) \
michael@0 162 }; \
michael@0 163 Name() {} \
michael@0 164 MOZ_CONSTEXPR Name(Enum aEnum) : mEnum(aEnum) {} \
michael@0 165 template<typename Other> \
michael@0 166 explicit MOZ_CONSTEXPR Name(Other num) : mEnum((Enum)num) {} \
michael@0 167 MOZ_CONSTEXPR operator Enum() const { return mEnum; } \
michael@0 168 explicit MOZ_CONSTEXPR Name(const mozilla::CastableTypedEnumResult<Name>& aOther) \
michael@0 169 : mEnum(aOther.get()) \
michael@0 170 {} \
michael@0 171 private: \
michael@0 172 Enum mEnum; \
michael@0 173 };
michael@0 174 # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) \
michael@0 175 inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 176 inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 177 inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 178 inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 179 inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 180 inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 181 inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 182 inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 183 inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 184 inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 185 inline int operator+(const Name::Enum&) MOZ_DELETE; \
michael@0 186 inline int operator-(const Name::Enum&) MOZ_DELETE; \
michael@0 187 inline int& operator++(Name::Enum&) MOZ_DELETE; \
michael@0 188 inline int operator++(Name::Enum&, int) MOZ_DELETE; \
michael@0 189 inline int& operator--(Name::Enum&) MOZ_DELETE; \
michael@0 190 inline int operator--(Name::Enum&, int) MOZ_DELETE; \
michael@0 191 inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 192 inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 193 inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 194 inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 195 inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 196 inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 197 inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 198 inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 199 inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 200 inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 201 inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 202 inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 203 inline bool operator!(const Name::Enum&) MOZ_DELETE; \
michael@0 204 inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
michael@0 205 inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
michael@0 206 inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
michael@0 207 inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
michael@0 208 inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 209 inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 210 inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 211 inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 212 inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 213 inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 214 inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 215 inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 216 inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \
michael@0 217 inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
michael@0 218 inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 219 inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 220 inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 221 inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 222 inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 223 inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 224 inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 225 inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 226 inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
michael@0 227 inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
michael@0 228
michael@0 229 /*
michael@0 230 * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
michael@0 231 * as template parameter types. For that, we need integer types.
michael@0 232 * In the present case, the integer type is the Enum nested type.
michael@0 233 */
michael@0 234 # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name::Enum
michael@0 235 /*
michael@0 236 * MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE is a variant of MOZ_ENUM_CLASS_ENUM_TYPE
michael@0 237 * to be used when the enum class at hand depends on template parameters.
michael@0 238 *
michael@0 239 * Indeed, if T depends on template parameters, in order to name a nested type
michael@0 240 * in T, C++ does not allow to just write "T::NestedType". Instead, we have
michael@0 241 * to write "typename T::NestedType". The role of this macro is to add
michael@0 242 * this "typename" keywords where needed.
michael@0 243 *
michael@0 244 * Example:
michael@0 245 *
michael@0 246 * template<typename T, MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(T) Value>
michael@0 247 * struct S {};
michael@0 248 *
michael@0 249 * MOZ_BEGIN_ENUM_CLASS(E)
michael@0 250 * Foo,
michael@0 251 * Bar
michael@0 252 * MOZ_END_ENUM_CLASS(E)
michael@0 253 *
michael@0 254 * S<E, E::Bar> s;
michael@0 255 *
michael@0 256 * In this example, the second template parameter to S is meant to be of type T,
michael@0 257 * but on non-C++11 compilers, type T is a class type, not an integer type, so
michael@0 258 * it is not accepted as the type of a constant template parameter. One would
michael@0 259 * then want to use MOZ_ENUM_CLASS_ENUM_TYPE(T), but that doesn't work either
michael@0 260 * as T depends on template parameters (more specifically here, T _is_ a template
michael@0 261 * parameter) so as MOZ_ENUM_CLASS_ENUM_TYPE(T) expands to T::Enum, we are missing
michael@0 262 * the required "typename" keyword. So here, MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE
michael@0 263 * is needed.
michael@0 264 */
michael@0 265 # define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) typename Name::Enum
michael@0 266 #endif
michael@0 267
michael@0 268 # define MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(a, b) a b
michael@0 269 # define MOZ_BEGIN_NESTED_ENUM_CLASS(...) \
michael@0 270 MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE( \
michael@0 271 MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER, \
michael@0 272 __VA_ARGS__), \
michael@0 273 (__VA_ARGS__))
michael@0 274
michael@0 275 # define MOZ_BEGIN_ENUM_CLASS(...) MOZ_BEGIN_NESTED_ENUM_CLASS(__VA_ARGS__)
michael@0 276 # define MOZ_END_ENUM_CLASS(Name) \
michael@0 277 MOZ_END_NESTED_ENUM_CLASS(Name) \
michael@0 278 MOZ_FINISH_NESTED_ENUM_CLASS(Name)
michael@0 279
michael@0 280 #endif /* __cplusplus */
michael@0 281
michael@0 282 #endif /* mozilla_TypedEnum_h */

mercurial