Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is there a null ostream (like /dev/null) in cpp? (http://www.velocityreviews.com/forums/t284398-is-there-a-null-ostream-like-dev-null-in-cpp.html)

Bo Peng 07-16-2004 02:48 AM

Is there a null ostream (like /dev/null) in cpp?
 
Dear list,

I need to add "disable output" feature to a bunch of objects that output
to ostream (or ofstream). The least intrusive way to do this is pass
them ofstream("/dev/null") but this makes my program less portable. Is
there a null ostream built in cpp? Is it easy to implement one?

Thanks.
Bo

Jonathan Turkanis 07-16-2004 03:31 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 

"Bo Peng" <bpeng@rice.edu> wrote in message
news:cd7flb$4tn$1@joe.rice.edu...
> Dear list,
>
> I need to add "disable output" feature to a bunch of objects that

output
> to ostream (or ofstream). The least intrusive way to do this is pass
> them ofstream("/dev/null") but this makes my program less portable.

Is
> there a null ostream built in cpp?


No.

>Is it easy to implement one?


Yes. Derive a class from std::streambuf and override the protected
virtual function overflow as follows

int overflow(int c) { return c; }

Then you can use a standard istream and set its stream buffer to an
instance of your streambuf class using rdbuf. Or you can define your
own derived ostream class which automatically uses an an instance of
your streambuf class.

BTW, Daryle Walker has submitted a null_stream class for inclusion in
Boost, which will be reviewed soon. (See
http://groups.yahoo.com/group/boost/files/more_io_4.zip. (You have to
sign up for the boost developers list to access this:
http://www.boost.org/more/mailing_lists.htm#main.) Also, I have
written an Iostreams Library
(http://home.comcast.net/~jturkanis/iostreams/) which allows a null
ostream to be defined as follows:

struct null_sink : boost::io::sink {
void write(const char*, std::streamsize) { }
};

typedef boost::io::stream_facade<null_sink> null_ostream;

Jonathan



Siemel Naran 07-16-2004 04:41 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 
"Bo Peng" <bpeng@rice.edu> wrote in message
news:cd7flb$4tn$1@joe.rice.edu...

> I need to add "disable output" feature to a bunch of objects that output
> to ostream (or ofstream). The least intrusive way to do this is pass
> them ofstream("/dev/null") but this makes my program less portable. Is
> there a null ostream built in cpp? Is it easy to implement one?


You can try o.clear(ios::failbit), and then any statement o << 3 will not do
anything.

You can also set the rdbuf() to NULL in case someone calls o.rdbuf() and
writes to the buffer directly. I'm not sure if the standard allows you to
call o.rdbuf(NULL) though.

Disadvantage:

o.clear(ios::failbit);
o << f(3); // program evaluates f(3) even though it doesn't have to




JKop 07-16-2004 08:54 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 
Bo Peng posted:

> Dear list,
>
> I need to add "disable output" feature to a bunch of

objects that output
> to ostream (or ofstream). The least intrusive way to do

this is pass
> them ofstream("/dev/null") but this makes my program less

portable. Is
> there a null ostream built in cpp? Is it easy to

implement one?
>
> Thanks.
> Bo



int cout;


-JKop

Richard Herring 07-16-2004 09:38 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 
In message <SIMJc.4687$Z14.5890@news.indigo.ie>, JKop <NULL@NULL.NULL>
writes
>Bo Peng posted:
>
>> Dear list,
>>
>> I need to add "disable output" feature to a bunch of

>objects that output
>> to ostream (or ofstream). The least intrusive way to do

>this is pass
>> them ofstream("/dev/null") but this makes my program less

>portable. Is
>> there a null ostream built in cpp? Is it easy to

>implement one?
>>

>
>int cout;


Yeah, right, that's really helpful.

He never mentions cout; he wants to pass an ostream to an object.

class some_class {
public:
void output_me(std::ostream &);
};

some_class x;
int cout;
x.output_me(cout); // and this is going to work how, exactly?


--
Richard Herring

JKop 07-16-2004 11:27 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 

It was a joke!

Consider code like this:

using std::cout;

int main()
{
cout << "blah";

cout << "jhskfh";
}


If you stick "int cout" at the start of main(), then it'll
hide the now global scope "cout" object.

Maybe I'm immature, but I find that amusing.


-JKop

Rolf Magnus 07-16-2004 11:28 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 
JKop wrote:

> Bo Peng posted:
>
>> Dear list,
>>
>> I need to add "disable output" feature to a bunch of

> objects that output
>> to ostream (or ofstream). The least intrusive way to do

> this is pass
>> them ofstream("/dev/null") but this makes my program less

> portable. Is
>> there a null ostream built in cpp? Is it easy to

> implement one?
>>
>> Thanks.
>> Bo

>
>
> int cout;


cout << "Hello world\n";


JKop 07-16-2004 11:34 AM

Re: Is there a null ostream (like /dev/null) in cpp?
 
Rolf Magnus posted:


> cout << "Hello world\n";
>
>


Allow me to sabotage your prog:


#include <iostream>

using std::cout;


int main()
{
int cout;

cout << "Hello World!!\n";
}



-JKop

Richard Herring 07-16-2004 12:05 PM

Re: Is there a null ostream (like /dev/null) in cpp?
 
In message <43PJc.4701$Z14.5836@news.indigo.ie>, JKop <NULL@NULL.NULL>
writes
>Rolf Magnus posted:
>
>
>> cout << "Hello world\n";
>>
>>

>
>Allow me to sabotage your prog:
>
>
>#include <iostream>
>
>using std::cout;
>
>
>int main()
>{
> int cout;
>
> cout << "Hello World!!\n";


E2087 Illegal use of pointer.
>}
>
>


--
Richard Herring

Rolf Magnus 07-16-2004 12:23 PM

Re: Is there a null ostream (like /dev/null) in cpp?
 
JKop wrote:

> Rolf Magnus posted:
>
>
>> cout << "Hello world\n";
>>
>>

>
> Allow me to sabotage your prog:
>
>
> #include <iostream>
>
> using std::cout;
>
>
> int main()
> {
> int cout;
>
> cout << "Hello World!!\n";
> }


g++ says:

fake_cout.cpp: In function `int main()':
fake_cout.cpp:10: error: invalid operands of types `int' and `const
char[15]' to binary `operator<<'

And even if this worked as you might have expected, how would it satisfy
the OP's request for a stream that just throws everything away?



All times are GMT. The time now is 02:51 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.