EnumClassHelp.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. // This macro defines some global operators, and hence must be used at global scope
  7. #define ENUM_CLASS_HELPERS(TEnum, TUnderlying) \
  8. inline TEnum operator +(const TEnum e, const TUnderlying n) \
  9. { \
  10. return static_cast<TEnum>(static_cast<TUnderlying>(e) + n); \
  11. } \
  12. \
  13. inline TEnum operator +(const TUnderlying n, const TEnum e) \
  14. { \
  15. return static_cast<TEnum>(n + static_cast<TUnderlying>(e)); \
  16. } \
  17. \
  18. inline TEnum operator +(const TEnum e0, const TEnum e1) \
  19. { \
  20. return static_cast<TUnderlying>(e0) + e1; \
  21. } \
  22. \
  23. inline TEnum &operator +=(TEnum &e, const TUnderlying n) \
  24. { \
  25. return e = e + n; \
  26. } \
  27. \
  28. inline TEnum &operator ++(TEnum &e) \
  29. { \
  30. return e += 1; \
  31. } \
  32. \
  33. inline TEnum operator ++(TEnum &e, const int) \
  34. { \
  35. const TEnum old = e; \
  36. ++e; \
  37. return old; \
  38. } \
  39. \
  40. inline TEnum operator -(const TEnum e, const TUnderlying n) \
  41. { \
  42. return static_cast<TEnum>(static_cast<TUnderlying>(e) - n); \
  43. } \
  44. \
  45. inline TEnum operator -(const TUnderlying n, const TEnum e) \
  46. { \
  47. return static_cast<TEnum>(n - static_cast<TUnderlying>(e)); \
  48. } \
  49. \
  50. inline TEnum operator -(const TEnum e0, const TEnum e1) \
  51. { \
  52. return static_cast<TUnderlying>(e0) - e1; \
  53. } \
  54. \
  55. inline TEnum &operator -=(TEnum &e, const TUnderlying n) \
  56. { \
  57. return e = e - n; \
  58. } \
  59. \
  60. inline TEnum &operator --(TEnum &e) \
  61. { \
  62. return e -= 1; \
  63. } \
  64. \
  65. inline TEnum operator --(TEnum &e, const int) \
  66. { \
  67. const TEnum old = e; \
  68. --e; \
  69. return old; \
  70. } \
  71. \
  72. inline TEnum operator &(const TEnum e0, const TEnum e1) \
  73. { \
  74. return static_cast<TEnum>(static_cast<TUnderlying>(e0) & static_cast<TUnderlying>(e1)); \
  75. } \
  76. \
  77. inline TEnum &operator &=(TEnum &e0, const TEnum e1) \
  78. { \
  79. return e0 = e0 & e1; \
  80. } \
  81. \
  82. inline TEnum operator ^(const TEnum e0, const TEnum e1) \
  83. { \
  84. return static_cast<TEnum>(static_cast<TUnderlying>(e0) ^ static_cast<TUnderlying>(e1)); \
  85. } \
  86. \
  87. inline TEnum &operator ^=(TEnum &e0, const TEnum e1) \
  88. { \
  89. return e0 = e0 ^ e1; \
  90. } \
  91. \
  92. inline TEnum operator |(const TEnum e0, const TEnum e1) \
  93. { \
  94. return static_cast<TEnum>(static_cast<TUnderlying>(e0) | static_cast<TUnderlying>(e1)); \
  95. } \
  96. \
  97. inline TEnum &operator |=(TEnum &e0, const TEnum e1) \
  98. { \
  99. return e0 = e0 | e1; \
  100. } \
  101. \
  102. inline TEnum operator <<(const TEnum e, const TUnderlying n) \
  103. { \
  104. return static_cast<TEnum>(static_cast<TUnderlying>(e) << n); \
  105. } \
  106. \
  107. inline TEnum &operator <<=(TEnum &e, const TUnderlying n) \
  108. { \
  109. return e = e << n; \
  110. } \
  111. \
  112. inline TEnum operator >>(const TEnum e, const TUnderlying n) \
  113. { \
  114. return static_cast<TEnum>(static_cast<TUnderlying>(e) >> n); \
  115. } \
  116. \
  117. inline TEnum &operator >>=(TEnum &e, const TUnderlying n) \
  118. { \
  119. return e = e >> n; \
  120. } \
  121. \
  122. inline TEnum operator ~(const TEnum e) \
  123. { \
  124. return static_cast<TEnum>(~static_cast<TUnderlying>(e)); \
  125. } \
  126. \
  127. inline TEnum operator +(const TEnum e) \
  128. { \
  129. return e; \
  130. } \
  131. \
  132. inline TEnum operator -(const TEnum e) \
  133. { \
  134. return static_cast<TEnum>(static_cast<TUnderlying>(0) - static_cast<TUnderlying>(e)); \
  135. } \
  136. \
  137. inline bool operator !(const TEnum e) \
  138. { \
  139. return !static_cast<TUnderlying>(e); \
  140. } \
  141. // For private enum classes defined inside other classes, this macro can be used inside the class to declare friends
  142. #define ENUM_CLASS_HELPER_FRIENDS(TEnum, TUnderlying) \
  143. friend TEnum operator +(const TEnum e, const TUnderlying n); \
  144. friend TEnum operator +(const TUnderlying n, const TEnum e); \
  145. friend TEnum operator +(const TEnum e0, const TEnum e1); \
  146. friend TEnum &operator +=(TEnum &e, const TUnderlying n); \
  147. friend TEnum &operator ++(TEnum &e); \
  148. friend TEnum operator ++(TEnum &e, const int); \
  149. friend TEnum operator -(const TEnum e, const TUnderlying n); \
  150. friend TEnum operator -(const TUnderlying n, const TEnum e); \
  151. friend TEnum operator -(const TEnum e0, const TEnum e1); \
  152. friend TEnum &operator -=(TEnum &e, const TUnderlying n); \
  153. friend TEnum &operator --(TEnum &e); \
  154. friend TEnum operator --(TEnum &e, const int); \
  155. friend TEnum operator &(const TEnum e0, const TEnum e1); \
  156. friend TEnum &operator &=(TEnum &e0, const TEnum e1); \
  157. friend TEnum operator ^(const TEnum e0, const TEnum e1); \
  158. friend TEnum &operator ^=(TEnum &e0, const TEnum e1); \
  159. friend TEnum operator |(const TEnum e0, const TEnum e1); \
  160. friend TEnum &operator |=(TEnum &e0, const TEnum e1); \
  161. friend TEnum operator <<(const TEnum e, const TUnderlying n); \
  162. friend TEnum &operator <<=(TEnum &e, const TUnderlying n); \
  163. friend TEnum operator >>(const TEnum e, const TUnderlying n); \
  164. friend TEnum &operator >>=(TEnum &e, const TUnderlying n); \
  165. friend TEnum operator ~(const TEnum e); \
  166. friend TEnum operator +(const TEnum e); \
  167. friend TEnum operator -(const TEnum e); \
  168. friend bool operator !(const TEnum e);