default.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file default.hpp
  3. /// Definintion of default_context, a default evaluation context for
  4. /// proto::eval() that uses Boost.Typeof to deduce return types
  5. /// of the built-in operators.
  6. //
  7. // Copyright 2008 Eric Niebler. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROTO_CONTEXT_DEFAULT_HPP_EAN_01_08_2007
  11. #define BOOST_PROTO_CONTEXT_DEFAULT_HPP_EAN_01_08_2007
  12. #include <boost/config.hpp>
  13. #include <boost/preprocessor/arithmetic/add.hpp>
  14. #include <boost/preprocessor/arithmetic/sub.hpp>
  15. #include <boost/preprocessor/iteration/iterate.hpp>
  16. #include <boost/preprocessor/repetition/enum.hpp>
  17. #include <boost/preprocessor/repetition/enum_shifted.hpp>
  18. #include <boost/utility/result_of.hpp>
  19. #include <boost/type_traits/is_const.hpp>
  20. #include <boost/type_traits/is_function.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include <boost/type_traits/is_member_pointer.hpp>
  23. #include <boost/type_traits/is_member_object_pointer.hpp>
  24. #include <boost/type_traits/is_member_function_pointer.hpp>
  25. #include <boost/proto/proto_fwd.hpp>
  26. #include <boost/proto/tags.hpp>
  27. #include <boost/proto/eval.hpp>
  28. #include <boost/proto/traits.hpp> // for proto::child_c()
  29. #include <boost/proto/detail/decltype.hpp>
  30. namespace boost { namespace proto
  31. {
  32. /// INTERNAL ONLY
  33. ///
  34. #define UNREF(x) typename boost::remove_reference<x>::type
  35. namespace context
  36. {
  37. template<
  38. typename Expr
  39. , typename Context
  40. , typename Tag // = typename Expr::proto_tag
  41. , long Arity // = Expr::proto_arity_c
  42. >
  43. struct default_eval
  44. {};
  45. template<typename Expr, typename Context>
  46. struct default_eval<Expr, Context, tag::terminal, 0>
  47. {
  48. typedef
  49. typename proto::result_of::value<Expr &>::type
  50. result_type;
  51. result_type operator ()(Expr &expr, Context &) const
  52. {
  53. return proto::value(expr);
  54. }
  55. };
  56. /// INTERNAL ONLY
  57. ///
  58. #define BOOST_PROTO_UNARY_DEFAULT_EVAL(OP, TAG, MAKE) \
  59. template<typename Expr, typename Context> \
  60. struct default_eval<Expr, Context, TAG, 1> \
  61. { \
  62. private: \
  63. typedef typename proto::result_of::child_c<Expr, 0>::type e0; \
  64. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0; \
  65. public: \
  66. BOOST_PROTO_DECLTYPE_(OP proto::detail::MAKE<r0>(), result_type) \
  67. result_type operator ()(Expr &expr, Context &ctx) const \
  68. { \
  69. return OP proto::eval(proto::child_c<0>(expr), ctx); \
  70. } \
  71. }; \
  72. /**/
  73. /// INTERNAL ONLY
  74. ///
  75. #define BOOST_PROTO_BINARY_DEFAULT_EVAL(OP, TAG, LMAKE, RMAKE) \
  76. template<typename Expr, typename Context> \
  77. struct default_eval<Expr, Context, TAG, 2> \
  78. { \
  79. private: \
  80. typedef typename proto::result_of::child_c<Expr, 0>::type e0; \
  81. typedef typename proto::result_of::child_c<Expr, 1>::type e1; \
  82. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0; \
  83. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1; \
  84. public: \
  85. BOOST_PROTO_DECLTYPE_( \
  86. proto::detail::LMAKE<r0>() OP proto::detail::RMAKE<r1>() \
  87. , result_type \
  88. ) \
  89. result_type operator ()(Expr &expr, Context &ctx) const \
  90. { \
  91. return proto::eval( \
  92. proto::child_c<0>(expr), ctx) OP proto::eval(proto::child_c<1>(expr) \
  93. , ctx \
  94. ); \
  95. } \
  96. }; \
  97. /**/
  98. BOOST_PROTO_UNARY_DEFAULT_EVAL(+, proto::tag::unary_plus, make)
  99. BOOST_PROTO_UNARY_DEFAULT_EVAL(-, proto::tag::negate, make)
  100. BOOST_PROTO_UNARY_DEFAULT_EVAL(*, proto::tag::dereference, make)
  101. BOOST_PROTO_UNARY_DEFAULT_EVAL(~, proto::tag::complement, make)
  102. BOOST_PROTO_UNARY_DEFAULT_EVAL(&, proto::tag::address_of, make)
  103. BOOST_PROTO_UNARY_DEFAULT_EVAL(!, proto::tag::logical_not, make)
  104. BOOST_PROTO_UNARY_DEFAULT_EVAL(++, proto::tag::pre_inc, make_mutable)
  105. BOOST_PROTO_UNARY_DEFAULT_EVAL(--, proto::tag::pre_dec, make_mutable)
  106. BOOST_PROTO_BINARY_DEFAULT_EVAL(<<, proto::tag::shift_left, make_mutable, make)
  107. BOOST_PROTO_BINARY_DEFAULT_EVAL(>>, proto::tag::shift_right, make_mutable, make)
  108. BOOST_PROTO_BINARY_DEFAULT_EVAL(*, proto::tag::multiplies, make, make)
  109. BOOST_PROTO_BINARY_DEFAULT_EVAL(/, proto::tag::divides, make, make)
  110. BOOST_PROTO_BINARY_DEFAULT_EVAL(%, proto::tag::modulus, make, make)
  111. BOOST_PROTO_BINARY_DEFAULT_EVAL(+, proto::tag::plus, make, make)
  112. BOOST_PROTO_BINARY_DEFAULT_EVAL(-, proto::tag::minus, make, make)
  113. BOOST_PROTO_BINARY_DEFAULT_EVAL(<, proto::tag::less, make, make)
  114. BOOST_PROTO_BINARY_DEFAULT_EVAL(>, proto::tag::greater, make, make)
  115. BOOST_PROTO_BINARY_DEFAULT_EVAL(<=, proto::tag::less_equal, make, make)
  116. BOOST_PROTO_BINARY_DEFAULT_EVAL(>=, proto::tag::greater_equal, make, make)
  117. BOOST_PROTO_BINARY_DEFAULT_EVAL(==, proto::tag::equal_to, make, make)
  118. BOOST_PROTO_BINARY_DEFAULT_EVAL(!=, proto::tag::not_equal_to, make, make)
  119. BOOST_PROTO_BINARY_DEFAULT_EVAL(||, proto::tag::logical_or, make, make)
  120. BOOST_PROTO_BINARY_DEFAULT_EVAL(&&, proto::tag::logical_and, make, make)
  121. BOOST_PROTO_BINARY_DEFAULT_EVAL(&, proto::tag::bitwise_and, make, make)
  122. BOOST_PROTO_BINARY_DEFAULT_EVAL(|, proto::tag::bitwise_or, make, make)
  123. BOOST_PROTO_BINARY_DEFAULT_EVAL(^, proto::tag::bitwise_xor, make, make)
  124. BOOST_PROTO_BINARY_DEFAULT_EVAL(=, proto::tag::assign, make_mutable, make)
  125. BOOST_PROTO_BINARY_DEFAULT_EVAL(<<=, proto::tag::shift_left_assign, make_mutable, make)
  126. BOOST_PROTO_BINARY_DEFAULT_EVAL(>>=, proto::tag::shift_right_assign, make_mutable, make)
  127. BOOST_PROTO_BINARY_DEFAULT_EVAL(*=, proto::tag::multiplies_assign, make_mutable, make)
  128. BOOST_PROTO_BINARY_DEFAULT_EVAL(/=, proto::tag::divides_assign, make_mutable, make)
  129. BOOST_PROTO_BINARY_DEFAULT_EVAL(%=, proto::tag::modulus_assign, make_mutable, make)
  130. BOOST_PROTO_BINARY_DEFAULT_EVAL(+=, proto::tag::plus_assign, make_mutable, make)
  131. BOOST_PROTO_BINARY_DEFAULT_EVAL(-=, proto::tag::minus_assign, make_mutable, make)
  132. BOOST_PROTO_BINARY_DEFAULT_EVAL(&=, proto::tag::bitwise_and_assign, make_mutable, make)
  133. BOOST_PROTO_BINARY_DEFAULT_EVAL(|=, proto::tag::bitwise_or_assign, make_mutable, make)
  134. BOOST_PROTO_BINARY_DEFAULT_EVAL(^=, proto::tag::bitwise_xor_assign, make_mutable, make)
  135. #undef BOOST_PROTO_UNARY_DEFAULT_EVAL
  136. #undef BOOST_PROTO_BINARY_DEFAULT_EVAL
  137. /// INTERNAL ONLY
  138. template<typename Expr, typename Context>
  139. struct is_member_function_eval
  140. : is_member_function_pointer<
  141. typename detail::uncvref<
  142. typename proto::result_of::eval<
  143. typename remove_reference<
  144. typename proto::result_of::child_c<Expr, 1>::type
  145. >::type
  146. , Context
  147. >::type
  148. >::type
  149. >
  150. {};
  151. /// INTERNAL ONLY
  152. template<typename Expr, typename Context, bool IsMemFunCall>
  153. struct memfun_eval
  154. {
  155. private:
  156. typedef typename result_of::child_c<Expr, 0>::type e0;
  157. typedef typename result_of::child_c<Expr, 1>::type e1;
  158. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  159. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1;
  160. public:
  161. typedef typename detail::mem_ptr_fun<r0, r1>::result_type result_type;
  162. result_type operator ()(Expr &expr, Context &ctx) const
  163. {
  164. return detail::mem_ptr_fun<r0, r1>()(
  165. proto::eval(proto::child_c<0>(expr), ctx)
  166. , proto::eval(proto::child_c<1>(expr), ctx)
  167. );
  168. }
  169. };
  170. /// INTERNAL ONLY
  171. template<typename Expr, typename Context>
  172. struct memfun_eval<Expr, Context, true>
  173. {
  174. private:
  175. typedef typename result_of::child_c<Expr, 0>::type e0;
  176. typedef typename result_of::child_c<Expr, 1>::type e1;
  177. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  178. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1;
  179. public:
  180. typedef detail::memfun<r0, r1> result_type;
  181. result_type const operator ()(Expr &expr, Context &ctx) const
  182. {
  183. return detail::memfun<r0, r1>(
  184. proto::eval(proto::child_c<0>(expr), ctx)
  185. , proto::eval(proto::child_c<1>(expr), ctx)
  186. );
  187. }
  188. };
  189. template<typename Expr, typename Context>
  190. struct default_eval<Expr, Context, tag::mem_ptr, 2>
  191. : memfun_eval<Expr, Context, is_member_function_eval<Expr, Context>::value>
  192. {};
  193. // Handle post-increment specially.
  194. template<typename Expr, typename Context>
  195. struct default_eval<Expr, Context, proto::tag::post_inc, 1>
  196. {
  197. private:
  198. typedef typename proto::result_of::child_c<Expr, 0>::type e0;
  199. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  200. public:
  201. BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() ++, result_type)
  202. result_type operator ()(Expr &expr, Context &ctx) const
  203. {
  204. return proto::eval(proto::child_c<0>(expr), ctx) ++;
  205. }
  206. };
  207. // Handle post-decrement specially.
  208. template<typename Expr, typename Context>
  209. struct default_eval<Expr, Context, proto::tag::post_dec, 1>
  210. {
  211. private:
  212. typedef typename proto::result_of::child_c<Expr, 0>::type e0;
  213. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  214. public:
  215. BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() --, result_type)
  216. result_type operator ()(Expr &expr, Context &ctx) const
  217. {
  218. return proto::eval(proto::child_c<0>(expr), ctx) --;
  219. }
  220. };
  221. // Handle subscript specially.
  222. template<typename Expr, typename Context>
  223. struct default_eval<Expr, Context, proto::tag::subscript, 2>
  224. {
  225. private:
  226. typedef typename proto::result_of::child_c<Expr, 0>::type e0;
  227. typedef typename proto::result_of::child_c<Expr, 1>::type e1;
  228. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  229. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1;
  230. public:
  231. BOOST_PROTO_DECLTYPE_(proto::detail::make_subscriptable<r0>()[proto::detail::make<r1>()], result_type)
  232. result_type operator ()(Expr &expr, Context &ctx) const
  233. {
  234. return proto::eval(proto::child_c<0>(expr), ctx)[proto::eval(proto::child_c<1>(expr), ctx)];
  235. }
  236. };
  237. // Handle if_else_ specially.
  238. template<typename Expr, typename Context>
  239. struct default_eval<Expr, Context, proto::tag::if_else_, 3>
  240. {
  241. private:
  242. typedef typename proto::result_of::child_c<Expr, 0>::type e0;
  243. typedef typename proto::result_of::child_c<Expr, 1>::type e1;
  244. typedef typename proto::result_of::child_c<Expr, 2>::type e2;
  245. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  246. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1;
  247. typedef typename proto::result_of::eval<UNREF(e2), Context>::type r2;
  248. public:
  249. BOOST_PROTO_DECLTYPE_(
  250. proto::detail::make<r0>()
  251. ? proto::detail::make<r1>()
  252. : proto::detail::make<r2>()
  253. , result_type
  254. )
  255. result_type operator ()(Expr &expr, Context &ctx) const
  256. {
  257. return proto::eval(proto::child_c<0>(expr), ctx)
  258. ? proto::eval(proto::child_c<1>(expr), ctx)
  259. : proto::eval(proto::child_c<2>(expr), ctx);
  260. }
  261. };
  262. // Handle comma specially.
  263. template<typename Expr, typename Context>
  264. struct default_eval<Expr, Context, proto::tag::comma, 2>
  265. {
  266. private:
  267. typedef typename proto::result_of::child_c<Expr, 0>::type e0;
  268. typedef typename proto::result_of::child_c<Expr, 1>::type e1;
  269. typedef typename proto::result_of::eval<UNREF(e0), Context>::type r0;
  270. typedef typename proto::result_of::eval<UNREF(e1), Context>::type r1;
  271. public:
  272. typedef typename proto::detail::comma_result<r0, r1>::type result_type;
  273. result_type operator ()(Expr &expr, Context &ctx) const
  274. {
  275. return proto::eval(proto::child_c<0>(expr), ctx), proto::eval(proto::child_c<1>(expr), ctx);
  276. }
  277. };
  278. // Handle function specially
  279. #define BOOST_PROTO_DEFAULT_EVAL_TYPE(Z, N, DATA) \
  280. typename proto::result_of::eval< \
  281. typename remove_reference< \
  282. typename proto::result_of::child_c<DATA, N>::type \
  283. >::type \
  284. , Context \
  285. >::type \
  286. /**/
  287. #define BOOST_PROTO_DEFAULT_EVAL(Z, N, DATA) \
  288. proto::eval(proto::child_c<N>(DATA), context) \
  289. /**/
  290. template<typename Expr, typename Context>
  291. struct default_eval<Expr, Context, proto::tag::function, 1>
  292. {
  293. typedef
  294. typename proto::detail::result_of_fixup<
  295. BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
  296. >::type
  297. function_type;
  298. typedef
  299. typename BOOST_PROTO_RESULT_OF<function_type()>::type
  300. result_type;
  301. result_type operator ()(Expr &expr, Context &context) const
  302. {
  303. return BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)();
  304. }
  305. };
  306. template<typename Expr, typename Context>
  307. struct default_eval<Expr, Context, proto::tag::function, 2>
  308. {
  309. typedef
  310. typename proto::detail::result_of_fixup<
  311. BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
  312. >::type
  313. function_type;
  314. typedef
  315. typename detail::result_of_<
  316. function_type(BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 1, Expr))
  317. >::type
  318. result_type;
  319. result_type operator ()(Expr &expr, Context &context) const
  320. {
  321. return this->invoke(
  322. expr
  323. , context
  324. , is_member_function_pointer<function_type>()
  325. , is_member_object_pointer<function_type>()
  326. );
  327. }
  328. private:
  329. result_type invoke(Expr &expr, Context &context, mpl::false_, mpl::false_) const
  330. {
  331. return BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)(BOOST_PROTO_DEFAULT_EVAL(~, 1, expr));
  332. }
  333. result_type invoke(Expr &expr, Context &context, mpl::true_, mpl::false_) const
  334. {
  335. BOOST_PROTO_USE_GET_POINTER();
  336. typedef typename detail::class_member_traits<function_type>::class_type class_type;
  337. return (
  338. BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, expr))) ->*
  339. BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)
  340. )();
  341. }
  342. result_type invoke(Expr &expr, Context &context, mpl::false_, mpl::true_) const
  343. {
  344. BOOST_PROTO_USE_GET_POINTER();
  345. typedef typename detail::class_member_traits<function_type>::class_type class_type;
  346. return (
  347. BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, expr))) ->*
  348. BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)
  349. );
  350. }
  351. };
  352. // Additional specialization are generated by the preprocessor
  353. #include <boost/proto/context/detail/default_eval.hpp>
  354. #undef BOOST_PROTO_DEFAULT_EVAL_TYPE
  355. #undef BOOST_PROTO_DEFAULT_EVAL
  356. /// default_context
  357. ///
  358. struct default_context
  359. {
  360. /// default_context::eval
  361. ///
  362. template<typename Expr, typename ThisContext = default_context const>
  363. struct eval
  364. : default_eval<Expr, ThisContext>
  365. {};
  366. };
  367. } // namespace context
  368. }} // namespace boost::proto
  369. #undef UNREF
  370. #endif