欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenRTMFP/Cumulus Primer(12)IO管理之IO流(续)

程序员文章站 2022-05-13 16:25:26
...

OpenRTMFP/Cumulus Primer(12)IO管理之IO流(续)

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

0 导语

本文所介绍的 CumulusServer 中用到的 IO 流,是基于 CumulusServer 中的 MemoryIOS 的,可以先阅读《OpenRTMFP/Cumulus Primer(11)IO 管理之IO流》一文,然后再阅读本文。

1 输入流

class MemoryInputStream: public MemoryIOS, public std::istream
{
public:
    MemoryInputStream(const char* pBuffer,Poco::UInt32 bufferSize);
        /// Creates a MemoryInputStream for the given memory area,
        /// ready for reading.
    MemoryInputStream(MemoryInputStream&);
    ~MemoryInputStream();
        /// Destroys the MemoryInputStream.
    char*           current();
};

构造函数、拷贝构造函数和析构函数也都没什么可说的,初始化 MemoryIOS 以及 istream。istream 是 iostream 中的 basic_istream 别名(typedef)。

MemoryInputStream::MemoryInputStream(const char* pBuffer, UInt32 bufferSize): 
    MemoryIOS(const_cast<char*>(pBuffer), bufferSize), istream(rdbuf()) {
}

MemoryInputStream::MemoryInputStream(MemoryInputStream& other):
    MemoryIOS(other), istream(rdbuf()) {
}

MemoryInputStream::~MemoryInputStream() {
}

唯一的一个成员函数是 current,封装了 MemoryIOS 的 MemoryStreamBuf 成员的 gCurrent 函数:

inline char* MemoryInputStream::current() {
    return rdbuf()->gCurrent();
}

2 输出流

class MemoryOutputStream: public MemoryIOS, public std::ostream
    /// An input stream for reading from a memory area.
{
public:
    MemoryOutputStream(char* pBuffer,Poco::UInt32 bufferSize);
        /// Creates a MemoryOutputStream for the given memory area,
        /// ready for writing.
    MemoryOutputStream(MemoryOutputStream&);
    ~MemoryOutputStream();
        /// Destroys the MemoryInputStream.

    Poco::UInt32    written();
    void            written(Poco::UInt32 size);
    char*           current();
};

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

如下,不赘述了。

MemoryOutputStream::MemoryOutputStream(char* pBuffer, UInt32 bufferSize): 
    MemoryIOS(pBuffer, bufferSize), ostream(rdbuf()) {
}
MemoryOutputStream::MemoryOutputStream(MemoryOutputStream& other):
    MemoryIOS(other), ostream(rdbuf()) {
}

MemoryOutputStream::~MemoryOutputStream(){
}

2.2 读取和设定已写字节数

读取:

inline Poco::UInt32 MemoryOutputStream::written() {
    return rdbuf()->written();
}

设定:

inline void MemoryOutputStream::written(Poco::UInt32 size) {
    rdbuf()->written(size);
}

2.3 当前位置

与 MemoryInputStream 中的封装类似:

inline char* MemoryOutputStream::current() {
    return rdbuf()->pCurrent();
}

-

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

-

相关标签: OpenRTMFP/Cumulus