Difference between revisions of "cpp/io/basic stringbuf/overflow"
From cppreference.com
< cpp | io | basic stringbuf
m (typo in comment) |
m (tt -> lc.) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/io/basic_stringbuf/title | overflow }} | + | {{cpp/io/basic_stringbuf/title|overflow}} |
{{cpp/io/basic_stringbuf/navbar}} | {{cpp/io/basic_stringbuf/navbar}} | ||
− | {{ddcl |1= | + | {{ddcl|1= |
protected: | protected: | ||
− | virtual int_type overflow ( int_type c = Traits::eof() ); | + | virtual int_type overflow( int_type c = Traits::eof() ); |
}} | }} | ||
− | Appends the character {{ | + | Appends the character {{|c}} to the output character sequence. |
− | If {{ | + | If {{|c}} is the end-of-file indicator {{c|traits::eq_int_type(c, traits::eof()) == true}}, then there is no character to append. The function does nothing and returns an unspecified value other than {{c|traits::eof()}}. |
− | Otherwise, if the output sequence has a write position available or this function can successfully make a write position available, then calls {{c|sputc(c)}} and returns {{ | + | Otherwise, if the output sequence has a write position available or this function can successfully make a write position available, then calls {{c|sputc(c)}} and returns {{|c}}. |
− | This function can make a write position available if the stringbuf is open for output ({{c|mode & ios_base::out) ! | + | This function can make a write position available if the stringbufis open for output ({{c|mode & ios_base::out) != 0}}): in this case, it reallocates (or initially allocates) the buffer big enough to hold the entire current buffer plus at least one more character. If the stringbufis also open for input ({{c|(mode & ios_base::in) != 0}}, then {{tt|overflow}} also increases the size of the get area by moving {{|egptr()}} to point just past the new write position. |
===Parameters=== | ===Parameters=== | ||
− | + | ||
+ | |||
+ | |||
===Return value=== | ===Return value=== | ||
− | {{c|Traits::eof()}} to indicate failure, {{ | + | {{c|Traits::eof()}} to indicate failure, {{|c}} if the character {{|c}} was successfully appended, or some value other than {{c|Traits::eof()}} if called with {{c|Traits::eof()}} as the argument. |
===Notes=== | ===Notes=== | ||
− | This function is different from a typical {{tt|overflow()}} which moves the contents of the buffer to the associated character sequence because for a {{ | + | This function is different from a typical {{tt|overflow()}} which moves the contents of the buffer to the associated character sequence because for a {{|std::basic_stringbuf}}, the buffer and the associated sequence are one and the same. |
+ | |||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | | | |
− | + | |code= | |
#include <sstream> | #include <sstream> | ||
#include <iostream> | #include <iostream> | ||
Line 33: | Line 36: | ||
{ | { | ||
mybuf(const std::string& new_str, | mybuf(const std::string& new_str, | ||
− | std::ios_base::openmode which = std::ios_base::in{{!}}std::ios_base::out) | + | 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 | int_type overflow(int_type c = EOF) override | ||
{ | { | ||
Line 40: | Line 44: | ||
<< "Before: size of get area: " << egptr() - eback() << '\n' | << "Before: size of get area: " << egptr() - eback() << '\n' | ||
<< " size of put area: " << epptr() - pbase() << '\n'; | << " size of put area: " << epptr() - pbase() << '\n'; | ||
+ | |||
int_type ret = std::stringbuf::overflow(c); | int_type ret = std::stringbuf::overflow(c); | ||
+ | |||
std::cout << "After : size of get area: " << egptr() - eback() << '\n' | std::cout << "After : size of get area: " << egptr() - eback() << '\n' | ||
<< " size of put area: " << epptr() - pbase() << '\n'; | << " size of put area: " << epptr() - pbase() << '\n'; | ||
+ | |||
return ret; | return ret; | ||
} | } | ||
Line 54: | Line 61: | ||
stream << 1234; | stream << 1234; | ||
std::cout << sbuf.str() << '\n'; | std::cout << sbuf.str() << '\n'; | ||
− | + | ||
std::cout << "\nread-only stream:\n"; | std::cout << "\nread-only stream:\n"; | ||
mybuf ro_buf(" ", std::ios_base::in); // read-only stream | mybuf ro_buf(" ", std::ios_base::in); // read-only stream | ||
std::iostream ro_stream(&ro_buf); | std::iostream ro_stream(&ro_buf); | ||
ro_stream << 1234; | ro_stream << 1234; | ||
− | + | ||
std::cout << "\nwrite-only stream:\n"; | std::cout << "\nwrite-only stream:\n"; | ||
mybuf wr_buf(" ", std::ios_base::out); // write-only stream | mybuf wr_buf(" ", std::ios_base::out); // write-only stream | ||
Line 65: | Line 72: | ||
wr_stream << 1234; | wr_stream << 1234; | ||
} | } | ||
− | + | |p=true | |
− | + | |output= | |
read-write stream: | read-write stream: | ||
stringbuf::overflow('4') called | stringbuf::overflow('4') called | ||
Line 89: | Line 96: | ||
size of put area: 512 | size of put area: 512 | ||
}} | }} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/io/basic_streambuf/dsc overflow}} | + | {{dsc inc|cpp/io/basic_streambuf/dsc overflow}} |
− | {{dsc inc | cpp/io/basic_stringbuf/dsc underflow}} | + | {{dsc inc|cpp/io/basic_stringbuf/dsc underflow}} |
{{dsc end}} | {{dsc end}} | ||
− | + | deesfritjaptruzh | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |