reorder_struct.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // The main purpose of this example is to show how a single fusion sequence
  6. // can be used to generate output of the elements in different sequences
  7. #include <boost/config/warning_disable.hpp>
  8. #include <boost/spirit/include/karma.hpp>
  9. #include <boost/fusion/include/struct.hpp>
  10. #include <boost/fusion/include/nview.hpp>
  11. #include <boost/assign/std/vector.hpp>
  12. namespace fusion = boost::fusion;
  13. namespace karma = boost::spirit::karma;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace client
  16. {
  17. // Our employee struct
  18. struct employee
  19. {
  20. std::string surname;
  21. std::string forename;
  22. int age;
  23. double salary;
  24. std::string department;
  25. };
  26. // define iterator type
  27. typedef std::back_insert_iterator<std::string> iterator_type;
  28. // This is the output routine taking a format description and the data to
  29. // print
  30. template <typename Generator, typename Sequence>
  31. void generate(Generator const& g, Sequence const& s)
  32. {
  33. std::string generated;
  34. iterator_type sink(generated);
  35. karma::generate(sink, g, s);
  36. std::cout << generated << std::endl;
  37. }
  38. }
  39. // We need to tell fusion about our employee struct to make it a first-class
  40. // fusion citizen. This has to be in global scope. Note that we don't need to
  41. // list the members of our struct in the same sequence a they are defined
  42. BOOST_FUSION_ADAPT_STRUCT(
  43. client::employee,
  44. (int, age)
  45. (std::string, surname)
  46. (std::string, forename)
  47. (std::string, department)
  48. (double, salary)
  49. )
  50. ///////////////////////////////////////////////////////////////////////////////
  51. int main()
  52. {
  53. std::string str;
  54. // some employees
  55. client::employee john = { "John", "Smith", 25, 2000.50, "Sales" };
  56. client::employee mary = { "Mary", "Higgins", 23, 2200.36, "Marketing" };
  57. client::employee tom = { "Tom", "Taylor", 48, 3200.00, "Boss" };
  58. // print data about employees in different formats
  59. {
  60. // print forename and age
  61. client::generate(
  62. karma::string << ", " << karma::int_,
  63. fusion::as_nview<2, 0>(john));
  64. // print surname, forename, and salary
  65. client::generate(
  66. karma::string << ' ' << karma::string << ": " << karma::double_,
  67. fusion::as_nview<1, 2, 4>(mary));
  68. // print forename, age, and department
  69. client::generate(
  70. karma::string << ": " << karma::int_ << " (" << karma::string << ')',
  71. fusion::as_nview<2, 0, 3>(tom));
  72. }
  73. // now make a list of all employees and print them all
  74. std::vector<client::employee> employees;
  75. {
  76. using namespace boost::assign;
  77. employees += john, mary, tom;
  78. }
  79. // print surname, forename, and salary for all employees
  80. {
  81. typedef
  82. fusion::result_of::as_nview<client::employee const, 1, 2, 4>::type
  83. names_and_salary;
  84. karma::rule<client::iterator_type, names_and_salary()> r =
  85. karma::string << ' ' << karma::string << ": " << karma::double_;
  86. client::generate(r % karma::eol, employees);
  87. }
  88. return 0;
  89. }