scoped_enum.hpp 7.5 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 BHO_CORE_SCOPED_ENUM_HPP
  8. #define BHO_CORE_SCOPED_ENUM_HPP
  9. #include <asio2/bho/config.hpp>
  10. #ifdef BHO_HAS_PRAGMA_ONCE
  11. #pragma once
  12. #endif
  13. namespace bho
  14. {
  15. #ifdef BHO_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. BHO_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BHO_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. BHO_CONSTEXPR typename EnumType::enum_type native_value(EnumType e) BHO_NOEXCEPT
  56. {
  57. return e.get_native_value_();
  58. }
  59. #else // BHO_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. BHO_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BHO_NOEXCEPT
  68. {
  69. return static_cast<UnderlyingType>(v);
  70. }
  71. template <typename EnumType>
  72. inline
  73. BHO_CONSTEXPR EnumType native_value(EnumType e) BHO_NOEXCEPT
  74. {
  75. return e;
  76. }
  77. #endif // BHO_NO_CXX11_SCOPED_ENUMS
  78. }
  79. #ifdef BHO_NO_CXX11_SCOPED_ENUMS
  80. #ifndef BHO_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  81. #define BHO_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
  82. explicit BHO_CONSTEXPR operator underlying_type() const BHO_NOEXCEPT { return get_underlying_value_(); }
  83. #else
  84. #define BHO_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 BHO_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
  93. struct EnumType { \
  94. typedef void is_bho_scoped_enum_tag; \
  95. typedef UnderlyingType underlying_type; \
  96. EnumType() BHO_NOEXCEPT {} \
  97. explicit BHO_CONSTEXPR EnumType(underlying_type v) BHO_NOEXCEPT : v_(v) {} \
  98. BHO_CONSTEXPR underlying_type get_underlying_value_() const BHO_NOEXCEPT { return v_; } \
  99. BHO_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 BHO_SCOPED_ENUM_DECLARE_END2() \
  106. BHO_CONSTEXPR enum_type get_native_value_() const BHO_NOEXCEPT { return enum_type(v_); } \
  107. friend BHO_CONSTEXPR bool operator ==(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
  108. friend BHO_CONSTEXPR bool operator ==(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
  109. friend BHO_CONSTEXPR bool operator ==(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
  110. friend BHO_CONSTEXPR bool operator !=(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
  111. friend BHO_CONSTEXPR bool operator !=(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
  112. friend BHO_CONSTEXPR bool operator !=(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
  113. friend BHO_CONSTEXPR bool operator <(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
  114. friend BHO_CONSTEXPR bool operator <(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
  115. friend BHO_CONSTEXPR bool operator <(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
  116. friend BHO_CONSTEXPR bool operator <=(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
  117. friend BHO_CONSTEXPR bool operator <=(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
  118. friend BHO_CONSTEXPR bool operator <=(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
  119. friend BHO_CONSTEXPR bool operator >(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
  120. friend BHO_CONSTEXPR bool operator >(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
  121. friend BHO_CONSTEXPR bool operator >(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
  122. friend BHO_CONSTEXPR bool operator >=(self_type lhs, self_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
  123. friend BHO_CONSTEXPR bool operator >=(self_type lhs, enum_type rhs) BHO_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
  124. friend BHO_CONSTEXPR bool operator >=(enum_type lhs, self_type rhs) BHO_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
  125. };
  126. #define BHO_SCOPED_ENUM_DECLARE_END(EnumType) \
  127. ; \
  128. BHO_CONSTEXPR EnumType(enum_type v) BHO_NOEXCEPT : v_(v) {} \
  129. BHO_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 BHO_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
  136. BHO_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 BHO_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
  143. /**
  144. * Forward declares an scoped enum.
  145. *
  146. * @param EnumType The scoped enum.
  147. */
  148. #define BHO_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
  149. #else // BHO_NO_CXX11_SCOPED_ENUMS
  150. #define BHO_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType
  151. #define BHO_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
  152. #define BHO_SCOPED_ENUM_DECLARE_END2()
  153. #define BHO_SCOPED_ENUM_DECLARE_END(EnumType) ;
  154. #define BHO_SCOPED_ENUM_NATIVE(EnumType) EnumType
  155. #define BHO_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
  156. #endif // BHO_NO_CXX11_SCOPED_ENUMS
  157. // Deprecated macros
  158. #define BHO_SCOPED_ENUM_START(name) BHO_SCOPED_ENUM_DECLARE_BEGIN(name)
  159. #define BHO_SCOPED_ENUM_END BHO_SCOPED_ENUM_DECLARE_END2()
  160. #define BHO_SCOPED_ENUM(name) BHO_SCOPED_ENUM_NATIVE(name)
  161. #endif // BHO_CORE_SCOPED_ENUM_HPP