K,

It's fairly easy to use a ostringstream object or a derived class to output text like a normal stream class like cout.

Eg.

class console: public ostringstream
{
public:
  void Render()
  {
     std::string buff = ostringstream::str(); 
    //Some code to put it to screen

   //Write it to a file
  }
}


//Example of useage
console con;

con<<"Asasds"<<213;

This works as long as I keep calling Render().


Now if I want the text to be put into a file, I could ofcourse write buff but the output will have to wait till a render is performed.


I would like to have the text written as soon as the << operator is used. And I dont want to have to write a definition each and every data type :P.

What can be done to overload << so that an internal function can be called to write it to a file without having to wait till Render() is called.

try
abstract args[] {
static boolean x;
}

i have no clue what i'm doing

>i have no clue what i'm doing
Obviously.

>It's fairly easy to use a ostringstream object or a derived class to output
>text like a normal stream class like cout.
And it's usually a disaster. The correct way to customize a stream to a different destination is to replace the streambuf with one of your own. However, off the top of my head, that seems like it would be overly complicated in this case, so it's up to you. Oh well, have you considered a manipulator?

#include <iostream>
#include <ostream>
#include <sstream>
#include <string>

class console: public std::ostringstream {
public:
  void Render()
  {
    std::cout<< str();
  }
};

std::ostream& send ( std::ostream& out )
{
  // Evil cast
  ((console&)out).Render();
  return out;
}

int main()
{
  console con;

  con<<"this is a test\n"<<send;
}

Found a simple solution: Just overload the darn << operator with a function template, like in the stl headers and then pipe it to ostream.

Works like a charm ;)

class UTIL_Console:public CONSOLE_TYPE, public ostringstream
{
public:
    template<class __input> inline
    CC_UTIL_Console& operator<<(
        const __input Val)
    {
        //Cast the current class to ostringstream to avoid a recursion
        *((ostringstream*)this)<<Val;
        
        DumpTextToExternalSource( str().c_str() );
        return *this;
    }

};

Man it's cool, and looking at the stl headers really helped. Use you debbugger to see what code get's executed and in what order. That's how I found the specific location in the jungle that is the standard template library.

--see ya around--

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.