• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Example program showing passing of slots through an interface.
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 
15 //[ passing_slots_defs_code_snippet
16 // a pretend GUI button
17 class Button
18 {
19   typedef boost::signals2::signal<void (int x, int y)> OnClick;
20 public:
21   typedef OnClick::slot_type OnClickSlotType;
22   // forward slots through Button interface to its private signal
23   boost::signals2::connection doOnClick(const OnClickSlotType & slot);
24 
25   // simulate user clicking on GUI button at coordinates 52, 38
26   void simulateClick();
27 private:
28   OnClick onClick;
29 };
30 
doOnClick(const OnClickSlotType & slot)31 boost::signals2::connection Button::doOnClick(const OnClickSlotType & slot)
32 {
33   return onClick.connect(slot);
34 }
35 
simulateClick()36 void Button::simulateClick()
37 {
38   onClick(52, 38);
39 }
40 
printCoordinates(long x,long y)41 void printCoordinates(long x, long y)
42 {
43   std::cout << "(" << x << ", " << y << ")\n";
44 }
45 //]
46 
main()47 int main()
48 {
49 //[ passing_slots_usage_code_snippet
50   Button button;
51   button.doOnClick(&printCoordinates);
52   button.simulateClick();
53 //]
54   return 0;
55 }
56