Disabled out stream (operator overloading problem)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Magnusson

    Disabled out stream (operator overloading problem)

    I have defined a number of custom stream buffers with corresponding in
    and out streams for IO operations in my program, such as IO::output,
    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_St ream, but still
    takes some time) is called instead of the Disabled_Out_St ream one (which
    does nothing).

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

    #include <iostream>

    class Disabled_Stream _Buffer: public std::streambuf
    {
    public:
    Disabled_Stream _Buffer() {}
    };

    class Disabled_Out_St ream : public std::ostream
    {
    public:
    Disabled_Out_St ream():
    std::ostream( new Disabled_Stream _Buffer )
    {}

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

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

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

    /* Should these go here?
    std::ostream& operator<< (Disabled_Out_S tream& os, char ch ){}
    std::ostream& operator<< (Disabled_Out_S tream& os, signed char ch ){}
    std::ostream& operator<< (Disabled_Out_S tream& os, unsigned char ch ){}

    std::ostream& operator<< (Disabled_Out_S tream& os, const char* str ){}
    std::ostream& operator<< (Disabled_Out_S tream& os, const signed char*
    str ){}
    std::ostream& operator<< (Disabled_Out_S tream& os, const unsigned
    char* str ){}
    */

    static Disabled_Out_St ream debug;

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

    std::ostream& operator<< (std::ostream& 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;
    }
Working...