Writing my own stream class
-
For a module I want to write my own stream class. Although not identical the following example illustrates what I want to do: suppose you have a class representing a printer:
class Printer { ...};
I want the user of this class to send data to this class using the << operator, so that any data type/class, for which << is defined on streams, e.g.
Printer myPrinter (printerName); PrinterStream &printStr = myPrinter.getStream(); printStr << "Hello world" << 123 << std::endl;
So, how can I write my own stream class so that I can intercept the data send to printStr? I probably need to inherit from some STL stream class, but I don't know where to start. Is this something that can be done easily, or is this almost impossible? Another alternative (although I don't really like it) is to let my getStream() method to return a stringstream. My class then simply needs a kind of flush method to get the data from the stringstream, send it to the output where I want it and then empty the stringstream. I don't like this alternative because it exposes the fact that's a stringstream to the caller. Also I cannot add my own output manipulators (e.g. to set the font or color) to the stream. Does anybody know a simple solution, or point me to an article that explains how to write your own stream class? Thanks in advance. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks
-
For a module I want to write my own stream class. Although not identical the following example illustrates what I want to do: suppose you have a class representing a printer:
class Printer { ...};
I want the user of this class to send data to this class using the << operator, so that any data type/class, for which << is defined on streams, e.g.
Printer myPrinter (printerName); PrinterStream &printStr = myPrinter.getStream(); printStr << "Hello world" << 123 << std::endl;
So, how can I write my own stream class so that I can intercept the data send to printStr? I probably need to inherit from some STL stream class, but I don't know where to start. Is this something that can be done easily, or is this almost impossible? Another alternative (although I don't really like it) is to let my getStream() method to return a stringstream. My class then simply needs a kind of flush method to get the data from the stringstream, send it to the output where I want it and then empty the stringstream. I don't like this alternative because it exposes the fact that's a stringstream to the caller. Also I cannot add my own output manipulators (e.g. to set the font or color) to the stream. Does anybody know a simple solution, or point me to an article that explains how to write your own stream class? Thanks in advance. Enjoy life, this is not a rehearsal !!! My Articles: - Implementing a Subject/Observer pattern with templates - Different ways of writing class factories - AutoRunner: a template class to automatically run start- and cleanup-code in code blocks
I would inherit from ostringstream, overloading output stream operators for your formatting classes. If the format written is binary (contains null characters) it needs special handling. (Remember that std::endl doesn't just append a newline character. It also makes the stream call flush(). Hence, you need to consider how the flush() method should behave when using this class.)