Disabled out stream (operator overloading problem)

Discussion in 'C++' started by Martin Magnusson, Aug 12, 2004.

  1. 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;
    }
    Martin Magnusson, Aug 12, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietmar Kuehl
    Replies:
    0
    Views:
    441
    Dietmar Kuehl
    Aug 12, 2004
  2. John Smith
    Replies:
    2
    Views:
    412
    Ivan Vecerina
    Oct 6, 2004
  3. mrstephengross
    Replies:
    3
    Views:
    390
    James Kanze
    May 10, 2007
  4. Grey Alien
    Replies:
    4
    Views:
    512
    Jerry Coffin
    Aug 4, 2007
  5. VirGin
    Replies:
    6
    Views:
    658
    James Kanze
    Jan 22, 2008
Loading...

Share This Page