/Src/Dependencies/Boost/libs/phoenix/example/define_expression.cpp

http://hadesmem.googlecode.com/ · C++ · 60 lines · 35 code · 15 blank · 10 comment · 0 complexity · 94027a78f4d83c34e026c7edf8451e15 MD5 · raw file

  1. /*==============================================================================
  2. Copyright (c) 2005-2010 Joel de Guzman
  3. Copyright (c) 2011 Thomas Heller
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/phoenix/core.hpp>
  8. namespace phoenix = boost::phoenix;
  9. namespace proto = boost::proto;
  10. // define the expression
  11. namespace expression
  12. {
  13. template <typename Lhs, typename Rhs>
  14. struct plus
  15. : phoenix::expr<proto::tag::plus, Lhs, Rhs>
  16. {};
  17. }
  18. // extend the grammar, to recognice the expression
  19. namespace boost { namespace phoenix {
  20. template <>
  21. struct meta_grammar::case_<proto::tag::plus>
  22. : enable_rule<
  23. ::expression::plus<
  24. meta_grammar
  25. , meta_grammar
  26. >
  27. >
  28. {};
  29. }}
  30. // build a generator
  31. template <typename Lhs, typename Rhs>
  32. typename expression::plus<Lhs, Rhs>::type
  33. plus(Lhs const & lhs, Rhs const & rhs)
  34. {
  35. return expression::plus<Lhs, Rhs>::make(lhs, rhs);
  36. }
  37. #include <boost/proto/proto.hpp>
  38. #include <iostream>
  39. int main()
  40. {
  41. plus(6, 5);
  42. proto::display_expr(plus(6, 5));
  43. std::cout << plus(5, 6)() << "\n";
  44. }