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

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 functionvalue
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

-

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值