Namespaces
Variants
Views
Actions

Talk:cpp/io/basic streambuf/overflow

From cppreference.com

Maybe it would be useful to put links what is:

put area

output sequence

output area

Thank you

vlakov--193.58.194.195 21:20, 23 July 2014 (PDT)

as in, additional links back to basic_streambuf? --Cubbi (talk) 03:26, 24 July 2014 (PDT)
I am very sorry. I just googled: "output area" "output sequence" "put area" and it resulted to this page (overflow). :-). In future I will try to search a little more. So as I understand it all the 3 terms are designating the one "put area". I wanted to look a little closer to the overflow as in the new Stroustrup's book the overflow is mentioned only/touched. Thank you. vlakov--88.212.37.48 05:16, 24 July 2014 (PDT)
No, there are 2 different things: put area (in-memory buffer), and output sequence (file, stdout, socket, etc). overflow() moves data from the put area into the output sequence. (updated the page because 'output area' was redundant) As for books, as this is a library component, Josuttis may be more appropriate --Cubbi (talk) 06:35, 24 July 2014 (PDT)
You are very helpful, thank you. I admit I was confused because on the page basic_streambuf is literally this text:
output sequence (also called put area)
input sequence (also called get area)
vlakov--193.58.194.195 07:39, 24 July 2014 (PDT)
indeed, this needs to be further reviewed for consistency. --Cubbi (talk) 08:49, 24 July 2014 (PDT)

[edit] Improve example

Hi,

I would like to recommend extending the example to showcase pbase, pptr.

Only 2 additional functions: write_out and operator<<

(Would also be a nice example for here: https://en.cppreference.com/w/cpp/io/basic_streambuf/pptr#Example )

#include <iostream>
#include <array>
 
// Buffer for std::ostream implemented by std::array
template<std::size_t SIZE, class CharT = char>
class ArrayedStreamBuffer : public std::basic_streambuf<CharT> {
public:
 
    using Base = std::basic_streambuf<CharT>;
    using char_type = typename Base::char_type;
    using int_type = typename Base::int_type;
 
    ArrayedStreamBuffer() : buffer_{} // value-initialize buffer_ to all zeroes
    {
        Base::setp(buffer_.begin(), buffer_.end()); // set std::basic_streambuf
            // put area pointers to work with 'buffer_'
    }
 
    int_type overflow(int_type ch) 
    {
        std::cout << "overflow\n";
        return Base::overflow(ch);
    }
 
    void print_buffer()
    {
        for (const auto& i: buffer_) {
            if (i == 0) {
                std::cout << "NULL";
            } else {
                std::cout << i;
            }
            std::cout << " ";
        }
        std::cout << "\n";
    }
 
    std::ostream &write_out(std::ostream &os) const
    {
        os.write(Base::pbase(), Base::pptr() - Base::pbase());
        return os;
    }
 
private:
    std::array<char_type, SIZE> buffer_;
};
 
template<std::size_t SIZE, class CharT = char>
inline std::ostream &operator<<(std::ostream &os, const ArrayedStreamBuffer<SIZE, CharT> &stream)
{
    return stream.write_out(os);
}
 
int main()
{
    ArrayedStreamBuffer<10> streambuf;
    std::ostream stream(&streambuf);
 
    stream << "hello";
    streambuf.print_buffer();
    std::cout << streambuf << std::endl;
    if (stream.good()) {
        std::cout << "stream is good\n\n";
    }
 
    stream << "world";
    streambuf.print_buffer();
    std::cout << streambuf << std::endl;
    if (stream.good()) {
        std::cout << "stream is good\n\n";
    }
 
    stream << "!";
    streambuf.print_buffer();
    std::cout << streambuf << std::endl;
    if (!stream.good()) {
        std::cout << "stream is not good\n\n";
    }
}

Output:

h e l l o NULL NULL NULL NULL NULL 
hello
stream is good
 
h e l l o w o r l d 
helloworld
stream is good
 
overflow
h e l l o w o r l d 
helloworld
stream is not good

Zella (talk)