]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/num_list3.cpp
1 /*=============================================================================
2 Copyright (c) 2002-2010 Hartmut Kaiser
3 Copyright (c) 2002-2010 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
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.
14 ///////////////////////////////////////////////////////////////////////////////
16 #include <boost/spirit/include/qi.hpp>
17 #include <boost/spirit/include/karma.hpp>
25 ///////////////////////////////////////////////////////////////////////////
26 // Our number list parser, please see the example qi/numlist1.cpp for
28 ///////////////////////////////////////////////////////////////////////////
29 template <typename Iterator
>
30 bool parse_numbers(Iterator first
, Iterator last
, std::vector
<double>& v
)
32 using boost::spirit::qi::double_
;
33 using boost::spirit::qi::phrase_parse
;
34 using boost::spirit::ascii::space
;
36 bool r
= phrase_parse(first
, last
, double_
% ',', space
, v
);
42 //[tutorial_karma_numlist3_complex
43 // a simple complex number representation z = a + bi
46 complex (double a
, double b
= 0.0) : a(a
), b(b
) {}
52 // the streaming operator for the type complex
54 operator<< (std::ostream
& os
, complex const& z
)
56 os
<< "{" << z
.a
<< "," << z
.b
<< "}";
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
)
68 using boost::spirit::karma::stream
;
69 using boost::spirit::karma::generate
;
70 using boost::spirit::karma::eol
;
73 sink
, // destination: output iterator
74 stream
% eol
, // the generator
75 v
// the data to output
82 ////////////////////////////////////////////////////////////////////////////
84 ////////////////////////////////////////////////////////////////////////////
88 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
89 std::cout
<< "\tA comma separated list generator for Spirit...\n\n";
90 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
92 std::cout
<< "Give me a comma separated list of numbers.\n";
93 std::cout
<< "Type [q or Q] to quit\n\n";
96 while (getline(std::cin
, str
))
98 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
101 std::vector
<double> v
; // here we put the data gotten from input
102 if (client::parse_numbers(str
.begin(), str
.end(), v
))
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();
113 vc
.push_back(client::complex(real
, *it
));
115 vc
.push_back(client::complex(real
));
120 std::cout
<< "-------------------------\n";
122 std::string generated
;
123 std::back_insert_iterator
<std::string
> sink(generated
);
124 if (!client::generate_numbers(sink
, vc
))
126 std::cout
<< "-------------------------\n";
127 std::cout
<< "Generating failed\n";
128 std::cout
<< "-------------------------\n";
132 std::cout
<< "-------------------------\n";
133 std::cout
<< "Generated:\n" << generated
<< "\n";
134 std::cout
<< "-------------------------\n";
139 std::cout
<< "-------------------------\n";
140 std::cout
<< "Parsing failed\n";
141 std::cout
<< "-------------------------\n";
145 std::cout
<< "Bye... :-) \n\n";