0

Hello,

I need to simulate a stream which behaves exactly like a ostringstream, except when handling objects of type color. What is the best way to do this?

user.display() << color() << "Name: " << color(color::Red)   << setw(10) << left  << str_name << endl
               << color() << "Date: " << color(color::Green) << setw(10) << right << int_date << endl;

should display the same as...

user.display() << color() << "Name: " << setw(10) << left  << color(color::Red)   << str_name << endl
               << color() << "Date: " << setw(10) << right << color(color::Green) << int_date << endl;

I programming a telnet server that has to display text in color. The color codes are ANSI escape sequences ie characters. These characters when passed to ostringstream via << mess up setw() formatting alignment.

I need a mechanism to receive and process escape sequence characters from operator << without passing them on to ostringstream. What is the best way to do this?

I could write a wrapper that emulates ostringstream but then I would have to provide every overloaded << function. That's a lot of busy work. There has to be a better way.

Last night, I tried to inherit public ostringstream and provide operator << for my color class. I realized that would not work because ostringstream operator<< returns a reference to ostringstream and not my class. Could I overload operator << in color() to return console()? How would that translate into code?

class console: public std::ostringstream
{
public:
   console& (operator<<) ( color &c )
   {
      std::cout << "color: " << c.i << std::endl;
   }
   // wont work if color is not the first obj passed to the stream
};

Next I tried to emulate the entire ostringstream interface. This works except for setw, which gives me compile error "error: declaration of 'operator<<' as non-function" in g++.

#include <iostream>
#include <iomanip>
using std::setw;

class console
{
private:
   std::ostringstream oStr;

public:
   console& (operator<<) ( color &c )
   {
      std::cout << "color: " << c.i << std::endl;
   }
   console& (operator<<) ( std::string &s )
   {
      std::cout << "string: " << s << std::endl;
      oStr << s;
   }

   console& (operator<<) ( setw p )
   {
      std::cout << sw << "setw: " << std::endl;
      oStr << p;
   }

   void Render()
   {
      std::cout << oStr.str();
   }
};

ideas? Thanks in advance.

1
Contributor
1
Reply
3
Views
7 Years
Discussion Span
Last Post by TimeFractal
0

ok. Problem solved. operator<< should not be a member function. Could someone enlighten me as to why this the case.

#include <iostream>
#include <iomanip>

class color
{
public: int i;
};

class console
{
private:
   std::ostringstream oStr;

public:
   std::ostringstream & display( void )
   {
      return oStr;
   }
   void render( void )
   {
      std::cout << oStr.str();
   }
};

std::ostream& operator<< ( std::ostream& out, const color& c )
{
   std::cout << "color: " << c.i;
   return out;
}

int main( void )
{
   using namespace std;

   console con;
   color c; c.i = 10;

   con.display() << setw(10) << left << " text " 
                        << c << " text " << endl;
   con.render();
   return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.