std::basic_stringbuf<CharT,Traits,Allocator>::overflow
| 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 を返します。
この関数は、std::stringbuf が出力用に開かれている場合 ((mode & ios_base::out) != 0) に書き込み位置を確保することができます。この場合、バッファ全体に加えて少なくとも1文字以上を格納できるようにバッファを再割り当て(または最初に割り当て)します。std::stringbuf が入力用にも開かれている場合 ((mode & ios_base::in) != 0) は、overflow は新しい書き込み位置の直後を指すように egptr() を移動させることで、取得領域のサイズも増やします。
目次 |
[edit] パラメータ
| c | - | 出力領域に格納する文字 |
[edit] 戻り値
失敗を示す場合は Traits::eof()、文字 c が正常に追加された場合は c、引数として Traits::eof() が指定された場合は Traits::eof() 以外の値。
[edit] 注釈
この関数は、バッファの内容を関連する文字シーケンスに移動させる典型的な overflow() とは異なります。なぜなら std::basic_stringbuf の場合、バッファと関連するシーケンスは同一であるためです。
[edit] 例
この例を実行するために使用される実装(例: GCC-4.9)では、overflow() はプット領域を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[edit] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 169 | C++98 | (再)割り当てられたバッファは追加の1文字しか保持できなかった | より多くの追加文字を許可 |
| LWG 432 | C++98 | overflow は、std::stringbuf が入力用に開かれている場合、新しい書き込み位置の直後を指すように epptr() を移動させた入力用に開かれている場合 |
移動されない |
[edit] 関連項目
| [virtual] |
関連付けられた出力シーケンスに、配置領域から文字を書き込む ( std::basic_streambuf<CharT,Traits> の仮想 protected メンバ関数) |
| [virtual] |
入力シーケンスで利用可能な次の文字を返す (virtual protected member function) |