extending_actors.qbk 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ===============================================================================/]
  8. [section Extending Actors]
  9. [link phoenix.inside.actor Actors] are one of the main parts of the
  10. library, and one of the many customization points. The default actor implementation
  11. provides several operator() overloads which deal with the evaluation of expressions.
  12. For some use cases this might not be enough. For convenience it is thinkable to
  13. provide custom member functions which generate new expressions. An example is the
  14. [link phoenix.modules.statement.___if_else_____statement '''if_else_'''
  15. Statement] which provides an additional else member for generating a lazy if-else
  16. expression. With this the actual Phoenix expression becomes more expressive.
  17. Another scenario is to give actors the semantics of a certain well known interface
  18. or concept. This tutorial like section will provide information on how to implement
  19. a custom actor which is usable as if it were a
  20. [@http://www.sgi.com/tech/stl/Container.html STL Container].
  21. [heading Requirements]
  22. Let's repeat what we want to have:
  23. [table
  24. [[Expression] [Semantics]]
  25. [[`a.begin()`] [Returns an iterator pointing to the first element in the container.]]
  26. [[`a.end()`] [Returns an iterator pointing one past the last element in the container.]]
  27. [[`a.size()`] [Returns the size of the container, that is, its number of elements.]]
  28. [[`a.max_size()`] [Returns the largest size that this container can ever have.]]
  29. [[`a.empty()`] [Equivalent to a.size() == 0. (But possibly faster.)]]
  30. [[`a.swap(b)`] [Equivalent to swap(a,b)]]
  31. ]
  32. Additionally, we want all the operator() overloads of the regular actor.
  33. [heading Defining the actor]
  34. The first version of our `container_actor` interface will show the general
  35. principle. This will be continually extended. For the sake of simplicity,
  36. every member function generator will return [link phoenix.modules.core.nothing `nothing`]
  37. at first.
  38. template <typename Expr>
  39. struct container_actor
  40. : actor<Expr>
  41. {
  42. typedef actor<Expr> base_type;
  43. typedef container_actor<Expr> that_type;
  44. container_actor( base_type const& base )
  45. : base_type( base ) {}
  46. expression::null<mpl::void_>::type const begin() const { return nothing; }
  47. expression::null<mpl::void_>::type const end() const { return nothing; }
  48. expression::null<mpl::void_>::type const size() const { return nothing; }
  49. expression::null<mpl::void_>::type const max_size() const { return nothing; }
  50. expression::null<mpl::void_>::type const empty() const { return nothing; }
  51. // Note that swap is the only function needing another container.
  52. template <typename Container>
  53. expression::null<mpl::void_>::type const swap( actor<Container> const& ) const { return nothing; }
  54. };
  55. [heading Using the actor]
  56. Although the member functions do nothing right now, we want to test if we can use
  57. our new actor.
  58. First, lets create a generator which wraps the `container_actor` around any other
  59. expression:
  60. template <typename Expr>
  61. container_actor<Expr> const
  62. container( actor<Expr> const& expr )
  63. {
  64. return expr;
  65. }
  66. Now let's test this:
  67. std::vector<int> v;
  68. v.push_back(0);
  69. v.push_back(1);
  70. v.push_back(2);
  71. v.push_back(3);
  72. (container(arg1).size())(v);
  73. Granted, this is not really elegant and not very practical (we could have just
  74. used phoenix::begin(v) from the [link phoenix.modules.stl.algorithm Phoenix algorithm module],
  75. but we can do better.
  76. Let's have an [link phoenix.modules.core.arguments argument placeholder]
  77. which is usable as if it was a STL container:
  78. container_actor<expression::argument<1>::type> const con1;
  79. // and so on ...
  80. The above example can be rewritten as:
  81. std::vector<int> v;
  82. v.push_back(0);
  83. v.push_back(1);
  84. v.push_back(2);
  85. v.push_back(3);
  86. (con1.size())(v);
  87. Wow, that was easy!
  88. [heading Adding life to the actor]
  89. This one will be even easier!
  90. First, we define a [link phoenix.modules.function lazy function] which
  91. evaluates the expression we want to implement.
  92. Following is the implementation of the size function:
  93. struct size_impl
  94. {
  95. // result_of protocol:
  96. template <typename Sig>
  97. struct result;
  98. template <typename This, typename Container>
  99. struct result<This(Container)>
  100. {
  101. // Note, remove reference here, because Container can be anything
  102. typedef typename boost::remove_reference<Container>::type container_type;
  103. // The result will be size_type
  104. typedef typename container_type::size_type type;
  105. };
  106. template <typename Container>
  107. typename result<size_impl(Container const&)>::type
  108. operator()(Container const& container) const
  109. {
  110. return container.size();
  111. }
  112. };
  113. Good, this was the first part. The second part will be to implement the size member
  114. function of `container_actor`:
  115. template <typename Expr>
  116. struct container_actor
  117. : actor<Expr>
  118. {
  119. typedef actor<Expr> base_type;
  120. typedef container_actor<Expr> that_type;
  121. container_actor( base_type const& base )
  122. : base_type( base ) {}
  123. typename expression::function<size_impl, that_type>::type const
  124. size() const
  125. {
  126. function<size_impl> const f = size_impl();
  127. return f(*this);
  128. }
  129. // the rest ...
  130. };
  131. It is left as an exercise to the user to implement the missing parts by reusing
  132. functions from the [link phoenix.modules.stl.algorithm Phoenix Algorithm Module]
  133. (the impatient take a look here: [@../../example/container_actor.cpp container_actor.cpp]).
  134. [endsect]