简体   繁体   中英

Trying to access source device from boost::iostreams

I wrote a custom source device that counts the bytes read so far:

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;

};

I'm using it like this:

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

How can I access my readSoFar variable ?

Or is there another way to count the bytes read so far from an istream ?

Just use the device access operators provided by boost::iostreams::stream, ie

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

In your code this suffice:

in->readSoFar;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM