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: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:stream
{
public:
Disabled_Out_Stream():
std:stream( new Disabled_Stream_Buffer )
{}
std:stream& operator<< (bool& val ){}
std:stream& operator<< (short& val ){}
std:stream& operator<< (unsigned short& val ){}
std:stream& operator<< (int& val ){}
std:stream& operator<< (unsigned int& val ){}
std:stream& operator<< (long& val ){}
std:stream& operator<< (unsigned long& val ){}
std:stream& operator<< (float& val ){}
std:stream& operator<< (double& val ){}
std:stream& operator<< (long double& val ){}
std:stream& operator<< (void*& val ){}
std:stream& operator<< (std::streambuf& sb ){}
std:stream& operator<< (std:stream& ( *pf )(std:stream&)){}
std:stream& operator<< (std::ios& ( *pf )(std::ios&)){}
std:stream& operator<< (std::ios_base& ( *pf )(std::ios_base&)){}
/*
Should these go here?
std:stream& operator<< ( char ch ){}
std:stream& operator<< ( signed char ch ){}
std:stream& operator<< ( unsigned char ch ){}
std:stream& operator<< ( const char* str ){}
std:stream& operator<< ( const signed char* str ){}
std:stream& operator<< ( const unsigned char* str ){}
*/
};
/* Should these go here?
std:stream& operator<< (Disabled_Out_Stream& os, char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, signed char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, unsigned char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, const char* str ){}
std:stream& operator<< (Disabled_Out_Stream& os, const signed char*
str ){}
std:stream& operator<< (Disabled_Out_Stream& os, const unsigned
char* str ){}
*/
static Disabled_Out_Stream debug;
class Mesh
{
friend std:stream& operator<< (std:stream& o, const Mesh& m);
int a;
};
std:stream& operator<< (std: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;
}
and out streams for IO operations in my program, such as IO: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:stream
{
public:
Disabled_Out_Stream():
std:stream( new Disabled_Stream_Buffer )
{}
std:stream& operator<< (bool& val ){}
std:stream& operator<< (short& val ){}
std:stream& operator<< (unsigned short& val ){}
std:stream& operator<< (int& val ){}
std:stream& operator<< (unsigned int& val ){}
std:stream& operator<< (long& val ){}
std:stream& operator<< (unsigned long& val ){}
std:stream& operator<< (float& val ){}
std:stream& operator<< (double& val ){}
std:stream& operator<< (long double& val ){}
std:stream& operator<< (void*& val ){}
std:stream& operator<< (std::streambuf& sb ){}
std:stream& operator<< (std:stream& ( *pf )(std:stream&)){}
std:stream& operator<< (std::ios& ( *pf )(std::ios&)){}
std:stream& operator<< (std::ios_base& ( *pf )(std::ios_base&)){}
/*
Should these go here?
std:stream& operator<< ( char ch ){}
std:stream& operator<< ( signed char ch ){}
std:stream& operator<< ( unsigned char ch ){}
std:stream& operator<< ( const char* str ){}
std:stream& operator<< ( const signed char* str ){}
std:stream& operator<< ( const unsigned char* str ){}
*/
};
/* Should these go here?
std:stream& operator<< (Disabled_Out_Stream& os, char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, signed char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, unsigned char ch ){}
std:stream& operator<< (Disabled_Out_Stream& os, const char* str ){}
std:stream& operator<< (Disabled_Out_Stream& os, const signed char*
str ){}
std:stream& operator<< (Disabled_Out_Stream& os, const unsigned
char* str ){}
*/
static Disabled_Out_Stream debug;
class Mesh
{
friend std:stream& operator<< (std:stream& o, const Mesh& m);
int a;
};
std:stream& operator<< (std: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;
}