Boost logo

Boost :

Subject: Re: [boost] [Review.Coroutine] More comments, questions and suggestions
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-09-10 18:02:32


Le 09/09/12 20:21, Oliver Kowalke a écrit :
> Am 09.09.2012 20:02, schrieb Vicente J. Botet Escriba:
>
>> * How the library behaves when the coroutine parameters are const&
>>
>> typedef boost::coro::coroutine< int( AType const& ) > coro_t;
>>
>> int fn( coro_t::self_t & self, AType const& v)
>> {
>> ???j = self.yield( v.get_int() );
>> /// ...
>> }
>> What should be the type (???) of j? Does AType const& works?
> It should work even pointers - I've a unit test
> (libs/coroutine/tests/test_coroutine.cpp)

I have installed Boost.Coroutine on the release branch and everything
compiles and run :).

I have tried it with std::string const&.

int f77( coro_void_string::self_t & self, std::string const& str)
{
     std::string const& str2 = self.yield( str.length());
     return str2.length();
}

void test_arg_string2()
{
     value2 = "";

     coro_int_string coro( boost::bind( f77, _1, _2) );
     BOOST_CHECK( ! coro.is_complete() );
     int r = coro( std::string("abc") );
     BOOST_CHECK( ! coro.is_complete() );
     BOOST_CHECK_EQUAL( r, 3);
     r = coro( std::string("") );
     BOOST_CHECK( coro.is_complete() );
     BOOST_CHECK_EQUAL( r, 0);
}

Of course I can not define it as

int f77( coro_int_string::self_t & self, std::string const& str)
{
     str = self.yield( str.length()); // COMPILE ERROR
     return str.length();
}

as the reference can not be reassigned. I would really like to be able
to use the same name to access the coroutine parameter during the whole
scope of the coroutine.

The single way I see of been able to reuse the same name is declaring a
new variable that can be reassigned each time the yield function
returns. Of course this variable can not be a reference, but a pointer.

int f78( coro_int_string::self_t & self, std::string const& str_dont_use_it)
{
     std::string const* str = &str_dont_use_it;

     // and reassign it each time yield is called
     str = &self.yield( str->length());
     str = &self.yield( str->length());
     return str->length();
}

While this is not satisfactory, maybe the self_t could take care of it.

int f78( coro_int_string::self_t & self, std::string const& str_dont_use_it)
{
     std::string const* str;
     self.bind(str, str_dont_use_it);

     // now self_t will reassign to str each time yield is called
     self.yield( str->length());
     self.yield( str->length());
     return str->length();
}

This is close to the goal I'm looking for. The user should not take care
of reassigning the actual parameters.

Oliver, I don't know if you , that know well your library, see if this
could be implemented or even improved.

Best,
Vicente

P.S. Note that all this issues appear only with references, which can
not be reassigned. For non-reference parameters I expect the user to
reuse the parameter itself.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk