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 #include <stdexcept> 12 #include <string> 13 14 #include <boost/bind.hpp> 15 #include <boost/throw_exception.hpp> 16 17 typedef boost::coroutines::asymmetric_coroutine< int >::pull_type pull_coro_t; 18 typedef boost::coroutines::asymmetric_coroutine< int >::push_type push_coro_t; 19 20 struct my_exception : public std::runtime_error 21 { my_exceptionmy_exception22 my_exception( std::string const& str) : 23 std::runtime_error( str) 24 {} 25 }; 26 echo(push_coro_t & sink,int j)27void echo( push_coro_t & sink, int j) 28 { 29 for ( int i = 0; i < j; ++i) 30 { 31 if ( i == 5) boost::throw_exception( my_exception("abc") ); 32 sink( i); 33 } 34 } 35 main(int argc,char * argv[])36int main( int argc, char * argv[]) 37 { 38 pull_coro_t source( boost::bind( echo, _1, 10) ); 39 try 40 { 41 while ( source) 42 { 43 std::cout << source.get() << std::endl; 44 source(); 45 } 46 } 47 catch ( my_exception const& ex) 48 { std::cout << "exception: " << ex.what() << std::endl; } 49 50 std::cout << "\nDone" << std::endl; 51 52 return EXIT_SUCCESS; 53 } 54