Boost.Coroutine and Object Inheritance
Hello to all. I need some help using the Boost.Coroutine lib. I wanted to use the coroutines as objects and for that, I tried to build a simple object and inherit the boost::coroutines::coroutine template. ex: class SimpleObj : public boost::coroutines::coroutine<SimpleObj>{ private: std::unique_ptrboost::asio::ip::tcp::socket tcpSkt_uPtr; std::unique_ptrboost::asio::ip::tcp::acceptor acc_uPtr; public: SimpleObj(std::shared_ptrboost::asio::io_service ioService_ptr, boost::asio::ip::tcp::endpoint &endPoint); ~SimpleObj(); void run(); }; Is this right? Can I actually inherit from the boost::coroutines::coroutine template ? If yes, how can I proceed from here, to use the run() object method as the coroutine itself, with an io_service provided through the constructor ? Thanks for any assistance! -- Carlos Miguel Ferreira Researcher at Telecommunications Institute Aveiro - Portugal Work E-mail - cmf@av.it.pt Skype & GTalk -> carlosmf.pt@gmail.com LinkedIn -> http://www.linkedin.com/in/carlosmferreira
On Thu, Dec 19, 2013 at 5:51 AM, Carlos Ferreira
simple object and inherit the boost::coroutines::coroutine template.
ex: <snip/>
Is this right? Can I actually inherit from the boost::coroutines::coroutine template ? If yes, how can I proceed from here, to use the run() object method as the coroutine itself, with an io_service provided through the constructor ?
Um. It's much more typical to instantiate a coroutine, passing it the code you want to run. That can be an object method, run() or whatever: use boost::bind() to bind the object pointer/reference to the method, along with any other parameters you want to pass it initially. I would have to let Oliver respond to the question about whether it's acceptable to derive from the coroutine template. But I claim that it will perplex future maintainers.
2013/12/19 Carlos Ferreira
Hello to all.
I need some help using the Boost.Coroutine lib. I wanted to use the coroutines as objects and for that, I tried to build a simple object and inherit the boost::coroutines::coroutine template.
ex:
class SimpleObj : public boost::coroutines::coroutine<SimpleObj>{ private: std::unique_ptrboost::asio::ip::tcp::socket tcpSkt_uPtr; std::unique_ptrboost::asio::ip::tcp::acceptor acc_uPtr;
public: SimpleObj(std::shared_ptrboost::asio::io_service ioService_ptr, boost::asio::ip::tcp::endpoint &endPoint); ~SimpleObj(); void run(); };
Is this right? Can I actually inherit from the boost::coroutines::coroutine template ? If yes, how can I proceed from here, to use the run() object method as the coroutine itself, with an io_service provided through the constructor ?
1.) do you know that boost.asio integrates boost.coroutine? example can be read at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/spawn... 2.) coroutine<> is a type holder, e.g. you have to derive from coroutine<>::push_type or coroutine<>::pull_type 3.) usually you would not track SimpleObj as is-a-coroutine -> I suggest to aggregate coroutine<> instead derive from it
Yes, I know this is a different approach but I wanted to maintain a pure object model. I don't know if this is a good approach or not, but then again, I'm implementing a prototype. 1.) do you know that boost.asio integrates boost.coroutine? example can be
read at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/spawn...
Yes, I noticed that but I kinda got lost when I saw that spawn(...) used stackfull coroutines but at the boost::asio::coroutine documentation, stackless coroutines were described. I wanted to ensure I was using stackfull coroutines. 2.) coroutine<> is a type holder, e.g. you have to derive from
coroutine<>::push_type or coroutine<>::pull_type
I also saw that documentation and also got lost there... What is the difference between the push_type and pull_type? For what purposes should I use them? 3.) usually you would not track SimpleObj as is-a-coroutine -> I suggest to
aggregate coroutine<> instead derive from it
I also though about that... I just didn't know what was the best approach.
Thinking a second time pushes me to go with the approach you just described.
Thanks for the info.
On 19 December 2013 14:06, Oliver Kowalke
2013/12/19 Carlos Ferreira
Hello to all.
I need some help using the Boost.Coroutine lib. I wanted to use the coroutines as objects and for that, I tried to build a simple object and inherit the boost::coroutines::coroutine template.
ex:
class SimpleObj : public boost::coroutines::coroutine<SimpleObj>{ private: std::unique_ptrboost::asio::ip::tcp::socket tcpSkt_uPtr; std::unique_ptrboost::asio::ip::tcp::acceptor acc_uPtr;
public: SimpleObj(std::shared_ptrboost::asio::io_service ioService_ptr, boost::asio::ip::tcp::endpoint &endPoint); ~SimpleObj(); void run(); };
Is this right? Can I actually inherit from the boost::coroutines::coroutine template ? If yes, how can I proceed from here, to use the run() object method as the coroutine itself, with an io_service provided through the constructor ?
1.) do you know that boost.asio integrates boost.coroutine? example can be read at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/spawn... 2.) coroutine<> is a type holder, e.g. you have to derive from coroutine<>::push_type or coroutine<>::pull_type 3.) usually you would not track SimpleObj as is-a-coroutine -> I suggest to aggregate coroutine<> instead derive from it
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Carlos Miguel Ferreira Researcher at Telecommunications Institute Aveiro - Portugal Work E-mail - cmf@av.it.pt Skype & GTalk -> carlosmf.pt@gmail.com LinkedIn -> http://www.linkedin.com/in/carlosmferreira
2013/12/19 Carlos Ferreira
1.) do you know that boost.asio integrates boost.coroutine? example can be
read at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/spawn...
Yes, I noticed that but I kinda got lost when I saw that spawn(...) used stackfull coroutines but at the boost::asio::coroutine documentation, stackless coroutines were described. I wanted to ensure I was using stackfull coroutines.
boost::asio::spawn(io_service, boost::bind(do_accept, boost::ref(io_service), atoi(argv[1]), _1)); spawn() creates internally a new coroutine with do_accept() as coroutine-fn (do_accept() is executed by the coroutine) and passes io_serve and port as arguments
2.) coroutine<> is a type holder, e.g. you have to derive from
coroutine<>::push_type or coroutine<>::pull_type
I also saw that documentation and also got lost there... What is the difference between the push_type and pull_type? For what purposes should I use them?
with the new interface boost.coroutine provides unidirectional transfer of data, e.g. you can push a data value (for instance std::string) from coroutinestd::string::push_type to coroutinestd::string::pull_type. if you create push_type or pull_type the framework automatically create the counterpart for you and passes the instance to the coroutine-fn: void coro_fn1( coroutinestd::string::pull_type & c) { std::string s = c.get(); } coroutinestd::string::push_type c( coro_fn1); std::string abc("abc"); c(abc); or void coro_fn2( coroutinestd::string::push_type & c) { std::string xyz("xyz"); c( xyz); } coroutinestd::string::pull_type c( coro_fn2); while ( c) { std::string s = c.get(); c(); }
spawn() creates internally a new coroutine with do_accept() as coroutine-fn (do_accept() is executed by the coroutine) and passes io_serve and port as arguments
How can the yield command be use, if it is not passed as an argument, like
in the echo_server example?
I dissected the echo_server example and I think I'm going to be able to go
from there.
Thanks for the info!
On 19 December 2013 17:28, Oliver Kowalke
2013/12/19 Carlos Ferreira
1.) do you know that boost.asio integrates boost.coroutine? example can
be read at http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/spawn...
Yes, I noticed that but I kinda got lost when I saw that spawn(...) used stackfull coroutines but at the boost::asio::coroutine documentation, stackless coroutines were described. I wanted to ensure I was using stackfull coroutines.
boost::asio::spawn(io_service, boost::bind(do_accept, boost::ref(io_service), atoi(argv[1]), _1));
spawn() creates internally a new coroutine with do_accept() as coroutine-fn (do_accept() is executed by the coroutine) and passes io_serve and port as arguments
2.) coroutine<> is a type holder, e.g. you have to derive from
coroutine<>::push_type or coroutine<>::pull_type
I also saw that documentation and also got lost there... What is the difference between the push_type and pull_type? For what purposes should I use them?
with the new interface boost.coroutine provides unidirectional transfer of data, e.g. you can push a data value (for instance std::string) from coroutinestd::string::push_type to coroutinestd::string::pull_type. if you create push_type or pull_type the framework automatically create the counterpart for you and passes the instance to the coroutine-fn:
void coro_fn1( coroutinestd::string::pull_type & c) { std::string s = c.get(); }
coroutinestd::string::push_type c( coro_fn1); std::string abc("abc"); c(abc);
or
void coro_fn2( coroutinestd::string::push_type & c) { std::string xyz("xyz"); c( xyz); }
coroutinestd::string::pull_type c( coro_fn2); while ( c) { std::string s = c.get(); c(); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Carlos Miguel Ferreira Researcher at Telecommunications Institute Aveiro - Portugal Work E-mail - cmf@av.it.pt Skype & GTalk -> carlosmf.pt@gmail.com LinkedIn -> http://www.linkedin.com/in/carlosmferreira
participants (3)
-
Carlos Ferreira
-
Nat Goodspeed
-
Oliver Kowalke