notes.qbk 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2006 Dan Marsden
  4. Copyright (C) 2010 Christopher Schmidt
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. ===============================================================================/]
  9. [section Notes]
  10. [heading Recursive Inlined Functions]
  11. An interesting peculiarity of functions like __at__ when applied to a
  12. __forward_sequence__ like __list__ is that what could have been linear
  13. runtime complexity effectively becomes constant O(1) due to compiler
  14. optimization of C++ inlined functions, however deeply recursive (up to a
  15. certain compiler limit of course). Compile time complexity remains linear.
  16. [heading Overloaded Functions]
  17. Associative sequences use function overloading to implement membership
  18. testing and type associated key lookup. This amounts to constant runtime
  19. and amortized constant compile time complexities. There is an overloaded
  20. function, `f(k)`, for each key /type/ `k`. The compiler chooses the
  21. appropriate function given a key, `k`.
  22. [heading Tag Dispatching]
  23. Tag dispatching is a generic programming technique for selecting template
  24. specializations. There are typically 3 components involved in the tag
  25. dispatching mechanism:
  26. # A type for which an appropriate template specialization is required
  27. # A metafunction that associates the type with a tag type
  28. # A template that is specialized for the tag type
  29. For example, the fusion `result_of::begin` metafunction is implemented
  30. as follows:
  31. template <typename Sequence>
  32. struct begin
  33. {
  34. typedef typename
  35. result_of::begin_impl<typename traits::tag_of<Sequence>::type>::
  36. template apply<Sequence>::type
  37. type;
  38. };
  39. In the case:
  40. # `Sequence` is the type for which a suitable implementation of
  41. `result_of::begin_impl` is required
  42. # `traits::tag_of` is the metafunction that associates `Sequence`
  43. with an appropriate tag
  44. # `result_of::begin_impl` is the template which is specialized to provide
  45. an implementation for each tag type
  46. [heading Extensibility]
  47. Unlike __mpl__, there is no extensible sequence concept in fusion. This does
  48. not mean that Fusion sequences are not extensible. In fact, all Fusion
  49. sequences are inherently extensible. It is just that the manner of sequence
  50. extension in Fusion is different from both __stl__ and __mpl__ on account of
  51. the lazy nature of fusion __algorithms__. __stl__ containers extend
  52. themselves in place though member functions such as __push_back__ and
  53. __insert__. __mpl__ sequences, on the other hand, are extended through
  54. "intrinsic" functions that actually return whole sequences. __mpl__ is
  55. purely functional and can not have side effects. For example, __mpl__'s
  56. `push_back` does not actually mutate an `mpl::vector`. It can't do that.
  57. Instead, it returns an extended `mpl::vector`.
  58. Like __mpl__, Fusion too is purely functional and can not have side
  59. effects. With runtime efficiency in mind, Fusion sequences are extended
  60. through generic functions that return __views__. __views__ are sequences
  61. that do not actually contain data, but instead impart an alternative
  62. presentation over the data from one or more underlying sequences. __views__
  63. are proxies. They provide an efficient yet purely functional way to work on
  64. potentially expensive sequence operations. For example, given a __vector__,
  65. Fusion's __push_back__ returns a __joint_view__, instead of an actual
  66. extended __vector__. A __joint_view__ holds a reference to the original
  67. sequence plus the appended data --making it very cheap to pass around.
  68. [heading Element Conversion]
  69. Functions that take in elemental values to form sequences (e.g.
  70. __make_list__) convert their arguments to something suitable to be stored
  71. as a sequence element. In general, the element types are stored as plain
  72. values. Example:
  73. __make_list__(1, 'x')
  74. returns a __list__`<int, char>`.
  75. There are a few exceptions, however.
  76. [*Arrays:]
  77. Array arguments are deduced to reference to const types. For example
  78. [footnote Note that the type of a string literal is an array of const
  79. characters, not `const char*`. To get __make_list__ to create a __list__
  80. with an element of a non-const array type one must use the `ref` wrapper
  81. (see __note_ref_wrappers__).]:
  82. __make_list__("Donald", "Daisy")
  83. creates a __list__ of type
  84. __list__<const char (&)[7], const char (&)[6]>
  85. [*Function pointers:]
  86. Function pointers are deduced to the plain non-reference type (i.e. to
  87. plain function pointer). Example:
  88. void f(int i);
  89. ...
  90. __make_list__(&f);
  91. creates a __list__ of type
  92. __list__<void (*)(int)>
  93. [heading Reference Wrappers]
  94. Fusion's generation functions (e.g. __make_list__) by default stores the
  95. element types as plain non-reference types. Example:
  96. void foo(const A& a, B& b) {
  97. ...
  98. __make_list__(a, b)
  99. creates a __list__ of type
  100. __list__<A, B>
  101. Sometimes the plain non-reference type is not desired. You can use
  102. `boost::ref` and `boost::cref` to store references or const references
  103. (respectively) instead. The mechanism does not compromise const correctness
  104. since a const object wrapped with ref results in a tuple element with const
  105. reference type (see the fifth code line below). Examples:
  106. For example:
  107. A a; B b; const A ca = a;
  108. __make_list__(cref(a), b); // creates list<const A&, B>
  109. __make_list__(ref(a), b); // creates list<A&, B>
  110. __make_list__(ref(a), cref(b)); // creates list<A&, const B&>
  111. __make_list__(cref(ca)); // creates list<const A&>
  112. __make_list__(ref(ca)); // creates list<const A&>
  113. See __boost_ref__ for details.
  114. Since C++11, the standard reference wrappers (`std::ref` and `std::cref`) work as well.
  115. [heading adt_attribute_proxy]
  116. To adapt arbitrary data types that do not allow direct access to their members,
  117. but allow indirect access via expressions (such as invocations of get- and
  118. set-methods), fusion's [^BOOST\_FUSION\_ADAPT\_['xxx]ADT['xxx]]-family (e.g.
  119. __adapt_adt__) may be used.
  120. To bypass the restriction of not having actual lvalues that
  121. represent the elements of the fusion sequence, but rather a sequence of paired
  122. expressions that access the elements, the actual return type of fusion's
  123. intrinsic sequence access functions (__at__, __at_c__, __at_key__, __deref__,
  124. and __deref_data__) is a proxy type, an instance of
  125. `adt_attribute_proxy`, that encapsulates these expressions.
  126. `adt_attribute_proxy` is defined in the namespace `boost::fusion::extension` and
  127. has three template arguments:
  128. namespace boost { namespace fusion { namespace extension
  129. {
  130. template<
  131. typename Type
  132. , int Index
  133. , bool Const
  134. >
  135. struct adt_attribute_proxy;
  136. }}}
  137. When adapting a class type, `adt_attribute_proxy` is specialized for every
  138. element of the adapted sequence, with `Type` being the class type that is
  139. adapted, `Index` the 0-based indices of the elements, and `Const` both `true`
  140. and `false`. The return type of fusion's intrinsic sequence access functions
  141. for the ['N]th element of an adapted class type `type_name` is
  142. [^adt_attribute_proxy<type_name, ['N], ['Const]>], with [^['Const]] being `true`
  143. for constant instances of `type_name` and `false` for non-constant ones.
  144. [variablelist Notation
  145. [[`type_name`]
  146. [The type to be adapted, with M attributes]]
  147. [[`inst`]
  148. [Object of type `type_name`]]
  149. [[`const_inst`]
  150. [Object of type `type_name const`]]
  151. [[[^(attribute_type['N], attribute_const_type['N], get_expr['N], set_expr['N])]]
  152. [Attribute descriptor of the ['N]th attribute of `type_name` as passed to the adaption macro, 0\u2264['N]<M]]
  153. [[[^proxy_type['N]]]
  154. [[^adt_attribute_proxy<type_name, ['N], `false`>] with ['N] being an integral constant, 0\u2264['N]<M]]
  155. [[[^const_proxy_type['N]]]
  156. [[^adt_attribute_proxy<type_name, ['N], `true`>] with ['N] being an integral constant, 0\u2264['N]<M]]
  157. [[[^proxy['N]]]
  158. [Object of type [^proxy_type['N]]]]
  159. [[[^const_proxy['N]]]
  160. [Object of type [^const_proxy_type['N]]]]
  161. ]
  162. [*Expression Semantics]
  163. [table
  164. [[Expression] [Semantics]]
  165. [[[^proxy_type['N](inst)]] [Creates an instance of [^proxy_type['N]] with underlying object `inst`]]
  166. [[[^const_proxy_type['N](const_inst)]] [Creates an instance of [^const_proxy_type['N]] with underlying object `const_inst`]]
  167. [[[^proxy_type['N]::type]] [Another name for [^attribute_type['N]]]]
  168. [[[^const_proxy_type['N]::type]] [Another name for [^const_attribute_type['N]]]]
  169. [[[^proxy['N]=t]] [Invokes [^set_expr['N]], with `t` being an arbitrary object. [^set_expr['N]] may access the variables named `obj` of type `type_name&`, which represent the corresponding instance of `type_name`, and `val` of an arbitrary const-qualified reference template type parameter `Val`, which represents `t`.]]
  170. [[[^proxy['N].get()]] [Invokes [^get_expr['N]] and forwards its return value. [^get_expr['N]] may access the variable named `obj` of type `type_name&` which represents the underlying instance of `type_name`. [^attribute_type['N]] may specify the type that [^get_expr['N]] denotes to.]]
  171. [[[^const_proxy['N].get()]] [Invokes [^get_expr['N]] and forwards its return value. [^get_expr['N]] may access the variable named `obj` of type `type_name const&` which represents the underlying instance of `type_name`. [^attribute_const_type['N]] may specify the type that [^get_expr['N]] denotes to.]]
  172. ]
  173. Additionally, [^proxy_type['N]] and [^const_proxy_type['N]] are copy
  174. constructible, copy assignable and implicitly convertible to
  175. [^proxy_type['N]::type] or [^const_proxy_type['N]::type].
  176. [tip To avoid the pitfalls of the proxy type, an arbitrary class type may also
  177. be adapted directly using fusion's [link fusion.extension intrinsic extension
  178. mechanism].]
  179. [endsect]