BioCMAMC-ST
stream_io.hpp
1#ifndef __STREAM_IO_HPP__
2#define __STREAM_IO_HPP__
3
4#include <biocma_cst_config.hpp> // IWYU pragma: keep
5#include <sstream>
6#include <streambuf>
21int redirect_stdout(std::streambuf*& original_buffer, std::stringstream& variable_stream);
22
34void restore_stdout(int original_stdout, std::streambuf*& original_buffer);
35
36// // C Style Macro to hide implementation detail in Main
37// // If redirect, capture the stdout during block execution in a buffer and print
38// // it at the end of the block
39#define REDIRECT_SCOPE(__block__) /*NOLINT*/ \
40 if constexpr ((AutoGenerated::FlagCompileTime::__f__redirect__)) \
41 { \
42 std::stringstream output_variable; \
43 output_variable.str(""); \
44 std::streambuf* original_buffer = nullptr; \
45 auto original_stdout_fd = redirect_stdout(original_buffer, output_variable); \
46 try \
47 { \
48 __block__; \
49 } \
50 catch (...) \
51 { \
52 restore_stdout(original_stdout_fd, original_buffer); \
53 throw; \
54 } \
55 restore_stdout(original_stdout_fd, original_buffer); \
56 if constexpr ((AutoGenerated::FlagCompileTime::verbose)) \
57 { \
58 std::cout << output_variable.str() << std::endl; \
59 } \
60 } \
61 else \
62 { \
63 __block__; \
64 }
65
66#endif