Bug 860222: use our own isascii() for sanitizing TURN passwords r=glandium
[gecko.git] / mfbt / TypedEnum.h
blob889960a32da978d102e66ed5e4433e117b40cd08
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Macros to emulate C++11 typed enums and enum classes. */
8 #ifndef mozilla_TypedEnum_h_
9 #define mozilla_TypedEnum_h_
11 #include "mozilla/Attributes.h"
13 #if defined(__cplusplus)
15 #if defined(__clang__)
17 * Per Clang documentation, "Note that marketing version numbers should not
18 * be used to check for language features, as different vendors use different
19 * numbering schemes. Instead, use the feature checking macros."
21 # ifndef __has_extension
22 # define __has_extension __has_feature /* compatibility, for older versions of clang */
23 # endif
24 # if __has_extension(cxx_strong_enums)
25 # define MOZ_HAVE_CXX11_ENUM_TYPE
26 # define MOZ_HAVE_CXX11_STRONG_ENUMS
27 # endif
28 #elif defined(__GNUC__)
29 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
30 # if MOZ_GCC_VERSION_AT_LEAST(4, 5, 1)
31 # define MOZ_HAVE_CXX11_ENUM_TYPE
32 # define MOZ_HAVE_CXX11_STRONG_ENUMS
33 # endif
34 # endif
35 #elif defined(_MSC_VER)
36 # if _MSC_VER >= 1400
37 # define MOZ_HAVE_CXX11_ENUM_TYPE
38 # endif
39 # if _MSC_VER >= 1700
40 # define MOZ_HAVE_CXX11_STRONG_ENUMS
41 # endif
42 #endif
44 /**
45 * MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's
46 * specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
47 * its declaration, and before the opening curly brace, like
49 * enum MyEnum MOZ_ENUM_TYPE(uint16_t)
50 * {
51 * A,
52 * B = 7,
53 * C
54 * };
56 * In supporting compilers, the macro will expand to ": uint16_t". The
57 * compiler will allocate exactly two bytes for MyEnum and will require all
58 * enumerators to have values between 0 and 65535. (Thus specifying "B =
59 * 100000" instead of "B = 7" would fail to compile.) In old compilers the
60 * macro expands to the empty string, and the underlying type is generally
61 * undefined.
63 #ifdef MOZ_HAVE_CXX11_ENUM_TYPE
64 # define MOZ_ENUM_TYPE(type) : type
65 #else
66 # define MOZ_ENUM_TYPE(type) /* no support */
67 #endif
69 /**
70 * MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
71 * strongly-typed enumeration feature of C++11 ("enum class"). If supported
72 * by the compiler, an enum defined using these macros will not be implicitly
73 * converted to any other type, and its enumerators will be scoped using the
74 * enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName, type) in place of
75 * "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
76 * "};". For example,
78 * MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
79 * A,
80 * B = 6
81 * MOZ_END_ENUM_CLASS(Enum)
83 * This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
84 * and "B" will not. In compilers that support C++11 strongly-typed
85 * enumerations, implicit conversions of Enum values to numeric types will
86 * fail. In other compilers, Enum itself will actually be defined as a class,
87 * and some implicit conversions will fail while others will succeed.
89 * The type argument specifies the underlying type for the enum where
90 * supported, as with MOZ_ENUM_TYPE(). For simplicity, it is currently
91 * mandatory. As with MOZ_ENUM_TYPE(), it will do nothing on compilers that do
92 * not support it.
94 * Note that the workaround implemented here is not compatible with enums
95 * nested inside a class.
97 #if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
99 * All compilers that support strong enums also support an explicit
100 * underlying type, so no extra check is needed.
102 # define MOZ_BEGIN_ENUM_CLASS(Name, type) enum class Name : type {
103 # define MOZ_END_ENUM_CLASS(Name) };
104 #else
106 * We need Name to both name a type, and scope the provided enumerator
107 * names. Namespaces and classes both provide scoping, but namespaces
108 * aren't types, so we need to use a class that wraps the enum values. We
109 * have an implicit conversion from the inner enum type to the class, so
110 * statements like
112 * Enum x = Enum::A;
114 * will still work. We need to define an implicit conversion from the class
115 * to the inner enum as well, so that (for instance) switch statements will
116 * work. This means that the class can be implicitly converted to a numeric
117 * value as well via the enum type, since C++ allows an implicit
118 * user-defined conversion followed by a standard conversion to still be
119 * implicit.
121 * We have an explicit constructor from int defined, so that casts like
122 * (Enum)7 will still work. We also have a zero-argument constructor with
123 * no arguments, so declaration without initialization (like "Enum foo;")
124 * will work.
126 * Additionally, we'll delete as many operators as possible for the inner
127 * enum type, so statements like this will still fail:
129 * f(5 + Enum::B); // deleted operator+
131 * But we can't prevent things like this, because C++ doesn't allow
132 * overriding conversions or assignment operators for enums:
134 * int x = Enum::A;
135 * int f()
137 * return Enum::A;
140 # define MOZ_BEGIN_ENUM_CLASS(Name, type) \
141 class Name \
143 public: \
144 enum Enum MOZ_ENUM_TYPE(type) \
146 # define MOZ_END_ENUM_CLASS(Name) \
147 }; \
148 Name() {} \
149 Name(Enum aEnum) : mEnum(aEnum) {} \
150 explicit Name(int num) : mEnum((Enum)num) {} \
151 operator Enum() const { return mEnum; } \
152 private: \
153 Enum mEnum; \
154 }; \
155 inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
156 inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
157 inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
158 inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
159 inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
160 inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
161 inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
162 inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
163 inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
164 inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
165 inline int operator+(const Name::Enum&) MOZ_DELETE; \
166 inline int operator-(const Name::Enum&) MOZ_DELETE; \
167 inline int& operator++(Name::Enum&) MOZ_DELETE; \
168 inline int operator++(Name::Enum&, int) MOZ_DELETE; \
169 inline int& operator--(Name::Enum&) MOZ_DELETE; \
170 inline int operator--(Name::Enum&, int) MOZ_DELETE; \
171 inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
172 inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
173 inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
174 inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
175 inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
176 inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
177 inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
178 inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
179 inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
180 inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
181 inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
182 inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
183 inline bool operator!(const Name::Enum&) MOZ_DELETE; \
184 inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
185 inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
186 inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
187 inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
188 inline int operator~(const Name::Enum&) 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 int&, const Name::Enum&) MOZ_DELETE; \
198 inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
199 inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
200 inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
201 inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
202 inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
203 inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
204 inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
205 inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
206 inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
207 inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
208 inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
209 #endif
211 #endif /* __cplusplus */
213 #endif /* mozilla_TypedEnum_h_ */