]>
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/spirit/include/karma.hpp>
10 #include <boost/fusion/include/struct.hpp>
11 #include <boost/fusion/include/nview.hpp>
12 #include <boost/assign/std/vector.hpp>
14 namespace fusion
= boost::fusion
;
15 namespace karma
= boost::spirit::karma
;
17 ///////////////////////////////////////////////////////////////////////////////
20 // Our employee struct
27 std::string department
;
30 // define iterator type
31 typedef std::back_insert_iterator
<std::string
> iterator_type
;
33 // This is the output routine taking a format description and the data to
35 template <typename Generator
, typename Sequence
>
36 void generate(Generator
const& g
, Sequence
const& s
)
38 std::string generated
;
39 iterator_type
sink(generated
);
40 karma::generate(sink
, g
, s
);
41 std::cout
<< generated
<< std::endl
;
45 // We need to tell fusion about our employee struct to make it a first-class
46 // fusion citizen. This has to be in global scope. Note that we don't need to
47 // list the members of our struct in the same sequence a they are defined
48 BOOST_FUSION_ADAPT_STRUCT(
51 (std::string
, surname
)
52 (std::string
, forename
)
53 (std::string
, department
)
57 ///////////////////////////////////////////////////////////////////////////////
63 client::employee john
= { "John", "Smith", 25, 2000.50, "Sales" };
64 client::employee mary
= { "Mary", "Higgins", 23, 2200.36, "Marketing" };
65 client::employee tom
= { "Tom", "Taylor", 48, 3200.00, "Boss" };
67 // print data about employees in different formats
69 // print forename and age
71 karma::string
<< ", " << karma::int_
,
72 fusion::as_nview
<2, 0>(john
));
74 // print surname, forename, and salary
76 karma::string
<< ' ' << karma::string
<< ": " << karma::double_
,
77 fusion::as_nview
<1, 2, 4>(mary
));
79 // print forename, age, and department
81 karma::string
<< ": " << karma::int_
<< " (" << karma::string
<< ')',
82 fusion::as_nview
<2, 0, 3>(tom
));
85 // now make a list of all employees and print them all
86 std::vector
<client::employee
> employees
;
88 using namespace boost::assign
;
89 employees
+= john
, mary
, tom
;
92 // print surname, forename, and salary for all employees
95 fusion::result_of::as_nview
<client::employee
const, 1, 2, 4>::type
98 karma::rule
<client::iterator_type
, names_and_salary()> r
=
99 karma::string
<< ' ' << karma::string
<< ": " << karma::double_
;
101 client::generate(r
% karma::eol
, employees
);