mixed.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //[ Mixed
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // Copyright 2008 Eric Niebler. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // This is an example of using BOOST_PROTO_DEFINE_OPERATORS to Protofy
  8. // expressions using std::vector<> and std::list, non-proto types. It is a port
  9. // of the Mixed example from PETE.
  10. // (http://www.codesourcery.com/pooma/download.html).
  11. #include <list>
  12. #include <cmath>
  13. #include <vector>
  14. #include <complex>
  15. #include <iostream>
  16. #include <stdexcept>
  17. #include <boost/proto/core.hpp>
  18. #include <boost/proto/debug.hpp>
  19. #include <boost/proto/context.hpp>
  20. #include <boost/proto/transform.hpp>
  21. #include <boost/utility/enable_if.hpp>
  22. #include <boost/typeof/std/list.hpp>
  23. #include <boost/typeof/std/vector.hpp>
  24. #include <boost/typeof/std/complex.hpp>
  25. #include <boost/type_traits/remove_reference.hpp>
  26. namespace proto = boost::proto;
  27. namespace mpl = boost::mpl;
  28. using proto::_;
  29. template<typename Expr>
  30. struct MixedExpr;
  31. template<typename Iter>
  32. struct iterator_wrapper
  33. {
  34. typedef Iter iterator;
  35. explicit iterator_wrapper(Iter iter)
  36. : it(iter)
  37. {}
  38. mutable Iter it;
  39. };
  40. struct begin : proto::callable
  41. {
  42. template<class Sig>
  43. struct result;
  44. template<class This, class Cont>
  45. struct result<This(Cont)>
  46. : proto::result_of::as_expr<
  47. iterator_wrapper<typename boost::remove_reference<Cont>::type::const_iterator>
  48. >
  49. {};
  50. template<typename Cont>
  51. typename result<begin(Cont const &)>::type
  52. operator ()(Cont const &cont) const
  53. {
  54. iterator_wrapper<typename Cont::const_iterator> it(cont.begin());
  55. return proto::as_expr(it);
  56. }
  57. };
  58. // Here is a grammar that replaces vector and list terminals with their
  59. // begin iterators
  60. struct Begin
  61. : proto::or_<
  62. proto::when< proto::terminal< std::vector<_, _> >, begin(proto::_value) >
  63. , proto::when< proto::terminal< std::list<_, _> >, begin(proto::_value) >
  64. , proto::when< proto::terminal<_> >
  65. , proto::when< proto::nary_expr<_, proto::vararg<Begin> > >
  66. >
  67. {};
  68. // Here is an evaluation context that dereferences iterator
  69. // terminals.
  70. struct DereferenceCtx
  71. {
  72. // Unless this is an iterator terminal, use the
  73. // default evaluation context
  74. template<typename Expr, typename EnableIf = void>
  75. struct eval
  76. : proto::default_eval<Expr, DereferenceCtx const>
  77. {};
  78. // Dereference iterator terminals.
  79. template<typename Expr>
  80. struct eval<
  81. Expr
  82. , typename boost::enable_if<
  83. proto::matches<Expr, proto::terminal<iterator_wrapper<_> > >
  84. >::type
  85. >
  86. {
  87. typedef typename proto::result_of::value<Expr>::type IteratorWrapper;
  88. typedef typename IteratorWrapper::iterator iterator;
  89. typedef typename std::iterator_traits<iterator>::reference result_type;
  90. result_type operator ()(Expr &expr, DereferenceCtx const &) const
  91. {
  92. return *proto::value(expr).it;
  93. }
  94. };
  95. };
  96. // Here is an evaluation context that increments iterator
  97. // terminals.
  98. struct IncrementCtx
  99. {
  100. // Unless this is an iterator terminal, use the
  101. // default evaluation context
  102. template<typename Expr, typename EnableIf = void>
  103. struct eval
  104. : proto::null_eval<Expr, IncrementCtx const>
  105. {};
  106. // advance iterator terminals.
  107. template<typename Expr>
  108. struct eval<
  109. Expr
  110. , typename boost::enable_if<
  111. proto::matches<Expr, proto::terminal<iterator_wrapper<_> > >
  112. >::type
  113. >
  114. {
  115. typedef void result_type;
  116. result_type operator ()(Expr &expr, IncrementCtx const &) const
  117. {
  118. ++proto::value(expr).it;
  119. }
  120. };
  121. };
  122. // A grammar which matches all the assignment operators,
  123. // so we can easily disable them.
  124. struct AssignOps
  125. : proto::switch_<struct AssignOpsCases>
  126. {};
  127. // Here are the cases used by the switch_ above.
  128. struct AssignOpsCases
  129. {
  130. template<typename Tag, int D = 0> struct case_ : proto::not_<_> {};
  131. template<int D> struct case_< proto::tag::plus_assign, D > : _ {};
  132. template<int D> struct case_< proto::tag::minus_assign, D > : _ {};
  133. template<int D> struct case_< proto::tag::multiplies_assign, D > : _ {};
  134. template<int D> struct case_< proto::tag::divides_assign, D > : _ {};
  135. template<int D> struct case_< proto::tag::modulus_assign, D > : _ {};
  136. template<int D> struct case_< proto::tag::shift_left_assign, D > : _ {};
  137. template<int D> struct case_< proto::tag::shift_right_assign, D > : _ {};
  138. template<int D> struct case_< proto::tag::bitwise_and_assign, D > : _ {};
  139. template<int D> struct case_< proto::tag::bitwise_or_assign, D > : _ {};
  140. template<int D> struct case_< proto::tag::bitwise_xor_assign, D > : _ {};
  141. };
  142. // An expression conforms to the MixedGrammar if it is a terminal or some
  143. // op that is not an assignment op. (Assignment will be handled specially.)
  144. struct MixedGrammar
  145. : proto::or_<
  146. proto::terminal<_>
  147. , proto::and_<
  148. proto::nary_expr<_, proto::vararg<MixedGrammar> >
  149. , proto::not_<AssignOps>
  150. >
  151. >
  152. {};
  153. // Expressions in the MixedDomain will be wrapped in MixedExpr<>
  154. // and must conform to the MixedGrammar
  155. struct MixedDomain
  156. : proto::domain<proto::generator<MixedExpr>, MixedGrammar>
  157. {};
  158. // Here is MixedExpr, a wrapper for expression types in the MixedDomain.
  159. template<typename Expr>
  160. struct MixedExpr
  161. : proto::extends<Expr, MixedExpr<Expr>, MixedDomain>
  162. {
  163. explicit MixedExpr(Expr const &expr)
  164. : MixedExpr::proto_extends(expr)
  165. {}
  166. private:
  167. // hide this:
  168. using proto::extends<Expr, MixedExpr<Expr>, MixedDomain>::operator [];
  169. };
  170. // Define a trait type for detecting vector and list terminals, to
  171. // be used by the BOOST_PROTO_DEFINE_OPERATORS macro below.
  172. template<typename T>
  173. struct IsMixed
  174. : mpl::false_
  175. {};
  176. template<typename T, typename A>
  177. struct IsMixed<std::list<T, A> >
  178. : mpl::true_
  179. {};
  180. template<typename T, typename A>
  181. struct IsMixed<std::vector<T, A> >
  182. : mpl::true_
  183. {};
  184. namespace MixedOps
  185. {
  186. // This defines all the overloads to make expressions involving
  187. // std::vector to build expression templates.
  188. BOOST_PROTO_DEFINE_OPERATORS(IsMixed, MixedDomain)
  189. struct assign_op
  190. {
  191. template<typename T, typename U>
  192. void operator ()(T &t, U const &u) const
  193. {
  194. t = u;
  195. }
  196. };
  197. struct plus_assign_op
  198. {
  199. template<typename T, typename U>
  200. void operator ()(T &t, U const &u) const
  201. {
  202. t += u;
  203. }
  204. };
  205. struct minus_assign_op
  206. {
  207. template<typename T, typename U>
  208. void operator ()(T &t, U const &u) const
  209. {
  210. t -= u;
  211. }
  212. };
  213. struct sin_
  214. {
  215. template<typename Sig>
  216. struct result;
  217. template<typename This, typename Arg>
  218. struct result<This(Arg)>
  219. : boost::remove_const<typename boost::remove_reference<Arg>::type>
  220. {};
  221. template<typename Arg>
  222. Arg operator ()(Arg const &a) const
  223. {
  224. return std::sin(a);
  225. }
  226. };
  227. template<typename A>
  228. typename proto::result_of::make_expr<
  229. proto::tag::function
  230. , MixedDomain
  231. , sin_ const
  232. , A const &
  233. >::type sin(A const &a)
  234. {
  235. return proto::make_expr<proto::tag::function, MixedDomain>(sin_(), boost::ref(a));
  236. }
  237. template<typename FwdIter, typename Expr, typename Op>
  238. void evaluate(FwdIter begin, FwdIter end, Expr const &expr, Op op)
  239. {
  240. IncrementCtx const inc = {};
  241. DereferenceCtx const deref = {};
  242. typename boost::result_of<Begin(Expr const &)>::type expr2 = Begin()(expr);
  243. for(; begin != end; ++begin)
  244. {
  245. op(*begin, proto::eval(expr2, deref));
  246. proto::eval(expr2, inc);
  247. }
  248. }
  249. // Add-assign to a vector from some expression.
  250. template<typename T, typename A, typename Expr>
  251. std::vector<T, A> &assign(std::vector<T, A> &arr, Expr const &expr)
  252. {
  253. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), assign_op());
  254. return arr;
  255. }
  256. // Add-assign to a list from some expression.
  257. template<typename T, typename A, typename Expr>
  258. std::list<T, A> &assign(std::list<T, A> &arr, Expr const &expr)
  259. {
  260. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), assign_op());
  261. return arr;
  262. }
  263. // Add-assign to a vector from some expression.
  264. template<typename T, typename A, typename Expr>
  265. std::vector<T, A> &operator +=(std::vector<T, A> &arr, Expr const &expr)
  266. {
  267. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), plus_assign_op());
  268. return arr;
  269. }
  270. // Add-assign to a list from some expression.
  271. template<typename T, typename A, typename Expr>
  272. std::list<T, A> &operator +=(std::list<T, A> &arr, Expr const &expr)
  273. {
  274. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), plus_assign_op());
  275. return arr;
  276. }
  277. // Minus-assign to a vector from some expression.
  278. template<typename T, typename A, typename Expr>
  279. std::vector<T, A> &operator -=(std::vector<T, A> &arr, Expr const &expr)
  280. {
  281. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), minus_assign_op());
  282. return arr;
  283. }
  284. // Minus-assign to a list from some expression.
  285. template<typename T, typename A, typename Expr>
  286. std::list<T, A> &operator -=(std::list<T, A> &arr, Expr const &expr)
  287. {
  288. evaluate(arr.begin(), arr.end(), proto::as_expr<MixedDomain>(expr), minus_assign_op());
  289. return arr;
  290. }
  291. }
  292. int main()
  293. {
  294. using namespace MixedOps;
  295. int n = 10;
  296. std::vector<int> a,b,c,d;
  297. std::list<double> e;
  298. std::list<std::complex<double> > f;
  299. int i;
  300. for(i = 0;i < n; ++i)
  301. {
  302. a.push_back(i);
  303. b.push_back(2*i);
  304. c.push_back(3*i);
  305. d.push_back(i);
  306. e.push_back(0.0);
  307. f.push_back(std::complex<double>(1.0, 1.0));
  308. }
  309. MixedOps::assign(b, 2);
  310. MixedOps::assign(d, a + b * c);
  311. a += if_else(d < 30, b, c);
  312. MixedOps::assign(e, c);
  313. e += e - 4 / (c + 1);
  314. f -= sin(0.1 * e * std::complex<double>(0.2, 1.2));
  315. std::list<double>::const_iterator ei = e.begin();
  316. std::list<std::complex<double> >::const_iterator fi = f.begin();
  317. for (i = 0; i < n; ++i)
  318. {
  319. std::cout
  320. << "a(" << i << ") = " << a[i]
  321. << " b(" << i << ") = " << b[i]
  322. << " c(" << i << ") = " << c[i]
  323. << " d(" << i << ") = " << d[i]
  324. << " e(" << i << ") = " << *ei++
  325. << " f(" << i << ") = " << *fi++
  326. << std::endl;
  327. }
  328. }
  329. //]