2
0

Enum.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Assert.h>
  7. namespace anki
  8. {
  9. /// @privatesection
  10. /// @{
  11. /// This is a template where the Type will be I64 if the T is any unsigned integer and U64 otherwise
  12. template<typename T, bool = std::is_unsigned<T>::value>
  13. class EnumSafeIntegerType
  14. {
  15. public:
  16. using Type = I64;
  17. };
  18. template<typename T>
  19. class EnumSafeIntegerType<T, true>
  20. {
  21. public:
  22. using Type = U64;
  23. };
  24. /// This macro will do an operation between 2 values. It will be used in constexpr functions. There is also an assertion
  25. /// which makes sure that the result will fit in an enum. Despite the fact that the assertion contains non-constexpr
  26. /// elements it will work on constexpr expressions. The compiler will compile-time ignore the non-constexpr part if the
  27. /// assert if the assertion expression is true.
  28. #define _ANKI_ENUM_OPERATION_BODY(enumType, regularOperator, a, b) \
  29. using EnumInt = std::underlying_type<enumType>::type; \
  30. using SafeInt = EnumSafeIntegerType<EnumInt>::Type; \
  31. const SafeInt c = SafeInt(a) regularOperator SafeInt(b); \
  32. ANKI_ASSERT(c <= SafeInt(std::numeric_limits<EnumInt>::max())); \
  33. return enumType(c)
  34. #define _ANKI_ENUM_OPERATOR(enumType, qualifier, regularOperator, assignmentOperator) \
  35. constexpr qualifier enumType operator regularOperator(const enumType a, const enumType b) \
  36. { \
  37. _ANKI_ENUM_OPERATION_BODY(enumType, regularOperator, a, b); \
  38. } \
  39. constexpr qualifier enumType operator regularOperator(const enumType a, \
  40. const std::underlying_type<enumType>::type b) \
  41. { \
  42. _ANKI_ENUM_OPERATION_BODY(enumType, regularOperator, a, b); \
  43. } \
  44. constexpr qualifier enumType operator regularOperator(const std::underlying_type<enumType>::type a, \
  45. const enumType b) \
  46. { \
  47. _ANKI_ENUM_OPERATION_BODY(enumType, regularOperator, a, b); \
  48. } \
  49. qualifier enumType& operator assignmentOperator(enumType& a, const enumType b) \
  50. { \
  51. a = a regularOperator b; \
  52. return a; \
  53. } \
  54. qualifier enumType& operator assignmentOperator(enumType& a, const std::underlying_type<enumType>::type b) \
  55. { \
  56. a = a regularOperator b; \
  57. return a; \
  58. } \
  59. qualifier std::underlying_type<enumType>::type& operator assignmentOperator( \
  60. std::underlying_type<enumType>::type& a, const enumType b) \
  61. { \
  62. using EnumInt = std::underlying_type<enumType>::type; \
  63. a = EnumInt(a regularOperator b); \
  64. return a; \
  65. }
  66. #define _ANKI_ENUM_UNARAY_OPERATOR(enumType, qualifier, regularOperator) \
  67. constexpr qualifier enumType operator regularOperator(const enumType a) \
  68. { \
  69. using EnumInt = std::underlying_type<enumType>::type; \
  70. return enumType(regularOperator EnumInt(a)); \
  71. }
  72. #define _ANKI_ENUM_INCREMENT_DECREMENT(enumType, qualifier) \
  73. qualifier enumType& operator++(enumType& a) \
  74. { \
  75. a = a + 1; \
  76. return a; \
  77. } \
  78. qualifier enumType& operator--(enumType& a) \
  79. { \
  80. a = a - 1; \
  81. return a; \
  82. } \
  83. qualifier enumType operator++(enumType& a, int) \
  84. { \
  85. const enumType old = a; \
  86. ++a; \
  87. return old; \
  88. } \
  89. qualifier enumType operator--(enumType& a, int) \
  90. { \
  91. const enumType old = a; \
  92. --a; \
  93. return old; \
  94. }
  95. #define _ANKI_ENUM_NEGATIVE_OPERATOR(enumType, qualifier) \
  96. constexpr qualifier Bool operator!(const enumType a) \
  97. { \
  98. using EnumInt = std::underlying_type<enumType>::type; \
  99. return EnumInt(a) == 0; \
  100. }
  101. #define _ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(enumType, qualifier) \
  102. _ANKI_ENUM_OPERATOR(enumType, qualifier, |, |=) \
  103. _ANKI_ENUM_OPERATOR(enumType, qualifier, &, &=) \
  104. _ANKI_ENUM_OPERATOR(enumType, qualifier, ^, ^=) \
  105. _ANKI_ENUM_OPERATOR(enumType, qualifier, +, +=) \
  106. _ANKI_ENUM_OPERATOR(enumType, qualifier, -, -=) \
  107. _ANKI_ENUM_OPERATOR(enumType, qualifier, *, *=) \
  108. _ANKI_ENUM_OPERATOR(enumType, qualifier, /, /=) \
  109. _ANKI_ENUM_OPERATOR(enumType, qualifier, <<, <<=) \
  110. _ANKI_ENUM_OPERATOR(enumType, qualifier, >>, >>=) \
  111. _ANKI_ENUM_UNARAY_OPERATOR(enumType, qualifier, ~) \
  112. _ANKI_ENUM_INCREMENT_DECREMENT(enumType, qualifier) \
  113. _ANKI_ENUM_NEGATIVE_OPERATOR(enumType, qualifier)
  114. /// @}
  115. /// @addtogroup util_other
  116. /// @{
  117. /// Implement all those functions that will make a stronly typed enum behave like the old type of enums.
  118. #define ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(enumType) _ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(enumType, inline)
  119. /// Same as ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS but for enums that are defined in a class.
  120. #define ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS_FRIEND(enumType) _ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(enumType, friend)
  121. /// @memberof EnumIterable
  122. template<typename TEnum>
  123. class EnumIterableIterator
  124. {
  125. public:
  126. using Type = typename std::underlying_type<TEnum>::type;
  127. EnumIterableIterator(TEnum val)
  128. : m_val(static_cast<Type>(val))
  129. {
  130. }
  131. TEnum operator*() const
  132. {
  133. return static_cast<TEnum>(m_val);
  134. }
  135. void operator++()
  136. {
  137. ++m_val;
  138. }
  139. bool operator!=(EnumIterableIterator b) const
  140. {
  141. return m_val != b.m_val;
  142. }
  143. private:
  144. Type m_val;
  145. };
  146. /// Allow an enum to be used in a for range loop.
  147. /// @code
  148. /// for(SomeEnum type : EnumIterable<SomeEnum>())
  149. /// {
  150. /// ...
  151. /// }
  152. /// @endcode
  153. template<typename TEnum>
  154. class EnumIterable
  155. {
  156. public:
  157. using Iterator = EnumIterableIterator<TEnum>;
  158. static Iterator begin()
  159. {
  160. return Iterator(TEnum::FIRST);
  161. }
  162. static Iterator end()
  163. {
  164. return Iterator(TEnum::COUNT);
  165. }
  166. };
  167. /// @}
  168. } // end namespace anki