scoped_enum.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // scoped_enum.hpp ---------------------------------------------------------//
  2. // Copyright Beman Dawes, 2009
  3. // Copyright (C) 2011-2012 Vicente J. Botet Escriba
  4. // Copyright (C) 2012 Anthony Williams
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. #ifndef BOOST_CORE_SCOPED_ENUM_HPP
  8. #define BOOST_CORE_SCOPED_ENUM_HPP
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. #pragma once
  12. #endif
  13. namespace boost
  14. {
  15. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  16. /**
  17. * Meta-function to get the native enum type associated to an enum class or its emulation.
  18. */
  19. template <typename EnumType>
  20. struct native_type
  21. {
  22. /**
  23. * The member typedef type names the native enum type associated to the scoped enum,
  24. * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
  25. */
  26. typedef typename EnumType::enum_type type;
  27. };
  28. /**
  29. * Casts a scoped enum to its underlying type.
  30. *
  31. * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
  32. * @param v A scoped enum.
  33. * @returns The underlying type.
  34. * @throws No-throws.
  35. */
  36. template <typename UnderlyingType, typename EnumType>
  37. inline
  38. BOOST_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BOOST_NOEXCEPT
  39. {
  40. return v.get_underlying_value_();
  41. }
  42. /**
  43. * Casts a scoped enum to its native enum type.
  44. *
  45. * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
  46. *
  47. * EnumType the scoped enum type
  48. *
  49. * @param v A scoped enum.
  50. * @returns The native enum value.
  51. * @throws No-throws.
  52. */
  53. template <typename EnumType>
  54. inline
  55. BOOST_CONSTEXPR typename EnumType::enum_type native_value(EnumType e) BOOST_NOEXCEPT
  56. {
  57. return e.get_native_value_();
  58. }
  59. #else // BOOST_NO_CXX11_SCOPED_ENUMS
  60. template <typename EnumType>
  61. struct native_type
  62. {
  63. typedef EnumType type;
  64. };
  65. template <typename UnderlyingType, typename EnumType>
  66. inline
  67. BOOST_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BOOST_NOEXCEPT
  68. {
  69. return static_cast<UnderlyingType>(v);
  70. }
  71. template <typename EnumType>
  72. inline
  73. BOOST_CONSTEXPR EnumType native_value(EnumType e) BOOST_NOEXCEPT
  74. {
  75. return e;
  76. }
  77. #endif // BOOST_NO_CXX11_SCOPED_ENUMS
  78. }
  79. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  80. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  81. #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
  82. explicit BOOST_CONSTEXPR operator underlying_type() const BOOST_NOEXCEPT { return get_underlying_value_(); }
  83. #else
  84. #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
  85. #endif
  86. /**
  87. * Start a declaration of a scoped enum.
  88. *
  89. * @param EnumType The new scoped enum.
  90. * @param UnderlyingType The underlying type.
  91. */
  92. #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
  93. struct EnumType { \
  94. typedef void is_boost_scoped_enum_tag; \
  95. typedef UnderlyingType underlying_type; \
  96. EnumType() BOOST_NOEXCEPT {} \
  97. explicit BOOST_CONSTEXPR EnumType(underlying_type v) BOOST_NOEXCEPT : v_(v) {} \
  98. BOOST_CONSTEXPR underlying_type get_underlying_value_() const BOOST_NOEXCEPT { return v_; } \
  99. BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
  100. private: \
  101. underlying_type v_; \
  102. typedef EnumType self_type; \
  103. public: \
  104. enum enum_type
  105. #define BOOST_SCOPED_ENUM_DECLARE_END2() \
  106. BOOST_CONSTEXPR enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
  107. friend BOOST_CONSTEXPR bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
  108. friend BOOST_CONSTEXPR bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
  109. friend BOOST_CONSTEXPR bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
  110. friend BOOST_CONSTEXPR bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
  111. friend BOOST_CONSTEXPR bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
  112. friend BOOST_CONSTEXPR bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
  113. friend BOOST_CONSTEXPR bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
  114. friend BOOST_CONSTEXPR bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
  115. friend BOOST_CONSTEXPR bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
  116. friend BOOST_CONSTEXPR bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
  117. friend BOOST_CONSTEXPR bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
  118. friend BOOST_CONSTEXPR bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
  119. friend BOOST_CONSTEXPR bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
  120. friend BOOST_CONSTEXPR bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
  121. friend BOOST_CONSTEXPR bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
  122. friend BOOST_CONSTEXPR bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
  123. friend BOOST_CONSTEXPR bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
  124. friend BOOST_CONSTEXPR bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
  125. };
  126. #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
  127. ; \
  128. BOOST_CONSTEXPR EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
  129. BOOST_SCOPED_ENUM_DECLARE_END2()
  130. /**
  131. * Starts a declaration of a scoped enum with the default int underlying type.
  132. *
  133. * @param EnumType The new scoped enum.
  134. */
  135. #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
  136. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
  137. /**
  138. * Name of the native enum type.
  139. *
  140. * @param EnumType The new scoped enum.
  141. */
  142. #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
  143. /**
  144. * Forward declares an scoped enum.
  145. *
  146. * @param EnumType The scoped enum.
  147. */
  148. #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
  149. #else // BOOST_NO_CXX11_SCOPED_ENUMS
  150. #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType
  151. #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
  152. #define BOOST_SCOPED_ENUM_DECLARE_END2()
  153. #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
  154. #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
  155. #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
  156. #endif // BOOST_NO_CXX11_SCOPED_ENUMS
  157. // Deprecated macros
  158. #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
  159. #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
  160. #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
  161. #endif // BOOST_CORE_SCOPED_ENUM_HPP