porting.qbk 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Porting from Phoenix 2.0]
  9. While reading the current documentation you might have noticed that the
  10. [link phoenix.starter_kit Starter Kit] didn't change very much. This is because
  11. a lot of effort was put into being compatible with Phoenix 2.0, at least on the
  12. outside.
  13. That being said, the only major difference is the result type deduction protocol.
  14. The everyday Phoenix-User will encounter this change only when writing
  15. [link phoenix.reference.modules.function Functions].
  16. To make your function implementations Phoenix compliant again change from
  17. the old Phoenix result type deduction protocol to the new (standard compliant)
  18. result type deduction protocol:
  19. [table
  20. [[Phoenix 2.0] [Phoenix 3.0] [Notes]]
  21. [
  22. [``
  23. struct is_odd_impl
  24. {
  25. template <typename Arg>
  26. struct result
  27. {
  28. typedef bool type;
  29. };
  30. template <typename Arg>
  31. bool operator()(Arg arg) const
  32. {
  33. return arg % 2 == 1;
  34. }
  35. };
  36. boost::phoenix::function<is_odd_impl> is_odd = is_odd_impl();
  37. ``]
  38. [``
  39. struct is_odd_impl
  40. {
  41. typedef bool result_type;
  42. template <typename Arg>
  43. bool operator()(Arg arg) const
  44. {
  45. return arg % 2 == 1;
  46. }
  47. };
  48. boost::phoenix::function<is_odd_impl> is_odd = is_odd_impl();
  49. ``]
  50. [ __note__
  51. The result_of protocol is particularly easy when you implement a monomorphic
  52. function (return type not dependent on the arguments). You then just need a
  53. single nested return_type typedef.
  54. ]
  55. ]
  56. [
  57. [``
  58. struct add_impl
  59. {
  60. template <typename Arg1, typename Arg2>
  61. struct result
  62. {
  63. typedef Arg1 type;
  64. };
  65. template <typename Arg1, typename Arg2>
  66. Arg1 operator()(Arg1 arg1, Arg2 arg2) const
  67. {
  68. return arg1 + arg2;
  69. }
  70. };
  71. boost::phoenix::function<add_impl> add = add_impl();
  72. ``]
  73. [``
  74. struct add_impl
  75. {
  76. template <typename Sig>
  77. struct result;
  78. template <typename This, typename Arg1, typename Arg2>
  79. struct result<This(Arg1, Arg2)>
  80. : boost::remove_reference<Arg1> {};
  81. template <typename Arg1, typename Arg2>
  82. typename boost::remove_reference<Arg1>::type
  83. operator()(Arg1 arg1, Arg2 arg2) const
  84. {
  85. return arg1 + arg2;
  86. }
  87. };
  88. boost::phoenix::function<add_impl> add = add_impl();
  89. ``]
  90. [__alert__ When dealing with polymorphic functions the template arguments can
  91. be any type including cv-qualifiers and references. For that reason, the calculated
  92. result type need to remove the reference whenever appropriate!]
  93. ]
  94. ]
  95. [blurb __tip__ There is no general guideline for porting code which relies on the
  96. internals of Phoenix 2.0. If you plan on porting your Phoenix 2.0 extensions
  97. please refer to the next sections.]
  98. [endsect]