Dune::Copasi 2.1.0-git79717215+dune.gitlab.629933
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages Concepts
ostream_redirect.hh
Go to the documentation of this file.
1#ifndef DUNE_COPASI_COMMON_OSTREAM_REDIRECT_HH
2#define DUNE_COPASI_COMMON_OSTREAM_REDIRECT_HH
3
4#include <dune-copasi-config.hh>
5
6#include <function2/function2.hpp>
7
8#include <memory>
9#include <ostream>
10#include <sstream>
11
12namespace Dune::Copasi {
13
42class OStreamRedirect final : public std::stringbuf
43{
44
45 using Base = std::stringbuf;
46
48 OStreamRedirect(std::ostream& ostream,
49 fu2::unique_function<void(std::string_view)> redirection,
50 bool line_buffered);
51
52public:
53 ~OStreamRedirect() override;
54
65 [[nodiscard]] static std::unique_ptr<OStreamRedirect> make(
66 std::ostream& ostream,
67 fu2::unique_function<void(std::string_view)> redirection,
68 bool line_buffered = true);
69
70 OStreamRedirect(const OStreamRedirect& other) = delete;
71 OStreamRedirect& operator=(const OStreamRedirect& other) = delete;
72
73 OStreamRedirect(OStreamRedirect&& other) noexcept = delete;
74 OStreamRedirect& operator=(OStreamRedirect&& other) noexcept = delete;
75
76private:
77 int sync() override;
78 std::ostream* _ostream;
79 std::streambuf* _ostream_buffer;
80 fu2::unique_function<void(std::string_view)> _redirection;
81 bool _line_buffered;
82};
83
84} // namespace Dune::Copasi
85
86#endif // DUNE_COPASI_COMMON_OSTREAM_REDIRECT_HH
Helper class to redirect std::ostream to a functor.
Definition: ostream_redirect.hh:43
static std::unique_ptr< OStreamRedirect > make(std::ostream &ostream, fu2::unique_function< void(std::string_view)> redirection, bool line_buffered=true)
RAII safe constructor of the redirection object While the object is alive, the output of ostream will...
OStreamRedirect & operator=(const OStreamRedirect &other)=delete
OStreamRedirect & operator=(OStreamRedirect &&other) noexcept=delete
OStreamRedirect(OStreamRedirect &&other) noexcept=delete
OStreamRedirect(const OStreamRedirect &other)=delete
Definition: axis_names.hh:7