custom_combiners.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Example program showing signals with custom combiners.
  2. //
  3. // Copyright Douglas Gregor 2001-2004.
  4. // Copyright Frank Mori Hess 2009.
  5. //
  6. // Use, modification and
  7. // distribution is subject to the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // For more information, see http://www.boost.org
  11. #include <iostream>
  12. #include <boost/signals2/signal.hpp>
  13. #include <vector>
  14. float product(float x, float y) { return x * y; }
  15. float quotient(float x, float y) { return x / y; }
  16. float sum(float x, float y) { return x + y; }
  17. float difference(float x, float y) { return x - y; }
  18. //[ custom_combiners_maximum_def_code_snippet
  19. // combiner which returns the maximum value returned by all slots
  20. template<typename T>
  21. struct maximum
  22. {
  23. typedef T result_type;
  24. template<typename InputIterator>
  25. T operator()(InputIterator first, InputIterator last) const
  26. {
  27. // If there are no slots to call, just return the
  28. // default-constructed value
  29. if(first == last ) return T();
  30. T max_value = *first++;
  31. while (first != last) {
  32. if (max_value < *first)
  33. max_value = *first;
  34. ++first;
  35. }
  36. return max_value;
  37. }
  38. };
  39. //]
  40. void maximum_combiner_example()
  41. {
  42. // signal which uses our custom "maximum" combiner
  43. boost::signals2::signal<float (float x, float y), maximum<float> > sig;
  44. //[ custom_combiners_maximum_usage_code_snippet
  45. sig.connect(&product);
  46. sig.connect(&quotient);
  47. sig.connect(&sum);
  48. sig.connect(&difference);
  49. // Outputs the maximum value returned by the connected slots, in this case
  50. // 15 from the product function.
  51. std::cout << "maximum: " << sig(5, 3) << std::endl;
  52. //]
  53. }
  54. //[ custom_combiners_aggregate_values_def_code_snippet
  55. // aggregate_values is a combiner which places all the values returned
  56. // from slots into a container
  57. template<typename Container>
  58. struct aggregate_values
  59. {
  60. typedef Container result_type;
  61. template<typename InputIterator>
  62. Container operator()(InputIterator first, InputIterator last) const
  63. {
  64. Container values;
  65. while(first != last) {
  66. values.push_back(*first);
  67. ++first;
  68. }
  69. return values;
  70. }
  71. };
  72. //]
  73. void aggregate_values_example()
  74. {
  75. // signal which uses aggregate_values as its combiner
  76. boost::signals2::signal<float (float, float),
  77. aggregate_values<std::vector<float> > > sig;
  78. //[ custom_combiners_aggregate_values_usage_code_snippet
  79. sig.connect(&quotient);
  80. sig.connect(&product);
  81. sig.connect(&sum);
  82. sig.connect(&difference);
  83. std::vector<float> results = sig(5, 3);
  84. std::cout << "aggregate values: ";
  85. std::copy(results.begin(), results.end(),
  86. std::ostream_iterator<float>(std::cout, " "));
  87. std::cout << "\n";
  88. //]
  89. }
  90. int main()
  91. {
  92. maximum_combiner_example();
  93. aggregate_values_example();
  94. }