Greetings,
On UNIX-like platforms,
boost::asio::detail::socket_option::{boolean,integer} templates are very
useful when defining socket option classes for non-standard options that
are supported by the platform, e.g.:
namespace so = boost::asio::detail::socket_option;
typedef so::boolean reuse_addr;
typedef so::boolean reuse_port;
typedef so::boolean pending_error;
typedef so::integer send_low_watermark;
typedef so::integer recv_low_watermark;
typedef so::boolean include_header;
typedef so::boolean do_not_fragment;
typedef so::integer type_of_service;
typedef so::integer time_to_live;
... and so on.
It is not suitable to include these non-standard options in the asio
library itself, but it seems justifiable to move the boolean/integer
templates out of boost::asio::detail and into boost::asio.
Also, many socket options take a C struct as an argument, so a template
for such options might be useful as well:
template
class typed
: public Value
{
typedef Value value;
template <typename Protocol>
int level(const Protocol &) const
{
return Level;
}
template <typename Protocol>
int name(const Protocol &) const
{
return Name;
}
template <typename Protocol>
value *data(const Protocol &)
{
return (value *)this;
}
template <typename Protocol>
std::size_t size(const Protocol &) const
{
return sizeof(Value);
}
template <typename Protocol>
void resize(const Protocol &, std::size_t s) const
{
if (s != sizeof(Value))
throw std::length_error("resizing fixed-length socket option");
}
};
Then it becomes trivial to define and use such socket options:
typedef typed send_timeout;
typedef typed recv_timeout;
send_timeout sndtimeo;
sndtimeo.tv_sec = 1;
sndtimeo.tv_usec = 0;
Would this template be appropriate for inclusion in a future version of
asio? Just my $.02.
Cheers,
Eugene