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
12 #include <iostream>
13 #include <boost/signals2/signal.hpp>
14 #include <vector>
15
product(float x,float y)16 float product(float x, float y) { return x * y; }
quotient(float x,float y)17 float quotient(float x, float y) { return x / y; }
sum(float x,float y)18 float sum(float x, float y) { return x + y; }
difference(float x,float y)19 float difference(float x, float y) { return x - y; }
20
21 //[ custom_combiners_maximum_def_code_snippet
22 // combiner which returns the maximum value returned by all slots
23 template<typename T>
24 struct maximum
25 {
26 typedef T result_type;
27
28 template<typename InputIterator>
operator ()maximum29 T operator()(InputIterator first, InputIterator last) const
30 {
31 // If there are no slots to call, just return the
32 // default-constructed value
33 if(first == last ) return T();
34 T max_value = *first++;
35 while (first != last) {
36 if (max_value < *first)
37 max_value = *first;
38 ++first;
39 }
40
41 return max_value;
42 }
43 };
44 //]
45
maximum_combiner_example()46 void maximum_combiner_example()
47 {
48 // signal which uses our custom "maximum" combiner
49 boost::signals2::signal<float (float x, float y), maximum<float> > sig;
50
51 //[ custom_combiners_maximum_usage_code_snippet
52 sig.connect(&product);
53 sig.connect("ient);
54 sig.connect(&sum);
55 sig.connect(&difference);
56
57 // Outputs the maximum value returned by the connected slots, in this case
58 // 15 from the product function.
59 std::cout << "maximum: " << sig(5, 3) << std::endl;
60 //]
61 }
62
63 //[ custom_combiners_aggregate_values_def_code_snippet
64 // aggregate_values is a combiner which places all the values returned
65 // from slots into a container
66 template<typename Container>
67 struct aggregate_values
68 {
69 typedef Container result_type;
70
71 template<typename InputIterator>
operator ()aggregate_values72 Container operator()(InputIterator first, InputIterator last) const
73 {
74 Container values;
75
76 while(first != last) {
77 values.push_back(*first);
78 ++first;
79 }
80 return values;
81 }
82 };
83 //]
84
aggregate_values_example()85 void aggregate_values_example()
86 {
87 // signal which uses aggregate_values as its combiner
88 boost::signals2::signal<float (float, float),
89 aggregate_values<std::vector<float> > > sig;
90
91 //[ custom_combiners_aggregate_values_usage_code_snippet
92 sig.connect("ient);
93 sig.connect(&product);
94 sig.connect(&sum);
95 sig.connect(&difference);
96
97 std::vector<float> results = sig(5, 3);
98 std::cout << "aggregate values: ";
99 std::copy(results.begin(), results.end(),
100 std::ostream_iterator<float>(std::cout, " "));
101 std::cout << "\n";
102 //]
103 }
104
main()105 int main()
106 {
107 maximum_combiner_example();
108 aggregate_values_example();
109 }
110