variant_fwd.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //-----------------------------------------------------------------------------
  2. // boost variant/variant_fwd.hpp header file
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. //-----------------------------------------------------------------------------
  5. //
  6. // Copyright (c) 2003 Eric Friedman, Itay Maman
  7. // Copyright (c) 2013 Antony Polukhin
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_VARIANT_VARIANT_FWD_HPP
  13. #define BOOST_VARIANT_VARIANT_FWD_HPP
  14. #include "boost/variant/detail/config.hpp"
  15. #include "boost/blank_fwd.hpp"
  16. #include "boost/mpl/arg.hpp"
  17. #include "boost/mpl/limits/arity.hpp"
  18. #include "boost/mpl/aux_/na.hpp"
  19. #include "boost/preprocessor/cat.hpp"
  20. #include "boost/preprocessor/enum.hpp"
  21. #include "boost/preprocessor/enum_params.hpp"
  22. #include "boost/preprocessor/enum_shifted_params.hpp"
  23. #include "boost/preprocessor/repeat.hpp"
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // macro BOOST_VARIANT_NO_REFERENCE_SUPPORT
  26. //
  27. // Defined if variant does not support references as bounded types.
  28. //
  29. #if defined(BOOST_VARIANT_AUX_BROKEN_CONSTRUCTOR_TEMPLATE_ORDERING) \
  30. && !defined(BOOST_VARIANT_AUX_HAS_CONSTRUCTOR_TEMPLATE_ORDERING_SFINAE_WKND) \
  31. && !defined(BOOST_VARIANT_NO_REFERENCE_SUPPORT)
  32. # define BOOST_VARIANT_NO_REFERENCE_SUPPORT
  33. #endif
  34. ///////////////////////////////////////////////////////////////////////////////
  35. // macro BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT
  36. //
  37. // Defined if variant does not support make_variant_over (see below).
  38. //
  39. #if defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
  40. # define BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT
  41. #endif
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // macro BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT
  44. //
  45. // Defined if make_recursive_variant cannot be supported as documented.
  46. //
  47. // Note: Currently, MPL lambda facility is used as workaround if defined, and
  48. // so only types declared w/ MPL lambda workarounds will work.
  49. //
  50. #include "boost/variant/detail/substitute_fwd.hpp"
  51. #if defined(BOOST_VARIANT_DETAIL_NO_SUBSTITUTE) \
  52. && !defined(BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT)
  53. # define BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT
  54. #endif
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // macro BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  57. //
  58. /*
  59. GCC before 4.0 had no variadic tempaltes;
  60. GCC 4.6 has incomplete implementation of variadic templates.
  61. MSVC2013 has variadic templates, but they have issues.
  62. NOTE: Clang compiler defines __GNUC__
  63. */
  64. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) \
  65. || (!defined(__clang__) && defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) \
  66. || (defined(_MSC_VER) && (_MSC_VER <= 1900)) \
  67. || defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE) \
  68. || defined (BOOST_VARIANT_NO_TYPE_SEQUENCE_SUPPORT)
  69. #ifndef BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  70. # define BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES
  71. #endif
  72. #endif
  73. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  74. #include <boost/preprocessor/seq/size.hpp>
  75. #define BOOST_VARIANT_CLASS_OR_TYPENAME_TO_SEQ_class class)(
  76. #define BOOST_VARIANT_CLASS_OR_TYPENAME_TO_SEQ_typename typename)(
  77. #define BOOST_VARIANT_CLASS_OR_TYPENAME_TO_VARIADIC_class class...
  78. #define BOOST_VARIANT_CLASS_OR_TYPENAME_TO_VARIADIC_typename typename...
  79. #define ARGS_VARIADER_1(x) x ## N...
  80. #define ARGS_VARIADER_2(x) BOOST_VARIANT_CLASS_OR_TYPENAME_TO_VARIADIC_ ## x ## N
  81. #define BOOST_VARIANT_MAKE_VARIADIC(sequence, x) BOOST_VARIANT_MAKE_VARIADIC_I(BOOST_PP_SEQ_SIZE(sequence), x)
  82. #define BOOST_VARIANT_MAKE_VARIADIC_I(argscount, x) BOOST_VARIANT_MAKE_VARIADIC_II(argscount, x)
  83. #define BOOST_VARIANT_MAKE_VARIADIC_II(argscount, orig) ARGS_VARIADER_ ## argscount(orig)
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // BOOST_VARIANT_ENUM_PARAMS and BOOST_VARIANT_ENUM_SHIFTED_PARAMS
  86. //
  87. // Convenience macro for enumeration of variant params.
  88. // When variadic templates are available expands:
  89. // BOOST_VARIANT_ENUM_PARAMS(class Something) => class Something0, class... SomethingN
  90. // BOOST_VARIANT_ENUM_PARAMS(typename Something) => typename Something0, typename... SomethingN
  91. // BOOST_VARIANT_ENUM_PARAMS(Something) => Something0, SomethingN...
  92. // BOOST_VARIANT_ENUM_PARAMS(Something) => Something0, SomethingN...
  93. // BOOST_VARIANT_ENUM_SHIFTED_PARAMS(class Something) => class... SomethingN
  94. // BOOST_VARIANT_ENUM_SHIFTED_PARAMS(typename Something) => typename... SomethingN
  95. // BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Something) => SomethingN...
  96. // BOOST_VARIANT_ENUM_SHIFTED_PARAMS(Something) => SomethingN...
  97. //
  98. // Rationale: Cleaner, simpler code for clients of variant library. Minimal
  99. // code modifications to move from C++03 to C++11.
  100. //
  101. // With BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES defined
  102. // will be used BOOST_VARIANT_ENUM_PARAMS and BOOST_VARIANT_ENUM_SHIFTED_PARAMS from below `#else`
  103. //
  104. #define BOOST_VARIANT_ENUM_PARAMS(x) \
  105. x ## 0, \
  106. BOOST_VARIANT_MAKE_VARIADIC( (BOOST_VARIANT_CLASS_OR_TYPENAME_TO_SEQ_ ## x), x) \
  107. /**/
  108. #define BOOST_VARIANT_ENUM_SHIFTED_PARAMS(x) \
  109. BOOST_VARIANT_MAKE_VARIADIC( (BOOST_VARIANT_CLASS_OR_TYPENAME_TO_SEQ_ ## x), x) \
  110. /**/
  111. #else // defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  112. ///////////////////////////////////////////////////////////////////////////////
  113. // macro BOOST_VARIANT_LIMIT_TYPES
  114. //
  115. // Implementation-defined preprocessor symbol describing the actual
  116. // length of variant's pseudo-variadic template parameter list.
  117. //
  118. #include "boost/mpl/limits/list.hpp"
  119. #define BOOST_VARIANT_LIMIT_TYPES \
  120. BOOST_MPL_LIMIT_LIST_SIZE
  121. ///////////////////////////////////////////////////////////////////////////////
  122. // macro BOOST_VARIANT_RECURSIVE_VARIANT_MAX_ARITY
  123. //
  124. // Exposes maximum allowed arity of class templates with recursive_variant
  125. // arguments. That is,
  126. // make_recursive_variant< ..., T<[1], recursive_variant_, ... [N]> >.
  127. //
  128. #include "boost/mpl/limits/arity.hpp"
  129. #define BOOST_VARIANT_RECURSIVE_VARIANT_MAX_ARITY \
  130. BOOST_MPL_LIMIT_METAFUNCTION_ARITY
  131. ///////////////////////////////////////////////////////////////////////////////
  132. // macro BOOST_VARIANT_ENUM_PARAMS
  133. //
  134. // Convenience macro for enumeration of BOOST_VARIANT_LIMIT_TYPES params.
  135. //
  136. // Rationale: Cleaner, simpler code for clients of variant library.
  137. //
  138. #define BOOST_VARIANT_ENUM_PARAMS( param ) \
  139. BOOST_PP_ENUM_PARAMS(BOOST_VARIANT_LIMIT_TYPES, param)
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // macro BOOST_VARIANT_ENUM_SHIFTED_PARAMS
  142. //
  143. // Convenience macro for enumeration of BOOST_VARIANT_LIMIT_TYPES-1 params.
  144. //
  145. #define BOOST_VARIANT_ENUM_SHIFTED_PARAMS( param ) \
  146. BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_VARIANT_LIMIT_TYPES, param)
  147. #endif // BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES workaround
  148. namespace boost {
  149. namespace detail { namespace variant {
  150. ///////////////////////////////////////////////////////////////////////////////
  151. // (detail) class void_ and class template convert_void
  152. //
  153. // Provides the mechanism by which void(NN) types are converted to
  154. // mpl::void_ (and thus can be passed to mpl::list).
  155. //
  156. // Rationale: This is particularly needed for the using-declarations
  157. // workaround (below), but also to avoid associating mpl namespace with
  158. // variant in argument dependent lookups (which used to happen because of
  159. // defaulting of template parameters to mpl::void_).
  160. //
  161. struct void_;
  162. template <typename T>
  163. struct convert_void
  164. {
  165. typedef T type;
  166. };
  167. template <>
  168. struct convert_void< void_ >
  169. {
  170. typedef mpl::na type;
  171. };
  172. ///////////////////////////////////////////////////////////////////////////////
  173. // (workaround) BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE
  174. //
  175. // Needed to work around compilers that don't support using-declaration
  176. // overloads. (See the variant::initializer workarounds below.)
  177. //
  178. #if defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
  179. // (detail) tags voidNN -- NN defined on [0, BOOST_VARIANT_LIMIT_TYPES)
  180. //
  181. // Defines void types that are each unique and specializations of
  182. // convert_void that yields mpl::na for each voidNN type.
  183. //
  184. #define BOOST_VARIANT_DETAIL_DEFINE_VOID_N(z,N,_) \
  185. struct BOOST_PP_CAT(void,N); \
  186. \
  187. template <> \
  188. struct convert_void< BOOST_PP_CAT(void,N) > \
  189. { \
  190. typedef mpl::na type; \
  191. }; \
  192. /**/
  193. BOOST_PP_REPEAT(
  194. BOOST_VARIANT_LIMIT_TYPES
  195. , BOOST_VARIANT_DETAIL_DEFINE_VOID_N
  196. , _
  197. )
  198. #undef BOOST_VARIANT_DETAIL_DEFINE_VOID_N
  199. #endif // BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE workaround
  200. }} // namespace detail::variant
  201. #if !defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  202. # define BOOST_VARIANT_AUX_DECLARE_PARAMS BOOST_VARIANT_ENUM_PARAMS(typename T)
  203. #else // defined(BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES)
  204. ///////////////////////////////////////////////////////////////////////////////
  205. // (detail) macro BOOST_VARIANT_AUX_DECLARE_PARAM
  206. //
  207. // Template parameter list for variant and recursive_variant declarations.
  208. //
  209. #if !defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
  210. # define BOOST_VARIANT_AUX_DECLARE_PARAMS_IMPL(z, N, T) \
  211. typename BOOST_PP_CAT(T,N) = detail::variant::void_ \
  212. /**/
  213. #else // defined(BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE)
  214. # define BOOST_VARIANT_AUX_DECLARE_PARAMS_IMPL(z, N, T) \
  215. typename BOOST_PP_CAT(T,N) = BOOST_PP_CAT(detail::variant::void,N) \
  216. /**/
  217. #endif // BOOST_NO_USING_DECLARATION_OVERLOADS_FROM_TYPENAME_BASE workaround
  218. #define BOOST_VARIANT_AUX_DECLARE_PARAMS \
  219. BOOST_PP_ENUM( \
  220. BOOST_VARIANT_LIMIT_TYPES \
  221. , BOOST_VARIANT_AUX_DECLARE_PARAMS_IMPL \
  222. , T \
  223. ) \
  224. /**/
  225. #endif // BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES workaround
  226. ///////////////////////////////////////////////////////////////////////////////
  227. // class template variant (concept inspired by Andrei Alexandrescu)
  228. //
  229. // Efficient, type-safe bounded discriminated union.
  230. //
  231. // Preconditions:
  232. // - Each type must be unique.
  233. // - No type may be const-qualified.
  234. //
  235. // Proper declaration form:
  236. // variant<types> (where types is a type-sequence)
  237. // or
  238. // variant<T0,T1,...,Tn> (where T0 is NOT a type-sequence)
  239. //
  240. template < BOOST_VARIANT_AUX_DECLARE_PARAMS > class variant;
  241. ///////////////////////////////////////////////////////////////////////////////
  242. // metafunction make_recursive_variant
  243. //
  244. // Exposes a boost::variant with recursive_variant_ tags (below) substituted
  245. // with the variant itself (wrapped as needed with boost::recursive_wrapper).
  246. //
  247. template < BOOST_VARIANT_AUX_DECLARE_PARAMS > struct make_recursive_variant;
  248. #undef BOOST_VARIANT_AUX_DECLARE_PARAMS_IMPL
  249. #undef BOOST_VARIANT_AUX_DECLARE_PARAMS
  250. ///////////////////////////////////////////////////////////////////////////////
  251. // type recursive_variant_
  252. //
  253. // Tag type indicates where recursive variant substitution should occur.
  254. //
  255. #if !defined(BOOST_VARIANT_NO_FULL_RECURSIVE_VARIANT_SUPPORT)
  256. struct recursive_variant_ {};
  257. #else
  258. typedef mpl::arg<1> recursive_variant_;
  259. #endif
  260. ///////////////////////////////////////////////////////////////////////////////
  261. // metafunction make_variant_over
  262. //
  263. // Result is a variant w/ types of the specified type sequence.
  264. //
  265. template <typename Types> struct make_variant_over;
  266. ///////////////////////////////////////////////////////////////////////////////
  267. // metafunction make_recursive_variant_over
  268. //
  269. // Result is a recursive variant w/ types of the specified type sequence.
  270. //
  271. template <typename Types> struct make_recursive_variant_over;
  272. } // namespace boost
  273. #endif // BOOST_VARIANT_VARIANT_FWD_HPP