OpenRTMFP/Cumulus Primer(10)IO 管理之流缓冲区
本文主要分析 MemoryStream.h 文件中定义的类。
首先要了解 streambuf 内置了一个 get 指针和一个 put 指针。streambuf 的所有操作基本都是对这两个指针的操作。其一些成员函数的缩写中的 g 和 p 就分别表示 get pointer 和 put pointer。
Increase get pointer: Advances the get pointer by n positions. The get pointer is the internal pointer that points to the next location in the controlled input sequence.
void gbump ( int n );Increase put pointer: Advances the put pointer by n positions. The put pointer is the internal pointer that points to the next location of the controlled output sequence.
void pbump ( int n );Pointer to current position of input sequence: Returns a reference to the current element of the controlled input sequence (i.e., the “get pointer”).
char * gptr ( ) const;Pointer to current position of output sequence: Returns a reference to the current element of the output sequence (the put pointer).
char * pptr ( ) const;Set input sequence pointers: Sets values for the pointers that define both the boundaries of the accessible part of the controlled input sequence and the get pointer itself.
void setg ( char* gbeg, char* gnext, char* gend );
- gbeg: New value for the pointer to the beginning of the accessible part of the controlled input sequence.
- gnext: New value for the get pointer, which points to the next element within the controlled input sequence where the next input operation shall be performed.
- gend: New value for the end pointer, just past the end of the accessible part of the controlled input sequence.
Set output sequence pointers: Sets the values that define the boundaries of the accessible part of the controlled output sequence.
void setp ( char* pbeg, char* pend );
- pbeg: New value for the pointer to the beginning of the accessible part of the controlled output sequence?and put pointer.
- pend: New value for the end pointer, just past the end of the accessible part of the controlled output sequence.
类定义:
class MemoryStreamBuf: public std::streambuf { friend class ScopedMemoryClip;public: MemoryStreamBuf(char* pBuffer,Poco::UInt32 bufferSize); MemoryStreamBuf(MemoryStreamBuf&); ~MemoryStreamBuf(); void next(Poco::UInt32 size); // Explaint below Poco::UInt32 written(); // Explaint below void written(Poco::UInt32 size); Poco::UInt32 size(); // Explaint below void resize(Poco::UInt32 newSize); // Explaint below char* begin(); // Explaint below void position(Poco::UInt32 pos=0); // Explaint below char* gCurrent(); // Explaint below char* pCurrent(); // Explaint belowprivate: virtual int overflow(int_type c); virtual int underflow(); virtual int sync(); Poco::UInt32 _written; char* _pBuffer; Poco::UInt32 _bufferSize; MemoryStreamBuf(); MemoryStreamBuf& operator = (const MemoryStreamBuf&);};ScopedMemoryClip 是 MemoryStreamBuf 的友元,其内部有 MemoryStreamBuf 的成员,这里暂且不管。构造函数传入的参数是缓冲区的地址和缓冲区大小(字节数)。拷贝构造函数和析构函数不必赘述。
put 和 get 指针都移动:
void MemoryStreamBuf::next(UInt32 size) { pbump(size); gbump(size);}封装 streambuf 的 gptr 和 pptr:
inline char* MemoryStreamBuf::gCurrent() { return gptr();}inline char* MemoryStreamBuf::pCurrent() { return pptr();}依赖于内置成员变量?pBuffer 和?bufferSize:
inline char* MemoryStreamBuf::begin() { return _pBuffer;}inline Poco::UInt32 MemoryStreamBuf::size() { return _bufferSize;}读取(其中也可能发生设置操作):
UInt32 MemoryStreamBuf::written() { int written = pCurrent() - begin(); // 已写字节数 if (written < 0) written = 0; if (written > _written) // 保存已写字节数 _written = (UInt32)written; return _written;}设置:
void MemoryStreamBuf::written(UInt32 size) { _written=size;}设定 put 和 get 指针为以缓冲区首地址为开始偏移量为 pos 的位置:
void MemoryStreamBuf::position(UInt32 pos) { // 保存已写字节数 written(); // Save nb char written // 移动 put 指针 setp(_pBuffer, _pBuffer + _bufferSize); if (pos > _bufferSize) pos = _bufferSize; pbump((int) pos); // 移动 get 指针 setg(_pBuffer, _pBuffer + pos, _pBuffer + _bufferSize);}void MemoryStreamBuf::resize(UInt32 newSize) { // 大小标识 _bufferSize = newSize; // gptr 当前位置 int pos = gCurrent() - _pBuffer; if (pos > _bufferSize) pos = _bufferSize; // 设置 gptr 可达范围和当前位置 setg(_pBuffer, _pBuffer + pos, _pBuffer + _bufferSize); // pptr 当前位置 pos = pCurrent() - _pBuffer; if (pos > _bufferSize) pos = _bufferSize; // 设置 pptr 可达范围和当前位置 setp(_pBuffer,_pBuffer + _bufferSize); pbump(pos);}构造函数会设定 pptr 和 gptr,并初始化?pBuffer 和?bufferSize
MemoryStreamBuf::MemoryStreamBuf(char* pBuffer, UInt32 bufferSize): _pBuffer(pBuffer),_bufferSize(bufferSize),_written(0) { setg(_pBuffer, _pBuffer,_pBuffer + _bufferSize); setp(_pBuffer, _pBuffer + _bufferSize);}析构函数会拷贝对方的?pBuffer、bufferSizse、_written,并设定 gptr、pptr。注意设定 pptr 时,要分别调用 setp 和 pbump,因为 setp 仅将 pptr 设定为传入的首个参数值(与可达范围的首地址相同)。
MemoryStreamBuf::MemoryStreamBuf(MemoryStreamBuf& other): _pBuffer(other._pBuffer),_bufferSize(other._bufferSize),_written(other._written) { setg(_pBuffer, other.gCurrent(), _pBuffer + _bufferSize); setp(_pBuffer, _pBuffer + _bufferSize); pbump((int)(other.pCurrent()-_pBuffer));}析构函数:
MemoryStreamBuf::~MemoryStreamBuf() {}-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant
-