expr.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #if !defined(BOOST_PROTO_DONT_USE_PREPROCESSED_FILES)
  2. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  3. #include <boost/proto/detail/preprocessed/expr_variadic.hpp>
  4. #else
  5. #include <boost/proto/detail/preprocessed/expr.hpp>
  6. #endif
  7. #elif !defined(BOOST_PP_IS_ITERATING)
  8. /// INTERNAL ONLY
  9. ///
  10. #define BOOST_PROTO_CHILD(Z, N, DATA) \
  11. typedef BOOST_PP_CAT(Arg, N) BOOST_PP_CAT(proto_child, N); \
  12. BOOST_PP_CAT(proto_child, N) BOOST_PP_CAT(child, N); \
  13. /**< INTERNAL ONLY */
  14. /// INTERNAL ONLY
  15. ///
  16. #define BOOST_PROTO_VOID(Z, N, DATA) \
  17. typedef void BOOST_PP_CAT(proto_child, N); \
  18. /**< INTERNAL ONLY */
  19. // Generate variadic versions of expr
  20. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  21. #pragma wave option(preserve: 2, line: 0, output: "preprocessed/expr_variadic.hpp")
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////////
  24. /// \file expr_variadic.hpp
  25. /// Contains definition of expr\<\> class template.
  26. //
  27. // Copyright 2008 Eric Niebler. Distributed under the Boost
  28. // Software License, Version 1.0. (See accompanying file
  29. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  30. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  31. #pragma wave option(preserve: 1)
  32. #endif
  33. // The expr<> specializations are actually defined here.
  34. #define BOOST_PROTO_DEFINE_TERMINAL
  35. #define BOOST_PP_ITERATION_PARAMS_1 \
  36. (3, (0, 0, <boost/proto/detail/expr.hpp>))
  37. #include BOOST_PP_ITERATE()
  38. #undef BOOST_PROTO_DEFINE_TERMINAL
  39. #define BOOST_PP_ITERATION_PARAMS_1 \
  40. (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/detail/expr.hpp>))
  41. #include BOOST_PP_ITERATE()
  42. // Generate non-variadic versions of expr
  43. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  44. #define BOOST_NO_CXX11_VARIADIC_TEMPLATES
  45. #pragma wave option(preserve: 2, line: 0, output: "preprocessed/expr.hpp")
  46. ///////////////////////////////////////////////////////////////////////////////
  47. /// \file expr.hpp
  48. /// Contains definition of expr\<\> class template.
  49. //
  50. // Copyright 2008 Eric Niebler. Distributed under the Boost
  51. // Software License, Version 1.0. (See accompanying file
  52. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  53. #pragma wave option(preserve: 1)
  54. // The expr<> specializations are actually defined here.
  55. #define BOOST_PROTO_DEFINE_TERMINAL
  56. #define BOOST_PP_ITERATION_PARAMS_1 (3, (0, 0, <boost/proto/detail/expr.hpp>))
  57. #include BOOST_PP_ITERATE()
  58. #undef BOOST_PROTO_DEFINE_TERMINAL
  59. #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/detail/expr.hpp>))
  60. #include BOOST_PP_ITERATE()
  61. #pragma wave option(output: null)
  62. #undef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  63. #endif
  64. #undef BOOST_PROTO_CHILD
  65. #undef BOOST_PROTO_VOID
  66. #else
  67. #define ARG_COUNT BOOST_PP_MAX(1, BOOST_PP_ITERATION())
  68. /// \brief Representation of a node in an expression tree.
  69. ///
  70. /// \c proto::expr\<\> is a node in an expression template tree. It
  71. /// is a container for its child sub-trees. It also serves as
  72. /// the terminal nodes of the tree.
  73. ///
  74. /// \c Tag is type that represents the operation encoded by
  75. /// this expression. It is typically one of the structs
  76. /// in the \c boost::proto::tag namespace, but it doesn't
  77. /// have to be.
  78. ///
  79. /// \c Args is a type list representing the type of the children
  80. /// of this expression. It is an instantiation of one
  81. /// of \c proto::list1\<\>, \c proto::list2\<\>, etc. The
  82. /// child types must all themselves be either \c expr\<\>
  83. /// or <tt>proto::expr\<\>&</tt>. If \c Args is an
  84. /// instantiation of \c proto::term\<\> then this
  85. /// \c expr\<\> type represents a terminal expression;
  86. /// the parameter to the \c proto::term\<\> template
  87. /// represents the terminal's value type.
  88. ///
  89. /// \c Arity is an integral constant representing the number of child
  90. /// nodes this node contains. If \c Arity is 0, then this
  91. /// node is a terminal.
  92. ///
  93. /// \c proto::expr\<\> is a valid Fusion random-access sequence, where
  94. /// the elements of the sequence are the child expressions.
  95. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  96. template<typename Tag, typename Arg0>
  97. struct expr<Tag, term<Arg0>, 0>
  98. #else
  99. template<typename Tag BOOST_PP_ENUM_TRAILING_PARAMS(ARG_COUNT, typename Arg)>
  100. struct expr<Tag, BOOST_PP_CAT(list, BOOST_PP_ITERATION())<BOOST_PP_ENUM_PARAMS(ARG_COUNT, Arg)>, BOOST_PP_ITERATION() >
  101. #endif
  102. {
  103. typedef Tag proto_tag;
  104. static const long proto_arity_c = BOOST_PP_ITERATION();
  105. typedef mpl::long_<BOOST_PP_ITERATION() > proto_arity;
  106. typedef expr proto_base_expr;
  107. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  108. typedef term<Arg0> proto_args;
  109. #else
  110. typedef BOOST_PP_CAT(list, BOOST_PP_ITERATION())<BOOST_PP_ENUM_PARAMS(ARG_COUNT, Arg)> proto_args;
  111. #endif
  112. typedef basic_expr<Tag, proto_args, BOOST_PP_ITERATION() > proto_grammar;
  113. typedef default_domain proto_domain;
  114. typedef default_generator proto_generator;
  115. typedef proto::tag::proto_expr<Tag, proto_domain> fusion_tag;
  116. typedef expr proto_derived_expr;
  117. typedef void proto_is_expr_; /**< INTERNAL ONLY */
  118. BOOST_PP_REPEAT(ARG_COUNT, BOOST_PROTO_CHILD, ~)
  119. BOOST_PP_REPEAT_FROM_TO(ARG_COUNT, BOOST_PROTO_MAX_ARITY, BOOST_PROTO_VOID, ~)
  120. /// \return *this
  121. ///
  122. BOOST_FORCEINLINE
  123. expr const &proto_base() const
  124. {
  125. return *this;
  126. }
  127. /// \overload
  128. ///
  129. BOOST_FORCEINLINE
  130. expr &proto_base()
  131. {
  132. return *this;
  133. }
  134. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  135. /// \return A new \c expr\<\> object initialized with the specified
  136. /// arguments.
  137. ///
  138. template<typename A0>
  139. BOOST_FORCEINLINE
  140. static expr const make(A0 &a0)
  141. {
  142. return detail::make_terminal(a0, static_cast<expr *>(0), static_cast<proto_args *>(0));
  143. }
  144. /// \overload
  145. ///
  146. template<typename A0>
  147. BOOST_FORCEINLINE
  148. static expr const make(A0 const &a0)
  149. {
  150. return detail::make_terminal(a0, static_cast<expr *>(0), static_cast<proto_args *>(0));
  151. }
  152. #else
  153. /// \return A new \c expr\<\> object initialized with the specified
  154. /// arguments.
  155. ///
  156. template<BOOST_PP_ENUM_PARAMS(ARG_COUNT, typename A)>
  157. BOOST_FORCEINLINE
  158. static expr const make(BOOST_PP_ENUM_BINARY_PARAMS(ARG_COUNT, A, const &a))
  159. {
  160. expr that = {BOOST_PP_ENUM_PARAMS(ARG_COUNT, a)};
  161. return that;
  162. }
  163. #endif
  164. #if 1 == BOOST_PP_ITERATION()
  165. /// If \c Tag is \c boost::proto::tag::address_of and \c proto_child0 is
  166. /// <tt>T&</tt>, then \c address_of_hack_type_ is <tt>T*</tt>.
  167. /// Otherwise, it is some undefined type.
  168. typedef typename detail::address_of_hack<Tag, proto_child0>::type address_of_hack_type_;
  169. /// \return The address of <tt>this->child0</tt> if \c Tag is
  170. /// \c boost::proto::tag::address_of. Otherwise, this function will
  171. /// fail to compile.
  172. ///
  173. /// \attention Proto overloads <tt>operator&</tt>, which means that
  174. /// proto-ified objects cannot have their addresses taken, unless we use
  175. /// the following hack to make \c &x implicitly convertible to \c X*.
  176. BOOST_FORCEINLINE
  177. operator address_of_hack_type_() const
  178. {
  179. return boost::addressof(this->child0);
  180. }
  181. #else
  182. /// INTERNAL ONLY
  183. ///
  184. typedef detail::not_a_valid_type address_of_hack_type_;
  185. #endif
  186. /// Assignment
  187. ///
  188. /// \param a The rhs.
  189. /// \return A new \c expr\<\> node representing an assignment of \c that to \c *this.
  190. BOOST_FORCEINLINE
  191. proto::expr<
  192. proto::tag::assign
  193. , list2<expr &, expr const &>
  194. , 2
  195. > const
  196. operator =(expr const &a)
  197. {
  198. proto::expr<
  199. proto::tag::assign
  200. , list2<expr &, expr const &>
  201. , 2
  202. > that = {*this, a};
  203. return that;
  204. }
  205. /// Assignment
  206. ///
  207. /// \param a The rhs.
  208. /// \return A new \c expr\<\> node representing an assignment of \c a to \c *this.
  209. template<typename A>
  210. BOOST_FORCEINLINE
  211. proto::expr<
  212. proto::tag::assign
  213. , list2<expr const &, typename result_of::as_child<A>::type>
  214. , 2
  215. > const
  216. operator =(A &a) const
  217. {
  218. proto::expr<
  219. proto::tag::assign
  220. , list2<expr const &, typename result_of::as_child<A>::type>
  221. , 2
  222. > that = {*this, proto::as_child(a)};
  223. return that;
  224. }
  225. /// \overload
  226. ///
  227. template<typename A>
  228. BOOST_FORCEINLINE
  229. proto::expr<
  230. proto::tag::assign
  231. , list2<expr const &, typename result_of::as_child<A const>::type>
  232. , 2
  233. > const
  234. operator =(A const &a) const
  235. {
  236. proto::expr<
  237. proto::tag::assign
  238. , list2<expr const &, typename result_of::as_child<A const>::type>
  239. , 2
  240. > that = {*this, proto::as_child(a)};
  241. return that;
  242. }
  243. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  244. /// \overload
  245. ///
  246. template<typename A>
  247. BOOST_FORCEINLINE
  248. proto::expr<
  249. proto::tag::assign
  250. , list2<expr &, typename result_of::as_child<A>::type>
  251. , 2
  252. > const
  253. operator =(A &a)
  254. {
  255. proto::expr<
  256. proto::tag::assign
  257. , list2<expr &, typename result_of::as_child<A>::type>
  258. , 2
  259. > that = {*this, proto::as_child(a)};
  260. return that;
  261. }
  262. /// \overload
  263. ///
  264. template<typename A>
  265. BOOST_FORCEINLINE
  266. proto::expr<
  267. proto::tag::assign
  268. , list2<expr &, typename result_of::as_child<A const>::type>
  269. , 2
  270. > const
  271. operator =(A const &a)
  272. {
  273. proto::expr<
  274. proto::tag::assign
  275. , list2<expr &, typename result_of::as_child<A const>::type>
  276. , 2
  277. > that = {*this, proto::as_child(a)};
  278. return that;
  279. }
  280. #endif
  281. /// Subscript
  282. ///
  283. /// \param a The rhs.
  284. /// \return A new \c expr\<\> node representing \c *this subscripted with \c a.
  285. template<typename A>
  286. BOOST_FORCEINLINE
  287. proto::expr<
  288. proto::tag::subscript
  289. , list2<expr const &, typename result_of::as_child<A>::type>
  290. , 2
  291. > const
  292. operator [](A &a) const
  293. {
  294. proto::expr<
  295. proto::tag::subscript
  296. , list2<expr const &, typename result_of::as_child<A>::type>
  297. , 2
  298. > that = {*this, proto::as_child(a)};
  299. return that;
  300. }
  301. /// \overload
  302. ///
  303. template<typename A>
  304. BOOST_FORCEINLINE
  305. proto::expr<
  306. proto::tag::subscript
  307. , list2<expr const &, typename result_of::as_child<A const>::type>
  308. , 2
  309. > const
  310. operator [](A const &a) const
  311. {
  312. proto::expr<
  313. proto::tag::subscript
  314. , list2<expr const &, typename result_of::as_child<A const>::type>
  315. , 2
  316. > that = {*this, proto::as_child(a)};
  317. return that;
  318. }
  319. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  320. /// \overload
  321. ///
  322. template<typename A>
  323. BOOST_FORCEINLINE
  324. proto::expr<
  325. proto::tag::subscript
  326. , list2<expr &, typename result_of::as_child<A>::type>
  327. , 2
  328. > const
  329. operator [](A &a)
  330. {
  331. proto::expr<
  332. proto::tag::subscript
  333. , list2<expr &, typename result_of::as_child<A>::type>
  334. , 2
  335. > that = {*this, proto::as_child(a)};
  336. return that;
  337. }
  338. /// \overload
  339. ///
  340. template<typename A>
  341. BOOST_FORCEINLINE
  342. proto::expr<
  343. proto::tag::subscript
  344. , list2<expr &, typename result_of::as_child<A const>::type>
  345. , 2
  346. > const
  347. operator [](A const &a)
  348. {
  349. proto::expr<
  350. proto::tag::subscript
  351. , list2<expr &, typename result_of::as_child<A const>::type>
  352. , 2
  353. > that = {*this, proto::as_child(a)};
  354. return that;
  355. }
  356. #endif
  357. /// Encodes the return type of \c expr\<\>::operator(), for use with \c boost::result_of\<\>
  358. ///
  359. template<typename Sig>
  360. struct result
  361. {
  362. typedef typename result_of::funop<Sig, expr, default_domain>::type const type;
  363. };
  364. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  365. /// \overload
  366. ///
  367. template<typename ...A>
  368. BOOST_FORCEINLINE
  369. typename result_of::funop<
  370. expr const(A const &...)
  371. , expr
  372. , default_domain
  373. >::type const
  374. operator ()(A const &... a) const
  375. {
  376. return result_of::funop<
  377. expr const(A const &...)
  378. , expr
  379. , default_domain
  380. >::call(*this, a...);
  381. }
  382. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  383. /// \overload
  384. ///
  385. template<typename ...A>
  386. BOOST_FORCEINLINE
  387. typename result_of::funop<
  388. expr(A const &...)
  389. , expr
  390. , default_domain
  391. >::type const
  392. operator ()(A const &... a)
  393. {
  394. return result_of::funop<
  395. expr(A const &...)
  396. , expr
  397. , default_domain
  398. >::call(*this, a...);
  399. }
  400. #endif
  401. #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
  402. /// Function call
  403. ///
  404. /// \return A new \c expr\<\> node representing the function invocation of \c (*this)().
  405. BOOST_FORCEINLINE
  406. proto::expr<proto::tag::function, list1<expr const &>, 1> const
  407. operator ()() const
  408. {
  409. proto::expr<proto::tag::function, list1<expr const &>, 1> that = {*this};
  410. return that;
  411. }
  412. #ifdef BOOST_PROTO_DEFINE_TERMINAL
  413. /// \overload
  414. ///
  415. BOOST_FORCEINLINE
  416. proto::expr<proto::tag::function, list1<expr &>, 1> const
  417. operator ()()
  418. {
  419. proto::expr<proto::tag::function, list1<expr &>, 1> that = {*this};
  420. return that;
  421. }
  422. #endif
  423. #define BOOST_PP_ITERATION_PARAMS_2 \
  424. (3, (1, BOOST_PP_DEC(BOOST_PROTO_MAX_FUNCTION_CALL_ARITY), <boost/proto/detail/expr_funop.hpp>))
  425. #include BOOST_PP_ITERATE()
  426. #endif
  427. };
  428. #undef ARG_COUNT
  429. #endif