1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Macros to emulate C++11 typed enums and enum classes. */
9 #ifndef mozilla_TypedEnum_h
10 #define mozilla_TypedEnum_h
12 #include "mozilla/Attributes.h"
14 #if defined(__cplusplus)
16 #if defined(__clang__)
18 * Per Clang documentation, "Note that marketing version numbers should not
19 * be used to check for language features, as different vendors use different
20 * numbering schemes. Instead, use the feature checking macros."
22 # ifndef __has_extension
23 # define __has_extension __has_feature /* compatibility, for older versions of clang */
25 # if __has_extension(cxx_strong_enums)
26 # define MOZ_HAVE_CXX11_ENUM_TYPE
27 # define MOZ_HAVE_CXX11_STRONG_ENUMS
29 #elif defined(__GNUC__)
30 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
31 # if MOZ_GCC_VERSION_AT_LEAST(4, 6, 3)
32 # define MOZ_HAVE_CXX11_ENUM_TYPE
33 # define MOZ_HAVE_CXX11_STRONG_ENUMS
36 #elif defined(_MSC_VER)
38 # define MOZ_HAVE_CXX11_ENUM_TYPE
41 # define MOZ_HAVE_CXX11_STRONG_ENUMS
46 * MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's
47 * specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
48 * its declaration, and before the opening curly brace, like
50 * enum MyEnum MOZ_ENUM_TYPE(uint16_t)
57 * In supporting compilers, the macro will expand to ": uint16_t". The
58 * compiler will allocate exactly two bytes for MyEnum and will require all
59 * enumerators to have values between 0 and 65535. (Thus specifying "B =
60 * 100000" instead of "B = 7" would fail to compile.) In old compilers the
61 * macro expands to the empty string, and the underlying type is generally
64 #ifdef MOZ_HAVE_CXX11_ENUM_TYPE
65 # define MOZ_ENUM_TYPE(type) : type
67 # define MOZ_ENUM_TYPE(type) /* no support */
71 * MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
72 * strongly-typed enumeration feature of C++11 ("enum class"). If supported
73 * by the compiler, an enum defined using these macros will not be implicitly
74 * converted to any other type, and its enumerators will be scoped using the
75 * enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName [, type]) in place of
76 * "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
79 * MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
82 * MOZ_END_ENUM_CLASS(Enum)
84 * This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
85 * and "B" will not. In compilers that support C++11 strongly-typed
86 * enumerations, implicit conversions of Enum values to numeric types will
87 * fail. In other compilers, Enum itself will actually be defined as a class,
88 * and some implicit conversions will fail while others will succeed.
90 * The optional type argument specifies the underlying type for the enum where
91 * supported, as with MOZ_ENUM_TYPE(). As with MOZ_ENUM_TYPE(), it will do
92 * nothing on compilers that do not support it.
94 * MOZ_{BEGIN,END}_ENUM_CLASS doesn't work for defining enum classes nested
95 * inside classes. To define an enum class nested inside another class, use
96 * MOZ_{BEGIN,END}_NESTED_ENUM_CLASS, and place a MOZ_FINISH_NESTED_ENUM_CLASS
97 * in namespace scope to handle bits that can only be implemented with
98 * namespace-scoped code. For example:
102 * MOZ_BEGIN_NESTED_ENUM_CLASS(Enum, int32_t)
105 * MOZ_END_NESTED_ENUM_CLASS(Enum)
109 * MOZ_FINISH_NESTED_ENUM_CLASS(FooBar::Enum)
111 #if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
113 * All compilers that support strong enums also support an explicit
114 * underlying type, so no extra check is needed.
117 /* Single-argument form. */
118 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
120 /* Two-argument form. */
121 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
122 enum class Name : type {
123 # define MOZ_END_NESTED_ENUM_CLASS(Name) \
125 # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) /* nothing */
128 * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
129 * as template parameter types. For that, we need integer types.
130 * In the present case where the compiler supports strong enums,
131 * these are already integer types so there is nothing more to do.
133 # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name
136 * We need Name to both name a type, and scope the provided enumerator
137 * names. Namespaces and classes both provide scoping, but namespaces
138 * aren't types, so we need to use a class that wraps the enum values. We
139 * have an implicit conversion from the inner enum type to the class, so
144 * will still work. We need to define an implicit conversion from the class
145 * to the inner enum as well, so that (for instance) switch statements will
146 * work. This means that the class can be implicitly converted to a numeric
147 * value as well via the enum type, since C++ allows an implicit
148 * user-defined conversion followed by a standard conversion to still be
151 * We have an explicit constructor from int defined, so that casts like
152 * (Enum)7 will still work. We also have a zero-argument constructor with
153 * no arguments, so declaration without initialization (like "Enum foo;")
156 * Additionally, we'll delete as many operators as possible for the inner
157 * enum type, so statements like this will still fail:
159 * f(5 + Enum::B); // deleted operator+
161 * But we can't prevent things like this, because C++ doesn't allow
162 * overriding conversions or assignment operators for enums:
171 /* Single-argument form. */
172 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
178 /* Two-argument form. */
179 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
183 enum Enum MOZ_ENUM_TYPE(type) \
185 # define MOZ_END_NESTED_ENUM_CLASS(Name) \
188 Name(Enum aEnum) : mEnum(aEnum) {} \
189 explicit Name(int num) : mEnum((Enum)num) {} \
190 operator Enum() const { return mEnum; } \
194 # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) \
195 inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
196 inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
197 inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
198 inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
199 inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
200 inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
201 inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
202 inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
203 inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
204 inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
205 inline int operator+(const Name::Enum&) MOZ_DELETE; \
206 inline int operator-(const Name::Enum&) MOZ_DELETE; \
207 inline int& operator++(Name::Enum&) MOZ_DELETE; \
208 inline int operator++(Name::Enum&, int) MOZ_DELETE; \
209 inline int& operator--(Name::Enum&) MOZ_DELETE; \
210 inline int operator--(Name::Enum&, int) MOZ_DELETE; \
211 inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
212 inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
213 inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
214 inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
215 inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
216 inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
217 inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
218 inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
219 inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
220 inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
221 inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
222 inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
223 inline bool operator!(const Name::Enum&) MOZ_DELETE; \
224 inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
225 inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
226 inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
227 inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
228 inline int operator~(const Name::Enum&) MOZ_DELETE; \
229 inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
230 inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
231 inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
232 inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
233 inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
234 inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
235 inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
236 inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \
237 inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \
238 inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
239 inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
240 inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
241 inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
242 inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
243 inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
244 inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
245 inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
246 inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
247 inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
248 inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
251 * MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
252 * as template parameter types. For that, we need integer types.
253 * In the present case, the integer type is the Enum nested type.
255 # define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name::Enum
259 * Count the number of arguments passed to MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS,
260 * very carefully tiptoeing around an MSVC bug where it improperly expands
261 * __VA_ARGS__ as a single token in argument lists. See these URLs for
264 * http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
265 * http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
267 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL2(_1, _2, count, ...) \
269 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL(args) \
270 MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL2 args
271 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS(...) \
272 MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL((__VA_ARGS__, 2, 1, 0))
273 /* Pick the right helper macro to invoke. */
274 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER2(count) \
275 MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER##count
276 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER1(count) \
277 MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER2(count)
278 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER(count) \
279 MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER1(count)
280 /* The actual macro. */
281 # define MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(x, y) x y
282 # define MOZ_BEGIN_NESTED_ENUM_CLASS(...) \
283 MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER(MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS(__VA_ARGS__)), \
286 # define MOZ_BEGIN_ENUM_CLASS(...) MOZ_BEGIN_NESTED_ENUM_CLASS(__VA_ARGS__)
287 # define MOZ_END_ENUM_CLASS(Name) \
288 MOZ_END_NESTED_ENUM_CLASS(Name) \
289 MOZ_FINISH_NESTED_ENUM_CLASS(Name)
291 #endif /* __cplusplus */
293 #endif /* mozilla_TypedEnum_h */