michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Internal stuff needed by TypedEnum.h and TypedEnumBits.h. */ michael@0: michael@0: // NOTE: When we can assume C++11 enum class support and TypedEnum.h goes away, michael@0: // we should then consider folding TypedEnumInternal.h into TypedEnumBits.h. michael@0: michael@0: #ifndef mozilla_TypedEnumInternal_h michael@0: #define mozilla_TypedEnumInternal_h michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #if defined(__cplusplus) michael@0: michael@0: #if defined(__clang__) michael@0: /* michael@0: * Per Clang documentation, "Note that marketing version numbers should not michael@0: * be used to check for language features, as different vendors use different michael@0: * numbering schemes. Instead, use the feature checking macros." michael@0: */ michael@0: # ifndef __has_extension michael@0: # define __has_extension __has_feature /* compatibility, for older versions of clang */ michael@0: # endif michael@0: # if __has_extension(cxx_strong_enums) michael@0: # define MOZ_HAVE_CXX11_ENUM_TYPE michael@0: # define MOZ_HAVE_CXX11_STRONG_ENUMS michael@0: # endif michael@0: #elif defined(__GNUC__) michael@0: # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L michael@0: # if MOZ_GCC_VERSION_AT_LEAST(4, 6, 3) michael@0: # define MOZ_HAVE_CXX11_ENUM_TYPE michael@0: # define MOZ_HAVE_CXX11_STRONG_ENUMS michael@0: # endif michael@0: # endif michael@0: #elif defined(_MSC_VER) michael@0: # if _MSC_VER >= 1400 michael@0: # define MOZ_HAVE_CXX11_ENUM_TYPE michael@0: # endif michael@0: # if _MSC_VER >= 1700 michael@0: # define MOZ_HAVE_CXX11_STRONG_ENUMS michael@0: # endif michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: michael@0: /* michael@0: * The problem that CastableTypedEnumResult aims to solve is that michael@0: * typed enums are not convertible to bool, and there is no way to make them michael@0: * be, yet user code wants to be able to write michael@0: * michael@0: * if (myFlags & Flags::SOME_PARTICULAR_FLAG) (1) michael@0: * michael@0: * There are different approaches to solving this. Most of them require michael@0: * adapting user code. For example, we could implement operator! and have michael@0: * the user write michael@0: * michael@0: * if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG)) (2) michael@0: * michael@0: * Or we could supply a IsNonZero() or Any() function returning whether michael@0: * an enum value is nonzero, and have the user write michael@0: * michael@0: * if (Any(Flags & Flags::SOME_PARTICULAR_FLAG)) (3) michael@0: * michael@0: * But instead, we choose to preserve the original user syntax (1) as it michael@0: * is inherently more readable, and to ease porting existing code to typed michael@0: * enums. We achieve this by having operator& and other binary bitwise michael@0: * operators have as return type a class, CastableTypedEnumResult, michael@0: * that wraps a typed enum but adds bool convertibility. michael@0: */ michael@0: template michael@0: class CastableTypedEnumResult michael@0: { michael@0: private: michael@0: const E mValue; michael@0: michael@0: public: michael@0: explicit MOZ_CONSTEXPR CastableTypedEnumResult(E value) michael@0: : mValue(value) michael@0: {} michael@0: michael@0: MOZ_CONSTEXPR operator E() const { return mValue; } michael@0: michael@0: template michael@0: MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR michael@0: operator DestinationType() const { michael@0: return DestinationType(mValue); michael@0: } michael@0: michael@0: MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); } michael@0: michael@0: #ifndef MOZ_HAVE_CXX11_STRONG_ENUMS michael@0: // This get() method is used to implement a constructor in the michael@0: // non-c++11 fallback path for MOZ_BEGIN_ENUM_CLASS, taking a michael@0: // CastableTypedEnumResult. If we try to implement it using the michael@0: // above conversion operator E(), then at least clang 3.3 michael@0: // (when forced to take the non-c++11 fallback path) compiles michael@0: // this constructor to an infinite recursion. So we introduce this michael@0: // get() method, that does exactly the same as the conversion operator, michael@0: // to work around this. michael@0: MOZ_CONSTEXPR E get() const { return mValue; } michael@0: #endif michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // __cplusplus michael@0: michael@0: #endif // mozilla_TypedEnumInternal_h