
Le 19/09/12 21:05, Oliver Kowalke a écrit :
Am 19.09.2012 20:12, schrieb Vicente J. Botet Escriba:
I forgot a suggestion made by Vicente:
5.a coroutine<>::caller_t provides function bind() which must be called after coroutine::caller_t::yield() returned in order to get the parameters
int f( caller_t & c, strg & s, int x) { c.yield( 7); // alternative c( 7) c.bind( s, x); // s and x will contain new values given by caller } My suggestion was to call bind just once at the beginning of function and before any call to yield. If you want to ensure that the bind has been done, you could request
Le 19/09/12 14:23, Oliver Kowalke a écrit : that yield is provided by the result of the bind operation, as e.g.
int f( caller_t & c, strg & s, int x) { auto b = c.bind( s, x); // s and x are now used to store the next calls. b.yield( 7); // alternative b( 7) // here s and x have been reassigned } OK - what if a user forgets to bind ? Maybe we can force the user to bind:
int f( caller_t & c, strg & s, int x) { caller_t::yield_t b = c.bind( s, x); // s and x are now used to store the next calls. b.yield( 7); // alternative b( 7) // here s and x have been reassigned }
c.bind() retrieves the addresses of the parameters used to store the values (for each entering of f). only yield_t will provide yield() function.
what do you think?
This is exactly what I proposed above :) and for the time been this is the best interface I have found. Note that as I commented in some of my first post related to bind, you must take care of const and reference parameters in some tricky ways, as in order to be able to reassign them you should do some casts. I hope this will not cache some undefined behavior. Note also that the access to the parameters need to be done using the get function on functions called by the coroutine function such as the below function 'g'. int f( caller_t & c, strg & s, int x) { caller_t::yield_t b = c.bind( s, x); // s and x are now used to store the next calls. b.yield( s.length() + x ); // here s and x have been reassigned g(b); } void g( caller_t::yield_t & b) { b.yield( b.get<0>.length() + 2*b.get<1> ); // here s and x have been reassigned } As you can see, the function called *could* not have the s and x parameters and could not return int, that is, needs just the yield context. Best, Vicente