BoB robotics
The Brains on Board robotics library
pybind11::scoped_ostream_redirect Class Reference

#include <iostream.h>

Inheritance diagram for pybind11::scoped_ostream_redirect:
pybind11::scoped_estream_redirect

Public Member Functions

 scoped_ostream_redirect (std::ostream &costream=std::cout, const object &pyostream=module_::import("sys").attr("stdout"))
 
 scoped_ostream_redirect (const scoped_ostream_redirect &)=delete
 
 scoped_ostream_redirect (scoped_ostream_redirect &&other)=default
 
scoped_ostream_redirectoperator= (const scoped_ostream_redirect &)=delete
 
scoped_ostream_redirectoperator= (scoped_ostream_redirect &&)=delete
 

Protected Attributes

std::streambuf * old
 
std::ostream & costream
 
detail::pythonbuf buffer
 

Detailed Description

\rst This a move-only guard that redirects output.

.. code-block:: cpp

#include <pybind11/iostream.h>

...

{
    py::scoped_ostream_redirect output;
    std::cout << "Hello, World!"; // Python stdout
} // <-- return std::cout to normal

You can explicitly pass the c++ stream and the python object, for example to guard stderr instead.

.. code-block:: cpp

{
    py::scoped_ostream_redirect output{std::cerr, py::module::import("sys").attr("stderr")};
    std::cout << "Hello, World!";
}

\endrst


The documentation for this class was generated from the following file: