michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Macros to emulate C++11 typed enums and enum classes. */ michael@0: michael@0: #ifndef mozilla_TypedEnum_h michael@0: #define mozilla_TypedEnum_h michael@0: michael@0: #include "mozilla/TypedEnumInternal.h" michael@0: #include "mozilla/MacroArgs.h" michael@0: michael@0: #if defined(__cplusplus) michael@0: michael@0: /** michael@0: * MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's michael@0: * specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in michael@0: * its declaration, and before the opening curly brace, like michael@0: * michael@0: * enum MyEnum MOZ_ENUM_TYPE(uint16_t) michael@0: * { michael@0: * A, michael@0: * B = 7, michael@0: * C michael@0: * }; michael@0: * michael@0: * In supporting compilers, the macro will expand to ": uint16_t". The michael@0: * compiler will allocate exactly two bytes for MyEnum and will require all michael@0: * enumerators to have values between 0 and 65535. (Thus specifying "B = michael@0: * 100000" instead of "B = 7" would fail to compile.) In old compilers the michael@0: * macro expands to the empty string, and the underlying type is generally michael@0: * undefined. michael@0: */ michael@0: #ifdef MOZ_HAVE_CXX11_ENUM_TYPE michael@0: # define MOZ_ENUM_TYPE(type) : type michael@0: #else michael@0: # define MOZ_ENUM_TYPE(type) /* no support */ michael@0: #endif michael@0: michael@0: /** michael@0: * MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the michael@0: * strongly-typed enumeration feature of C++11 ("enum class"). If supported michael@0: * by the compiler, an enum defined using these macros will not be implicitly michael@0: * converted to any other type, and its enumerators will be scoped using the michael@0: * enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName [, type]) in place of michael@0: * "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing michael@0: * "};". For example, michael@0: * michael@0: * MOZ_BEGIN_ENUM_CLASS(Enum, int32_t) michael@0: * A, michael@0: * B = 6 michael@0: * MOZ_END_ENUM_CLASS(Enum) michael@0: * michael@0: * This will make "Enum::A" and "Enum::B" appear in the global scope, but "A" michael@0: * and "B" will not. In compilers that support C++11 strongly-typed michael@0: * enumerations, implicit conversions of Enum values to numeric types will michael@0: * fail. In other compilers, Enum itself will actually be defined as a class, michael@0: * and some implicit conversions will fail while others will succeed. michael@0: * michael@0: * The optional type argument specifies the underlying type for the enum where michael@0: * supported, as with MOZ_ENUM_TYPE(). As with MOZ_ENUM_TYPE(), it will do michael@0: * nothing on compilers that do not support it. michael@0: * michael@0: * MOZ_{BEGIN,END}_ENUM_CLASS doesn't work for defining enum classes nested michael@0: * inside classes. To define an enum class nested inside another class, use michael@0: * MOZ_{BEGIN,END}_NESTED_ENUM_CLASS, and place a MOZ_FINISH_NESTED_ENUM_CLASS michael@0: * in namespace scope to handle bits that can only be implemented with michael@0: * namespace-scoped code. For example: michael@0: * michael@0: * class FooBar { michael@0: * michael@0: * MOZ_BEGIN_NESTED_ENUM_CLASS(Enum, int32_t) michael@0: * A, michael@0: * B = 6 michael@0: * MOZ_END_NESTED_ENUM_CLASS(Enum) michael@0: * michael@0: * }; michael@0: * michael@0: * MOZ_FINISH_NESTED_ENUM_CLASS(FooBar::Enum) michael@0: */ michael@0: #if defined(MOZ_HAVE_CXX11_STRONG_ENUMS) michael@0: /* michael@0: * All compilers that support strong enums also support an explicit michael@0: * underlying type, so no extra check is needed. michael@0: */ michael@0: michael@0: /* Single-argument form. */ michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \ michael@0: enum class Name { michael@0: /* Two-argument form. */ michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \ michael@0: enum class Name : type { michael@0: # define MOZ_END_NESTED_ENUM_CLASS(Name) \ michael@0: }; michael@0: # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) /* nothing */ michael@0: michael@0: /* michael@0: * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes michael@0: * as template parameter types. For that, we need integer types. michael@0: * In the present case where the compiler supports strong enums, michael@0: * these are already integer types so there is nothing more to do. michael@0: */ michael@0: # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name michael@0: /* michael@0: * See the comment below about MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE. michael@0: */ michael@0: # define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) Name michael@0: #else michael@0: /** michael@0: * We need Name to both name a type, and scope the provided enumerator michael@0: * names. Namespaces and classes both provide scoping, but namespaces michael@0: * aren't types, so we need to use a class that wraps the enum values. We michael@0: * have an implicit conversion from the inner enum type to the class, so michael@0: * statements like michael@0: * michael@0: * Enum x = Enum::A; michael@0: * michael@0: * will still work. We need to define an implicit conversion from the class michael@0: * to the inner enum as well, so that (for instance) switch statements will michael@0: * work. This means that the class can be implicitly converted to a numeric michael@0: * value as well via the enum type, since C++ allows an implicit michael@0: * user-defined conversion followed by a standard conversion to still be michael@0: * implicit. michael@0: * michael@0: * We have an explicit constructor from int defined, so that casts like michael@0: * (Enum)7 will still work. We also have a zero-argument constructor with michael@0: * no arguments, so declaration without initialization (like "Enum foo;") michael@0: * will work. michael@0: * michael@0: * Additionally, we'll delete as many operators as possible for the inner michael@0: * enum type, so statements like this will still fail: michael@0: * michael@0: * f(5 + Enum::B); // deleted operator+ michael@0: * michael@0: * But we can't prevent things like this, because C++ doesn't allow michael@0: * overriding conversions or assignment operators for enums: michael@0: * michael@0: * int x = Enum::A; michael@0: * int f() michael@0: * { michael@0: * return Enum::A; michael@0: * } michael@0: */ michael@0: michael@0: /* Single-argument form. */ michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \ michael@0: class Name \ michael@0: { \ michael@0: public: \ michael@0: enum Enum \ michael@0: { michael@0: /* Two-argument form. */ michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \ michael@0: class Name \ michael@0: { \ michael@0: public: \ michael@0: enum Enum MOZ_ENUM_TYPE(type) \ michael@0: { michael@0: # define MOZ_END_NESTED_ENUM_CLASS(Name) \ michael@0: }; \ michael@0: Name() {} \ michael@0: MOZ_CONSTEXPR Name(Enum aEnum) : mEnum(aEnum) {} \ michael@0: template \ michael@0: explicit MOZ_CONSTEXPR Name(Other num) : mEnum((Enum)num) {} \ michael@0: MOZ_CONSTEXPR operator Enum() const { return mEnum; } \ michael@0: explicit MOZ_CONSTEXPR Name(const mozilla::CastableTypedEnumResult& aOther) \ michael@0: : mEnum(aOther.get()) \ michael@0: {} \ michael@0: private: \ michael@0: Enum mEnum; \ michael@0: }; michael@0: # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) \ michael@0: inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator+(const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator-(const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator++(Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator++(Name::Enum&, int) MOZ_DELETE; \ michael@0: inline int& operator--(Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator--(Name::Enum&, int) MOZ_DELETE; \ michael@0: inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline bool operator!(const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \ michael@0: inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \ michael@0: inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \ michael@0: inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \ michael@0: inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE; michael@0: michael@0: /* michael@0: * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes michael@0: * as template parameter types. For that, we need integer types. michael@0: * In the present case, the integer type is the Enum nested type. michael@0: */ michael@0: # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name::Enum michael@0: /* michael@0: * MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE is a variant of MOZ_ENUM_CLASS_ENUM_TYPE michael@0: * to be used when the enum class at hand depends on template parameters. michael@0: * michael@0: * Indeed, if T depends on template parameters, in order to name a nested type michael@0: * in T, C++ does not allow to just write "T::NestedType". Instead, we have michael@0: * to write "typename T::NestedType". The role of this macro is to add michael@0: * this "typename" keywords where needed. michael@0: * michael@0: * Example: michael@0: * michael@0: * template michael@0: * struct S {}; michael@0: * michael@0: * MOZ_BEGIN_ENUM_CLASS(E) michael@0: * Foo, michael@0: * Bar michael@0: * MOZ_END_ENUM_CLASS(E) michael@0: * michael@0: * S s; michael@0: * michael@0: * In this example, the second template parameter to S is meant to be of type T, michael@0: * but on non-C++11 compilers, type T is a class type, not an integer type, so michael@0: * it is not accepted as the type of a constant template parameter. One would michael@0: * then want to use MOZ_ENUM_CLASS_ENUM_TYPE(T), but that doesn't work either michael@0: * as T depends on template parameters (more specifically here, T _is_ a template michael@0: * parameter) so as MOZ_ENUM_CLASS_ENUM_TYPE(T) expands to T::Enum, we are missing michael@0: * the required "typename" keyword. So here, MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE michael@0: * is needed. michael@0: */ michael@0: # define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) typename Name::Enum michael@0: #endif michael@0: michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(a, b) a b michael@0: # define MOZ_BEGIN_NESTED_ENUM_CLASS(...) \ michael@0: MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE( \ michael@0: MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER, \ michael@0: __VA_ARGS__), \ michael@0: (__VA_ARGS__)) michael@0: michael@0: # define MOZ_BEGIN_ENUM_CLASS(...) MOZ_BEGIN_NESTED_ENUM_CLASS(__VA_ARGS__) michael@0: # define MOZ_END_ENUM_CLASS(Name) \ michael@0: MOZ_END_NESTED_ENUM_CLASS(Name) \ michael@0: MOZ_FINISH_NESTED_ENUM_CLASS(Name) michael@0: michael@0: #endif /* __cplusplus */ michael@0: michael@0: #endif /* mozilla_TypedEnum_h */