| 1 | // Boost.Signals library |
|---|
| 2 | |
|---|
| 3 | // Copyright Douglas Gregor 2001-2003. Use, modification and |
|---|
| 4 | // distribution is subject to the Boost Software License, Version |
|---|
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | // For more information, see http://www.boost.org |
|---|
| 9 | |
|---|
| 10 | #include <boost/bind.hpp> |
|---|
| 11 | #include <boost/signals/signal1.hpp> |
|---|
| 12 | #include <iostream> |
|---|
| 13 | |
|---|
| 14 | struct print_string : public boost::signals::trackable { |
|---|
| 15 | typedef void result_type; |
|---|
| 16 | |
|---|
| 17 | void print(const std::string& s) const { std::cout << s << std::endl; } |
|---|
| 18 | }; |
|---|
| 19 | |
|---|
| 20 | struct my_button { |
|---|
| 21 | typedef boost::signal1<void, const std::string&> click_signal_type; |
|---|
| 22 | typedef click_signal_type::slot_type click_slot_type; |
|---|
| 23 | |
|---|
| 24 | boost::signals::connection on_click_connect(const click_slot_type& s) |
|---|
| 25 | { return on_click.connect(s); } |
|---|
| 26 | |
|---|
| 27 | my_button(const std::string& l) : label(l) {} |
|---|
| 28 | |
|---|
| 29 | virtual ~my_button() {} |
|---|
| 30 | |
|---|
| 31 | void click(); |
|---|
| 32 | |
|---|
| 33 | protected: |
|---|
| 34 | virtual void clicked() { on_click(label); } |
|---|
| 35 | |
|---|
| 36 | private: |
|---|
| 37 | std::string label; |
|---|
| 38 | click_signal_type on_click; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | void my_button::click() |
|---|
| 42 | { |
|---|
| 43 | clicked(); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | int main() |
|---|
| 47 | { |
|---|
| 48 | my_button* b = new my_button("OK!"); |
|---|
| 49 | print_string* ps = new print_string(); |
|---|
| 50 | b->on_click_connect(boost::bind(&print_string::print, ps, _1)); |
|---|
| 51 | |
|---|
| 52 | b->click(); // prints OK! |
|---|
| 53 | |
|---|
| 54 | delete ps; |
|---|
| 55 | |
|---|
| 56 | b->click(); // prints nothing |
|---|
| 57 | |
|---|
| 58 | return 0; |
|---|
| 59 | } |
|---|