Boost logo

Boost Users :

Subject: [Boost-users] Next layer: Boost.Asio mixins?
From: Boris Schaeling (boris_at_[hidden])
Date: 2009-05-11 14:19:03


Did anyone create and use Boost.Asio mixins and can share his experience?
I especially wonder if there is a benefit or if it's just an exercise in
templates.

The reason I started to think about Boost.Asio mixins is that I see the
very same code in various classes which all use Boost.Asio objects. I
would assume that this is not only true in my projects but also in others.

Here's an example. Let's say we create a client class to connect to
another host. We could come up with something like this:

class client
{
public:
   void resolve(std::string host, std::string service);
   void connect(boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
   void send(std::string msg);
   void receive();

private:
   boost::asio::ip::tcp::resolver r;
   boost::asio::ip::tcp::socket s;
};

If we create another class to encapsulate a POSIX file descriptor we don't
need resolve() and connect() but send() and receive():

class posix_fd
{
public:
   void send(std::string msg);
   void receive();

private:
   boost::asio::posix::stream_descriptor fd;
};

While the two classes are based on different I/O objects the
implementation of send() and receive() could be the same. That lead me to
create mixins with which I can define client and posix_fd like this:

class client :
   public resolver<boost::asio::ip::tcp::resolver, handler>,
   public connector<boost::asio::ip::tcp::socket, handler>,
   public sender<boost::asio::ip::tcp::socket, handler>,
   public receiver<boost::asio::ip::tcp::socket, handler>
{
   // Inherited from parent classes:
   // void resolve(std::string host, std::string service);
   // void connect(boost::asio::ip::tcp::resolver::iterator
endpoint_iterator);
   // void send(std::string msg);
   // void receive();
};

class posix_fd :
   public sender<boost::asio::posix::stream_descriptor, handler>,
   public receiver<boost::asio::posix::stream_descriptor, handler>
{
   // Inherited from parent classes:
   // void send(std::string msg);
   // void receive();
};

Sending and receiving data asynchronously has been implemented now once
and for all in the sender and receiver template classes. As the
implementation doesn't differ for Boost.Asio I/O objects they can be
reused. And if we had concepts already in C++ the mixins could be based on
them.

It all seems to fit nicely together. While there are some problems, too, I
haven't made my mind yet if these Boost.Asio mixins are an improvement or
make code more complicated. I'm not asking about mixins in general but
wonder if anyone had a good or bad experience using them with Boost.Asio?
Or are all Boost.Asio users simply calling the very same async_write() and
async_read() functions again and again, creating yet another buffer,
checking again the error code in the handlers etc.?

Boris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net