STL之自定义缓冲区
簡(jiǎn)介
流緩沖區(qū)是一種I/O緩沖區(qū),接口由basic_streambuf定義。針對(duì)字符型別char和wchar,標(biāo)準(zhǔn)程序庫(kù)分別提供預(yù)先定義好的流緩沖區(qū)(streambuf)和寬字符流緩沖區(qū)(wstreambuf)。尤其是在特殊通道上,各類(lèi)可以當(dāng)做基類(lèi)。
- 函數(shù)eback(),gptr()和egptr()構(gòu)成了read(input)緩沖區(qū)的界面。
- 函數(shù)pbase(),pptr(),epptr()構(gòu)成了write(output)緩沖區(qū)的界面。
輸出緩沖區(qū)
對(duì)于程序員和程序開(kāi)發(fā)者來(lái)說(shuō),類(lèi)basic_streambuf僅僅是發(fā)送(sent)和提取(extracted)字符的地方。通常有兩個(gè)公共函數(shù),用于寫(xiě)入字符。
int streambuf::sputc( int nCh );int streambuf::sputn( const char* pch, int nCount );
函數(shù)sputc發(fā)生錯(cuò)誤時(shí),會(huì)返回traits_type::eof()。
成員函數(shù)sputc()可用來(lái)向緩沖區(qū)中寫(xiě)入一個(gè)字符,如果當(dāng)時(shí)有一個(gè)空的改寫(xiě)位置,字符就會(huì)被復(fù)制到該位置上。之后,指向當(dāng)前改寫(xiě)位置的那個(gè)指針就會(huì)加1。如果緩沖區(qū)是空的,就調(diào)用虛函數(shù)overflow()將output緩沖區(qū)的內(nèi)容發(fā)送至對(duì)應(yīng)的輸出管道中。
所以,通過(guò)重載overflow()函數(shù)可以自定義streambuffer。
#include <IOSTREAM> #include <streambuf> #include <LOCALE> #include <CSTDIO>using namespace std;class outbuf : public std::streambuf { protected:virtual int_type overflow(int_type c){if(c != EOF){c = std::toupper(c ,getloc());if(putchar(c) == EOF){return EOF;}}return c;} };void main() {outbuf ob;std::ostream out(&ob);int num = 56;out << "56 十六進(jìn)制數(shù)值: " << std::hex << std::showbase << num << endl; } 上面是一個(gè)沒(méi)有緩沖區(qū)的demo。因?yàn)榫彌_區(qū)為空,每輸出一個(gè)字符都會(huì)調(diào)用一次overflow()函數(shù),在overflow中直接將字符輸出。 #include <iostream> #include <io.h> #include <streambuf> #include <cstdio> using namespace std;static const int bufferSize = 10;class outbuf : public std::streambuf { protected:char buffer[bufferSize];//定義一個(gè)數(shù)組表示緩沖區(qū)。 public:outbuf(){setp(buffer ,buffer + bufferSize - 1);//設(shè)置streambuf對(duì)象的緩沖區(qū);}virtual ~outbuf(){sync();//程序退出時(shí),將數(shù)組中的殘余數(shù)據(jù)輸出至外部設(shè)備。}protected:int flushBuffer(){int num = pptr() - pbase();if(write(1 ,buffer , num) != num)//輸出。{return EOF;}pbump(-num);//將當(dāng)前寫(xiě)指針清空,指向緩沖區(qū)的開(kāi)始位置。return num;}virtual int_type overflow(int_type c){/*注意setp(buffer ,buffer + bufferSize - 1),所以pptr()==epptr()的時(shí)候,(也就是調(diào)用underflow的時(shí)候),pptr()指向緩沖區(qū)的最后一個(gè)位置(數(shù)組的最后一個(gè)位置)。所以pptr()指向有效內(nèi)存。*/if(c != EOF){*pptr() = c;pbump(1);//使當(dāng)前位置指向數(shù)組最后一個(gè)元素之后。}if(flushBuffer() == EOF){return EOF;}return c;}virtual int sync(){if(flushBuffer() == EOF){return -1;}return 0;} };class fdostream : public std::ostream { protected:outbuf buf; public:fdostream(int fd):ostream(0){rdbuf(&buf);//設(shè)置streambuf對(duì)象。} };void main() {fdostream out(1);out << "51 hexadecimal: " << std::hex << std::showbase << 51 << endl; } 輸入緩沖區(qū)函數(shù)sgetc()和sbumpc()可以從streambuf中讀取一個(gè)字符,不同之處是sbumpc()會(huì)使當(dāng)前指針后移,而sgetc()僅返回當(dāng)前字符。
如果緩沖區(qū)為空,就沒(méi)有可用字符了。緩沖區(qū)必須重新補(bǔ)給。如果沒(méi)有可用字符,函數(shù)sbumpc()會(huì)調(diào)用虛函數(shù)uflow(),而uflow()的默認(rèn)行為是調(diào)用underflow(),移動(dòng)“讀取指針”。
需要說(shuō)明一下的時(shí),eback()返回緩沖區(qū)中有效數(shù)據(jù)的起始位置。epptr()返回緩沖區(qū)有效數(shù)據(jù)末端位置。pptr()返回當(dāng)前讀取位置。這里需要注意的是,eback()并不一定等于pptr(),因?yàn)闉榱酥С只赝?#xff0c;eback()和pptr()之間存儲(chǔ)已經(jīng)讀取過(guò)的字符。而epptr()并不一定是緩沖區(qū)數(shù)組的最后一個(gè)元素,因?yàn)榭赡軟](méi)有從輸入設(shè)備讀取這么多的數(shù)據(jù)。
在給出demo之前,由于輸入緩沖區(qū)支持回退,也就是讀取指針可以左移,讀取之前讀取過(guò)的字符。所以,在重寫(xiě)函數(shù)underflow自定義自己的streambuf時(shí),也需要支持這種特性。
#include <iostream> #include <streambuf> #include <cstring> #include <io.h> using namespace std;static const int bufferSize = 10; static const int maxBackNum = 4;class inbuf : public std::streambuf { protected:char buffer[bufferSize]; public:inbuf(){setg(buffer + maxBackNum ,buffer + maxBackNum ,buffer + maxBackNum);} protected:virtual int_type underflow(){if(gptr() < egptr()){return *gptr();}int numputback;numputback = gptr() - eback();if(numputback > maxBackNum)numputback = maxBackNum;memcpy(buffer + maxBackNum - numputback ,gptr() - numputback ,numputback);int num ;num = read(0 ,buffer + maxBackNum ,bufferSize - maxBackNum);if(num < 0)return EOF;setg(buffer + (maxBackNum - numputback) ,buffer + maxBackNum ,buffer + maxBackNum + num);return *gptr();} };void main() {inbuf ib;std::istream in(&ib);char c;for(int i = 1 ;i <= 20 ;i++){in.get(c);cout << c << flush;if(i == 8){in.unget();in.unget();}}cout << endl; }
總結(jié)
以上是生活随笔為你收集整理的STL之自定义缓冲区的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Unity3D资源管理架构
- 下一篇: 轻触开关实现自锁功能