external_transforms.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //[ CheckedCalc
  2. // Copyright 2011 Eric Niebler. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // This is an example of how to specify a transform externally so
  7. // that a single grammar can be used to drive multiple differnt
  8. // calculations. In particular, it defines a calculator grammar
  9. // that computes the result of an expression with either checked
  10. // or non-checked division.
  11. #include <iostream>
  12. #include <boost/assert.hpp>
  13. #include <boost/mpl/int.hpp>
  14. #include <boost/mpl/next.hpp>
  15. #include <boost/mpl/min_max.hpp>
  16. #include <boost/fusion/container/vector.hpp>
  17. #include <boost/fusion/container/generation/make_vector.hpp>
  18. #include <boost/proto/proto.hpp>
  19. namespace mpl = boost::mpl;
  20. namespace proto = boost::proto;
  21. namespace fusion = boost::fusion;
  22. // The argument placeholder type
  23. template<typename I> struct placeholder : I {};
  24. // Give each rule in the grammar a "name". This is so that we
  25. // can easily dispatch on it later.
  26. struct calc_grammar;
  27. struct divides_rule : proto::divides<calc_grammar, calc_grammar> {};
  28. // Use external transforms in calc_gramar
  29. struct calc_grammar
  30. : proto::or_<
  31. proto::when<
  32. proto::terminal<placeholder<proto::_> >
  33. , proto::functional::at(proto::_state, proto::_value)
  34. >
  35. , proto::when<
  36. proto::terminal<proto::convertible_to<double> >
  37. , proto::_value
  38. >
  39. , proto::when<
  40. proto::plus<calc_grammar, calc_grammar>
  41. , proto::_default<calc_grammar>
  42. >
  43. , proto::when<
  44. proto::minus<calc_grammar, calc_grammar>
  45. , proto::_default<calc_grammar>
  46. >
  47. , proto::when<
  48. proto::multiplies<calc_grammar, calc_grammar>
  49. , proto::_default<calc_grammar>
  50. >
  51. // Note that we don't specify how division nodes are
  52. // handled here. Proto::external_transform is a placeholder
  53. // for an actual transform.
  54. , proto::when<
  55. divides_rule
  56. , proto::external_transform
  57. >
  58. >
  59. {};
  60. template<typename E> struct calc_expr;
  61. struct calc_domain : proto::domain<proto::generator<calc_expr> > {};
  62. template<typename E>
  63. struct calc_expr
  64. : proto::extends<E, calc_expr<E>, calc_domain>
  65. {
  66. calc_expr(E const &e = E()) : calc_expr::proto_extends(e) {}
  67. };
  68. calc_expr<proto::terminal<placeholder<mpl::int_<0> > >::type> _1;
  69. calc_expr<proto::terminal<placeholder<mpl::int_<1> > >::type> _2;
  70. // Use proto::external_transforms to map from named grammar rules to
  71. // transforms.
  72. struct non_checked_division
  73. : proto::external_transforms<
  74. proto::when< divides_rule, proto::_default<calc_grammar> >
  75. >
  76. {};
  77. struct division_by_zero : std::exception {};
  78. struct do_checked_divide
  79. : proto::callable
  80. {
  81. typedef int result_type;
  82. int operator()(int left, int right) const
  83. {
  84. if (right == 0) throw division_by_zero();
  85. return left / right;
  86. }
  87. };
  88. // Use proto::external_transforms again, this time to map the divides_rule
  89. // to a transforms that performs checked division.
  90. struct checked_division
  91. : proto::external_transforms<
  92. proto::when<
  93. divides_rule
  94. , do_checked_divide(calc_grammar(proto::_left), calc_grammar(proto::_right))
  95. >
  96. >
  97. {};
  98. int main()
  99. {
  100. non_checked_division non_checked;
  101. int result2 = calc_grammar()(_1 / _2, fusion::make_vector(6, 2), non_checked);
  102. BOOST_ASSERT(result2 == 3);
  103. try
  104. {
  105. checked_division checked;
  106. // This should throw
  107. int result3 = calc_grammar()(_1 / _2, fusion::make_vector(6, 0), checked);
  108. BOOST_ASSERT(false); // shouldn't get here!
  109. }
  110. catch(division_by_zero)
  111. {
  112. std::cout << "caught division by zero!\n";
  113. }
  114. }
  115. //]