]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/adapt_template_struct.cpp
360707d1f82f482b953ff2724bf95b344d0a70ac
1 // Copyright (c) 2001-2010 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // This example demonstrates a trick allowing to adapt a template data
7 // structure as a Fusion sequence in order to use is for direct attribute
8 // propagation. For more information see
9 // http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence
11 #include <boost/spirit/include/qi.hpp>
13 namespace qi
= boost::spirit::qi
;
14 namespace fusion
= boost::fusion
;
18 template <typename A
, typename B
>
25 template <typename Iterator
, typename A
, typename B
>
26 struct data_grammar
: qi::grammar
<Iterator
, data
<A
, B
>()>
28 data_grammar() : data_grammar::base_type(start
)
31 real_start
= qi::auto_
>> ',' >> qi::auto_
;
34 qi::rule
<Iterator
, data
<A
, B
>()> start
;
35 qi::rule
<Iterator
, fusion::vector
<A
&, B
&>()> real_start
;
39 namespace boost
{ namespace spirit
{ namespace traits
41 template <typename A
, typename B
>
42 struct transform_attribute
<client::data
<A
, B
>, fusion::vector
<A
&, B
&>, qi::domain
>
44 typedef fusion::vector
<A
&, B
&> type
;
46 static type
pre(client::data
<A
, B
>& val
) { return type(val
.a
, val
.b
); }
47 static void post(client::data
<A
, B
>&, fusion::vector
<A
&, B
&> const&) {}
48 static void fail(client::data
<A
, B
>&) {}
52 ///////////////////////////////////////////////////////////////////////////////
55 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
56 std::cout
<< "\t\tA parser for Spirit utilizing an adapted template ...\n\n";
57 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
59 std::cout
<< "Give me two comma separated integers:\n";
60 std::cout
<< "Type [q or Q] to quit\n\n";
63 client::data_grammar
<std::string::const_iterator
, long, int> g
; // Our grammar
64 while (getline(std::cin
, str
))
66 if (str
.empty() || str
[0] == 'q' || str
[0] == 'Q')
69 client::data
<long, int> d
;
70 std::string::const_iterator iter
= str
.begin();
71 std::string::const_iterator end
= str
.end();
72 bool r
= phrase_parse(iter
, end
, g
, qi::space
, d
);
76 std::cout
<< "-------------------------\n";
77 std::cout
<< "Parsing succeeded\n";
78 std::cout
<< "got: " << d
.a
<< "," << d
.b
<< std::endl
;
79 std::cout
<< "\n-------------------------\n";
83 std::cout
<< "-------------------------\n";
84 std::cout
<< "Parsing failed\n";
85 std::cout
<< "-------------------------\n";