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