]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/reorder_struct.cpp
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 // The main purpose of this example is to show how a single fusion sequence
7 // can be used to generate output of the elements in different sequences
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/spirit/include/karma.hpp>
11 #include <boost/fusion/include/struct.hpp>
12 #include <boost/fusion/include/nview.hpp>
13 #include <boost/assign/std/vector.hpp>
15 namespace fusion
= boost::fusion
;
16 namespace karma
= boost::spirit::karma
;
18 ///////////////////////////////////////////////////////////////////////////////
21 // Our employee struct
28 std::string department
;
31 // define iterator type
32 typedef std::back_insert_iterator
<std::string
> iterator_type
;
34 // This is the output routine taking a format description and the data to
36 template <typename Generator
, typename Sequence
>
37 void generate(Generator
const& g
, Sequence
const& s
)
39 std::string generated
;
40 iterator_type
sink(generated
);
41 karma::generate(sink
, g
, s
);
42 std::cout
<< generated
<< std::endl
;
46 // We need to tell fusion about our employee struct to make it a first-class
47 // fusion citizen. This has to be in global scope. Note that we don't need to
48 // list the members of our struct in the same sequence a they are defined
49 BOOST_FUSION_ADAPT_STRUCT(
52 (std::string
, surname
)
53 (std::string
, forename
)
54 (std::string
, department
)
58 ///////////////////////////////////////////////////////////////////////////////
64 client::employee john
= { "John", "Smith", 25, 2000.50, "Sales" };
65 client::employee mary
= { "Mary", "Higgins", 23, 2200.36, "Marketing" };
66 client::employee tom
= { "Tom", "Taylor", 48, 3200.00, "Boss" };
68 // print data about employees in different formats
70 // print forename and age
72 karma::string
<< ", " << karma::int_
,
73 fusion::as_nview
<2, 0>(john
));
75 // print surname, forename, and salary
77 karma::string
<< ' ' << karma::string
<< ": " << karma::double_
,
78 fusion::as_nview
<1, 2, 4>(mary
));
80 // print forename, age, and department
82 karma::string
<< ": " << karma::int_
<< " (" << karma::string
<< ')',
83 fusion::as_nview
<2, 0, 3>(tom
));
86 // now make a list of all employees and print them all
87 std::vector
<client::employee
> employees
;
89 using namespace boost::assign
;
90 employees
+= john
, mary
, tom
;
93 // print surname, forename, and salary for all employees
96 fusion::result_of::as_nview
<client::employee
const, 1, 2, 4>::type
99 karma::rule
<client::iterator_type
, names_and_salary()> r
=
100 karma::string
<< ' ' << karma::string
<< ": " << karma::double_
;
102 client::generate(r
% karma::eol
, employees
);