num_list3.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*=============================================================================
  2. Copyright (c) 2002-2010 Hartmut Kaiser
  3. Copyright (c) 2002-2010 Joel de Guzman
  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. //
  9. // This sample demonstrates a generator for a comma separated list of numbers.
  10. // No actions. It is based on the example qi/num_lists.cpp for reading in
  11. // some numbers to generate.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/config/warning_disable.hpp>
  15. #include <boost/spirit/include/qi.hpp>
  16. #include <boost/spirit/include/karma.hpp>
  17. #include <iostream>
  18. #include <string>
  19. #include <vector>
  20. namespace client
  21. {
  22. ///////////////////////////////////////////////////////////////////////////
  23. // Our number list parser, please see the example qi/numlist1.cpp for
  24. // more information
  25. ///////////////////////////////////////////////////////////////////////////
  26. template <typename Iterator>
  27. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  28. {
  29. using boost::spirit::qi::double_;
  30. using boost::spirit::qi::phrase_parse;
  31. using boost::spirit::ascii::space;
  32. bool r = phrase_parse(first, last, double_ % ',', space, v);
  33. if (first != last)
  34. return false;
  35. return r;
  36. }
  37. //[tutorial_karma_numlist3_complex
  38. // a simple complex number representation z = a + bi
  39. struct complex
  40. {
  41. complex (double a, double b = 0.0) : a(a), b(b) {}
  42. double a;
  43. double b;
  44. };
  45. // the streaming operator for the type complex
  46. std::ostream&
  47. operator<< (std::ostream& os, complex const& z)
  48. {
  49. os << "{" << z.a << "," << z.b << "}";
  50. return os;
  51. }
  52. //]
  53. ///////////////////////////////////////////////////////////////////////////
  54. // Our number list generator
  55. ///////////////////////////////////////////////////////////////////////////
  56. //[tutorial_karma_numlist3
  57. template <typename OutputIterator, typename Container>
  58. bool generate_numbers(OutputIterator& sink, Container const& v)
  59. {
  60. using boost::spirit::karma::stream;
  61. using boost::spirit::karma::generate;
  62. using boost::spirit::karma::eol;
  63. bool r = generate(
  64. sink, // destination: output iterator
  65. stream % eol, // the generator
  66. v // the data to output
  67. );
  68. return r;
  69. }
  70. //]
  71. }
  72. ////////////////////////////////////////////////////////////////////////////
  73. // Main program
  74. ////////////////////////////////////////////////////////////////////////////
  75. int
  76. main()
  77. {
  78. std::cout << "/////////////////////////////////////////////////////////\n\n";
  79. std::cout << "\tA comma separated list generator for Spirit...\n\n";
  80. std::cout << "/////////////////////////////////////////////////////////\n\n";
  81. std::cout << "Give me a comma separated list of numbers.\n";
  82. std::cout << "Type [q or Q] to quit\n\n";
  83. std::string str;
  84. while (getline(std::cin, str))
  85. {
  86. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  87. break;
  88. std::vector<double> v; // here we put the data gotten from input
  89. if (client::parse_numbers(str.begin(), str.end(), v))
  90. {
  91. // ok, we got some numbers, fill a vector of client::complex
  92. // instances and print them back out
  93. std::vector<client::complex> vc;
  94. std::vector<double>::const_iterator end = v.end();
  95. for (std::vector<double>::const_iterator it = v.begin();
  96. it != end; ++it)
  97. {
  98. double real(*it);
  99. if (++it != end)
  100. vc.push_back(client::complex(real, *it));
  101. else {
  102. vc.push_back(client::complex(real));
  103. break;
  104. }
  105. }
  106. std::cout << "-------------------------\n";
  107. std::string generated;
  108. std::back_insert_iterator<std::string> sink(generated);
  109. if (!client::generate_numbers(sink, vc))
  110. {
  111. std::cout << "-------------------------\n";
  112. std::cout << "Generating failed\n";
  113. std::cout << "-------------------------\n";
  114. }
  115. else
  116. {
  117. std::cout << "-------------------------\n";
  118. std::cout << "Generated:\n" << generated << "\n";
  119. std::cout << "-------------------------\n";
  120. }
  121. }
  122. else
  123. {
  124. std::cout << "-------------------------\n";
  125. std::cout << "Parsing failed\n";
  126. std::cout << "-------------------------\n";
  127. }
  128. }
  129. std::cout << "Bye... :-) \n\n";
  130. return 0;
  131. }