名前空間
変種
操作

std::basic_stringbuf<CharT,Traits,Allocator>::overflow

提供: cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
protected:
virtual int_type overflow ( int_type c = Traits::eof() );

文字 c を出力文字シーケンスに追加します。

c がファイル終端指示子 (traits::eq_int_type(c,traits::eof()) == true) の場合は、追加される文字はありません。 この関数は何もせず、 traits::eof() 以外の未規定の値を返します。

そうでなく、出力シーケンスに利用可能な書き込み位置があるか、この関数が利用可能な書き込み位置を作ることに成功した場合は、 sputc(c) を呼び、 c を返します。

stringbuf が出力用に開かれている (mode & ios_base::out) != 0) 場合、この関数は利用可能な書き込み位置を作ることができます。 この場合、現在のバッファ全体プラス少なくとももう1文字を保持するのに十分大きなバッファを再確保 (または初期確保) します。 stringbuf が入力用にも開かれている ((mode & ios_base::in) != 0) 場合、 overflow は新しい書き込み位置の次を指すように egptr() を移動することによって get 領域のサイズも増加させます。

目次

引数

c - put 領域に格納する文字

戻り値

失��した場合は Traits::eof()、文字 c の追加に成功した場合は c、引数として Traits::eof() が渡された場合は Traits::eof() 以外の何らかの値。

ノート

std::basic_stringbuf の場合は、バッファと紐付けられているシーケンスはひとつであり同じであるため、この関数はバッファの内容を紐付けられている文字シーケンスに移動する一般的な overflow() とは異なります。

この例を実行するための使用した処理系では、 overflow() は put 領域を512バイトに過剰確保します。 str() の呼び出しはその初期化済みの4バイトのみを返しますが、次の 508 回の sputc() の呼び出しは新たな overflow() の呼び出しを要求しません。

#include <sstream>
#include <iostream>
 
struct mybuf : std::stringbuf
{
    mybuf(const std::string& new_str,
          std::ios_base::openmode which = std::ios_base::in|std::ios_base::out)
           : std::stringbuf(new_str, which) {}
    int_type overflow(int_type c = EOF) override
    {
        std::cout << "stringbuf::overflow('" << char(c) << "') called\n"
                  << "Before: size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        int_type ret = std::stringbuf::overflow(c);
        std::cout << "After : size of get area: " << egptr() - eback() << '\n'
                  << "        size of put area: " << epptr() - pbase() << '\n';
        return ret;
    }
};
 
int main()
{
    std::cout << "read-write stream:\n";
    mybuf sbuf("   "); // read-write stream
    std::iostream stream(&sbuf);
    stream << 1234;
    std::cout << sbuf.str() << '\n';
 
    std::cout << "\nread-only stream:\n";
    mybuf ro_buf("   ", std::ios_base::in); // read-only stream
    std::iostream ro_stream(&ro_buf);
    ro_stream << 1234;
 
    std::cout << "\nwrite-only stream:\n";
    mybuf wr_buf("   ", std::ios_base::out); // write-only stream
    std::iostream wr_stream(&wr_buf);
    wr_stream << 1234;
}

出力例:

read-write stream:
stringbuf::overflow('4') called
Before: size of get area: 3
        size of put area: 3
After : size of get area: 4
        size of put area: 512
1234
 
read-only stream:
stringbuf::overflow('1') called
Before: size of get area: 3
        size of put area: 0
After : size of get area: 3
        size of put area: 0
 
write-only stream:
stringbuf::overflow('4') called
Before: size of get area: 0
        size of put area: 3
After : size of get area: 0
        size of put area: 512

関連項目

[仮想]
put 領域から紐付けられている出力シーケンスに文字を書き込みます
(std::basic_streambuf<CharT,Traits>の仮想プロテクテッドメンバ関数) [edit]
[仮想]
入力シーケンスの次の利用可能な文字を返します
(仮想プロテクテッドメンバ関数) [edit]