Bug 1111290 - Part 3: Remove TypedEnum.h and fold TypedEnumInternal.h into TypedEnumBits.h. r=waldo

This commit is contained in:
Masatoshi Kimura 2015-01-26 07:22:11 +09:00
Родитель 86502e6537
Коммит 6cc5dabbee
51 изменённых файлов: 63 добавлений и 518 удалений

Просмотреть файл

@ -7,8 +7,6 @@
#ifndef mozilla_a11y_relationtype_h_
#define mozilla_a11y_relationtype_h_
#include "mozilla/TypedEnum.h"
namespace mozilla {
namespace a11y {

Просмотреть файл

@ -8440,10 +8440,10 @@ class CGEnum(CGThing):
def declare(self):
decl = fill(
"""
MOZ_BEGIN_ENUM_CLASS(${name}, uint32_t)
enum class ${name} : uint32_t {
$*{enums}
EndGuard_
MOZ_END_ENUM_CLASS(${name})
};
""",
name=self.enum.identifier.name,
enums=",\n".join(map(getEnumValueName, self.enum.values())) + ",\n")

Просмотреть файл

@ -6,8 +6,6 @@
#ifndef WEBGLTYPES_H_
#define WEBGLTYPES_H_
#include "mozilla/TypedEnum.h"
// Most WebIDL typedefs are identical to their OpenGL counterparts.
#include "GLTypes.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define mozilla_EventStateManager_h_
#include "mozilla/EventForwards.h"
#include "mozilla/TypedEnum.h"
#include "nsIObserver.h"
#include "nsWeakReference.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define mozilla_dom_HTMLCanvasElement_h
#include "mozilla/Attributes.h"
#include "mozilla/TypedEnum.h"
#include "nsIDOMHTMLCanvasElement.h"
#include "nsGenericHTMLElement.h"
#include "nsGkAtoms.h"

Просмотреть файл

@ -13,8 +13,6 @@
#include "MediaQueue.h"
#include "AudioCompactor.h"
#include "mozilla/TypedEnum.h"
namespace mozilla {
namespace dom {

Просмотреть файл

@ -10,7 +10,6 @@
#include <algorithm>
#include "mozilla/Assertions.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/PodOperations.h"
#include "nsTArray.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define MOZILLA_CONTENT_SVGPRESERVEASPECTRATIO_H_
#include "mozilla/HashFunctions.h" // for HashGeneric
#include "mozilla/TypedEnum.h"
#include "nsWrapperCache.h"
#include "nsAutoPtr.h"

Просмотреть файл

@ -11,7 +11,6 @@
#include "mozilla/Attributes.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Preferences.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/TypedEnumBits.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/dom/BindingUtils.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define __editor_h__
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc.
#include "mozilla/TypedEnum.h" // for MOZ_BEGIN_ENUM_CLASS, etc.
#include "mozilla/dom/Text.h"
#include "nsAutoPtr.h" // for nsRefPtr
#include "nsCOMArray.h" // for nsCOMArray

Просмотреть файл

@ -21,7 +21,6 @@
#include "Point.h"
#include "BaseRect.h"
#include "Matrix.h"
#include "mozilla/TypedEnum.h"
#ifdef WIN32
// This file gets included from nsGlobalWindow.cpp, which doesn't like

Просмотреть файл

@ -8,7 +8,6 @@
#include "2D.h"
#include "mozilla/Constants.h"
#include "mozilla/TypedEnum.h"
#include "UserData.h"
namespace mozilla {

Просмотреть файл

@ -6,8 +6,6 @@
#ifndef MOZILLA_GFX_TYPES_H_
#define MOZILLA_GFX_TYPES_H_
#include "mozilla/TypedEnum.h"
#include <stddef.h>
#include <stdint.h>

Просмотреть файл

@ -12,7 +12,6 @@
#include "GLDefs.h"
#include "nsISupports.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/gfx/2D.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define GLCONTEXT_TYPES_H_
#include "GLTypes.h"
#include "mozilla/TypedEnum.h"
namespace mozilla {
namespace gl {

Просмотреть файл

@ -6,7 +6,6 @@
#ifndef SURFACE_TYPES_H_
#define SURFACE_TYPES_H_
#include "mozilla/TypedEnum.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Attributes.h"
#include <stdint.h>

Просмотреть файл

@ -34,13 +34,7 @@
namespace mozilla {
typedef gfxImageFormat PixelFormat;
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
typedef ::GraphicsFilter GraphicsFilterType;
#else
// If we don't have support for enum classes, then we need to use the actual
// enum type here instead of the simulated enum class.
typedef GraphicsFilter::Enum GraphicsFilterType;
#endif
} // namespace mozilla

Просмотреть файл

@ -13,7 +13,6 @@
#include "mozilla/gfx/Types.h"
#include "mozilla/EnumSet.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/TypedEnumBits.h"
namespace mozilla {

Просмотреть файл

@ -6,8 +6,6 @@
#ifndef GFX_IMAGETYPES_H
#define GFX_IMAGETYPES_H
#include "mozilla/TypedEnum.h"
namespace mozilla {
enum class ImageFormat {

Просмотреть файл

@ -10,7 +10,6 @@
#include "nsPoint.h" // for nsIntPoint
#include "nsRegion.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/TypedEnumBits.h"
#ifdef MOZ_WIDGET_GONK

Просмотреть файл

@ -8,7 +8,6 @@
#include "mozilla/Attributes.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/gfx/Matrix.h"
#include "mozilla/gfx/2D.h"

Просмотреть файл

@ -18,7 +18,6 @@
#include "nsMargin.h" // for nsIntMargin
#include "nsStringGlue.h" // for nsCString
#include "xpcom-config.h" // for CPP_THROW_NEW
#include "mozilla/TypedEnum.h" // for the VisitEdges typed enum
#include "mozilla/Move.h" // for mozilla::Move
class nsIntRegion;

Просмотреть файл

@ -6,8 +6,6 @@
#ifndef DrawMode_h
#define DrawMode_h
#include "mozilla/TypedEnum.h"
// Options for how the text should be drawn
enum class DrawMode : int {
// GLYPH_FILL and GLYPH_STROKE draw into the current context

Просмотреть файл

@ -6,8 +6,6 @@
#ifndef GraphicsFilter_h
#define GraphicsFilter_h
#include "mozilla/TypedEnum.h"
enum class GraphicsFilter : int {
FILTER_FAST,
FILTER_GOOD,

Просмотреть файл

@ -9,7 +9,6 @@
#include <stdint.h>
#include "mozilla/Assertions.h"
#include "mozilla/Constants.h" // for M_PI
#include "mozilla/TypedEnum.h"
// First time gfxPrefs::GetSingleton() needs to be called on the main thread,
// before any of the methods accessing the values are used, but after

Просмотреть файл

@ -7,7 +7,6 @@
#define GFX_TYPES_H
#include <stdint.h>
#include "mozilla/TypedEnum.h"
typedef struct _cairo_surface cairo_surface_t;
typedef struct _cairo_user_data_key cairo_user_data_key_t;

Просмотреть файл

@ -7,7 +7,6 @@
#define MOZILLA_IMAGELIB_EXIF_H
#include <stdint.h>
#include "mozilla/TypedEnum.h"
#include "nsDebug.h"
#include "Orientation.h"

Просмотреть файл

@ -12,7 +12,6 @@
#include "mozilla/Mutex.h"
#include "mozilla/StaticPtr.h"
#include <mozilla/TypedEnum.h>
#include "nsCOMPtr.h"
#include "nsIEventTarget.h"
#include "nsIObserver.h"

Просмотреть файл

@ -7,7 +7,6 @@
#define MOZILLA_IMAGELIB_ORIENTATION_H_
#include <stdint.h>
#include "mozilla/TypedEnum.h"
namespace mozilla {
namespace image {

Просмотреть файл

@ -30,7 +30,6 @@
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/UniquePtr.h"
#ifdef DEBUG

Просмотреть файл

@ -10,7 +10,6 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/Monitor.h"
#include "mozilla/Move.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/VolatileBuffer.h"
#include "gfxDrawable.h"
#include "imgIContainer.h"

Просмотреть файл

@ -16,7 +16,6 @@
#ifdef XP_WIN
#include "mozilla/TimeStamp_windows.h"
#endif
#include "mozilla/TypedEnum.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/IntegerTypeTraits.h"

Просмотреть файл

@ -7,8 +7,6 @@
#ifndef js_ProfilingStack_h
#define js_ProfilingStack_h
#include "mozilla/TypedEnum.h"
#include "jsbytecode.h"
#include "jstypes.h"

Просмотреть файл

@ -7,7 +7,6 @@
#include "frontend/FoldConstants.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/TypedEnum.h"
#include "jslibmath.h"

Просмотреть файл

@ -8,7 +8,6 @@
#define jit_IonTypes_h
#include "mozilla/HashFunctions.h"
#include "mozilla/TypedEnum.h"
#include "jstypes.h"

Просмотреть файл

@ -13,7 +13,6 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/Range.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/TypedEnum.h"
#include <stdarg.h>
#include <stddef.h>

Просмотреть файл

@ -9,7 +9,6 @@
#include "mozilla/Casting.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/UniquePtr.h"
#include "jsapi.h" // For JSAutoByteString. See bug 1033916.

Просмотреть файл

@ -10,7 +10,6 @@
#define jsinfer_h
#include "mozilla/MemoryReporting.h"
#include "mozilla/TypedEnum.h"
#include "jsalloc.h"
#include "jsfriendapi.h"

Просмотреть файл

@ -31,7 +31,6 @@
#include "mozilla/Endian.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/TypedEnum.h"
#include <algorithm>

Просмотреть файл

@ -28,7 +28,6 @@
#include "nsFrameList.h"
#include "mozilla/layout/FrameChildList.h"
#include "FramePropertyTable.h"
#include "mozilla/TypedEnum.h"
#include "nsDirection.h"
#include "WritingModes.h"
#include <algorithm>

Просмотреть файл

@ -53,7 +53,6 @@
#include "mozilla/Preferences.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/Likely.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/TypedEnumBits.h"
using namespace mozilla;

Просмотреть файл

@ -10,7 +10,6 @@
#define mozilla_EnumeratedArray_h
#include "mozilla/Array.h"
#include "mozilla/TypedEnum.h"
namespace mozilla {

Просмотреть файл

@ -1,256 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/TypedEnumInternal.h"
#include "mozilla/MacroArgs.h"
#if defined(__cplusplus)
/**
* 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 optional type argument specifies the underlying type for the enum where
* supported, as with MOZ_ENUM_TYPE(). As with MOZ_ENUM_TYPE(), it will do
* nothing on compilers that do not support it.
*
* MOZ_{BEGIN,END}_ENUM_CLASS doesn't work for defining enum classes nested
* inside classes. To define an enum class nested inside another class, use
* MOZ_{BEGIN,END}_NESTED_ENUM_CLASS, and place a MOZ_FINISH_NESTED_ENUM_CLASS
* in namespace scope to handle bits that can only be implemented with
* namespace-scoped code. For example:
*
* class FooBar
* {
* MOZ_BEGIN_NESTED_ENUM_CLASS(Enum, int32_t)
* A,
* B = 6
* MOZ_END_NESTED_ENUM_CLASS(Enum)
* };
*
* MOZ_FINISH_NESTED_ENUM_CLASS(FooBar::Enum)
*/
#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.
*/
/* Single-argument form. */
# define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
enum class Name {
/* Two-argument form. */
# define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
enum class Name : type {
# define MOZ_END_NESTED_ENUM_CLASS(Name) \
};
# define MOZ_FINISH_NESTED_ENUM_CLASS(Name) /* nothing */
/*
* MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
* as template parameter types. For that, we need integer types.
* In the present case where the compiler supports strong enums,
* these are already integer types so there is nothing more to do.
*/
# define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name
/*
* See the comment below about MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE.
*/
# define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) 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;
* }
*/
/* Single-argument form. */
# define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER1(Name) \
class Name \
{ \
public: \
enum Enum \
{
/* Two-argument form. */
# define MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER2(Name, type) \
class Name \
{ \
public: \
enum Enum : type \
{
# define MOZ_END_NESTED_ENUM_CLASS(Name) \
}; \
Name() {} \
MOZ_CONSTEXPR Name(Enum aEnum) : mEnum(aEnum) {} \
template<typename Other> \
explicit MOZ_CONSTEXPR Name(Other num) : mEnum((Enum)num) {} \
MOZ_CONSTEXPR operator Enum() const { return mEnum; } \
explicit MOZ_CONSTEXPR Name(const mozilla::CastableTypedEnumResult<Name>& aOther) \
: mEnum(aOther.get()) \
{} \
private: \
Enum mEnum; \
};
# define MOZ_FINISH_NESTED_ENUM_CLASS(Name) \
inline int operator+(const int&, const Name::Enum&) = delete; \
inline int operator+(const Name::Enum&, const int&) = delete; \
inline int operator-(const int&, const Name::Enum&) = delete; \
inline int operator-(const Name::Enum&, const int&) = delete; \
inline int operator*(const int&, const Name::Enum&) = delete; \
inline int operator*(const Name::Enum&, const int&) = delete; \
inline int operator/(const int&, const Name::Enum&) = delete; \
inline int operator/(const Name::Enum&, const int&) = delete; \
inline int operator%(const int&, const Name::Enum&) = delete; \
inline int operator%(const Name::Enum&, const int&) = delete; \
inline int operator+(const Name::Enum&) = delete; \
inline int operator-(const Name::Enum&) = delete; \
inline int& operator++(Name::Enum&) = delete; \
inline int operator++(Name::Enum&, int) = delete; \
inline int& operator--(Name::Enum&) = delete; \
inline int operator--(Name::Enum&, int) = delete; \
inline bool operator==(const int&, const Name::Enum&) = delete; \
inline bool operator==(const Name::Enum&, const int&) = delete; \
inline bool operator!=(const int&, const Name::Enum&) = delete; \
inline bool operator!=(const Name::Enum&, const int&) = delete; \
inline bool operator>(const int&, const Name::Enum&) = delete; \
inline bool operator>(const Name::Enum&, const int&) = delete; \
inline bool operator<(const int&, const Name::Enum&) = delete; \
inline bool operator<(const Name::Enum&, const int&) = delete; \
inline bool operator>=(const int&, const Name::Enum&) = delete; \
inline bool operator>=(const Name::Enum&, const int&) = delete; \
inline bool operator<=(const int&, const Name::Enum&) = delete; \
inline bool operator<=(const Name::Enum&, const int&) = delete; \
inline bool operator!(const Name::Enum&) = delete; \
inline bool operator&&(const bool&, const Name::Enum&) = delete; \
inline bool operator&&(const Name::Enum&, const bool&) = delete; \
inline bool operator||(const bool&, const Name::Enum&) = delete; \
inline bool operator||(const Name::Enum&, const bool&) = delete; \
inline int operator&(const int&, const Name::Enum&) = delete; \
inline int operator&(const Name::Enum&, const int&) = delete; \
inline int operator|(const int&, const Name::Enum&) = delete; \
inline int operator|(const Name::Enum&, const int&) = delete; \
inline int operator^(const int&, const Name::Enum&) = delete; \
inline int operator^(const Name::Enum&, const int&) = delete; \
inline int operator<<(const int&, const Name::Enum&) = delete; \
inline int operator<<(const Name::Enum&, const int&) = delete; \
inline int operator>>(const int&, const Name::Enum&) = delete; \
inline int operator>>(const Name::Enum&, const int&) = delete; \
inline int& operator+=(int&, const Name::Enum&) = delete; \
inline int& operator-=(int&, const Name::Enum&) = delete; \
inline int& operator*=(int&, const Name::Enum&) = delete; \
inline int& operator/=(int&, const Name::Enum&) = delete; \
inline int& operator%=(int&, const Name::Enum&) = delete; \
inline int& operator&=(int&, const Name::Enum&) = delete; \
inline int& operator|=(int&, const Name::Enum&) = delete; \
inline int& operator^=(int&, const Name::Enum&) = delete; \
inline int& operator<<=(int&, const Name::Enum&) = delete; \
inline int& operator>>=(int&, const Name::Enum&) = delete;
/*
* MOZ_ENUM_CLASS_ENUM_TYPE allows using enum classes
* as template parameter types. For that, we need integer types.
* In the present case, the integer type is the Enum nested type.
*/
# define MOZ_ENUM_CLASS_ENUM_TYPE(Name) Name::Enum
/*
* MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE is a variant of MOZ_ENUM_CLASS_ENUM_TYPE
* to be used when the enum class at hand depends on template parameters.
*
* Indeed, if T depends on template parameters, in order to name a nested type
* in T, C++ does not allow to just write "T::NestedType". Instead, we have
* to write "typename T::NestedType". The role of this macro is to add
* this "typename" keywords where needed.
*
* Example:
*
* template<typename T, MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(T) Value>
* struct S {};
*
* MOZ_BEGIN_ENUM_CLASS(E)
* Foo,
* Bar
* MOZ_END_ENUM_CLASS(E)
*
* S<E, E::Bar> s;
*
* In this example, the second template parameter to S is meant to be of type
* T, but on non-C++11 compilers, type T is a class type, not an integer
* type, so it is not accepted as the type of a constant template parameter.
* One would then want to use MOZ_ENUM_CLASS_ENUM_TYPE(T), but that doesn't
* work either as T depends on template parameters (more specifically here, T
* _is_ a template parameter) so as MOZ_ENUM_CLASS_ENUM_TYPE(T) expands to
* T::Enum, we are missing the required "typename" keyword. So here,
* MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE is needed.
*/
# define MOZ_TEMPLATE_ENUM_CLASS_ENUM_TYPE(Name) typename Name::Enum
#endif
# define MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE(a, b) a b
# define MOZ_BEGIN_NESTED_ENUM_CLASS(...) \
MOZ_BEGIN_NESTED_ENUM_CLASS_GLUE( \
MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_BEGIN_NESTED_ENUM_CLASS_HELPER, \
__VA_ARGS__), \
(__VA_ARGS__))
# define MOZ_BEGIN_ENUM_CLASS(...) MOZ_BEGIN_NESTED_ENUM_CLASS(__VA_ARGS__)
# define MOZ_END_ENUM_CLASS(Name) \
MOZ_END_NESTED_ENUM_CLASS(Name) \
MOZ_FINISH_NESTED_ENUM_CLASS(Name)
#endif /* __cplusplus */
#endif /* mozilla_TypedEnum_h */

Просмотреть файл

@ -11,11 +11,55 @@
#ifndef mozilla_TypedEnumBits_h
#define mozilla_TypedEnumBits_h
#include "mozilla/Attributes.h"
#include "mozilla/IntegerTypeTraits.h"
#include "mozilla/TypedEnumInternal.h"
namespace mozilla {
/*
* The problem that CastableTypedEnumResult aims to solve is that
* typed enums are not convertible to bool, and there is no way to make them
* be, yet user code wants to be able to write
*
* if (myFlags & Flags::SOME_PARTICULAR_FLAG) (1)
*
* There are different approaches to solving this. Most of them require
* adapting user code. For example, we could implement operator! and have
* the user write
*
* if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG)) (2)
*
* Or we could supply a IsNonZero() or Any() function returning whether
* an enum value is nonzero, and have the user write
*
* if (Any(Flags & Flags::SOME_PARTICULAR_FLAG)) (3)
*
* But instead, we choose to preserve the original user syntax (1) as it
* is inherently more readable, and to ease porting existing code to typed
* enums. We achieve this by having operator& and other binary bitwise
* operators have as return type a class, CastableTypedEnumResult,
* that wraps a typed enum but adds bool convertibility.
*/
template<typename E>
class CastableTypedEnumResult
{
private:
const E mValue;
public:
explicit MOZ_CONSTEXPR CastableTypedEnumResult(E aValue)
: mValue(aValue)
{}
MOZ_CONSTEXPR operator E() const { return mValue; }
template<typename DestinationType>
MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR
operator DestinationType() const { return DestinationType(mValue); }
MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); }
};
#define MOZ_CASTABLETYPEDENUMRESULT_BINOP(Op, OtherType, ReturnType) \
template<typename E> \
MOZ_CONSTEXPR ReturnType \
@ -69,32 +113,6 @@ MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(^=)
#undef MOZ_CASTABLETYPEDENUMRESULT_BINOP
#ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
#define MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(Op, ReturnType) \
template<typename E> \
MOZ_CONSTEXPR ReturnType \
operator Op(typename E::Enum aE, const CastableTypedEnumResult<E>& aR) \
{ \
return ReturnType(aE Op E(aR)); \
} \
template<typename E> \
MOZ_CONSTEXPR ReturnType \
operator Op(const CastableTypedEnumResult<E>& aR, typename E::Enum aE) \
{ \
return ReturnType(E(aR) Op aE); \
}
MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(|, CastableTypedEnumResult<E>)
MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(&, CastableTypedEnumResult<E>)
MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(^, CastableTypedEnumResult<E>)
MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(==, bool)
MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11(!=, bool)
#undef MOZ_CASTABLETYPEDENUMRESULT_BINOP_EXTRA_NON_CXX11
#endif // not MOZ_HAVE_CXX11_STRONG_ENUMS
namespace detail {
template<typename E>
struct UnsignedIntegerTypeForEnum
@ -119,7 +137,11 @@ struct UnsignedIntegerTypeForEnum
return a = a Op b; \
}
#define MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name) \
/**
* MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS generates standard bitwise operators
* for the given enum type. Use this to enable using an enum type as bit-field.
*/
#define MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Name) \
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, |) \
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, &) \
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, ^) \
@ -131,54 +153,4 @@ struct UnsignedIntegerTypeForEnum
return Result(Name(~(U(a)))); \
}
#ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
# define MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, Op) \
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
operator Op(Name a, Name::Enum b) \
{ \
return a Op Name(b); \
} \
\
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
operator Op(Name::Enum a, Name b) \
{ \
return Name(a) Op b; \
} \
\
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
operator Op(Name::Enum a, Name::Enum b) \
{ \
return Name(a) Op Name(b); \
} \
\
inline Name& \
operator Op##=(Name& a, Name::Enum b) \
{ \
return a = a Op Name(b); \
}
# define MOZ_MAKE_ENUM_CLASS_OPS_EXTRA_NON_CXX11(Name) \
MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, |) \
MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, &) \
MOZ_MAKE_ENUM_CLASS_BITWISE_BINOP_EXTRA_NON_CXX11(Name, ^) \
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
operator~(Name::Enum a) \
{ \
return ~(Name(a)); \
}
#endif
/**
* MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS generates standard bitwise operators
* for the given enum type. Use this to enable using an enum type as bit-field.
*/
#ifdef MOZ_HAVE_CXX11_STRONG_ENUMS
# define MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Name) \
MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name)
#else
# define MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(Name) \
MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name) \
MOZ_MAKE_ENUM_CLASS_OPS_EXTRA_NON_CXX11(Name)
#endif
#endif // mozilla_TypedEnumBits_h

Просмотреть файл

@ -1,103 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* Internal stuff needed by TypedEnum.h and TypedEnumBits.h. */
// NOTE: When we can assume C++11 enum class support and TypedEnum.h goes away,
// we should then consider folding TypedEnumInternal.h into TypedEnumBits.h.
#ifndef mozilla_TypedEnumInternal_h
#define mozilla_TypedEnumInternal_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_STRONG_ENUMS
# endif
#elif defined(__GNUC__)
# if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
# if MOZ_GCC_VERSION_AT_LEAST(4, 6, 3)
# define MOZ_HAVE_CXX11_STRONG_ENUMS
# endif
# endif
#elif defined(_MSC_VER)
# define MOZ_HAVE_CXX11_STRONG_ENUMS
#endif
namespace mozilla {
/*
* The problem that CastableTypedEnumResult aims to solve is that
* typed enums are not convertible to bool, and there is no way to make them
* be, yet user code wants to be able to write
*
* if (myFlags & Flags::SOME_PARTICULAR_FLAG) (1)
*
* There are different approaches to solving this. Most of them require
* adapting user code. For example, we could implement operator! and have
* the user write
*
* if (!!(myFlags & Flags::SOME_PARTICULAR_FLAG)) (2)
*
* Or we could supply a IsNonZero() or Any() function returning whether
* an enum value is nonzero, and have the user write
*
* if (Any(Flags & Flags::SOME_PARTICULAR_FLAG)) (3)
*
* But instead, we choose to preserve the original user syntax (1) as it
* is inherently more readable, and to ease porting existing code to typed
* enums. We achieve this by having operator& and other binary bitwise
* operators have as return type a class, CastableTypedEnumResult,
* that wraps a typed enum but adds bool convertibility.
*/
template<typename E>
class CastableTypedEnumResult
{
private:
const E mValue;
public:
explicit MOZ_CONSTEXPR CastableTypedEnumResult(E aValue)
: mValue(aValue)
{}
MOZ_CONSTEXPR operator E() const { return mValue; }
template<typename DestinationType>
MOZ_EXPLICIT_CONVERSION MOZ_CONSTEXPR
operator DestinationType() const { return DestinationType(mValue); }
MOZ_CONSTEXPR bool operator !() const { return !bool(mValue); }
#ifndef MOZ_HAVE_CXX11_STRONG_ENUMS
// This get() method is used to implement a constructor in the
// non-c++11 fallback path for MOZ_BEGIN_ENUM_CLASS, taking a
// CastableTypedEnumResult. If we try to implement it using the
// above conversion operator E(), then at least clang 3.3
// (when forced to take the non-c++11 fallback path) compiles
// this constructor to an infinite recursion. So we introduce this
// get() method, that does exactly the same as the conversion operator,
// to work around this.
MOZ_CONSTEXPR E get() const { return mValue; }
#endif
};
} // namespace mozilla
#endif // __cplusplus
#endif // mozilla_TypedEnumInternal_h

Просмотреть файл

@ -69,9 +69,7 @@ EXPORTS.mozilla = [
'TemplateLib.h',
'ThreadLocal.h',
'ToString.h',
'TypedEnum.h',
'TypedEnumBits.h',
'TypedEnumInternal.h',
'Types.h',
'TypeTraits.h',
'UniquePtr.h',

Просмотреть файл

@ -6,7 +6,6 @@
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/TypedEnum.h"
#include "mozilla/TypedEnumBits.h"
#include <stdint.h>
@ -81,29 +80,29 @@ enum class CharEnumBitField : char {
struct Nested
{
MOZ_BEGIN_NESTED_ENUM_CLASS(AutoEnum)
enum class AutoEnum {
A,
B,
C = -1
MOZ_END_NESTED_ENUM_CLASS(AutoEnum)
};
MOZ_BEGIN_NESTED_ENUM_CLASS(CharEnum, char)
enum class CharEnum : char {
A = 4,
B,
C = 1
MOZ_END_NESTED_ENUM_CLASS(CharEnum)
};
MOZ_BEGIN_NESTED_ENUM_CLASS(AutoEnumBitField)
enum class AutoEnumBitField {
A,
B = 0x20,
C
MOZ_END_NESTED_ENUM_CLASS(AutoEnumBitField)
};
MOZ_BEGIN_NESTED_ENUM_CLASS(CharEnumBitField, char)
enum class CharEnumBitField : char {
A = 1,
B = 1,
C = 1
MOZ_END_NESTED_ENUM_CLASS(CharEnumBitField)
};
};
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(AutoEnumBitField)
@ -154,7 +153,7 @@ TestNonConvertibilityForOneType()
{
using mozilla::IsConvertible;
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS) && defined(MOZ_HAVE_EXPLICIT_CONVERSION)
#if defined(MOZ_HAVE_EXPLICIT_CONVERSION)
static_assert(!IsConvertible<T, bool>::value, "should not be convertible");
static_assert(!IsConvertible<T, int>::value, "should not be convertible");
static_assert(!IsConvertible<T, uint64_t>::value, "should not be convertible");

Просмотреть файл

@ -8,8 +8,6 @@
#include <stdint.h>
#include "mozilla/TypedEnum.h"
/**
* XXX Following enums should be in BasicEvents.h. However, currently, it's
* impossible to use foward delearation for enum.

Просмотреть файл

@ -8,7 +8,6 @@
#define VibrancyManager_h
#include "mozilla/Assertions.h"
#include "mozilla/TypedEnum.h"
#include "nsClassHashtable.h"
#include "nsRegion.h"
#include "nsTArray.h"

Просмотреть файл

@ -8,7 +8,6 @@
#define nsError_h__
#include "mozilla/Likely.h"
#include "mozilla/TypedEnum.h"
#include <stdint.h>
@ -122,7 +121,7 @@
* either can be converted to the other, so it's ambiguous. So we have to fall
* back to a regular enum.
*/
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
#if defined(__cplusplus)
typedef enum class tag_nsresult : uint32_t
{
#undef ERROR
@ -136,14 +135,6 @@
* #define's for compatibility with old code.
*/
#include "ErrorListCxxDefines.h"
#elif defined(__cplusplus)
typedef enum tag_nsresult : uint32_t
{
#undef ERROR
#define ERROR(key, val) key = val
#include "ErrorList.h"
#undef ERROR
} nsresult;
#else
/*
* C doesn't have any way to fix the type underlying an enum, and enum

Просмотреть файл

@ -9,6 +9,7 @@
/*
* Double hashing, a la Knuth 6.
*/
#include "mozilla/Attributes.h" // for MOZ_ALWAYS_INLINE
#include "mozilla/fallible.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Types.h"