scoped_enum_emulation.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // scoped_enum_emulation.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. /*
  8. [section:scoped_enums Scoped Enums]
  9. Generates C++0x scoped enums if the feature is present, otherwise emulates C++0x
  10. scoped enums with C++03 namespaces and enums. The Boost.Config BOOST_NO_CXX11_SCOPED_ENUMS
  11. macro is used to detect feature support.
  12. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf for a
  13. description of the scoped enum feature. Note that the committee changed the name
  14. from strongly typed enum to scoped enum.
  15. Some of the enumerations defined in the standard library are scoped enums.
  16. enum class future_errc
  17. {
  18. broken_promise,
  19. future_already_retrieved,
  20. promise_already_satisfied,
  21. no_state
  22. };
  23. On compilers that don't support them, the library provides two emulations:
  24. [heading Strict]
  25. * Able to specify the underlying type.
  26. * explicit conversion to/from underlying type.
  27. * The wrapper is not a C++03 enum type.
  28. The user can declare declare these types as
  29. BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
  30. {
  31. broken_promise,
  32. future_already_retrieved,
  33. promise_already_satisfied,
  34. no_state
  35. }
  36. BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
  37. These macros allows to use 'future_errc' in almost all the cases as an scoped enum.
  38. future_errc err = future_errc::no_state;
  39. There are however some limitations:
  40. * The type is not a C++ enum, so 'is_enum<future_errc>' will be false_type.
  41. * The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some macros.
  42. Instead of
  43. switch (ev)
  44. {
  45. case future_errc::broken_promise:
  46. // ...
  47. use
  48. switch (boost::native_value(ev))
  49. {
  50. case future_errc::broken_promise:
  51. And instead of
  52. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  53. template <>
  54. struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc> : public true_type { };
  55. #endif
  56. use
  57. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  58. template <>
  59. struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc::enum_type > : public true_type { };
  60. #endif
  61. Sample usage:
  62. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(algae, char) { green, red, cyan }; BOOST_SCOPED_ENUM_DECLARE_END(algae)
  63. ...
  64. algae sample( algae::red );
  65. void foo( algae color );
  66. ...
  67. sample = algae::green;
  68. foo( algae::cyan );
  69. Light
  70. Caution: only the syntax is emulated; the semantics are not emulated and
  71. the syntax emulation doesn't include being able to specify the underlying
  72. representation type.
  73. The literal scoped emulation is via struct rather than namespace to allow use within classes.
  74. Thanks to Andrey Semashev for pointing that out.
  75. However the type is an real C++03 enum and so convertible implicitly to an int.
  76. Sample usage:
  77. BOOST_SCOPED_ENUM_START(algae) { green, red, cyan }; BOOST_SCOPED_ENUM_END
  78. ...
  79. BOOST_SCOPED_ENUM(algae) sample( algae::red );
  80. void foo( BOOST_SCOPED_ENUM(algae) color );
  81. ...
  82. sample = algae::green;
  83. foo( algae::cyan );
  84. Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
  85. Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Vicente
  86. Botet, and Daniel James.
  87. [endsect]
  88. */
  89. #ifndef BOOST_SCOPED_ENUM_EMULATION_HPP
  90. #define BOOST_SCOPED_ENUM_EMULATION_HPP
  91. #include <boost/config.hpp>
  92. #include <boost/detail/workaround.hpp>
  93. namespace boost
  94. {
  95. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  96. /**
  97. * Meta-function to get the underlying type of a scoped enum.
  98. *
  99. * Requires EnumType must be an enum type or the emulation of a scoped enum
  100. */
  101. template <typename EnumType>
  102. struct underlying_type
  103. {
  104. /**
  105. * The member typedef type names the underlying type of EnumType. It is EnumType::underlying_type when the EnumType is an emulated scoped enum,
  106. * std::underlying_type<EnumType>::type when the standard library std::underlying_type is provided.
  107. *
  108. * The user will need to specialize it when the compiler supports scoped enums but don't provides std::underlying_type.
  109. */
  110. typedef typename EnumType::underlying_type type;
  111. };
  112. /**
  113. * Meta-function to get the native enum type associated to an enum class or its emulation.
  114. */
  115. template <typename EnumType>
  116. struct native_type
  117. {
  118. /**
  119. * The member typedef type names the native enum type associated to the scoped enum,
  120. * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
  121. */
  122. typedef typename EnumType::enum_type type;
  123. };
  124. /**
  125. * Casts a scoped enum to its underlying type.
  126. *
  127. * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
  128. * @param v A scoped enum.
  129. * @returns The underlying type.
  130. * @throws No-throws.
  131. */
  132. template <typename UnderlyingType, typename EnumType>
  133. UnderlyingType underlying_cast(EnumType v)
  134. {
  135. return v.get_underlying_value_();
  136. }
  137. /**
  138. * Casts a scoped enum to its native enum type.
  139. *
  140. * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
  141. *
  142. * EnumType the scoped enum type
  143. *
  144. * @param v A scoped enum.
  145. * @returns The native enum value.
  146. * @throws No-throws.
  147. */
  148. template <typename EnumType>
  149. inline
  150. typename EnumType::enum_type native_value(EnumType e)
  151. {
  152. return e.native_value_();
  153. }
  154. #else // BOOST_NO_CXX11_SCOPED_ENUMS
  155. template <typename EnumType>
  156. struct underlying_type
  157. {
  158. //typedef typename std::underlying_type<EnumType>::type type;
  159. };
  160. template <typename EnumType>
  161. struct native_type
  162. {
  163. typedef EnumType type;
  164. };
  165. template <typename UnderlyingType, typename EnumType>
  166. UnderlyingType underlying_cast(EnumType v)
  167. {
  168. return static_cast<UnderlyingType>(v);
  169. }
  170. template <typename EnumType>
  171. inline
  172. EnumType native_value(EnumType e)
  173. {
  174. return e;
  175. }
  176. #endif
  177. }
  178. #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  179. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  180. #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
  181. explicit operator underlying_type() const { return get_underlying_value_(); }
  182. #else
  183. #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
  184. #endif
  185. /**
  186. * Start a declaration of a scoped enum.
  187. *
  188. * @param EnumType The new scoped enum.
  189. * @param UnderlyingType The underlying type.
  190. */
  191. #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
  192. struct EnumType { \
  193. typedef UnderlyingType underlying_type; \
  194. EnumType() BOOST_NOEXCEPT {} \
  195. explicit EnumType(underlying_type v) : v_(v) {} \
  196. underlying_type get_underlying_value_() const { return v_; } \
  197. BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
  198. private: \
  199. underlying_type v_; \
  200. typedef EnumType self_type; \
  201. public: \
  202. enum enum_type
  203. #define BOOST_SCOPED_ENUM_DECLARE_END2() \
  204. enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
  205. operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); } \
  206. friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
  207. friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
  208. friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
  209. friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
  210. friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
  211. friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
  212. friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
  213. friend bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
  214. friend bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
  215. friend bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
  216. friend bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
  217. friend bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
  218. friend bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
  219. friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
  220. friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
  221. friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
  222. friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
  223. friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
  224. };
  225. #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
  226. ; \
  227. EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
  228. BOOST_SCOPED_ENUM_DECLARE_END2()
  229. /**
  230. * Starts a declaration of a scoped enum with the default int underlying type.
  231. *
  232. * @param EnumType The new scoped enum.
  233. */
  234. #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
  235. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
  236. /**
  237. * Name of the native enum type.
  238. *
  239. * @param NT The new scoped enum.
  240. */
  241. #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
  242. /**
  243. * Forward declares an scoped enum.
  244. *
  245. * @param NT The scoped enum.
  246. */
  247. #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
  248. #else // BOOST_NO_CXX11_SCOPED_ENUMS
  249. #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType:UnderlyingType
  250. #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
  251. #define BOOST_SCOPED_ENUM_DECLARE_END2()
  252. #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
  253. #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
  254. #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
  255. #endif // BOOST_NO_CXX11_SCOPED_ENUMS
  256. #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
  257. #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
  258. #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
  259. //#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
  260. //
  261. //# define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_type
  262. //# define BOOST_SCOPED_ENUM_END };
  263. //# define BOOST_SCOPED_ENUM(name) name::enum_type
  264. //
  265. //#else
  266. //
  267. //# define BOOST_SCOPED_ENUM_START(name) enum class name
  268. //# define BOOST_SCOPED_ENUM_END
  269. //# define BOOST_SCOPED_ENUM(name) name
  270. //
  271. //#endif
  272. #endif // BOOST_SCOPED_ENUM_EMULATION_HPP