Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a null stream? optional printing

I use functions like

void doStuff(type thing, bool print = false, std::ostream& S = std::cout)
{
    thing++;
    if(print)
        S << "new thing: " << thing << '\n';
}

so that I can use the same function and decide on call if I want it to print documentation of what is happening and if I wish I can print that on seperate streams -I don't know if I can even do that with std::ostream-

I now believe that it will be better to do

void doStuff(type thing, std::ostream& S = NULL)
{
    thing++;
    if(S)
        S << "new thing: " << thing << '\n';
}

but that doesn't work as std::ostream doesn't accept NULL

questions:
-is there some kind of constant of the stream type that stops the if condition?
-can I use a different type of stream that is more flexible to accept streams like string streams and file streams?
-is there a better way to handle flexible documentation?

like image 275
ZKlack Avatar asked Nov 19 '25 16:11

ZKlack


2 Answers

You can use the Null sink of the boost::iostream library.

Here is a working example:

#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/null.hpp>

boost::iostreams::stream<boost::iostreams::null_sink> nullstream{boost::iostreams::null_sink()};

void print_to_stream(std::ostream& s = nullstream)
{
  s << "hello" << std::endl;
}

int main()
{
  std::cout << "calling print_to_stream with s = std::cout" << std::endl;
  print_to_stream(std::cout);
  std::cout << "calling print_to_stream without argument" << std::endl;
  print_to_stream();

  return 0;
}

You may want to hide the variable nullstream inside some namespace or classs.

like image 56
francesco Avatar answered Nov 21 '25 06:11

francesco


Writing own streams is not difficult and can be found in good books.

I created a "NULL" stream class for you.

Please see the very simple example below.

#include <iostream>

// This is a stream which does not output anything
class NullStream : public std::ostream
{
    // streambuffer doing nothing
    class NullBuffer : public std::streambuf
    {
    public:
        int overflow(int c) noexcept override { return c; }
    } nullBuffer;

public:
#pragma warning(suppress: 26455)
    NullStream() : std::ostream(&nullBuffer) {}
    NullStream(const NullStream&) = delete;
    NullStream& operator=(const NullStream&) = delete;
};


// Define a global null stream
NullStream nout;

void doStuff(int& i, std::ostream& stream = nout)
{
    i++;
    stream << i << '\n';
}

int main() {

    int i{};
    doStuff(i, std::cout);
    doStuff(i, std::cout);
    doStuff(i, std::cout);
    doStuff(i);
    doStuff(i);
    doStuff(i);
    doStuff(i, std::cout);
}
like image 20
Armin Montigny Avatar answered Nov 21 '25 06:11

Armin Montigny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!