尝试从boost :: iostreams访问源设备



我编写了一个自定义源设备,该设备计数到目前为止读取字节:

class socket_stream_source : public boost::iostreams::source
{
public:
    int readSoFar=0;
    socket_stream_source(socket_ptr sock) : _sock(sock)
    {
    }
    std::streamsize read(char* s, std::streamsize n)
    {
        int readCount = _sock->read_some(boost::asio::buffer(s, n));
        readSoFar += readCount;
        return readCount;
    }
private:
    socket_ptr _sock;
};

我是这样使用的:

boost::iostreams::stream<socket_stream_source> in(sock);

如何访问我的读取变量?

或其他方法可以计算距离距离很远的字节?

只需使用boost :: iostreams提供的设备访问运算符:: stream,即。

T& operator*();
T* operator->();

在您的代码中,这足够了:

in->readSoFar;

最新更新