generate.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file generate.hpp
  3. /// Contains definition of generate\<\> class template, which end users can
  4. /// specialize for generating domain-specific expression wrappers.
  5. //
  6. // Copyright 2008 Eric Niebler. Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007
  10. #define BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007
  11. #include <boost/config.hpp>
  12. #include <boost/version.hpp>
  13. #include <boost/preprocessor/cat.hpp>
  14. #include <boost/preprocessor/iteration/iterate.hpp>
  15. #include <boost/preprocessor/facilities/intercept.hpp>
  16. #include <boost/preprocessor/repetition/enum_params.hpp>
  17. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  18. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  19. #include <boost/mpl/bool.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. #include <boost/utility/result_of.hpp>
  22. #include <boost/proto/proto_fwd.hpp>
  23. #include <boost/proto/args.hpp>
  24. #if defined(_MSC_VER)
  25. # pragma warning(push)
  26. # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined
  27. #endif
  28. namespace boost { namespace proto
  29. {
  30. namespace detail
  31. {
  32. template<typename Expr>
  33. struct by_value_generator_;
  34. template<typename Tag, typename Arg>
  35. struct by_value_generator_<proto::expr<Tag, term<Arg>, 0> >
  36. {
  37. typedef
  38. proto::expr<
  39. Tag
  40. , term<typename detail::term_traits<Arg>::value_type>
  41. , 0
  42. >
  43. type;
  44. BOOST_FORCEINLINE
  45. static type const call(proto::expr<Tag, term<Arg>, 0> const &e)
  46. {
  47. type that = {e.child0};
  48. return that;
  49. }
  50. };
  51. template<typename Tag, typename Arg>
  52. struct by_value_generator_<proto::basic_expr<Tag, term<Arg>, 0> >
  53. {
  54. typedef
  55. proto::basic_expr<
  56. Tag
  57. , term<typename detail::term_traits<Arg>::value_type>
  58. , 0
  59. >
  60. type;
  61. BOOST_FORCEINLINE
  62. static type const call(proto::basic_expr<Tag, term<Arg>, 0> const &e)
  63. {
  64. type that = {e.child0};
  65. return that;
  66. }
  67. };
  68. // Include the other specializations of by_value_generator_
  69. #include <boost/proto/detail/generate_by_value.hpp>
  70. }
  71. /// \brief Annotate a generator to indicate that it would
  72. /// prefer to be passed instances of \c proto::basic_expr\<\> rather
  73. /// than \c proto::expr\<\>. <tt>use_basic_expr\<Generator\></tt> is
  74. /// itself a generator.
  75. ///
  76. template<typename Generator>
  77. struct use_basic_expr
  78. : Generator
  79. {
  80. BOOST_PROTO_USE_BASIC_EXPR()
  81. };
  82. /// \brief A simple generator that passes an expression
  83. /// through unchanged.
  84. ///
  85. /// Generators are intended for use as the first template parameter
  86. /// to the \c domain\<\> class template and control if and how
  87. /// expressions within that domain are to be customized.
  88. /// The \c default_generator makes no modifications to the expressions
  89. /// passed to it.
  90. struct default_generator
  91. {
  92. BOOST_PROTO_CALLABLE()
  93. template<typename Sig>
  94. struct result;
  95. template<typename This, typename Expr>
  96. struct result<This(Expr)>
  97. {
  98. typedef Expr type;
  99. };
  100. /// \param expr A Proto expression
  101. /// \return expr
  102. template<typename Expr>
  103. BOOST_FORCEINLINE
  104. BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(Expr, Expr const &)
  105. operator ()(Expr const &e) const
  106. {
  107. return e;
  108. }
  109. };
  110. /// \brief A simple generator that passes an expression
  111. /// through unchanged and specifies a preference for
  112. /// \c proto::basic_expr\<\> over \c proto::expr\<\>.
  113. ///
  114. /// Generators are intended for use as the first template parameter
  115. /// to the \c domain\<\> class template and control if and how
  116. /// expressions within that domain are to be customized.
  117. /// The \c default_generator makes no modifications to the expressions
  118. /// passed to it.
  119. struct basic_default_generator
  120. : proto::use_basic_expr<default_generator>
  121. {};
  122. /// \brief A generator that wraps expressions passed
  123. /// to it in the specified extension wrapper.
  124. ///
  125. /// Generators are intended for use as the first template parameter
  126. /// to the \c domain\<\> class template and control if and how
  127. /// expressions within that domain are to be customized.
  128. /// \c generator\<\> wraps each expression passed to it in
  129. /// the \c Extends\<\> wrapper.
  130. template<template<typename> class Extends>
  131. struct generator
  132. {
  133. BOOST_PROTO_CALLABLE()
  134. BOOST_PROTO_USE_BASIC_EXPR()
  135. template<typename Sig>
  136. struct result;
  137. template<typename This, typename Expr>
  138. struct result<This(Expr)>
  139. {
  140. typedef Extends<Expr> type;
  141. };
  142. template<typename This, typename Expr>
  143. struct result<This(Expr &)>
  144. {
  145. typedef Extends<Expr> type;
  146. };
  147. template<typename This, typename Expr>
  148. struct result<This(Expr const &)>
  149. {
  150. typedef Extends<Expr> type;
  151. };
  152. /// \param expr A Proto expression
  153. /// \return Extends<Expr>(expr)
  154. template<typename Expr>
  155. BOOST_FORCEINLINE
  156. Extends<Expr> operator ()(Expr const &e) const
  157. {
  158. return Extends<Expr>(e);
  159. }
  160. };
  161. /// \brief A generator that wraps expressions passed
  162. /// to it in the specified extension wrapper and uses
  163. /// aggregate initialization for the wrapper.
  164. ///
  165. /// Generators are intended for use as the first template parameter
  166. /// to the \c domain\<\> class template and control if and how
  167. /// expressions within that domain are to be customized.
  168. /// \c pod_generator\<\> wraps each expression passed to it in
  169. /// the \c Extends\<\> wrapper, and uses aggregate initialzation
  170. /// for the wrapped object.
  171. template<template<typename> class Extends>
  172. struct pod_generator
  173. {
  174. BOOST_PROTO_CALLABLE()
  175. BOOST_PROTO_USE_BASIC_EXPR()
  176. template<typename Sig>
  177. struct result;
  178. template<typename This, typename Expr>
  179. struct result<This(Expr)>
  180. {
  181. typedef Extends<Expr> type;
  182. };
  183. template<typename This, typename Expr>
  184. struct result<This(Expr &)>
  185. {
  186. typedef Extends<Expr> type;
  187. };
  188. template<typename This, typename Expr>
  189. struct result<This(Expr const &)>
  190. {
  191. typedef Extends<Expr> type;
  192. };
  193. /// \param expr The expression to wrap
  194. /// \return <tt>Extends\<Expr\> that = {expr}; return that;</tt>
  195. template<typename Expr>
  196. BOOST_FORCEINLINE
  197. Extends<Expr> operator ()(Expr const &e) const
  198. {
  199. Extends<Expr> that = {e};
  200. return that;
  201. }
  202. // Work-around for:
  203. // https://connect.microsoft.com/VisualStudio/feedback/details/765449/codegen-stack-corruption-using-runtime-checks-when-aggregate-initializing-struct
  204. #if BOOST_WORKAROUND(BOOST_MSVC, < 1800)
  205. template<typename Class, typename Member>
  206. BOOST_FORCEINLINE
  207. Extends<expr<tag::terminal, proto::term<Member Class::*> > > operator ()(expr<tag::terminal, proto::term<Member Class::*> > const &e) const
  208. {
  209. Extends<expr<tag::terminal, proto::term<Member Class::*> > > that;
  210. proto::value(that.proto_expr_) = proto::value(e);
  211. return that;
  212. }
  213. template<typename Class, typename Member>
  214. BOOST_FORCEINLINE
  215. Extends<basic_expr<tag::terminal, proto::term<Member Class::*> > > operator ()(basic_expr<tag::terminal, proto::term<Member Class::*> > const &e) const
  216. {
  217. Extends<basic_expr<tag::terminal, proto::term<Member Class::*> > > that;
  218. proto::value(that.proto_expr_) = proto::value(e);
  219. return that;
  220. }
  221. #endif
  222. };
  223. /// \brief A generator that replaces child nodes held by
  224. /// reference with ones held by value. Use with
  225. /// \c compose_generators to forward that result to another
  226. /// generator.
  227. ///
  228. /// Generators are intended for use as the first template parameter
  229. /// to the \c domain\<\> class template and control if and how
  230. /// expressions within that domain are to be customized.
  231. /// \c by_value_generator ensures all child nodes are
  232. /// held by value. This generator is typically composed with a
  233. /// second generator for further processing, as
  234. /// <tt>compose_generators\<by_value_generator, MyGenerator\></tt>.
  235. struct by_value_generator
  236. {
  237. BOOST_PROTO_CALLABLE()
  238. template<typename Sig>
  239. struct result;
  240. template<typename This, typename Expr>
  241. struct result<This(Expr)>
  242. {
  243. typedef
  244. typename detail::by_value_generator_<Expr>::type
  245. type;
  246. };
  247. template<typename This, typename Expr>
  248. struct result<This(Expr &)>
  249. {
  250. typedef
  251. typename detail::by_value_generator_<Expr>::type
  252. type;
  253. };
  254. template<typename This, typename Expr>
  255. struct result<This(Expr const &)>
  256. {
  257. typedef
  258. typename detail::by_value_generator_<Expr>::type
  259. type;
  260. };
  261. /// \param expr The expression to modify.
  262. /// \return <tt>deep_copy(expr)</tt>
  263. template<typename Expr>
  264. BOOST_FORCEINLINE
  265. typename result<by_value_generator(Expr)>::type operator ()(Expr const &e) const
  266. {
  267. return detail::by_value_generator_<Expr>::call(e);
  268. }
  269. };
  270. /// \brief A composite generator that first applies one
  271. /// transform to an expression and then forwards the result
  272. /// on to another generator for further transformation.
  273. ///
  274. /// Generators are intended for use as the first template parameter
  275. /// to the \c domain\<\> class template and control if and how
  276. /// expressions within that domain are to be customized.
  277. /// \c compose_generators\<\> is a composite generator that first
  278. /// applies one transform to an expression and then forwards the
  279. /// result on to another generator for further transformation.
  280. template<typename First, typename Second>
  281. struct compose_generators
  282. {
  283. BOOST_PROTO_CALLABLE()
  284. template<typename Sig>
  285. struct result;
  286. template<typename This, typename Expr>
  287. struct result<This(Expr)>
  288. {
  289. typedef
  290. typename Second::template result<
  291. Second(typename First::template result<First(Expr)>::type)
  292. >::type
  293. type;
  294. };
  295. template<typename This, typename Expr>
  296. struct result<This(Expr &)>
  297. {
  298. typedef
  299. typename Second::template result<
  300. Second(typename First::template result<First(Expr)>::type)
  301. >::type
  302. type;
  303. };
  304. template<typename This, typename Expr>
  305. struct result<This(Expr const &)>
  306. {
  307. typedef
  308. typename Second::template result<
  309. Second(typename First::template result<First(Expr)>::type)
  310. >::type
  311. type;
  312. };
  313. /// \param expr The expression to modify.
  314. /// \return Second()(First()(expr))
  315. template<typename Expr>
  316. BOOST_FORCEINLINE
  317. typename result<compose_generators(Expr)>::type operator ()(Expr const &e) const
  318. {
  319. return Second()(First()(e));
  320. }
  321. };
  322. /// \brief Tests a generator to see whether it would prefer
  323. /// to be passed instances of \c proto::basic_expr\<\> rather than
  324. /// \c proto::expr\<\>.
  325. ///
  326. template<typename Generator, typename Void>
  327. struct wants_basic_expr
  328. : mpl::false_
  329. {};
  330. template<typename Generator>
  331. struct wants_basic_expr<Generator, typename Generator::proto_use_basic_expr_>
  332. : mpl::true_
  333. {};
  334. /// INTERNAL ONLY
  335. template<>
  336. struct is_callable<default_generator>
  337. : mpl::true_
  338. {};
  339. /// INTERNAL ONLY
  340. template<template<typename> class Extends>
  341. struct is_callable<generator<Extends> >
  342. : mpl::true_
  343. {};
  344. /// INTERNAL ONLY
  345. template<template<typename> class Extends>
  346. struct is_callable<pod_generator<Extends> >
  347. : mpl::true_
  348. {};
  349. /// INTERNAL ONLY
  350. template<>
  351. struct is_callable<by_value_generator>
  352. : mpl::true_
  353. {};
  354. /// INTERNAL ONLY
  355. template<typename First, typename Second>
  356. struct is_callable<compose_generators<First, Second> >
  357. : mpl::true_
  358. {};
  359. }}
  360. // Specializations of boost::result_of and boost::tr1_result_of to eliminate
  361. // some unnecessary template instantiations
  362. namespace boost
  363. {
  364. template<typename Expr>
  365. struct result_of<proto::default_domain(Expr)>
  366. {
  367. typedef Expr type;
  368. };
  369. template<typename Expr>
  370. struct result_of<proto::basic_default_domain(Expr)>
  371. {
  372. typedef Expr type;
  373. };
  374. template<typename Expr>
  375. struct result_of<proto::default_generator(Expr)>
  376. {
  377. typedef Expr type;
  378. };
  379. template<typename Expr>
  380. struct result_of<proto::basic_default_generator(Expr)>
  381. {
  382. typedef Expr type;
  383. };
  384. #if BOOST_VERSION >= 104400
  385. template<typename Expr>
  386. struct tr1_result_of<proto::default_domain(Expr)>
  387. {
  388. typedef Expr type;
  389. };
  390. template<typename Expr>
  391. struct tr1_result_of<proto::basic_default_domain(Expr)>
  392. {
  393. typedef Expr type;
  394. };
  395. template<typename Expr>
  396. struct tr1_result_of<proto::default_generator(Expr)>
  397. {
  398. typedef Expr type;
  399. };
  400. template<typename Expr>
  401. struct tr1_result_of<proto::basic_default_generator(Expr)>
  402. {
  403. typedef Expr type;
  404. };
  405. #endif
  406. }
  407. #if defined(_MSC_VER)
  408. # pragma warning(pop)
  409. #endif
  410. #endif // BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007