I have a function that uses progress_display to show a loading bar. I would like to be able to be able to disable the output for testing. The loading bar prints the second it is inititialized, and if I initialize it in an if statement, I get an error from calling ++loading_bar; from another if statement.
I was wondering if I could disable it by initializing it with my own stream that does nothing, but I have no idea how I would make this.
#include <boost/progress.hpp>
int myfuntion(bool silent)
{
int computations = 100;
boost::progress_display loading_bar(computations);
++loading_bar;
}
You must pass an object derived from std::ostream in the constructor and the progress_display stores a reference, so it is not possible to change it afterwards.
As you say, you can create your own object that controls the output, but it must comply:
std::ostream. progress_display.You may find more information here about the derivation from std::ostream. Based on the link, you may define the following struct;
struct Output : std::ostream, std::streambuf
{
Output(bool enabled) : std::ostream(this), m_enabled(enabled) {}
int overflow(int c) {
if(m_enabled) std::cout.put(c);
return 0;
}
bool m_enabled;
};
Now, this struct can enable/disable the output from its constructor if you declare the progress_display as:
// Visible progress
Output out_enabled(true);
boost::progress_display loading_bar_visible(computations, out_enabled);
// Invisible progress
Output out_disabled(false);
boost::progress_display loading_bar_invisible(computations, out_disabled);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With