【发布时间】:2017-06-01 14:31:11
【问题描述】:
我想使用 std::istream 从只提供 2 种方法的给定类中读取数据:
// Returns a byte from the stream (consuming it)
uint8_t getChar(OwnIOStream stream);
// Makes the passed pointer point to the data in the stream
bool getCharBlockPtr(OwnIOStream stream, uint8_t** buffer, uin32_t maxSize, uint32_t* size);
我首先想到的是继承stream_buf,使用getChar()方法实现underflow方法。但是我想使用 getCharBlockPtr() 来避免数据副本(我假设为每个读取字节调用下溢会降低性能)。问题是我需要提前知道我每次要读取的字节数。这就是为什么我在考虑是否可以覆盖 istream 的读取方法。
使用 boost 不是一种选择。
【问题讨论】:
-
是否可以在
streambuf实现中使用内部缓冲区并在underflow()中使用getCharBlockPtr填充它? -
不,我想避免数据副本。我需要直接使用 getCharBlockPtr 提供的缓冲区(即将内容直接放入文件中,无需额外副本)。
标签: c++