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