2014-04-26 06:34:04 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS allows using a typed enum as bit flags.
|
|
|
|
*/
|
2014-04-26 06:34:04 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_TypedEnumBits_h
|
|
|
|
#define mozilla_TypedEnumBits_h
|
|
|
|
|
|
|
|
#include "mozilla/IntegerTypeTraits.h"
|
|
|
|
#include "mozilla/TypedEnumInternal.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
#define MOZ_CASTABLETYPEDENUMRESULT_BINOP(Op, OtherType, ReturnType) \
|
|
|
|
template<typename E> \
|
|
|
|
MOZ_CONSTEXPR ReturnType \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(const OtherType& aE, const CastableTypedEnumResult<E>& aR) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return ReturnType(aE Op OtherType(aR)); \
|
2014-04-26 06:34:04 +04:00
|
|
|
} \
|
|
|
|
template<typename E> \
|
|
|
|
MOZ_CONSTEXPR ReturnType \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(const CastableTypedEnumResult<E>& aR, const OtherType& aE) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return ReturnType(OtherType(aR) Op aE); \
|
2014-04-26 06:34:04 +04:00
|
|
|
} \
|
|
|
|
template<typename E> \
|
|
|
|
MOZ_CONSTEXPR ReturnType \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(const CastableTypedEnumResult<E>& aR1, \
|
|
|
|
const CastableTypedEnumResult<E>& aR2) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return ReturnType(OtherType(aR1) Op OtherType(aR2)); \
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(|, E, CastableTypedEnumResult<E>)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(&, E, CastableTypedEnumResult<E>)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(^, E, CastableTypedEnumResult<E>)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(==, E, bool)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(!=, E, bool)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(||, bool, bool)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_BINOP(&&, bool, bool)
|
|
|
|
|
|
|
|
template <typename E>
|
|
|
|
MOZ_CONSTEXPR CastableTypedEnumResult<E>
|
2014-07-11 06:10:17 +04:00
|
|
|
operator ~(const CastableTypedEnumResult<E>& aR)
|
2014-04-26 06:34:04 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return CastableTypedEnumResult<E>(~(E(aR)));
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(Op) \
|
|
|
|
template<typename E> \
|
|
|
|
E& \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(E& aR1, \
|
|
|
|
const CastableTypedEnumResult<E>& aR2) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return aR1 Op E(aR2); \
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(&=)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(|=)
|
|
|
|
MOZ_CASTABLETYPEDENUMRESULT_COMPOUND_ASSIGN_OP(^=)
|
|
|
|
|
|
|
|
#undef 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 \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(typename E::Enum aE, const CastableTypedEnumResult<E>& aR) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return ReturnType(aE Op E(aR)); \
|
2014-04-26 06:34:04 +04:00
|
|
|
} \
|
|
|
|
template<typename E> \
|
|
|
|
MOZ_CONSTEXPR ReturnType \
|
2014-07-11 06:10:17 +04:00
|
|
|
operator Op(const CastableTypedEnumResult<E>& aR, typename E::Enum aE) \
|
2014-04-26 06:34:04 +04:00
|
|
|
{ \
|
2014-07-11 06:10:17 +04:00
|
|
|
return ReturnType(E(aR) Op aE); \
|
2014-04-26 06:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
: UnsignedStdintTypeForSize<sizeof(E)>
|
|
|
|
{};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#define MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, Op) \
|
|
|
|
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
|
|
|
|
operator Op(Name a, Name b) \
|
|
|
|
{ \
|
|
|
|
typedef mozilla::CastableTypedEnumResult<Name> Result; \
|
|
|
|
typedef mozilla::detail::UnsignedIntegerTypeForEnum<Name>::Type U; \
|
|
|
|
return Result(Name(U(a) Op U(b))); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
inline Name& \
|
|
|
|
operator Op##=(Name& a, Name b) \
|
|
|
|
{ \
|
|
|
|
return a = a Op b; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MOZ_MAKE_ENUM_CLASS_OPS_IMPL(Name) \
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, |) \
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, &) \
|
|
|
|
MOZ_MAKE_ENUM_CLASS_BINOP_IMPL(Name, ^) \
|
|
|
|
inline MOZ_CONSTEXPR mozilla::CastableTypedEnumResult<Name> \
|
|
|
|
operator~(Name a) \
|
|
|
|
{ \
|
|
|
|
typedef mozilla::CastableTypedEnumResult<Name> Result; \
|
|
|
|
typedef mozilla::detail::UnsignedIntegerTypeForEnum<Name>::Type U; \
|
|
|
|
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
|