variant_fwd.hpp 12 KB

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