Disabled out stream (operator overloading problem)

M

Martin Magnusson

I have defined a number of custom stream buffers with corresponding in
and out streams for IO operations in my program, such as IO::eek:utput,
IO::warning and IO::debug. Now, the debug stream should be disabled in a
release build, and to do that efficiently, I suppose I need to overload
the << operator.

My current implementation of the stream in release mode is posted below.

Everything works fine for POD type like bool and int, but the problem is
that I have another class which has a friend operator <<, providing
output. It seems that no matter what I do, my Mesh << operator (which
produces no output, since it is sent to a Disabled_Out_Stream, but still
takes some time) is called instead of the Disabled_Out_Stream one (which
does nothing).

How can I make sure that anything that is sent to a Disabled_Out_Stream
is handled by an "empty" << operator?

#include <iostream>

class Disabled_Stream_Buffer: public std::streambuf
{
public:
Disabled_Stream_Buffer() {}
};

class Disabled_Out_Stream : public std::eek:stream
{
public:
Disabled_Out_Stream():
std::eek:stream( new Disabled_Stream_Buffer )
{}

std::eek:stream& operator<< (bool& val ){}
std::eek:stream& operator<< (short& val ){}
std::eek:stream& operator<< (unsigned short& val ){}
std::eek:stream& operator<< (int& val ){}
std::eek:stream& operator<< (unsigned int& val ){}
std::eek:stream& operator<< (long& val ){}
std::eek:stream& operator<< (unsigned long& val ){}
std::eek:stream& operator<< (float& val ){}
std::eek:stream& operator<< (double& val ){}
std::eek:stream& operator<< (long double& val ){}
std::eek:stream& operator<< (void*& val ){}
std::eek:stream& operator<< (std::streambuf& sb ){}
std::eek:stream& operator<< (std::eek:stream& ( *pf )(std::eek:stream&)){}
std::eek:stream& operator<< (std::ios& ( *pf )(std::ios&)){}
std::eek:stream& operator<< (std::ios_base& ( *pf )(std::ios_base&)){}

/*
Should these go here?
std::eek:stream& operator<< ( char ch ){}
std::eek:stream& operator<< ( signed char ch ){}
std::eek:stream& operator<< ( unsigned char ch ){}

std::eek:stream& operator<< ( const char* str ){}
std::eek:stream& operator<< ( const signed char* str ){}
std::eek:stream& operator<< ( const unsigned char* str ){}
*/
};

/* Should these go here?
std::eek:stream& operator<< (Disabled_Out_Stream& os, char ch ){}
std::eek:stream& operator<< (Disabled_Out_Stream& os, signed char ch ){}
std::eek:stream& operator<< (Disabled_Out_Stream& os, unsigned char ch ){}

std::eek:stream& operator<< (Disabled_Out_Stream& os, const char* str ){}
std::eek:stream& operator<< (Disabled_Out_Stream& os, const signed char*
str ){}
std::eek:stream& operator<< (Disabled_Out_Stream& os, const unsigned
char* str ){}
*/

static Disabled_Out_Stream debug;

class Mesh
{
friend std::eek:stream& operator<< (std::eek:stream& o, const Mesh& m);
int a;
};

std::eek:stream& operator<< (std::eek:stream& o, const Mesh& m)
{
return o << "This is a mesh." << m.a;
}


int main()
{
Mesh m;

std::cout << m;

for (int i = 0; i < 1e6; ++i)
debug << m;

return 0;
}
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top