summaryrefslogtreecommitdiff
path: root/mfbt/TypedEnum.h
blob: 889960a32da978d102e66ed5e4433e117b40cd08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Macros to emulate C++11 typed enums and enum classes. */

#ifndef mozilla_TypedEnum_h_
#define mozilla_TypedEnum_h_

#include "mozilla/Attributes.h"

#if defined(__cplusplus)

#if defined(__clang__)
   /*
    * Per Clang documentation, "Note that marketing version numbers should not
    * be used to check for language features, as different vendors use different
    * numbering schemes. Instead, use the feature checking macros."
    */
#  ifndef __has_extension
#    define __has_extension __has_feature /* compatibility, for older versions of clang */
#  endif
#  if __has_extension(cxx_strong_enums)
#    define MOZ_HAVE_CXX11_ENUM_TYPE
#    define MOZ_HAVE_CXX11_STRONG_ENUMS
#  endif
#elif defined(__GNUC__)
#  if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
#    if MOZ_GCC_VERSION_AT_LEAST(4, 5, 1)
#      define MOZ_HAVE_CXX11_ENUM_TYPE
#      define MOZ_HAVE_CXX11_STRONG_ENUMS
#    endif
#  endif
#elif defined(_MSC_VER)
#  if _MSC_VER >= 1400
#    define MOZ_HAVE_CXX11_ENUM_TYPE
#  endif
#  if _MSC_VER >= 1700
#    define MOZ_HAVE_CXX11_STRONG_ENUMS
#  endif
#endif

/**
 * MOZ_ENUM_TYPE specifies the underlying numeric type for an enum.  It's
 * specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
 * its declaration, and before the opening curly brace, like
 *
 *   enum MyEnum MOZ_ENUM_TYPE(uint16_t)
 *   {
 *     A,
 *     B = 7,
 *     C
 *   };
 *
 * In supporting compilers, the macro will expand to ": uint16_t".  The
 * compiler will allocate exactly two bytes for MyEnum and will require all
 * enumerators to have values between 0 and 65535.  (Thus specifying "B =
 * 100000" instead of "B = 7" would fail to compile.)  In old compilers the
 * macro expands to the empty string, and the underlying type is generally
 * undefined.
 */
#ifdef MOZ_HAVE_CXX11_ENUM_TYPE
#  define MOZ_ENUM_TYPE(type)   : type
#else
#  define MOZ_ENUM_TYPE(type)   /* no support */
#endif

/**
 * MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
 * strongly-typed enumeration feature of C++11 ("enum class").  If supported
 * by the compiler, an enum defined using these macros will not be implicitly
 * converted to any other type, and its enumerators will be scoped using the
 * enumeration name.  Place MOZ_BEGIN_ENUM_CLASS(EnumName, type) in place of
 * "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
 * "};".  For example,
 *
 *   MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
 *     A,
 *     B = 6
 *   MOZ_END_ENUM_CLASS(Enum)
 *
 * This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
 * and "B" will not.  In compilers that support C++11 strongly-typed
 * enumerations, implicit conversions of Enum values to numeric types will
 * fail.  In other compilers, Enum itself will actually be defined as a class,
 * and some implicit conversions will fail while others will succeed.
 *
 * The type argument specifies the underlying type for the enum where
 * supported, as with MOZ_ENUM_TYPE().  For simplicity, it is currently
 * mandatory.  As with MOZ_ENUM_TYPE(), it will do nothing on compilers that do
 * not support it.
 *
 * Note that the workaround implemented here is not compatible with enums
 * nested inside a class.
 */
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
  /*
   * All compilers that support strong enums also support an explicit
   * underlying type, so no extra check is needed.
   */
#  define MOZ_BEGIN_ENUM_CLASS(Name, type) enum class Name : type {
#  define MOZ_END_ENUM_CLASS(Name)         };
#else
   /**
    * We need Name to both name a type, and scope the provided enumerator
    * names.  Namespaces and classes both provide scoping, but namespaces
    * aren't types, so we need to use a class that wraps the enum values.  We
    * have an implicit conversion from the inner enum type to the class, so
    * statements like
    *
    *   Enum x = Enum::A;
    *
    * will still work.  We need to define an implicit conversion from the class
    * to the inner enum as well, so that (for instance) switch statements will
    * work.  This means that the class can be implicitly converted to a numeric
    * value as well via the enum type, since C++ allows an implicit
    * user-defined conversion followed by a standard conversion to still be
    * implicit.
    *
    * We have an explicit constructor from int defined, so that casts like
    * (Enum)7 will still work.  We also have a zero-argument constructor with
    * no arguments, so declaration without initialization (like "Enum foo;")
    * will work.
    *
    * Additionally, we'll delete as many operators as possible for the inner
    * enum type, so statements like this will still fail:
    *
    *   f(5 + Enum::B); // deleted operator+
    *
    * But we can't prevent things like this, because C++ doesn't allow
    * overriding conversions or assignment operators for enums:
    *
    *   int x = Enum::A;
    *   int f()
    *   {
    *     return Enum::A;
    *   }
    */
#  define MOZ_BEGIN_ENUM_CLASS(Name, type) \
     class Name \
     { \
       public: \
         enum Enum MOZ_ENUM_TYPE(type) \
         {
#  define MOZ_END_ENUM_CLASS(Name) \
         }; \
         Name() {} \
         Name(Enum aEnum) : mEnum(aEnum) {} \
         explicit Name(int num) : mEnum((Enum)num) {} \
         operator Enum() const { return mEnum; } \
       private: \
         Enum mEnum; \
     }; \
     inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator+(const Name::Enum&) MOZ_DELETE; \
     inline int operator-(const Name::Enum&) MOZ_DELETE; \
     inline int& operator++(Name::Enum&) MOZ_DELETE; \
     inline int operator++(Name::Enum&, int) MOZ_DELETE; \
     inline int& operator--(Name::Enum&) MOZ_DELETE; \
     inline int operator--(Name::Enum&, int) MOZ_DELETE; \
     inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
     inline bool operator!(const Name::Enum&) MOZ_DELETE; \
     inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
     inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
     inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
     inline int operator~(const Name::Enum&) MOZ_DELETE; \
     inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \
     inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
     inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
     inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
#endif

#endif /* __cplusplus */

#endif  /* mozilla_TypedEnum_h_ */