Bumping gaia.json for 7 gaia revision(s) a=gaia-bump
[gecko.git] / mfbt / TypedEnumInternal.h
blob5af19e20202ec1c99b7719268713e57998071f71
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 /* Internal stuff needed by TypedEnum.h and TypedEnumBits.h. */
9 // NOTE: When we can assume C++11 enum class support and TypedEnum.h goes away,
10 // we should then consider folding TypedEnumInternal.h into TypedEnumBits.h.
12 #ifndef mozilla_TypedEnumInternal_h
13 #define mozilla_TypedEnumInternal_h
15 #include "mozilla/Attributes.h"
17 #if defined(__cplusplus)
19 #if defined(__clang__)
21 * Per Clang documentation, "Note that marketing version numbers should not
22 * be used to check for language features, as different vendors use different
23 * numbering schemes. Instead, use the feature checking macros."
25 # ifndef __has_extension
26 # define __has_extension __has_feature /* compatibility, for older versions of clang */
27 # endif
28 # if __has_extension(cxx_strong_enums)
29 # define MOZ_HAVE_CXX11_ENUM_TYPE
30 # define MOZ_HAVE_CXX11_STRONG_ENUMS
31 # endif
32 #elif defined(__GNUC__)
33 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
34 # if MOZ_GCC_VERSION_AT_LEAST(4, 6, 3)
35 # define MOZ_HAVE_CXX11_ENUM_TYPE
36 # define MOZ_HAVE_CXX11_STRONG_ENUMS
37 # endif
38 # endif
39 #elif defined(_MSC_VER)
40 # define MOZ_HAVE_CXX11_ENUM_TYPE
41 # define MOZ_HAVE_CXX11_STRONG_ENUMS
42 #endif
44 namespace mozilla {
47 * The problem that CastableTypedEnumResult aims to solve is that
48 * typed enums are not convertible to bool, and there is no way to make them
49 * be, yet user code wants to be able to write
51 * if (myFlags & Flags::SOME_PARTICULAR_FLAG) (1)
53 * There are different approaches to solving this. Most of them require
54 * adapting user code. For example, we could implement operator! and have
55 * the user write
57 * if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG)) (2)
59 * Or we could supply a IsNonZero() or Any() function returning whether
60 * an enum value is nonzero, and have the user write
62 * if (Any(Flags & Flags::SOME_PARTICULAR_FLAG)) (3)
64 * But instead, we choose to preserve the original user syntax (1) as it
65 * is inherently more readable, and to ease porting existing code to typed
66 * enums. We achieve this by having operator& and other binary bitwise
67 * operators have as return type a class, CastableTypedEnumResult,
68 * that wraps a typed enum but adds bool convertibility.
70 template<typename E>
71 class CastableTypedEnumResult
73 private:
74 const E mValue;
76 public:
77 explicit MOZ_CONSTEXPR CastableTypedEnumResult(E aValue)
78 : mValue(aValue)
81 MOZ_CONSTEXPR operator E() const { return mValue; }
83 template<typename DestinationType>
84 MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR
85 operator DestinationType() const { return DestinationType(mValue); }
87 MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); }
89 #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
90 // This get() method is used to implement a constructor in the
91 // non-c++11 fallback path for MOZ_BEGIN_ENUM_CLASS, taking a
92 // CastableTypedEnumResult. If we try to implement it using the
93 // above conversion operator E(), then at least clang 3.3
94 // (when forced to take the non-c++11 fallback path) compiles
95 // this constructor to an infinite recursion. So we introduce this
96 // get() method, that does exactly the same as the conversion operator,
97 // to work around this.
98 MOZ_CONSTEXPR E get() const { return mValue; }
99 #endif
102 } // namespace mozilla
104 #endif // __cplusplus
106 #endif // mozilla_TypedEnumInternal_h