std::basic_streambuf::setp(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::basic_streambuf::setp πŸ’…

okay so bestie, this page is from the C++ Standard Libary manual, and it's all about std::basic_streambuf::setp. It's a protected member function situation, very behind-the-scenes, very main-character-internals energy πŸ’–.


πŸ’– NAME πŸ’–

std::basic_streambuf::setp β€” std::basic_streambuf::setp, yes the name is literally the name, periodt πŸ’….


✨ Synopsis ✨

okay so it's protected:, and the signature is exactly this:

protected:
void setp( char_type* pbeg, char_type* pend );

It sets the values of the pointers that define the put area. Specifically, after the call:

pbase() == pbeg, pptr() == pbeg, epptr() == pend

Like, all three pointer vibes get updated in one go. Slay ✨.


πŸŽ€ Parameters πŸŽ€

  • pbeg β€” a pointer to the new beginning of the put area. The start of the era, basically 🫢.
  • pend β€” a pointer to the new end of the put area. The end of the era, the limit, the boundary πŸ¦‹.

🌸 Return value 🌸

Literally (none), bestie. No return value, nothing, nada, empty-handed but cute πŸ’€.


πŸ¦‹ Example πŸ¦‹

Run this code, it's giving custom stream buffer realness πŸ˜­πŸ’–:

// Run this code

#include <array>
#include <cstddef>
#include <iostream>

// Buffer for std::ostream implemented by std::array
template<std::size_t size, class CharT = char>
struct ArrayedStreamBuffer : std::basic_streambuf<CharT>
{
    using Base = std::basic_streambuf<CharT>;
    using char_type = typename Base::char_type;

    ArrayedStreamBuffer()
    {
        // put area pointers to work with 'buffer'
        Base::setp(buffer.data(), buffer.data() + size);
    }

    void print_buffer()
    {
        for (char_type i : buffer)
        {
            if (i == 0)
                std::cout << "\\0";
            else
                std::cout << i;
            std::cout << ' ';
        }
        std::cout << '\n';
    }

private:
    std::array<char_type, size> buffer{}; // value-initialize buffer
};

int main()
{
    ArrayedStreamBuffer<10> streambuf;
    std::ostream stream(&streambuf);

    stream << "hello";
    stream << ",";

    streambuf.print_buffer();
}

⭐ Output ⭐

h e l l o , \0 \0 \0 \0

look at those little \0 besties filling the buffer, iconic 🌈.


πŸ’œ See also πŸ’œ

  • setg β€” repositions the beginning, next, and end pointers of the input sequence (protected member function).

mwah πŸ’‹ β€” source/version: http://cppreference.com, date: 2024.06.10. bestie behavior only πŸ§šβ€β™€οΈβœ¨

πŸ“¦ where this page comes from