complex_number_adapt.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2001-2010 Joel de Guzman
  3. //
  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. //
  8. // A complex number micro generator - take 3.
  9. //
  10. // Look'ma, still no semantic actions! And no explicit access to member
  11. // functions any more.
  12. //
  13. // [ HK April 6, 2010 ] spirit2
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <boost/config/warning_disable.hpp>
  17. #include <boost/spirit/include/qi.hpp>
  18. #include <boost/spirit/include/karma.hpp>
  19. #include <boost/spirit/include/phoenix_core.hpp>
  20. #include <boost/spirit/include/phoenix_operator.hpp>
  21. #include <boost/fusion/include/std_pair.hpp>
  22. #include <boost/fusion/include/adapt_adt.hpp>
  23. #include <boost/spirit/include/support_adapt_adt_attributes.hpp>
  24. #include <iostream>
  25. #include <string>
  26. #include <complex>
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // The following macro adapts the type std::complex<double> as a fusion
  29. // sequence.
  30. //[tutorial_karma_complex_number_adapt_class
  31. // We can leave off the setters as Karma does not need them.
  32. BOOST_FUSION_ADAPT_ADT(
  33. std::complex<double>,
  34. (bool, bool, obj.imag() != 0, /**/)
  35. (double, double, obj.real(), /**/)
  36. (double, double, obj.imag(), /**/)
  37. )
  38. //]
  39. namespace client
  40. {
  41. ///////////////////////////////////////////////////////////////////////////
  42. // Our complex number parser/compiler (that's just a copy of the complex
  43. // number example from Qi (see examples/qi/complex_number.cpp)
  44. ///////////////////////////////////////////////////////////////////////////
  45. template <typename Iterator>
  46. bool parse_complex(Iterator first, Iterator last, std::complex<double>& c)
  47. {
  48. using boost::spirit::qi::double_;
  49. using boost::spirit::qi::_1;
  50. using boost::spirit::qi::phrase_parse;
  51. using boost::spirit::ascii::space;
  52. using boost::phoenix::ref;
  53. double rN = 0.0;
  54. double iN = 0.0;
  55. bool r = phrase_parse(first, last,
  56. (
  57. '(' >> double_[ref(rN) = _1]
  58. >> -(',' >> double_[ref(iN) = _1]) >> ')'
  59. | double_[ref(rN) = _1]
  60. ),
  61. space);
  62. if (!r || first != last) // fail if we did not get a full match
  63. return false;
  64. c = std::complex<double>(rN, iN);
  65. return r;
  66. }
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Our complex number generator
  69. ///////////////////////////////////////////////////////////////////////////
  70. //[tutorial_karma_complex_number_adapt
  71. template <typename OutputIterator>
  72. bool generate_complex(OutputIterator sink, std::complex<double> const& c)
  73. {
  74. using boost::spirit::karma::double_;
  75. using boost::spirit::karma::bool_;
  76. using boost::spirit::karma::true_;
  77. using boost::spirit::karma::omit;
  78. using boost::spirit::karma::generate;
  79. return generate(sink,
  80. // Begin grammar
  81. (
  82. &true_ << '(' << double_ << ", " << double_ << ')'
  83. | omit[bool_] << double_
  84. ),
  85. // End grammar
  86. c // Data to output
  87. );
  88. }
  89. //]
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. // Main program
  93. ///////////////////////////////////////////////////////////////////////////////
  94. int main()
  95. {
  96. std::cout << "/////////////////////////////////////////////////////////\n\n";
  97. std::cout << "\t\tA complex number micro generator for Spirit...\n\n";
  98. std::cout << "/////////////////////////////////////////////////////////\n\n";
  99. std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
  100. std::cout << "Type [q or Q] to quit\n\n";
  101. std::string str;
  102. while (getline(std::cin, str))
  103. {
  104. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  105. break;
  106. std::complex<double> c;
  107. if (client::parse_complex(str.begin(), str.end(), c))
  108. {
  109. std::cout << "-------------------------\n";
  110. std::string generated;
  111. std::back_insert_iterator<std::string> sink(generated);
  112. if (!client::generate_complex(sink, c))
  113. {
  114. std::cout << "-------------------------\n";
  115. std::cout << "Generating failed\n";
  116. std::cout << "-------------------------\n";
  117. }
  118. else
  119. {
  120. std::cout << "-------------------------\n";
  121. std::cout << "Generated: " << generated << "\n";
  122. std::cout << "-------------------------\n";
  123. }
  124. }
  125. else
  126. {
  127. std::cout << "-------------------------\n";
  128. std::cout << "Parsing failed\n";
  129. std::cout << "-------------------------\n";
  130. }
  131. }
  132. std::cout << "Bye... :-) \n\n";
  133. return 0;
  134. }