Bumping manifests a=b2g-bump
[gecko.git] / mfbt / TypedEnumInternal.h
blob8c88cb589b7ea15c0f2d8bd6859ddf5ac0db8b49
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 # if _MSC_VER >= 1400
41 # define MOZ_HAVE_CXX11_ENUM_TYPE
42 # endif
43 # if _MSC_VER >= 1700
44 # define MOZ_HAVE_CXX11_STRONG_ENUMS
45 # endif
46 #endif
48 namespace mozilla {
51 * The problem that CastableTypedEnumResult aims to solve is that
52 * typed enums are not convertible to bool, and there is no way to make them
53 * be, yet user code wants to be able to write
55 * if (myFlags & Flags::SOME_PARTICULAR_FLAG) (1)
57 * There are different approaches to solving this. Most of them require
58 * adapting user code. For example, we could implement operator! and have
59 * the user write
61 * if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG)) (2)
63 * Or we could supply a IsNonZero() or Any() function returning whether
64 * an enum value is nonzero, and have the user write
66 * if (Any(Flags & Flags::SOME_PARTICULAR_FLAG)) (3)
68 * But instead, we choose to preserve the original user syntax (1) as it
69 * is inherently more readable, and to ease porting existing code to typed
70 * enums. We achieve this by having operator& and other binary bitwise
71 * operators have as return type a class, CastableTypedEnumResult,
72 * that wraps a typed enum but adds bool convertibility.
74 template<typename E>
75 class CastableTypedEnumResult
77 private:
78 const E mValue;
80 public:
81 explicit MOZ_CONSTEXPR CastableTypedEnumResult(E aValue)
82 : mValue(aValue)
85 MOZ_CONSTEXPR operator E() const { return mValue; }
87 template<typename DestinationType>
88 MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR
89 operator DestinationType() const { return DestinationType(mValue); }
91 MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); }
93 #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
94 // This get() method is used to implement a constructor in the
95 // non-c++11 fallback path for MOZ_BEGIN_ENUM_CLASS, taking a
96 // CastableTypedEnumResult. If we try to implement it using the
97 // above conversion operator E(), then at least clang 3.3
98 // (when forced to take the non-c++11 fallback path) compiles
99 // this constructor to an infinite recursion. So we introduce this
100 // get() method, that does exactly the same as the conversion operator,
101 // to work around this.
102 MOZ_CONSTEXPR E get() const { return mValue; }
103 #endif
106 } // namespace mozilla
108 #endif // __cplusplus
110 #endif // mozilla_TypedEnumInternal_h