Perform output if stream is good

W

William Payne

Hi, I have a function declared as:

void foo(const std::string& s, std::eek:stream& verbose_output);

I want foo() to write a lot of data to the ostream if it's a valid stream.
If it's valid or not should depend on user input (command line arguments
actually). If the user decides he/she wants verbose output, I will pass
std::cout as the last argument when calling foo(). But what should I pass if
the user doesn't want any output? And how should foo() check if the stream
is good? verbose_output.good()?.

Thanks for any replies

/ WP
 
V

Victor Bazarov

William Payne said:
void foo(const std::string& s, std::eek:stream& verbose_output);

I want foo() to write a lot of data to the ostream if it's a valid stream.
If it's valid or not should depend on user input (command line arguments
actually). If the user decides he/she wants verbose output, I will pass
std::cout as the last argument when calling foo(). But what should I pass
if the user doesn't want any output? And how should foo() check if the
stream is good? verbose_output.good()?.

Yes...

Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.

The problem is that if you have a reference to a stream, it must be some
real stream, otherwise where did you get a reference to it. Of course,
you could use 'ostringstream' instead, but I am not sure what you make it
"bad"...

Victor
 
W

William Payne

Victor Bazarov said:
Yes...

Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.

The problem is that if you have a reference to a stream, it must be some
real stream, otherwise where did you get a reference to it. Of course,
you could use 'ostringstream' instead, but I am not sure what you make it
"bad"...

Victor

Thanks for your reply, Victor. I think I will just add a third parameter to
foo(), a boolean telling it if to use the stream or not.

/ WP
 
P

Pete Becker

William said:
Hi, I have a function declared as:

void foo(const std::string& s, std::eek:stream& verbose_output);

I want foo() to write a lot of data to the ostream if it's a valid stream.

If the stream isn't valid writes to it do nothing. So all you need to do
is set badbit.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Victor said:
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.

I have this tiny class that can be useful:

//*********************************************************
// Null ostream class.
//*********************************************************

class Nullbuff : public std::streambuf
{
public:
Nullbuff ()
{
setbuf (0, 0);
}
protected:
int overflow (int)
{
setp (pbase (), epptr () );
return 0;
}
int sync ()
{
return 0;
}
};

class Nullostream : public std::eek:stream
{
public:
Nullostream () :
std::eek:stream (& buff)
{ }
private:
Nullbuff buff;
};
 
D

David Hilsee

William Payne said:
Thanks for your reply, Victor. I think I will just add a third parameter to
foo(), a boolean telling it if to use the stream or not.

Pointers are typically used for "optional" parameters. If you use a pointer
instead of a reference, you can pass a null pointer when you don't want the
output, and check to see if the pointer is null instead of using a separate
boolean value. I think most programmers would find that approach more
appealing. I'm assuming that foo() will completely ignore the stream when
the boolean flag indicates that it should, of course.
 
R

Ron Natalie

Victor Bazarov said:
Invent your own stream with '/dev/null' buffer which will always be valid
and pass when you don't need verbosity.

You can just write your own streambuf class that just has no-op put functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top