]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/karma/reorder_struct.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / example / karma / reorder_struct.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
2 //
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)
5
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
8
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>
13
14 namespace fusion = boost::fusion;
15 namespace karma = boost::spirit::karma;
16
17 ///////////////////////////////////////////////////////////////////////////////
18 namespace client
19 {
20 // Our employee struct
21 struct employee
22 {
23 std::string surname;
24 std::string forename;
25 int age;
26 double salary;
27 std::string department;
28 };
29
30 // define iterator type
31 typedef std::back_insert_iterator<std::string> iterator_type;
32
33 // This is the output routine taking a format description and the data to
34 // print
35 template <typename Generator, typename Sequence>
36 void generate(Generator const& g, Sequence const& s)
37 {
38 std::string generated;
39 iterator_type sink(generated);
40 karma::generate(sink, g, s);
41 std::cout << generated << std::endl;
42 }
43 }
44
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(
49 client::employee,
50 (int, age)
51 (std::string, surname)
52 (std::string, forename)
53 (std::string, department)
54 (double, salary)
55 )
56
57 ///////////////////////////////////////////////////////////////////////////////
58 int main()
59 {
60 std::string str;
61
62 // some employees
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" };
66
67 // print data about employees in different formats
68 {
69 // print forename and age
70 client::generate(
71 karma::string << ", " << karma::int_,
72 fusion::as_nview<2, 0>(john));
73
74 // print surname, forename, and salary
75 client::generate(
76 karma::string << ' ' << karma::string << ": " << karma::double_,
77 fusion::as_nview<1, 2, 4>(mary));
78
79 // print forename, age, and department
80 client::generate(
81 karma::string << ": " << karma::int_ << " (" << karma::string << ')',
82 fusion::as_nview<2, 0, 3>(tom));
83 }
84
85 // now make a list of all employees and print them all
86 std::vector<client::employee> employees;
87 {
88 using namespace boost::assign;
89 employees += john, mary, tom;
90 }
91
92 // print surname, forename, and salary for all employees
93 {
94 typedef
95 fusion::result_of::as_nview<client::employee const, 1, 2, 4>::type
96 names_and_salary;
97
98 karma::rule<client::iterator_type, names_and_salary()> r =
99 karma::string << ' ' << karma::string << ": " << karma::double_;
100
101 client::generate(r % karma::eol, employees);
102 }
103 return 0;
104 }