现在的位置: 首页 > 综合 > 正文

OpenRTMFP/Cumulus Primer(11)IO 管理之IO流

2013年07月24日 ⁄ 综合 ⁄ 共 1981字 ⁄ 字号 评论关闭


OpenRTMFP/Cumulus Primer(11)IO管理之IO流

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Blog: Blog.CSDN.net/Poechant
  • Date: April 24th, 2012

本文主要分析 MemoryStream.h 文件中定义的类。

1 了解 std::ios

  • 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()

2 MemoryIOS

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;
};

2.1 构造函数、拷贝构造函数和析构函数

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() {
}

2.2 得到 MemoryStreamBuf 成员的地址

inline MemoryStreamBuf* MemoryIOS::rdbuf() {
    return &_buf;
}

2.3 当前位置

这是一个纯虚函数,由 MemoryInputStream 和 MemoryOutpuStream 继承时实现:

virtual char*   current()=0;

2.4 封装 MemoryStreamBuf 成员的一些函数

  • 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();
      }
    

2.5 缓冲区可读数据的字节数

UInt32 MemoryIOS::available() {
    int result = rdbuf()->size() - (current() - begin()); // 缓冲区剩余可读数据字节数
    if (result < 0)
        return 0;
    return (UInt32)result;
}

Reference

  1. http://www.cplusplus.com/reference/iostream/ios/init/

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant

-

抱歉!评论已关闭.