I'm writing a C++11 networking library that uses Boost.Asio under the hood. I want to expose an API that allows users to use stackful coroutines.
boost::asio::yield_context
overloads the []
operator so that an asynchronous operation may set an error code instead of throwing an exception. For example:
std::size_t n = my_socket.async_read_some(buffer, yield[ec]);
if (ec)
{
// An error occurred.
}
My library uses std::error_code
and std::system_error
to report errors. My question is how can I make boost::asio::yield_context
set a std::error_code
instead of boost::system::error_code
? I'd like users of my library to be able to do this:
std::error_code ec;
auto result = remoteProdedureCall(args, yield[ec]);
if (ec)
handleError();
where remoteProcedureCall
would look something like:
Result remoteProcedureCall(Args args, boost::asio::yield_context yield)
{
//...
boost::asio::async_write(socket_, argsBuffer, yield);
boost::asio::async_read(socket_, resultBuffer, yield);
if (invalidResult())
// Return a std::error_code via the yield object somehow???
// (My error codes belong to a custom error_category)
// ...
return result;
}
P.S. I should indicate that my library uses error codes that belong to a custom error_category
.