Brian Silverman | ce4aa8d | 2018-08-04 23:36:03 -0700 | [diff] [blame^] | 1 | [/ |
| 2 | / Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. |
| 3 | / Copyright (c) 2003-2008 Peter Dimov |
| 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 | |
| 10 | [section:examples Examples] |
| 11 | |
| 12 | [section Using bind with standard algorithms] |
| 13 | |
| 14 | class image; |
| 15 | |
| 16 | class animation |
| 17 | { |
| 18 | public: |
| 19 | void advance(int ms); |
| 20 | bool inactive() const; |
| 21 | void render(image & target) const; |
| 22 | }; |
| 23 | |
| 24 | std::vector<animation> anims; |
| 25 | |
| 26 | template<class C, class P> void erase_if(C & c, P pred) |
| 27 | { |
| 28 | c.erase(std::remove_if(c.begin(), c.end(), pred), c.end()); |
| 29 | } |
| 30 | |
| 31 | void update(int ms) |
| 32 | { |
| 33 | std::for_each(anims.begin(), anims.end(), boost::bind(&animation::advance, _1, ms)); |
| 34 | erase_if(anims, boost::mem_fn(&animation::inactive)); |
| 35 | } |
| 36 | |
| 37 | void render(image & target) |
| 38 | { |
| 39 | std::for_each(anims.begin(), anims.end(), boost::bind(&animation::render, _1, boost::ref(target))); |
| 40 | } |
| 41 | |
| 42 | [endsect] |
| 43 | |
| 44 | [section Using bind with Boost.Function] |
| 45 | |
| 46 | class button |
| 47 | { |
| 48 | public: |
| 49 | ``[@boost:/libs/function/index.html `boost::function`]``<void()> onClick; |
| 50 | }; |
| 51 | |
| 52 | class player |
| 53 | { |
| 54 | public: |
| 55 | void play(); |
| 56 | void stop(); |
| 57 | }; |
| 58 | |
| 59 | button playButton, stopButton; |
| 60 | player thePlayer; |
| 61 | |
| 62 | void connect() |
| 63 | { |
| 64 | playButton.onClick = boost::bind(&player::play, &thePlayer); |
| 65 | stopButton.onClick = boost::bind(&player::stop, &thePlayer); |
| 66 | } |
| 67 | |
| 68 | [endsect] |
| 69 | |
| 70 | [endsect] |