quick_start.qbk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2006 Dan Marsden
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ===============================================================================/]
  8. [section Quick Start]
  9. I assume the reader is already familiar with tuples (__tuple__) and its
  10. ancestor `std::pair`. The tuple is a generalization of `std::pair` for
  11. multiple heterogeneous elements (triples, quadruples, etc.). The tuple is
  12. more or less a synonym for fusion's `__vector__`.
  13. For starters, we shall include all of Fusion's __sequence__(s) [footnote There
  14. are finer grained header files available if you wish to have more control
  15. over which components to include (see section __organization__ for
  16. details).]:
  17. #include <boost/fusion/sequence.hpp>
  18. #include <boost/fusion/include/sequence.hpp>
  19. Let's begin with a `__vector__` [footnote Unless otherwise noted, components are
  20. in namespace `boost::fusion`. For the sake of simplicity, code in this
  21. quick start implies `using` directives for the fusion components we will be
  22. using.]:
  23. __vector__<int, char, std::string> stuff(1, 'x', "howdy");
  24. int i = __at_c__<0>(stuff);
  25. char ch = __at_c__<1>(stuff);
  26. std::string s = __at_c__<2>(stuff);
  27. Just replace `tuple` for `__vector__` and `get` for `__at_c__` and this is exactly
  28. like __tuple__. Actually, either names can be used interchangeably. Yet,
  29. the similarity ends there. You can do a lot more with Fusion `__vector__` or
  30. `tuple`. Let's see some examples.
  31. [heading Print the vector as XML]
  32. First, let's include the algorithms:
  33. #include <boost/fusion/algorithm.hpp>
  34. #include <boost/fusion/include/algorithm.hpp>
  35. Now, let's write a function object that prints XML of the form <type>data</type>
  36. for each member in the tuple.
  37. struct print_xml
  38. {
  39. template <typename T>
  40. void operator()(T const& x) const
  41. {
  42. std::cout
  43. << '<' << typeid(x).name() << '>'
  44. << x
  45. << "</" << typeid(x).name() << '>'
  46. ;
  47. }
  48. };
  49. Now, finally:
  50. __for_each__(stuff, print_xml());
  51. That's it! `__for_each__` is a fusion algorithm. It is a generic algorithm
  52. similar to __stl__'s. It iterates over the sequence and calls a user
  53. supplied function. In our case, it calls `print_xml`'s `operator()` for
  54. each element in `stuff`.
  55. [caution The result of `typeid(x).name()` is platform specific. The code
  56. here is just for exposition. Of course you already know that :-)]
  57. `__for_each__` is generic. With `print_xml`, you can use it to print just about
  58. any Fusion __sequence__.
  59. [heading Print only pointers]
  60. Let's get a little cleverer. Say we wish to write a /generic/ function
  61. that takes in an arbitrary sequence and XML prints only those elements
  62. which are pointers. Ah, easy. First, let's include the `is_pointer` boost
  63. type trait:
  64. #include <boost/type_traits/is_pointer.hpp>
  65. Then, simply:
  66. template <typename Sequence>
  67. void xml_print_pointers(Sequence const& seq)
  68. {
  69. __for_each__(__filter_if__<boost::is_pointer<_> >(seq), print_xml());
  70. }
  71. `__filter_if__` is another Fusion algorithm. It returns a __filter_view__,
  72. a conforming Fusion sequence. This view reflects only those elements that
  73. pass the given predicate. In this case, the predicate is
  74. `boost::is_pointer<_>`. This "filtered view" is then passed to the
  75. __for_each__ algorithm, which then prints the "filtered view" as XML.
  76. Easy, right?
  77. [heading Associative tuples]
  78. Ok, moving on...
  79. Apart from `__vector__`, fusion has a couple of other sequence types to choose
  80. from. Each sequence has its own characteristics. We have `__list__`, `__set__`,
  81. `__map__`, plus a multitude of `views` that provide various ways to present the
  82. sequences.
  83. Fusion's `__map__` associate types with elements. It can be used as a cleverer
  84. replacement of the `struct`. Example:
  85. namespace fields
  86. {
  87. struct name;
  88. struct age;
  89. }
  90. typedef __map__<
  91. __fusion_pair__<fields::name, std::string>
  92. , __fusion_pair__<fields::age, int> >
  93. person;
  94. `__map__` is an associative sequence. Its elements are Fusion pairs which differ
  95. somewhat from `std::pair`. Fusion pairs only contain one member, with the type of
  96. their second template parameter. The first type parameter of the pair is used as an
  97. index to the associated element in the sequence. For example, given a `a_person`
  98. of type, `person`, you can do:
  99. using namespace fields;
  100. std::string person_name = __at_key__<name>(a_person);
  101. int person_age = __at_key__<age>(a_person);
  102. Why go through all this trouble, you say? Well, for one, unlike the
  103. `struct`, we are dealing with a generic data structure. There are a
  104. multitude of facilities available at your disposal provided out of the box
  105. with fusion or written by others. With these facilities, introspection
  106. comes for free, for example. We can write one serialization function (well,
  107. two, if you consider loading and saving) that will work for all your fusion
  108. `__map__`s. Example:
  109. struct saver
  110. {
  111. template <typename Pair>
  112. void operator()(Pair const& data) const
  113. {
  114. some_archive << data.second;
  115. }
  116. };
  117. template <typename Stuff>
  118. void save(Stuff const& stuff)
  119. {
  120. __for_each__(stuff, saver());
  121. }
  122. The `save` function is generic and will work for all types of `stuff`
  123. regardless if it is a `person`, a `dog` or a whole `alternate_universe`.
  124. [heading Tip of the Iceberg]
  125. And... we've barely scratched the surface! You can compose and expand the
  126. data structures, remove elements from the structures, find specific data
  127. types, query the elements, filter out types for inspection, transform data
  128. structures, etc. What you've seen is just the tip of the iceberg.
  129. [endsect]