Boost logo

Boost :

From: Chris Fairles (chris.fairles_at_[hidden])
Date: 2007-11-11 16:49:11


For getting things like the Content-Length in the http protocol when
using scatter/gather IO, I've found the following buffer_size
overloads to be useful. Might be worth adding them to the lib (after
perhaps specializing for all bidriectional sequence containers).

Cheers,
Chris

namespace boost {
namespace asio {
namespace detail {
struct buffer_size_helper {
    std::size_t operator()(std::size_t s, boost::asio::const_buffer
const& b) const {
        return s + boost::asio::buffer_size(b);
    }
    std::size_t operator()(std::size_t s, boost::asio::mutable_buffer
const& b) const {
        return s + boost::asio::buffer_size(b);
    }
};
}

std::size_t buffer_size(std::vector<boost::asio::const_buffer> const& v) {
    return std::accumulate(v.begin(), v.end(), std::size_t(0),
detail::buffer_size_helper());
}

template <int N>
std::size_t buffer_size(boost::array<boost::asio::const_buffer, N> const& v) {
    return std::accumulate(v.begin(), v.end(), std::size_t(0),
detail::buffer_size_helper());
}

std::size_t buffer_size(std::vector<boost::asio::mutable_buffer> const& v) {
    return std::accumulate(v.begin(), v.end(), std::size_t(0),
detail::buffer_size_helper());
}

template <int N>
std::size_t buffer_size(boost::array<boost::asio::mutable_buffer, N> const& v) {
    return std::accumulate(v.begin(), v.end(), std::size_t(0),
detail::buffer_size_helper());
}
}
}


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