]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/complex_number_easier.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
2 // Copyright (c) 2001-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)
7 ///////////////////////////////////////////////////////////////////////////////
9 // A complex number micro generator - take 2. Look'ma no semantic actions!
11 // [ HK July 26, 2009 ] spirit2
13 ///////////////////////////////////////////////////////////////////////////////
15 #include <boost/config/warning_disable.hpp>
16 #include <boost/spirit/include/qi.hpp>
17 #include <boost/spirit/include/karma.hpp>
18 #include <boost/spirit/include/phoenix_core.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 #include <boost/fusion/include/std_pair.hpp>
28 ///////////////////////////////////////////////////////////////////////////
29 // Our complex number parser/compiler (that's just a copy of the complex
30 // number example from Qi (see examples/qi/complex_number.cpp)
31 ///////////////////////////////////////////////////////////////////////////
32 template <typename Iterator
>
33 bool parse_complex(Iterator first
, Iterator last
, std::complex<double>& c
)
35 using boost::spirit::qi::double_
;
36 using boost::spirit::qi::_1
;
37 using boost::spirit::qi::phrase_parse
;
38 using boost::spirit::ascii::space
;
39 using boost::phoenix::ref
;
43 bool r
= phrase_parse(first
, last
,
45 '(' >> double_
[ref(rN
) = _1
]
46 >> -(',' >> double_
[ref(iN
) = _1
]) >> ')'
47 | double_
[ref(rN
) = _1
]
51 if (!r
|| first
!= last
) // fail if we did not get a full match
53 c
= std::complex<double>(rN
, iN
);
57 ///////////////////////////////////////////////////////////////////////////
58 // Our complex number generator
59 ///////////////////////////////////////////////////////////////////////////
60 //[tutorial_karma_complex_number_easier
61 template <typename OutputIterator
>
62 bool generate_complex(OutputIterator sink
, std::complex<double> const& c
)
64 using boost::spirit::karma::double_
;
65 using boost::spirit::karma::omit
;
66 using boost::spirit::karma::generate
;
72 !double_(0.0) << '(' << double_
<< ", " << double_
<< ')'
73 | omit
[double_
] << double_
77 c
.imag(), c
.real(), c
.imag() // Data to output
83 ///////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////
88 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
89 std::cout
<< "\t\tA complex number micro generator for Spirit...\n\n";
90 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
92 std::cout
<< "Give me a complex number of the form r or (r) or (r,i) \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::complex<double> c
;
102 if (client::parse_complex(str
.begin(), str
.end(), c
))
104 std::cout
<< "-------------------------\n";
106 std::string generated
;
107 std::back_insert_iterator
<std::string
> sink(generated
);
108 if (!client::generate_complex(sink
, c
))
110 std::cout
<< "-------------------------\n";
111 std::cout
<< "Generating failed\n";
112 std::cout
<< "-------------------------\n";
116 std::cout
<< "-------------------------\n";
117 std::cout
<< "Generated: " << generated
<< "\n";
118 std::cout
<< "-------------------------\n";
123 std::cout
<< "-------------------------\n";
124 std::cout
<< "Parsing failed\n";
125 std::cout
<< "-------------------------\n";
129 std::cout
<< "Bye... :-) \n\n";