本文主要分析 MemoryStream.h 文件中定义的类。
Initialize object [protected]: This protected member initializes the values of the stream’s internal flags and member variables.
void init ( streambuf* sb );
初始化后如下函数的返回值:
member function | value |
---|---|
rdbuf() | sb |
tie() | 0 |
rdstate() | goodbit if sb is not a null pointer, badbit otherwise |
exceptions() | goodbit |
flags() | skipws | dec |
width() | 0 |
precision() | 6 |
fill() | ‘ ’ (whitespace) |
getloc() | a copy of locale() |
MemoryIOS 封装 MemoryStreamBuf,且是 MemoryInputStream 和 MemoryOutputStream 的基类,用以确保流缓冲区和基类的初始化序列的正确性。该类继承自 std::ios。
class MemoryIOS: public virtual std::ios
{
public:
MemoryIOS(char* pBuffer,Poco::UInt32 bufferSize);
MemoryIOS(MemoryIOS&);
~MemoryIOS();
MemoryStreamBuf* rdbuf();
virtual char* current()=0;
void reset(Poco::UInt32 newPos);
void resize(Poco::UInt32 newSize);
char* begin();
void next(Poco::UInt32 size);
Poco::UInt32 available();
private:
MemoryStreamBuf _buf;
};
MemoryIOS::MemoryIOS(char* pBuffer, UInt32 bufferSize):_buf(pBuffer, bufferSize) {
poco_ios_init(&_buf);
}
poco_ios_init 为 init 的宏定义,用于初始化成员 _buf。
MemoryIOS::MemoryIOS(MemoryIOS& other):_buf(other._buf) {
poco_ios_init(&_buf);
}
拷贝构造函数同构造函数。如下的析构函数不必赘述:
MemoryIOS::~MemoryIOS() {
}
inline MemoryStreamBuf* MemoryIOS::rdbuf() {
return &_buf;
}
这是一个纯虚函数,由 MemoryInputStream 和 MemoryOutpuStream 继承时实现:
virtual char* current()=0;
begin
inline char* MemoryIOS::begin() {
return rdbuf()->begin();
}
resize
inline void MemoryIOS::resize(Poco::UInt32 newSize) {
rdbuf()->resize(newSize);
}
next
inline void MemoryIOS::next(Poco::UInt32 size) {
rdbuf()->next(size);
}
position 封装为 reset
void MemoryIOS::reset(UInt32 newPos) {
if(newPos>=0)
rdbuf()->position(newPos);
clear();
}
UInt32 MemoryIOS::available() {
int result = rdbuf()->size() - (current() - begin()); // 缓冲区剩余可读数据字节数
if (result < 0)
return 0;
return (UInt32)result;
}
-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant
-