| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #pragma once
- // This macro defines some global operators, and hence must be used at global scope
- #define ENUM_CLASS_HELPERS(TEnum, TUnderlying) \
- inline TEnum operator +(const TEnum e, const TUnderlying n) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e) + n); \
- } \
- \
- inline TEnum operator +(const TUnderlying n, const TEnum e) \
- { \
- return static_cast<TEnum>(n + static_cast<TUnderlying>(e)); \
- } \
- \
- inline TEnum operator +(const TEnum e0, const TEnum e1) \
- { \
- return static_cast<TUnderlying>(e0) + e1; \
- } \
- \
- inline TEnum &operator +=(TEnum &e, const TUnderlying n) \
- { \
- return e = e + n; \
- } \
- \
- inline TEnum &operator ++(TEnum &e) \
- { \
- return e += 1; \
- } \
- \
- inline TEnum operator ++(TEnum &e, const int) \
- { \
- const TEnum old = e; \
- ++e; \
- return old; \
- } \
- \
- inline TEnum operator -(const TEnum e, const TUnderlying n) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e) - n); \
- } \
- \
- inline TEnum operator -(const TUnderlying n, const TEnum e) \
- { \
- return static_cast<TEnum>(n - static_cast<TUnderlying>(e)); \
- } \
- \
- inline TEnum operator -(const TEnum e0, const TEnum e1) \
- { \
- return static_cast<TUnderlying>(e0) - e1; \
- } \
- \
- inline TEnum &operator -=(TEnum &e, const TUnderlying n) \
- { \
- return e = e - n; \
- } \
- \
- inline TEnum &operator --(TEnum &e) \
- { \
- return e -= 1; \
- } \
- \
- inline TEnum operator --(TEnum &e, const int) \
- { \
- const TEnum old = e; \
- --e; \
- return old; \
- } \
- \
- inline TEnum operator &(const TEnum e0, const TEnum e1) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e0) & static_cast<TUnderlying>(e1)); \
- } \
- \
- inline TEnum &operator &=(TEnum &e0, const TEnum e1) \
- { \
- return e0 = e0 & e1; \
- } \
- \
- inline TEnum operator ^(const TEnum e0, const TEnum e1) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e0) ^ static_cast<TUnderlying>(e1)); \
- } \
- \
- inline TEnum &operator ^=(TEnum &e0, const TEnum e1) \
- { \
- return e0 = e0 ^ e1; \
- } \
- \
- inline TEnum operator |(const TEnum e0, const TEnum e1) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e0) | static_cast<TUnderlying>(e1)); \
- } \
- \
- inline TEnum &operator |=(TEnum &e0, const TEnum e1) \
- { \
- return e0 = e0 | e1; \
- } \
- \
- inline TEnum operator <<(const TEnum e, const TUnderlying n) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e) << n); \
- } \
- \
- inline TEnum &operator <<=(TEnum &e, const TUnderlying n) \
- { \
- return e = e << n; \
- } \
- \
- inline TEnum operator >>(const TEnum e, const TUnderlying n) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(e) >> n); \
- } \
- \
- inline TEnum &operator >>=(TEnum &e, const TUnderlying n) \
- { \
- return e = e >> n; \
- } \
- \
- inline TEnum operator ~(const TEnum e) \
- { \
- return static_cast<TEnum>(~static_cast<TUnderlying>(e)); \
- } \
- \
- inline TEnum operator +(const TEnum e) \
- { \
- return e; \
- } \
- \
- inline TEnum operator -(const TEnum e) \
- { \
- return static_cast<TEnum>(static_cast<TUnderlying>(0) - static_cast<TUnderlying>(e)); \
- } \
- \
- inline bool operator !(const TEnum e) \
- { \
- return !static_cast<TUnderlying>(e); \
- } \
- // For private enum classes defined inside other classes, this macro can be used inside the class to declare friends
- #define ENUM_CLASS_HELPER_FRIENDS(TEnum, TUnderlying) \
- friend TEnum operator +(const TEnum e, const TUnderlying n); \
- friend TEnum operator +(const TUnderlying n, const TEnum e); \
- friend TEnum operator +(const TEnum e0, const TEnum e1); \
- friend TEnum &operator +=(TEnum &e, const TUnderlying n); \
- friend TEnum &operator ++(TEnum &e); \
- friend TEnum operator ++(TEnum &e, const int); \
- friend TEnum operator -(const TEnum e, const TUnderlying n); \
- friend TEnum operator -(const TUnderlying n, const TEnum e); \
- friend TEnum operator -(const TEnum e0, const TEnum e1); \
- friend TEnum &operator -=(TEnum &e, const TUnderlying n); \
- friend TEnum &operator --(TEnum &e); \
- friend TEnum operator --(TEnum &e, const int); \
- friend TEnum operator &(const TEnum e0, const TEnum e1); \
- friend TEnum &operator &=(TEnum &e0, const TEnum e1); \
- friend TEnum operator ^(const TEnum e0, const TEnum e1); \
- friend TEnum &operator ^=(TEnum &e0, const TEnum e1); \
- friend TEnum operator |(const TEnum e0, const TEnum e1); \
- friend TEnum &operator |=(TEnum &e0, const TEnum e1); \
- friend TEnum operator <<(const TEnum e, const TUnderlying n); \
- friend TEnum &operator <<=(TEnum &e, const TUnderlying n); \
- friend TEnum operator >>(const TEnum e, const TUnderlying n); \
- friend TEnum &operator >>=(TEnum &e, const TUnderlying n); \
- friend TEnum operator ~(const TEnum e); \
- friend TEnum operator +(const TEnum e); \
- friend TEnum operator -(const TEnum e); \
- friend bool operator !(const TEnum e);
|