multifunction.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. //[multifunction
  11. /*`
  12. (For the source of this example see
  13. [@boost:/libs/type_erasure/example/multifunction.cpp multifunction.cpp])
  14. This example implements an extension of Boost.Function that supports
  15. multiple signatures.
  16. [note This example uses C++11 features. You'll need a
  17. recent compiler for it to work.]
  18. */
  19. #include <boost/type_erasure/any.hpp>
  20. #include <boost/type_erasure/builtin.hpp>
  21. #include <boost/type_erasure/callable.hpp>
  22. #include <boost/mpl/vector.hpp>
  23. #include <boost/variant.hpp>
  24. #include <boost/phoenix/core.hpp>
  25. #include <boost/phoenix/operator.hpp>
  26. #include <boost/range/algorithm.hpp>
  27. #include <algorithm>
  28. #include <vector>
  29. #include <string>
  30. #include <iostream>
  31. namespace mpl = boost::mpl;
  32. using namespace boost::type_erasure;
  33. namespace phoenix = boost::phoenix;
  34. // First of all we'll declare the multifunction template.
  35. // multifunction is like Boost.Function but instead of
  36. // taking one signature, it takes any number of them.
  37. template<class... Sig>
  38. using multifunction =
  39. any<
  40. mpl::vector<
  41. copy_constructible<>,
  42. typeid_<>,
  43. relaxed,
  44. callable<Sig>...
  45. >
  46. >;
  47. // Let's use multifunction to process a variant. We'll start
  48. // by defining a simple recursive variant to use.
  49. typedef boost::make_recursive_variant<
  50. int,
  51. double,
  52. std::string,
  53. std::vector<boost::recursive_variant_> >::type variant_type;
  54. typedef std::vector<variant_type> vector_type;
  55. // Now we'll define a multifunction that can operate
  56. // on the leaf nodes of the variant.
  57. typedef multifunction<void(int), void(double), void(std::string)> function_type;
  58. class variant_handler
  59. {
  60. public:
  61. void handle(const variant_type& arg)
  62. {
  63. boost::apply_visitor(impl, arg);
  64. }
  65. void set_handler(function_type f)
  66. {
  67. impl.f = f;
  68. }
  69. private:
  70. // A class that works with boost::apply_visitor
  71. struct dispatcher : boost::static_visitor<void>
  72. {
  73. // used for the leaves
  74. template<class T>
  75. void operator()(const T& t) { f(t); }
  76. // For a vector, we recursively operate on the elements
  77. void operator()(const vector_type& v)
  78. {
  79. boost::for_each(v, boost::apply_visitor(*this));
  80. }
  81. function_type f;
  82. };
  83. dispatcher impl;
  84. };
  85. int main() {
  86. variant_handler x;
  87. x.set_handler(std::cout << phoenix::val("Value: ") << phoenix::placeholders::_1 << std::endl);
  88. x.handle(1);
  89. x.handle(2.718);
  90. x.handle("The quick brown fox jumps over the lazy dog.");
  91. x.handle(vector_type{ 1.618, "Gallia est omnis divisa in partes tres", 42 });
  92. }
  93. //]