| 1 | |
| 2 | // Copyright Oliver Kowalke 2009. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #include <boost/coroutine/all.hpp> |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | #include <iostream> |
| 11 | |
| 12 | #include <boost/bind.hpp> |
| 13 | #include <boost/foreach.hpp> |
| 14 | #include <boost/range.hpp> |
| 15 | |
| 16 | void power( boost::coroutines::asymmetric_coroutine< int >::push_type & sink, int number, int exponent) |
| 17 | { |
| 18 | int counter = 0; |
| 19 | int result = 1; |
| 20 | while ( counter++ < exponent) |
| 21 | { |
| 22 | result = result * number; |
| 23 | sink( result); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | int main() |
| 28 | { |
| 29 | { |
| 30 | std::cout << "using range functions" << std::endl; |
| 31 | boost::coroutines::asymmetric_coroutine< int >::pull_type source( boost::bind( f: power, a1: _1, a2: 2, a3: 8) ); |
| 32 | boost::coroutines::asymmetric_coroutine< int >::pull_type::iterator e( boost::end( r&: source) ); |
| 33 | for ( boost::coroutines::asymmetric_coroutine< int >::pull_type::iterator i( boost::begin( r&: source) ); |
| 34 | i != e; ++i) |
| 35 | std::cout << * i << " " ; |
| 36 | } |
| 37 | |
| 38 | { |
| 39 | std::cout << "\nusing BOOST_FOREACH" << std::endl; |
| 40 | boost::coroutines::asymmetric_coroutine< int >::pull_type source( boost::bind( f: power, a1: _1, a2: 2, a3: 8) ); |
| 41 | BOOST_FOREACH( int i, source) |
| 42 | { std::cout << i << " " ; } |
| 43 | } |
| 44 | |
| 45 | std::cout << "\nDone" << std::endl; |
| 46 | |
| 47 | return EXIT_SUCCESS; |
| 48 | } |
| 49 | |