Merge fx-team to m-c.
[gecko.git] / mfbt / TypedEnum.h
blob9c7590deff2d26049c2c35793555b962db8585be
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 */
24 # endif
25 # if __has_extension(cxx_strong_enums)
26 # define MOZ_HAVE_CXX11_ENUM_TYPE
27 # define MOZ_HAVE_CXX11_STRONG_ENUMS
28 # endif
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
34 # endif
35 # endif
36 #elif defined(_MSC_VER)
37 # if _MSC_VER >= 1400
38 # define MOZ_HAVE_CXX11_ENUM_TYPE
39 # endif
40 # if _MSC_VER >= 1700
41 # define MOZ_HAVE_CXX11_STRONG_ENUMS
42 # endif
43 #endif
45 /**
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)
51 * {
52 * A,
53 * B = 7,
54 * C
55 * };
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
62 * undefined.
64 #ifdef MOZ_HAVE_CXX11_ENUM_TYPE
65 # define MOZ_ENUM_TYPE(type) : type
66 #else
67 # define MOZ_ENUM_TYPE(type) /* no support */
68 #endif
70 /**
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
77 * "};". For example,
79 * MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
80 * A,
81 * B = 6
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:
100 * class FooBar {
102 * MOZ_BEGIN_NESTED_ENUM_CLASS(Enum, int32_t)
103 * A,
104 * B = 6
105 * MOZ_END_NESTED_ENUM_CLASS(Enum)
107 * };
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) \
119 enum class 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 */
126 #else
128 * We need Name to both name a type, and scope the provided enumerator
129 * names. Namespaces and classes both provide scoping, but namespaces
130 * aren't types, so we need to use a class that wraps the enum values. We
131 * have an implicit conversion from the inner enum type to the class, so
132 * statements like
134 * Enum x = Enum::A;
136 * will still work. We need to define an implicit conversion from the class
137 * to the inner enum as well, so that (for instance) switch statements will
138 * work. This means that the class can be implicitly converted to a numeric
139 * value as well via the enum type, since C++ allows an implicit
140 * user-defined conversion followed by a standard conversion to still be
141 * implicit.
143 * We have an explicit constructor from int defined, so that casts like
144 * (Enum)7 will still work. We also have a zero-argument constructor with
145 * no arguments, so declaration without initialization (like "Enum foo;")
146 * will work.
148 * Additionally, we'll delete as many operators as possible for the inner
149 * enum type, so statements like this will still fail:
151 * f(5 + Enum::B); // deleted operator+
153 * But we can't prevent things like this, because C++ doesn't allow
154 * overriding conversions or assignment operators for enums:
156 * int x = Enum::A;
157 * int f()
159 * return Enum::A;
163 /* Single-argument form. */
164 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
165 class Name \
167 public: \
168 enum Enum \
170 /* Two-argument form. */
171 # define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
172 class Name \
174 public: \
175 enum Enum MOZ_ENUM_TYPE(type) \
177 # define MOZ_END_NESTED_ENUM_CLASS(Name) \
178 }; \
179 Name() {} \
180 Name(Enum aEnum) : mEnum(aEnum) {} \
181 explicit Name(int num) : mEnum((Enum)num) {} \
182 operator Enum() const { return mEnum; } \
183 private: \
184 Enum mEnum; \
186 # define MOZ_FINISH_NESTED_ENUM_CLASS(Name) \
187 inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
188 inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
189 inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
190 inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
191 inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
192 inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
193 inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
194 inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
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 Name::Enum&) MOZ_DELETE; \
198 inline int operator-(const Name::Enum&) MOZ_DELETE; \
199 inline int& operator++(Name::Enum&) MOZ_DELETE; \
200 inline int operator++(Name::Enum&, int) MOZ_DELETE; \
201 inline int& operator--(Name::Enum&) MOZ_DELETE; \
202 inline int operator--(Name::Enum&, int) MOZ_DELETE; \
203 inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
204 inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
205 inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
206 inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
207 inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
208 inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
209 inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
210 inline bool operator<(const Name::Enum&, const 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 Name::Enum&) MOZ_DELETE; \
216 inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
217 inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
218 inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
219 inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
220 inline int operator~(const Name::Enum&) MOZ_DELETE; \
221 inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
222 inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
223 inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
224 inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
225 inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
226 inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
227 inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
228 inline int operator<<(const Name::Enum&, const int&) 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+=(int&, const Name::Enum&) MOZ_DELETE; \
232 inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
233 inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
234 inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
235 inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
236 inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
237 inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
238 inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
239 inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
240 inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
241 #endif
244 * Count the number of arguments passed to MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS,
245 * very carefully tiptoeing around an MSVC bug where it improperly expands
246 * __VA_ARGS__ as a single token in argument lists. See these URLs for
247 * details:
249 * http://connect.microsoft.com/VisualStudio/feedback/details/380090/variadic-macro-replacement
250 * http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/#comment-644
252 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL2(_1, _2, count, ...) \
253 count
254 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL(args) \
255 MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL2 args
256 # define MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS(...) \
257 MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS_IMPL((__VA_ARGS__, 2, 1, 0))
258 /* Pick the right helper macro to invoke. */
259 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER2(count) \
260 MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER##count
261 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER1(count) \
262 MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER2(count)
263 # define MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER(count) \
264 MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER1(count)
265 /* The actual macro. */
266 # define MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(x, y) x y
267 # define MOZ_BEGIN_NESTED_ENUM_CLASS(...) \
268 MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(MOZ_BEGIN_NESTED_ENUM_CLASS_CHOOSE_HELPER(MOZ_COUNT_BEGIN_ENUM_CLASS_ARGS(__VA_ARGS__)), \
269 (__VA_ARGS__))
271 # define MOZ_BEGIN_ENUM_CLASS(...) MOZ_BEGIN_NESTED_ENUM_CLASS(__VA_ARGS__)
272 # define MOZ_END_ENUM_CLASS(Name) \
273 MOZ_END_NESTED_ENUM_CLASS(Name) \
274 MOZ_FINISH_NESTED_ENUM_CLASS(Name)
276 #endif /* __cplusplus */
278 #endif /* mozilla_TypedEnum_h */